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 int32_t GetStreamIDByTrackType(TrackType type); 105 int32_t GetStreamDemuxerNewStreamID(TrackType trackType, std::shared_ptr<BaseStreamDemuxer> streamDemuxer); 106 107 TrackType GetTrackTypeByTrackID(int32_t trackId); 108 109 Status LoadCurrentAllPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, MediaInfo& mediaInfo); 110 Status LoadCurrentSubtitlePlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, 111 Plugins::MediaInfo& mediaInfo); 112 Status LoadDemuxerPlugin(int32_t streamID, std::shared_ptr<BaseStreamDemuxer> streamDemuxer); 113 Status Reset(); 114 Status Start(); 115 Status Stop(); 116 Status Flush(); 117 Status SeekTo(int64_t seekTime, Plugins::SeekMode mode, int64_t& realSeekTime); 118 int32_t GetStreamID(int32_t trackId); 119 int32_t GetInnerTrackID(int32_t trackId); 120 bool IsDash() const; 121 Status StopPlugin(int32_t streamId, std::shared_ptr<BaseStreamDemuxer> streamDemuxer); 122 Status StartPlugin(int32_t streamId, std::shared_ptr<BaseStreamDemuxer> streamDemuxer); 123 Status RebootPlugin(int32_t streamId, TrackType trackType, std::shared_ptr<BaseStreamDemuxer> streamDemuxer, 124 bool& isRebooted); 125 Status UpdateDefaultStreamID(Plugins::MediaInfo& mediaInfo, StreamType type, int32_t newStreamID); 126 127 std::shared_ptr<Meta> GetUserMeta(); 128 uint32_t GetCurrentBitRate(); 129 void SetResetEosStatus(bool flag); 130 size_t GetStreamCount() const; 131 Status SetCacheLimit(uint32_t limitSize); 132 bool CheckTrackIsActive(int32_t trackId); 133 int32_t AddExternalSubtitle(); 134 Status localSubtitleSeekTo(int64_t seekTime); 135 private: 136 bool CreatePlugin(std::string pluginName, int32_t id); 137 bool InitPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, const std::string& pluginName, int32_t id); 138 void MediaTypeFound(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, const std::string& pluginName, int32_t id); 139 void InitAudioTrack(const StreamInfo& info); 140 void InitVideoTrack(const StreamInfo& info); 141 void InitSubtitleTrack(const StreamInfo& info); 142 void AddMediaInfo(int32_t streamID, Plugins::MediaInfo& mediaInfo); 143 static Status UpdateGeneralValue(int32_t trackCount, const Meta& format, Meta& formatNew); 144 static Status AddGeneral(const MediaStreamInfo& info, Meta& formatNew); 145 146 Status AddTrackMapInfo(int32_t streamID, int32_t trackIndex); 147 Status UpdateMediaInfo(int32_t streamID); 148 bool IsSubtitleMime(const std::string& mime); 149 private: 150 std::map<int32_t, MediaStreamInfo> streamInfoMap_; // <streamId, MediaStreamInfo> 151 std::map<int32_t, MediaTrackMap> trackInfoMap_; // 保存所有的track信息,使用映射的trackID <trackId, MediaTrackMap> 152 std::map<int32_t, MediaTrackMap> temp2TrackInfoMap_; // 保存正在播放的track和tempTrackInfoMap_索引映射 153 int32_t curVideoStreamID_ = -1; 154 int32_t curAudioStreamID_ = -1; 155 int32_t curSubTitleStreamID_ = -1; 156 157 Plugins::MediaInfo curMediaInfo_; 158 bool isDash_ = false; 159 bool needResetEosStatus_ = false; 160 }; 161 } // namespace Media 162 } // namespace OHOS 163 #endif // MEDIA_DEMUXER_H 164