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 DEMUXER_PLUGIN_MANAGER_H 17 #define DEMUXER_PLUGIN_MANAGER_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 class BaseStreamDemuxer; 41 42 enum TrackType { 43 TRACK_VIDEO = 0, 44 TRACK_AUDIO, 45 TRACK_SUBTITLE, 46 TRACK_INVALID 47 }; 48 49 class DataSourceImpl : public Plugins::DataSource { 50 public: 51 explicit DataSourceImpl(const std::shared_ptr<BaseStreamDemuxer>& stream, int32_t streamID); 52 ~DataSourceImpl() override = default; 53 Status ReadAt(int64_t offset, std::shared_ptr<Buffer>& buffer, size_t expectedLen) override; 54 Status GetSize(uint64_t& size) override; 55 Plugins::Seekable GetSeekable() override; 56 Status SetStreamID(int32_t streamID); 57 int32_t GetStreamID() override; 58 bool IsDash() override; 59 void SetIsDash(bool flag); 60 private: 61 bool IsOffsetValid(int64_t offset) const; 62 private: 63 std::shared_ptr<BaseStreamDemuxer> stream_; 64 int32_t streamID_; 65 bool isDash_ = false; 66 }; 67 68 class MediaStreamInfo { 69 public: 70 int32_t streamID = -1; 71 bool activated = false; 72 StreamType type; 73 uint32_t bitRate; 74 std::string pluginName = ""; 75 std::shared_ptr<Plugins::DemuxerPlugin> plugin = nullptr; 76 std::shared_ptr<DataSourceImpl> dataSource = nullptr; 77 Plugins::MediaInfo mediaInfo; // dash中每个streamid只有一个track 78 }; 79 80 class MediaTrackMap { 81 public: 82 int32_t trackID = -1; 83 int32_t streamID = -1; 84 int32_t innerTrackIndex = -1; 85 }; 86 87 class DemuxerPluginManager { 88 public: 89 explicit DemuxerPluginManager(); 90 virtual ~DemuxerPluginManager(); 91 92 Status InitDefaultPlay(const std::vector<StreamInfo>& streams); 93 std::shared_ptr<Plugins::DemuxerPlugin> GetPluginByStreamID(int32_t streamID); 94 void GetTrackInfoByStreamID(int32_t streamID, int32_t& trackId, int32_t& innerTrackId); 95 96 int32_t GetTmpStreamIDByTrackID(int32_t trackId); 97 int32_t GetTmpInnerTrackIDByTrackID(int32_t trackId); 98 void UpdateTempTrackMapInfo(int32_t oldTrackId, int32_t newTrackId, int32_t newInnerTrackIndex); 99 void DeleteTempTrackMapInfo(int32_t oldTrackId); 100 101 int32_t GetInnerTrackIDByTrackID(int32_t trackId); 102 StreamType GetStreamTypeByTrackID(int32_t trackId); 103 int32_t GetStreamIDByTrackID(int32_t trackId); 104 105 TrackType GetTrackTypeByTrackID(int32_t trackID); 106 107 Status LoadCurrentAllPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, MediaInfo& mediaInfo); 108 Status LoadCurrentSubtitlePlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, 109 Plugins::MediaInfo& mediaInfo); 110 Status LoadDemuxerPlugin(int32_t streamID, std::shared_ptr<BaseStreamDemuxer> streamDemuxer); 111 Status Reset(); 112 Status Start(); 113 Status Stop(); 114 Status Flush(); 115 Status SeekTo(int64_t seekTime, Plugins::SeekMode mode, int64_t& realSeekTime); 116 int32_t GetStreamID(int32_t trackId); 117 int32_t GetInnerTrackID(int32_t trackId); 118 bool IsDash() const; 119 bool IsSubtitle() const; 120 Status StopPlugin(int32_t streamId, std::shared_ptr<BaseStreamDemuxer> streamDemuxer); 121 Status StartPlugin(int32_t streamId, std::shared_ptr<BaseStreamDemuxer> streamDemuxer); 122 Status StartAllPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer); 123 Status StopAllPlugin(); 124 Status UpdateDefaultStreamID(Plugins::MediaInfo& mediaInfo, StreamType type, int32_t newStreamID); 125 126 std::shared_ptr<Meta> GetUserMeta(); 127 uint32_t GetCurrentBitRate(); 128 size_t GetStreamCount() const; 129 void SetResetEosStatus(bool flag); 130 bool CheckTrackIsActive(int32_t trackId); 131 int32_t AddExternalSubtitle(); 132 Status localSubtitleSeekTo(int64_t seekTime); 133 private: 134 bool CreatePlugin(std::string pluginName, int32_t id); 135 bool InitPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, const std::string& pluginName, int32_t id); 136 void MediaTypeFound(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, const std::string& pluginName, int32_t id); 137 void InitAudioTrack(const StreamInfo& info); 138 void InitVideoTrack(const StreamInfo& info); 139 void InitSubtitleTrack(const StreamInfo& info); 140 void AddMediaInfo(int32_t streamID, Plugins::MediaInfo& mediaInfo); 141 static Status UpdateGeneralValue(int32_t trackCount, const Meta& format, Meta& formatNew); 142 static Status AddGeneral(const MediaStreamInfo& info, Meta& formatNew); 143 144 Status AddTrackMapInfo(int32_t streamID, int32_t trackIndex); 145 Status UpdateMediaInfo(int32_t streamID); 146 bool IsSubtitleMime(const std::string& mime); 147 private: 148 std::map<int32_t, MediaStreamInfo> streamInfoMap_; // <streamId, MediaStreamInfo> 149 std::map<int32_t, MediaTrackMap> trackInfoMap_; // 保存所有的track信息,使用映射的trackID <trackId, MediaTrackMap> 150 std::map<int32_t, MediaTrackMap> temp2TrackInfoMap_; // 保存正在播放的track和tempTrackInfoMap_索引映射 151 int32_t curVideoStreamID_ = -1; 152 int32_t curAudioStreamID_ = -1; 153 int32_t curSubTitleStreamID_ = -1; 154 155 Plugins::MediaInfo curMediaInfo_; 156 bool isDash_ = false; 157 bool needResetEosStatus_ = false; 158 }; 159 } // namespace Media 160 } // namespace OHOS 161 #endif // MEDIA_DEMUXER_H 162