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 19 #include <cstdio> 20 #include <string> 21 #include <memory> 22 23 using OnError = void(*)(int httpError, int localError, void *param, int supportRetry); 24 constexpr unsigned int DEFAULT_SOURCE_SIZE = 20 * 1024; 25 constexpr unsigned int DEFAULT_PRIORITY = 32; 26 27 struct HttpLiteAttr { 28 std::string certFile; 29 OnError callbackFunc; 30 void *pluginHandle; 31 int bufferSize; 32 int priority; 33 }; 34 35 enum HttpLiteStatus { 36 HTTP_STATUS_IDLE = 0, 37 HTTP_STATUS_PLAY, 38 HTTP_STATUS_PAUSE, 39 HTTP_STATUS_SEEK, 40 HTTP_STATUS_END, 41 HTTP_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, bool &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() const; 71 HttpLiteStatus GetHttpStatus() const; 72 unsigned int GetLastReadTime() const; 73 void GetHttpBufferRange(unsigned int *read, unsigned int *write); 74 void SetWaterline(int high, int low); 75 bool 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, DEFAULT_SOURCE_SIZE, DEFAULT_PRIORITY}; 86 HttpLiteStatus status_ {HTTP_STATUS_IDLE}; 87 unsigned int lastReadTime_ {0}; 88 bool isRetry_ {false}; 89 int retryTimes_ {0}; 90 }; 91 92 93 #endif