1 /*
2  * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef HISTREAMER_HTTP_SOURCE_PLUGIN_H
16 #define HISTREAMER_HTTP_SOURCE_PLUGIN_H
17 
18 #include "foundation/osal/thread/scoped_lock.h"
19 #include "http_lite_manager.h"
20 #include "plugin/common/plugin_types.h"
21 #include "plugin/interface/source_plugin.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Plugin {
26 namespace HttpLitePlugin {
27 class HttpSourceAllocator : public Allocator {
28 public:
29     HttpSourceAllocator() = default;
30     ~HttpSourceAllocator() override = default;
31 
32     void *Alloc(size_t size) override;
33     void Free(void *ptr) override;
34 };
35 
36 class HttpSourcePlugin : public SourcePlugin {
37 public:
38     explicit HttpSourcePlugin(const std::string& name) noexcept;
39     ~HttpSourcePlugin();
40     Status Init() override;
41     Status Deinit() override;
42     Status Prepare() override;
43     Status Reset() override;
44     Status Stop() override;
45     Status GetParameter(Tag tag, ValueType &value) override;
46     Status SetParameter(Tag tag, const ValueType &value) override;
47     std::shared_ptr<Allocator> GetAllocator() override;
48     Status SetCallback(Callback* cb) override;
49     Status SetSource(std::shared_ptr<MediaSource> source) override;
50     Status Read(std::shared_ptr<Buffer> &buffer, size_t expectedLen) override;
51     Status GetSize(uint64_t& size) override;
52     Seekable GetSeekable() override;
53     Status SeekTo(uint64_t offset) override;
54     static void OnError(int httpError, int localError, void *param, int supportRetry);
55     Status OnHttpEvent(void *priv, int errorType, int32_t errorCode);
56 
57 private:
58     std::string url_;
59     std::string certFile_;
60     bool needExit_ {false};
61     bool isStream_ {false};
62     uint32_t bufferSize_ {0};
63     unsigned int position_ {0};
64     uint32_t waterline_ {0};
65     size_t   fileSize_ {0};
66     Callback* callback_ {};
67     std::shared_ptr<HttpLiteManager> httpHandle_;
68     std::shared_ptr<HttpSourceAllocator> mAllocator_;
69     mutable OSAL::Mutex httpMutex_ {};
70     Status OpenUri(std::string &url);
71     void CloseUri();
72 };
73 } // namespace HttpLitePlugin
74 } // namespace Plugin
75 } // namespace Media
76 } // namespace OHOS
77 
78 #endif