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_STREAM_SOURCE_PLUGIN_H 17 #define HISTREAMER_STREAM_SOURCE_PLUGIN_H 18 19 #include "source.h" 20 #include "foundation/error_code.h" 21 #include "foundation/osal/thread/task.h" 22 #include "utils/blocking_queue.h" 23 #include "utils/buffer_pool.h" 24 #include "utils/constants.h" 25 #include "utils/type_define.h" 26 #include "plugin/common/plugin_types.h" 27 #include "plugin/interface/source_plugin.h" 28 29 namespace OHOS { 30 namespace Media { 31 namespace Plugin { 32 namespace StreamSource { 33 using StreamCallback = OHOS::Media::StreamCallback; 34 using StreamSource = OHOS::Media::StreamSource; 35 36 class StreamSourcePlugin; 37 38 class StreamSourceAllocator : public Allocator { 39 public: 40 StreamSourceAllocator() = default; 41 ~StreamSourceAllocator() = default; 42 43 void* Alloc(size_t size) override; 44 void Free(void* ptr) override; // NOLINT: void* 45 }; 46 47 class StreamSourceCallback : public StreamCallback { 48 public: 49 StreamSourceCallback(std::shared_ptr<StreamSourcePlugin> dataSource, std::shared_ptr<StreamSource>& stream); 50 virtual ~StreamSourceCallback() = default; 51 52 uint8_t* GetBuffer(size_t index) override; 53 void QueueBuffer(size_t index, size_t offset, size_t size, int64_t timestampUs, uint32_t flags) override; SetParameters(const Media::Format & params)54 void SetParameters(const Media::Format& params) override 55 { 56 } 57 58 private: 59 std::shared_ptr<StreamSourcePlugin> dataSource_; 60 std::weak_ptr<StreamSource> streamSource_; 61 }; 62 63 class StreamSourcePlugin : public SourcePlugin, std::enable_shared_from_this<StreamSourcePlugin> { 64 public: 65 explicit StreamSourcePlugin(std::string name); 66 ~StreamSourcePlugin(); 67 68 Status Init() override; 69 Status Deinit() override; 70 Status Prepare() override; 71 Status Reset() override; 72 Status Start() override; 73 Status Stop() override; 74 bool IsParameterSupported(Tag tag) override; 75 Status GetParameter(Tag tag, ValueType& value) override; 76 Status SetParameter(Tag tag, const ValueType& value) override; 77 std::shared_ptr<Allocator> GetAllocator() override; 78 Status SetCallback(Callback* cb) override; 79 Status SetSource(std::shared_ptr<MediaSource> source) override; 80 Status Read(std::shared_ptr<Buffer>& buffer, size_t expectedLen) override; 81 Status GetSize(size_t& size) override; 82 bool IsSeekable() override; 83 Status SeekTo(uint64_t offset) override; 84 85 AVBufferPtr AllocateBuffer(); 86 AVBufferPtr FindBuffer(size_t idx); 87 void EraseBuffer(size_t idx); 88 void EnqueBuffer(AVBufferPtr& bufferPtr); 89 90 protected: 91 AVBufferPool bufferPool_; 92 93 private: 94 State state_; 95 bool isSeekable_; 96 OSAL::Mutex mutex_ {}; 97 std::map<size_t, AVBufferPtr> waitBuffers_; 98 std::weak_ptr<StreamSource> streamSource_ {}; 99 std::shared_ptr<StreamSourceCallback> streamCallback_ {nullptr}; 100 size_t idx_ {0}; 101 BlockingQueue<AVBufferPtr> bufferQueue_; 102 std::shared_ptr<OSAL::Task> taskPtr_ {nullptr}; 103 std::shared_ptr<StreamSourceAllocator> mAllocator_ {nullptr}; 104 105 void NotifyAvilableBufferLoop(); GetUniqueIdx()106 size_t GetUniqueIdx() 107 { 108 return ++idx_; 109 } 110 }; 111 } // namespace StreamSource 112 } // namespace Plugin 113 } // namespace Media 114 } // namespace OHOS 115 116 #endif // HISTREAMER_STREAM_SOURCE_PLUGIN_H 117