• 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 HISTREAMER_PLUGIN_MEDIA_SOURCE_H
17 #define HISTREAMER_PLUGIN_MEDIA_SOURCE_H
18 
19 #include <map>
20 #include <memory>
21 #ifndef OHOS_LITE
22 #include "common/media_data_source.h"
23 #endif
24 #include "meta/media_types.h"
25 
26 namespace OHOS {
27 namespace Media {
28 namespace Plugins {
29 /// End of Stream Buffer Flag
30 #define BUFFER_FLAG_EOS 0x00000001
31 /// Video Key Frame Flag
32 #define BUFFER_FLAG_KEY_FRAME 0x00000002
33 /**
34  * @brief Unified enumerates media source types.
35  *
36  * @since 1.0
37  * @version 1.0
38  */
39 enum class SourceType : int32_t {
40     /** Local file path or network address */
41     SOURCE_TYPE_URI = 0,
42     /** Local file descriptor */
43     SOURCE_TYPE_FD,
44     /** Stream data */
45     SOURCE_TYPE_STREAM,
46 };
47 
48 typedef struct PlayStrategy {
49     uint32_t width {0};
50     uint32_t height {0};
51     uint32_t duration {0};
52     bool preferHDR {false};
53     std::string audioLanguage {""};
54     std::string subtitleLanguage {""};
55     double bufferDurationForPlaying {0};
56     double thresholdForAutoQuickPlay {0};
57 } PlayStrategy;
58 
59 struct PlayMediaStream {
60     std::string url {""};
61     uint32_t width {0};
62     uint32_t height {0};
63     uint32_t bitrate {0};
64 };
65 
66 class AVMimeTypes {
67 public:
68     static constexpr std::string_view APPLICATION_M3U8 = "application/m3u8";
69 };
70 
71 typedef struct DownloadInfo {
72     int32_t avgDownloadRate {0};
73     int32_t avgDownloadSpeed {0};
74     uint64_t totalDownLoadBits {0};
75     bool isTimeOut {false};
76 } DownloadInfo;
77 
78 typedef struct PlaybackInfo {
79     std::string serverIpAddress {};
80     int64_t averageDownloadRate = 0;
81     int64_t downloadRate = 0;
82     bool isDownloading = false;
83     int64_t bufferDuration = 0;
84 } PlaybackInfo;
85 
86 /**
87  * IMediaSourceLoader::OPEN & IMediaSourceLoadingRequest::RespondData
88  */
89 enum MediaSourceError : int32_t {
90     /**
91      * The callback interface times out and needs to be retried.
92      */
93     MEDIA_SOURCE_ERROR_RETRY = 0,
94     /**
95      * Used for internal exceptions
96      */
97     MEDIA_SOURCE_ERROR_IO = -1,
98 };
99 
100 // preDonwload start
101 enum class LoadingRequestError : int32_t {
102     /**
103      * If reach the resource end, client should return.
104      */
105     LOADING_ERROR_SUCCESS = 0,
106 
107     /**
108      * If resource not reay for access, client should return.
109      */
110     LOADING_ERROR_NOT_READY = 1,
111 
112     /**
113      * If resource url not exit, client should return.
114      */
115     LOADING_ERROR_NO_RESOURCE  = 2,
116 
117     /**
118      * If the uuid of resource handle invalid, client should return.
119      */
120     LOADING_ERROR_INVAID_HANDLE = 3,
121 
122     /**
123      * If client has no right to request the resouce, client should return.
124      */
125     LOADING_ERROR_ACCESS_DENIED = 4,
126 
127     /**
128      * If access time out, client should return by this error.
129      */
130     LOADING_ERROR_ACCESS_TIMEOUT = 5,
131 
132     /**
133      * If authorization failed, client should return this error.
134      */
135     LOADING_ERROR_AUTHORIZE_FAILED = 6,
136 };
137 
138 class IMediaSourceLoadingRequest {
139 public:
140     virtual ~IMediaSourceLoadingRequest() = default;
141 
142     virtual int32_t RespondData(int64_t uuid, int64_t offset, const std::shared_ptr<AVSharedMemory> &mem) = 0;
143 
144     virtual int32_t RespondHeader(int64_t uuid, std::map<std::string, std::string> header, std::string redirectUrl) = 0;
145 
146     virtual int32_t FinishLoading(int64_t uuid, LoadingRequestError requestedError) = 0;
147 
Release()148     virtual void Release() {};
149 
150 private:
151     std::string url_ {};
152     std::map<std::string, std::string> header_ {};
153 };
154 
155 class IMediaSourceLoader {
156 public:
157     virtual ~IMediaSourceLoader() = default;
158 
159     virtual int32_t Init(std::shared_ptr<IMediaSourceLoadingRequest> &request) = 0;
160 
161     virtual int64_t Open(const std::string &url, const std::map<std::string, std::string> &header) = 0;
162 
163     virtual int32_t Read(int64_t uuid, int64_t requestedOffset, int64_t requestedLength) = 0;
164 
165     virtual int32_t Close(int64_t uuid) = 0;
166 };
167 
168 using MediaStreamList = std::vector<std::shared_ptr<PlayMediaStream>>;
169 class MediaSource {
170 public:
171     /// Construct an a specified URI.
172     explicit MediaSource(std::string uri);
173 
174 #ifndef OHOS_LITE
175     explicit MediaSource(std::shared_ptr<IMediaDataSource> dataSrc);
176 #endif
177 
178     MediaSource(std::string uri, std::map<std::string, std::string> header);
179 
180     /// Destructor
181     virtual ~MediaSource() = default;
182 
183     /// Obtains the source type.
184     SourceType GetSourceType() const;
185 
186     /// Obtains the media source URI.
187     const std::string &GetSourceUri() const;
188 
189     const std::map<std::string, std::string> &GetSourceHeader() const;
190 
191     void AddMediaStream(const std::shared_ptr<PlayMediaStream>& playMediaStream);
192     MediaStreamList GetMediaStreamList() const;
193 
194     void SetPlayStrategy(const std::shared_ptr<PlayStrategy>& playStrategy);
195 
196     std::shared_ptr<PlayStrategy> GetPlayStrategy() const;
197 
198     void SetMimeType(const std::string& mimeType);
199 
200     std::string GetMimeType() const;
201 
202     void SetAppUid(int32_t appUid);
203 
204     int32_t GetAppUid();
205 
206     void SetSourceLoader(std::shared_ptr<IMediaSourceLoader> mediaSourceLoader);
207 
208     std::shared_ptr<IMediaSourceLoader> GetSourceLoader() const;
209 
210     //std::shared_ptr<DataConsumer> GetDataConsumer() const;
211 #ifndef OHOS_LITE
212     std::shared_ptr<IMediaDataSource> GetDataSrc() const;
213 #endif
214 private:
215     std::string uri_ {};
216     std::string mimeType_ {};
217     SourceType type_ {};
218     std::map<std::string, std::string> header_ {};
219     std::shared_ptr<PlayStrategy> playStrategy_ {};
220     int32_t appUid_ {-1};
221     //std::shared_ptr<DataConsumer> dataConsumer_ {};
222 #ifndef OHOS_LITE
223     std::shared_ptr<IMediaDataSource> dataSrc_ {};
224 #endif
225     std::shared_ptr<IMediaSourceLoader> sourceLoader_ {};
226     MediaStreamList playMediaStreamVec_;
227 };
228 } // namespace Plugins
229 } // namespace Media
230 } // namespace OHOS
231 #endif // HISTREAMER_PLUGIN_MEDIA_SOURCE_H