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 
16 #ifndef HISTREAMER_HTTP_LITE_PLUGIN_HTTP_MANAGER_H
17 #define HISTREAMER_HTTP_LITE_PLUGIN_HTTP_MANAGER_H
18 #include <string>
19 #include <memory>
20 
21 namespace OHOS {
22 namespace Media {
23 namespace Plugin {
24 namespace HttpLitePlugin {
25 using OnError = void(*)(int httpError, int localError, void *param, int supportRetry);
26 struct HttpLiteAttr {
27     OnError callbackFunc;
28     void *pluginHandle;
29     int bufferSize;
30     int priority;
31     std::string certFile;
32     bool notVerifyCert; // false for verify; true for not verify
33 };
34 
35 enum HttpLiteStatus {
36     HTTP_LITE_STATUS_IDLE = 0,
37     HTTP_LITE_STATUS_PLAY,
38     HTTP_LITE_STATUS_PAUSE,
39     HTTP_LITE_STATUS_SEEK,
40     HTTP_LITE_STATUS_END,
41     HTTP_LITE_STATUS_STOP
42 };
43 
44 struct HttpLiteRunningInfo {
45     unsigned int readPos;
46     unsigned int writePos;
47     unsigned int lastReadTime;
48     HttpLiteStatus state;
49     bool isRetry;
50 };
51 
52 enum HttpLiteUrlType {
53     URL_HTTP,
54     URL_HLS,
55     URL_WEBSOCKET,
56     URL_UNKNOWN
57 };
58 
59 class HttpLiteManager {
60 public:
61     HttpLiteManager() noexcept;
62     virtual ~HttpLiteManager();
63     bool HttpOpen(std::string &url, HttpLiteAttr &attr);
64     void HttpClose();
65     bool HttpRead(unsigned char *buff, unsigned int wantReadLength, unsigned int &realReadLength, int &flag);
66     bool HttpPeek(unsigned char *buff, unsigned int wantReadLength, unsigned int &realReadLength);
67     bool HttpSeek(int offset);
68     bool HttpPause();
69     bool HttpReset();
70     unsigned int GetContentLength();
71     HttpLiteStatus GetHttpStatus();
72     unsigned int GetLastReadTime();
73     void GetHttpBufferRange(unsigned int *read, unsigned int *write);
74     void SetWaterline(int high, int low);
75     int IsStreaming();
76     HttpLiteUrlType IsHlsSource(std::string &url);
77     void GetHttpRunningInfo(HttpLiteRunningInfo &info);
78     void SetHttpRunningInfo(bool isRetry);
79 
80 private:
81     friend void ReceiveData(unsigned char *data, int len, void *priv);
82     friend void OnFinished(void *priv);
83     friend void OnFailed(int httpError, int localError, void *priv, int supportRetry);
84     bool IsNeedRetry(int localError, int supportRetry);
85     HttpLiteAttr httpAttr_ {nullptr, nullptr, 0, 0, {}};
86     HttpLiteStatus status_ {HTTP_LITE_STATUS_IDLE};
87     unsigned int lastReadTime_ {0};
88     bool isRetry_ {false};
89     int retryTimes_ {0};
90     void *httpHandle_;
91     long sourceLen_;
92 };
93 } // HttpLitePlugin
94 } // Plugin
95 } // Media
96 } // OHOS
97 
98 #endif // HISTREAMER_HTTP_LITE_PLUGIN_HTTP_MANAGER_H