• 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 
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     std::mutex readMutex_;
67 };
68 
69 class MediaStreamInfo {
70 public:
71     int32_t streamID = -1;
72     bool activated = false;
73     StreamType type;
74     uint32_t bitRate;
75     std::string pluginName = "";
76     std::shared_ptr<Plugins::DemuxerPlugin> plugin = nullptr;
77     std::shared_ptr<DataSourceImpl> dataSource = nullptr;
78     Plugins::MediaInfo mediaInfo;   // dash中每个streamid只有一个track
79 };
80 
81 class MediaTrackMap {
82 public:
83     int32_t trackID = -1;
84     int32_t streamID = -1;
85     int32_t innerTrackIndex = -1;
86 };
87 
88 class DemuxerPluginManager {
89 public:
90     explicit DemuxerPluginManager();
91     virtual ~DemuxerPluginManager();
92 
93     Status InitDefaultPlay(const std::vector<StreamInfo>& streams);
94     std::shared_ptr<Plugins::DemuxerPlugin> GetPluginByStreamID(int32_t streamID);
95     void GetTrackInfoByStreamID(int32_t streamID, int32_t& trackId, int32_t& innerTrackId);
96 
97     int32_t GetTmpStreamIDByTrackID(int32_t trackId);
98     int32_t GetTmpInnerTrackIDByTrackID(int32_t trackId);
99     void UpdateTempTrackMapInfo(int32_t oldTrackId, int32_t newTrackId, int32_t newInnerTrackIndex);
100     void DeleteTempTrackMapInfo(int32_t oldTrackId);
101 
102     int32_t GetInnerTrackIDByTrackID(int32_t trackId);
103     StreamType GetStreamTypeByTrackID(int32_t trackId);
104     int32_t GetStreamIDByTrackID(int32_t trackId);
105     int32_t GetStreamIDByTrackType(TrackType type);
106     int32_t GetStreamDemuxerNewStreamID(TrackType trackType, std::shared_ptr<BaseStreamDemuxer> streamDemuxer);
107 
108     TrackType GetTrackTypeByTrackID(int32_t trackId);
109 
110     Status LoadCurrentAllPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, MediaInfo& mediaInfo);
111     Status LoadCurrentSubtitlePlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer,
112         Plugins::MediaInfo& mediaInfo);
113     Status LoadDemuxerPlugin(int32_t streamID, std::shared_ptr<BaseStreamDemuxer> streamDemuxer);
114     Status Reset();
115     Status Start();
116     Status Stop();
117     Status Flush();
118     Status SeekTo(int64_t seekTime, Plugins::SeekMode mode, int64_t& realSeekTime);
119     int32_t GetStreamID(int32_t trackId);
120     int32_t GetInnerTrackID(int32_t trackId);
121     bool IsDash() const;
122     Status StopPlugin(int32_t streamId, std::shared_ptr<BaseStreamDemuxer> streamDemuxer);
123     Status StartPlugin(int32_t streamId, std::shared_ptr<BaseStreamDemuxer> streamDemuxer);
124     Status RebootPlugin(int32_t streamId, TrackType trackType, std::shared_ptr<BaseStreamDemuxer> streamDemuxer,
125         bool& isRebooted);
126     Status UpdateDefaultStreamID(Plugins::MediaInfo& mediaInfo, StreamType type, int32_t newStreamID);
127 
128     std::shared_ptr<Meta> GetUserMeta();
129     uint32_t GetCurrentBitRate();
130     void SetResetEosStatus(bool flag);
131     size_t GetStreamCount() const;
132     Status SetCacheLimit(uint32_t limitSize);
133     bool CheckTrackIsActive(int32_t trackId);
134     int32_t AddExternalSubtitle();
135     Status localSubtitleSeekTo(int64_t seekTime);
136     void SetApiVersion(int32_t apiVersion);
137 private:
138     bool CreatePlugin(std::string pluginName, int32_t id);
139     bool InitPlugin(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, const std::string& pluginName, int32_t id);
140     void MediaTypeFound(std::shared_ptr<BaseStreamDemuxer> streamDemuxer, const std::string& pluginName, int32_t id);
141     void InitAudioTrack(const StreamInfo& info);
142     void InitVideoTrack(const StreamInfo& info);
143     void InitSubtitleTrack(const StreamInfo& info);
144     void AddMediaInfo(int32_t streamID, Plugins::MediaInfo& mediaInfo);
145     static Status UpdateGeneralValue(int32_t trackCount, const Meta& format, Meta& formatNew);
146     static Status AddGeneral(const MediaStreamInfo& info, Meta& formatNew);
147 
148     Status AddTrackMapInfo(int32_t streamID, int32_t trackIndex);
149     Status UpdateMediaInfo(int32_t streamID);
150     bool IsSubtitleMime(const std::string& mime);
151 private:
152     std::map<int32_t, MediaStreamInfo> streamInfoMap_; // <streamId, MediaStreamInfo>
153     std::map<int32_t, MediaTrackMap> trackInfoMap_;    // 保存所有的track信息,使用映射的trackID <trackId, MediaTrackMap>
154     std::map<int32_t, MediaTrackMap> temp2TrackInfoMap_;     // 保存正在播放的track和tempTrackInfoMap_索引映射
155     int32_t curVideoStreamID_ = -1;
156     int32_t curAudioStreamID_ = -1;
157     int32_t curSubTitleStreamID_ = -1;
158 
159     Plugins::MediaInfo curMediaInfo_;
160     bool isDash_ = false;
161     bool needResetEosStatus_ = false;
162     int32_t apiVersion_ {0};
163 };
164 } // namespace Media
165 } // namespace OHOS
166 #endif // MEDIA_DEMUXER_H
167