1 /* 2 * Copyright (c) 2022-2022 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_STREAMING_EXECUTOR_H 17 #define HISTREAMER_STREAMING_EXECUTOR_H 18 19 #include <string> 20 #include <memory> 21 #include "client_factory.h" 22 #include "ring_buffer.h" 23 #include "network_client.h" 24 #include "osal/thread/task.h" 25 #include "plugin/interface/plugin_base.h" 26 27 namespace OHOS { 28 namespace Media { 29 namespace Plugin { 30 namespace HttpPlugin { 31 32 struct HeaderInfo { 33 char contentType[32]; // 32 chars 34 size_t fileContentLen; 35 long contentLen; 36 bool isChunked {false}; 37 }; 38 39 class StreamingExecutor { 40 public: 41 StreamingExecutor() noexcept; 42 virtual ~StreamingExecutor(); 43 bool Open(const std::string &url); 44 void Close(); 45 bool Read(unsigned char *buff, unsigned int wantReadLength, unsigned int &realReadLength, bool &isEos); 46 bool Seek(int offset); 47 48 size_t GetContentLength() const; 49 bool IsStreaming() const; 50 void SetCallback(Callback* cb); 51 private: 52 void HttpDownloadThread(); 53 static size_t RxBodyData(void *buffer, size_t size, size_t nitems, void *userParam); 54 static size_t RxHeaderData(void *buffer, size_t size, size_t nitems, void *userParam); 55 56 private: 57 std::shared_ptr<RingBuffer> buffer_; 58 std::shared_ptr<ClientFactory> factory_; 59 std::shared_ptr<NetworkClient> client_; 60 bool isEos_ {false}; // file download finished 61 int64_t startPos_; 62 HeaderInfo headerInfo_; 63 std::shared_ptr<OSAL::Task> task_; 64 bool isDownloading_; 65 int requestSize_; 66 Callback* callback_ {nullptr}; 67 bool aboveWaterline_ {false}; 68 }; 69 } 70 } 71 } 72 } 73 #endif