1 /* 2 * Copyright (C) 2023-2025 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_STREAM_SOURCE_PLUGIN_H 17 #define HISTREAMER_STREAM_SOURCE_PLUGIN_H 18 19 #include "plugin/plugin_buffer.h" 20 #include "plugin/source_plugin.h" 21 #include "common/media_data_source.h" 22 #include "common/avsharedmemorypool.h" 23 24 namespace OHOS { 25 namespace Media { 26 namespace Plugin { 27 namespace DataStreamSource { 28 class DataStreamSourcePlugin : public Plugins::SourcePlugin { 29 public: 30 explicit DataStreamSourcePlugin(std::string name); 31 ~DataStreamSourcePlugin() override; 32 33 Status SetCallback(Plugins::Callback* cb) override; 34 Status SetSource(std::shared_ptr<Plugins::MediaSource> source) override; 35 Status Read(std::shared_ptr<Plugins::Buffer>& buffer, uint64_t offset, size_t expectedLen) override; 36 Status GetSize(uint64_t& size) override; 37 Plugins::Seekable GetSeekable() override; 38 Status SeekTo(uint64_t offset) override; 39 Status Pause() override; 40 Status Resume() override; 41 Status Reset() override; 42 bool IsNeedPreDownload() override; 43 void SetInterruptState(bool isInterruptNeeded) override; 44 private: 45 std::shared_ptr<Plugins::Buffer> WrapAVSharedMemory( 46 const std::shared_ptr<AVSharedMemory>& avSharedMemory, int32_t realLen); 47 void InitPool(); 48 void HandleBufferingStart(); 49 void HandleBufferingEnd(); 50 uint32_t GetRetryTime(); 51 void WaitForRetry(uint32_t time); 52 std::shared_ptr<AVSharedMemory> GetMemory(); 53 void ResetPool(); 54 Status ReadAt(std::shared_ptr<AVSharedMemory> memory, size_t &expectedLen, int32_t &realLen); 55 Plugins::Seekable seekable_ {Plugins::Seekable::INVALID}; 56 std::shared_ptr<IMediaDataSource> dataSrc_; 57 std::shared_ptr<AVSharedMemoryPool> pool_; 58 std::atomic<bool> isBufferingStart{false}; 59 std::atomic<bool> isInterrupted_ {false}; 60 std::atomic<bool> isExitRead_ {false}; 61 std::mutex mutex_; 62 std::condition_variable readCond_; 63 Plugins::Callback* callback_ {nullptr}; 64 int64_t size_ {0}; 65 uint64_t offset_ {0}; 66 uint32_t retryTimes_ = 0; 67 }; 68 } // namespace DataStreamSource 69 } // namespace Plugin 70 } // namespace Media 71 } // namespace OHOS 72 #endif // HISTREAMER_STREAM_SOURCE_PLUGIN_H 73