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