• 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 Stop();
120     Status Flush();
121     Status SeekTo(int64_t seekTime, Plugins::SeekMode mode, int64_t& realSeekTime);
122     int32_t GetStreamID(int32_t trackId);
123     int32_t GetInnerTrackID(int32_t trackId);
124     bool IsDash() const;
125     bool IsSubtitle() const;
126     Status StopPlugin(int32_t streamId, std::shared_ptr<BaseStreamDemuxer> streamDemuxer);
127     Status StartPlugin(int32_t streamId, std::shared_ptr<BaseStreamDemuxer> streamDemuxer);
128     Status RebootPlugin(int32_t streamId, TrackType trackType, std::shared_ptr<BaseStreamDemuxer> streamDemuxer,
129         bool& isRebooted);
130     Status StartAllPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer);
131     Status StopAllPlugin();
132     Status UpdateDefaultStreamID(Plugins::MediaInfo& mediaInfo, StreamType type, int32_t newStreamID);
133     Status SingleStreamSeekTo(int64_t seekTime, Plugins::SeekMode mode, int32_t streamID, int64_t& realSeekTime);
134 
135     std::shared_ptr<Meta> GetUserMeta();
136     uint32_t GetCurrentBitRate();
137     size_t GetStreamCount() const;
138     void SetResetEosStatus(bool flag);
139     void SetInterruptState(bool isInterruptNeeded);
140     bool CheckTrackIsActive(int32_t trackId);
141     int32_t AddExternalSubtitle();
142     Status localSubtitleSeekTo(int64_t seekTime);
143     void NotifyInitialBufferingEnd(bool isInitialBufferingSucc);
144     void SetApiVersion(int32_t apiVersion);
145     void SetIsHlsFmp4(bool isHlsFmp4);
146 private:
147     bool CreatePlugin(std::string pluginName, int32_t id);
148     bool InitPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, const std::string& pluginName, int32_t id);
149     void MediaTypeFound(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, const std::string& pluginName, int32_t id);
150     void InitAudioTrack(const StreamInfo& info);
151     void InitVideoTrack(const StreamInfo& info);
152     void InitSubtitleTrack(const StreamInfo& info);
153     void AddMediaInfo(int32_t streamID, Plugins::MediaInfo& mediaInfo);
154     static Status UpdateGeneralValue(int32_t trackCount, const Meta& format, Meta& formatNew);
155     static Status AddGeneral(const MediaStreamInfo& info, Meta& formatNew);
156 
157     Status AddTrackMapInfo(int32_t streamID, int32_t trackIndex);
158     Status UpdateMediaInfo(int32_t streamID);
159     bool IsSubtitleMime(const std::string& mime);
160     void WaitForInitialBufferingEnd(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, int32_t offset, int32_t size);
161 private:
162     std::map<int32_t, MediaStreamInfo> streamInfoMap_; // <streamId, MediaStreamInfo>
163     std::map<int32_t, MediaTrackMap> trackInfoMap_;    // 保存所有的track信息,使用映射的trackID <trackId, MediaTrackMap>
164     std::map<int32_t, MediaTrackMap> temp2TrackInfoMap_;     // 保存正在播放的track和tempTrackInfoMap_索引映射
165     int32_t curVideoStreamID_ = -1;
166     int32_t curAudioStreamID_ = -1;
167     int32_t curSubTitleStreamID_ = -1;
168 
169     Plugins::MediaInfo curMediaInfo_;
170     bool isDash_ = false;
171     bool needResetEosStatus_ = false;
172     FairMutex initialBufferingEndMutex_ {};
173     std::atomic<bool> isInitialBufferingSucc_ = false;
174     std::atomic<bool> isInitialBufferingNotified_ = false;
175     ConditionVariable initialBufferingEndCond_;
176     int32_t apiVersion_ {0};
177     bool isHlsFmp4_ {false};
178 };
179 } // namespace Media
180 } // namespace OHOS
181 #endif // MEDIA_DEMUXER_H
182