1 /* 2 * Copyright (c) 2023-2024 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 BASE_STREAM_DEMUXER_H 17 #define BASE_STREAM_DEMUXER_H 18 19 #include <atomic> 20 #include <limits> 21 #include <string> 22 #include <shared_mutex> 23 24 #include "avcodec_common.h" 25 #include "buffer/avbuffer.h" 26 #include "common/media_source.h" 27 #include "demuxer/type_finder.h" 28 #include "filter/filter.h" 29 #include "meta/media_types.h" 30 #include "osal/task/task.h" 31 #include "plugin/plugin_base.h" 32 #include "plugin/plugin_info.h" 33 #include "plugin/plugin_time.h" 34 #include "plugin/demuxer_plugin.h" 35 #include "source/source.h" 36 37 namespace OHOS { 38 namespace Media { 39 40 enum class DemuxerState { 41 DEMUXER_STATE_NULL, 42 DEMUXER_STATE_PARSE_HEADER, 43 DEMUXER_STATE_PARSE_FIRST_FRAME, 44 DEMUXER_STATE_PARSE_FRAME 45 }; 46 47 class CacheData { 48 public: CacheData()49 CacheData() {} ~CacheData()50 ~CacheData() 51 { 52 Reset(); 53 } Reset()54 void Reset() 55 { 56 data = nullptr; 57 offset = 0; 58 } CheckCacheExist(uint64_t len)59 bool CheckCacheExist(uint64_t len) 60 { 61 if (data != nullptr) { 62 if (data->GetMemory() != nullptr) { 63 return len >= offset && len < (offset + data->GetMemory()->GetSize()); 64 } 65 } 66 return false; 67 } GetOffset()68 uint64_t GetOffset() 69 { 70 return offset; 71 } GetData()72 std::shared_ptr<Buffer> GetData() 73 { 74 return data; 75 } SetData(const std::shared_ptr<Buffer> & buffer)76 void SetData(const std::shared_ptr<Buffer>& buffer) 77 { 78 data = buffer; 79 } Init(const std::shared_ptr<Buffer> & buffer,uint64_t bufferOffset)80 void Init(const std::shared_ptr<Buffer>& buffer, uint64_t bufferOffset) 81 { 82 data = buffer; 83 offset = bufferOffset; 84 } 85 private: 86 std::shared_ptr<Buffer> data = nullptr; 87 uint64_t offset = 0; 88 }; 89 90 class BaseStreamDemuxer { 91 public: 92 explicit BaseStreamDemuxer(); 93 virtual ~BaseStreamDemuxer(); 94 95 virtual Status Init(const std::string& uri) = 0; 96 virtual Status Pause() = 0; 97 virtual Status Resume() = 0; 98 virtual Status Start() = 0; 99 virtual Status Stop() = 0; 100 virtual Status Flush() = 0; 101 virtual Status ResetCache(int32_t streamID) = 0; 102 virtual Status ResetAllCache() = 0; 103 104 void SetSource(const std::shared_ptr<Source>& source); 105 106 virtual Status CallbackReadAt(int32_t streamID, int64_t offset, std::shared_ptr<Buffer>& buffer, 107 size_t expectedLen) = 0; 108 void SetDemuxerState(int32_t streamId, DemuxerState state); 109 void SetBundleName(const std::string& bundleName); 110 void SetIsIgnoreParse(bool state); 111 bool GetIsIgnoreParse(); 112 Plugins::Seekable GetSeekable(); 113 void SetInterruptState(bool isInterruptNeeded); 114 115 std::string SnifferMediaType(int32_t streamID); 116 bool IsDash() const; 117 void SetIsDash(bool flag); 118 119 Status SetNewAudioStreamID(int32_t streamID); 120 Status SetNewVideoStreamID(int32_t streamID); 121 Status SetNewSubtitleStreamID(int32_t streamID); 122 int32_t GetNewVideoStreamID(); 123 int32_t GetNewAudioStreamID(); 124 int32_t GetNewSubtitleStreamID(); 125 bool CanDoChangeStream(); 126 void SetChangeFlag(bool flag); 127 bool GetIsExtSubtitle(); 128 void SetIsExtSubtitle(bool flag); 129 void SetSourceType(SourceType type); 130 bool GetIsDataSrcNoSeek(); 131 protected: 132 std::shared_ptr<Source> source_; 133 std::function<Status(int32_t, uint64_t, size_t)> checkRange_; 134 std::function<Status(int32_t, uint64_t, size_t, std::shared_ptr<Buffer>&)> peekRange_; 135 std::function<Status(int32_t, uint64_t, size_t, std::shared_ptr<Buffer>&)> getRange_; 136 std::map<int32_t, DemuxerState> pluginStateMap_; 137 std::atomic<bool> isIgnoreParse_{false}; 138 std::atomic<bool> isIgnoreRead_{false}; 139 std::string bundleName_ {}; 140 std::string uri_ {}; 141 std::atomic<bool> isInterruptNeeded_{false}; 142 public: 143 uint64_t mediaDataSize_{0}; 144 Plugins::Seekable seekable_ = Plugins::Seekable::UNSEEKABLE; 145 private: 146 bool isDash_ = {false}; 147 bool isDataSrcNoSeek_ = {false}; 148 SourceType sourceType_ = {SourceType::SOURCE_TYPE_FD}; 149 std::atomic<int32_t> newVideoStreamID_ = -1; 150 std::atomic<int32_t> newAudioStreamID_ = -1; 151 std::atomic<int32_t> newSubtitleStreamID_ = -1; 152 std::atomic<bool> changeStreamFlag_ = true; 153 std::atomic<bool> isExSubtitle_ = false; 154 }; 155 } // namespace Media 156 } // namespace OHOS 157 #endif // MEDIA_DEMUXER_H 158