1 /* 2 * Copyright (c) 2020 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 /** 17 * @addtogroup MultiMedia_MediaCommon 18 * @{ 19 * 20 * @brief Provides data types and media formats required for recording and playing audio and videos. 21 * 22 * 23 * @since 1.0 24 * @version 1.0 25 */ 26 27 /** 28 * @file source.h 29 * 30 * @brief Declares the <b>Source</b> class, which is used to implement source-related operations. 31 * 32 * 33 * @since 1.0 34 * @version 1.0 35 */ 36 37 #ifndef SOURCE_H 38 #define SOURCE_H 39 #include <memory> 40 #include <map> 41 #include <string> 42 #include "data_stream.h" 43 #include "format.h" 44 #ifndef SURFACE_DISABLED 45 #include "surface.h" 46 #endif 47 48 using std::shared_ptr; 49 50 namespace OHOS { 51 namespace Media { 52 /** 53 * @brief Enumerates media source types. 54 * 55 * @since 1.0 56 * @version 1.0 57 */ 58 enum class SourceType : int32_t { 59 /** Local file path or network address */ 60 SOURCE_TYPE_URI = 0, 61 /** Local file descriptor */ 62 SOURCE_TYPE_FD, 63 /** Stream data, such as Advanced Audio Coding (AAC) stream data */ 64 SOURCE_TYPE_STREAM, 65 }; 66 67 /** 68 * @brief Provides functions to obtain the address of a buffer memory and write the filled buffers into the playback 69 * queue. You need to implement the <b>StreamCallback</b> functions in a player object. 70 * 71 * @since 1.0 72 * @version 1.0 73 */ 74 struct StreamCallback { 75 virtual ~StreamCallback()=default; 76 77 /** 78 * @brief Enumerates buffer types of stream sources. 79 * 80 * @since 1.0 81 * @version 1.0 82 */ 83 enum BufferFlags : uint32_t { 84 /** Synchronous frame */ 85 STREAM_FLAG_SYNCFRAME = 1, 86 /** Codec configuration information */ 87 STREAM_FLAG_CODECCONFIG = 2, 88 /** End of Stream (EOS) */ 89 STREAM_FLAG_EOS = 4, 90 /** Part of a frame */ 91 STREAM_FLAG_PARTIAL_FRAME = 8, 92 /** End of a frame. It is used in pair with <b>STREAM_FLAG_PARTIAL_FRAME</b>. */ 93 STREAM_FLAG_ENDOFFRAME = 16, 94 /** Container file data, such as MP4 file data (not supported yet) */ 95 STREAM_FLAG_MUXER_DATA = 32, 96 }; 97 98 /** 99 * @brief Obtains the virtual address of a buffer memory block based on its index. 100 * 101 * @param index Indicates the index of the buffer memory block. 102 * @return Returns the pointer to the virtual address of the buffer memory block. 103 * @since 1.0 104 * @version 1.0 105 */ 106 virtual uint8_t *GetBuffer(size_t index) = 0; 107 108 /** 109 * @brief Writes the filled buffer memory block into the player memory. 110 * 111 * @param index Indicates the index of the buffer memory block. 112 * @param offset Indicates the start offset into which the buffer memory block will be written. 113 * @param size Indicates the size of the data filled in the buffer memory block. 114 * @param timestampUs Indicates the timestamp of the frame filled in the buffer memory block. As data in AAC 115 * streams can be filled not on a frame basis, set this parameter to <b>0</b> for AAC streams. 116 * @param flags Indicates the type of the current buffer memory block. For details, see {@link BufferFlags}. 117 * @since 1.0 118 * @version 1.0 119 */ 120 virtual void QueueBuffer(size_t index, size_t offset, size_t size, int64_t timestampUs, uint32_t flags) = 0; 121 122 /** 123 * @brief Sets additional information about a stream. 124 * 125 * @param params Indicates the parameters for additional stream information. For details, see {@link Format}. 126 * @since 1.0 127 * @version 1.0 128 */ 129 virtual void SetParameters(const Format ¶ms) = 0; 130 }; 131 132 /** 133 * @brief Provides functions related to the stream source for upper-layer applications. 134 * 135 * After the {@link SetSource} function is called, the player invokes {@link OnBufferAvailable} to notify your 136 * application of the buffer memory block that can be filled with data.\n 137 * The player can invoke {@link SetStreamCallback} to register a callback for your application. For example, 138 * the {@link GetBuffer} callback obtains the address of the buffer block and sends the filled buffer memory block to 139 * the player. The buffer memory block is allocated and processed on the player.\n 140 * <b>StreamSource</b>is available only for the media source of the <b>SOURCE_TYPE_STREAM</b> type. 141 * For details, see {@link SourceType}.\n 142 * 143 * @since 1.0 144 * @version 1.0 145 */ 146 class StreamSource { 147 public: 148 StreamSource(void); 149 150 virtual ~StreamSource(void); 151 152 #ifndef SURFACE_DISABLED 153 void SetSurface(Surface* surface); 154 155 Surface* GetSurface(void); 156 #endif 157 158 uint8_t* GetSharedBuffer(size_t& size); 159 160 int QueueSharedBuffer(void* buffer, size_t size); 161 /** 162 * @brief Notifies your application of the information about the buffer memory block that can be filled with data. 163 * 164 * @param index Indicates the index of the buffer memory block. 165 * @param offset Indicates the start offset into which the data will be written. 166 * @param size Indicates the size of data that the buffer memory block can store. 167 * @since 1.0 168 * @version 1.0 169 */ OnBufferAvailable(size_t index,size_t offset,size_t size)170 virtual void OnBufferAvailable(size_t index, size_t offset, size_t size) {} 171 172 /** 173 * @brief Sets a callback function for your application. 174 * 175 * @param callback Indicates the {@link StreamCallback} function to set. 176 * @since 1.0 177 * @version 1.0 178 */ SetStreamCallback(const std::shared_ptr<StreamCallback> & callback)179 virtual void SetStreamCallback(const std::shared_ptr<StreamCallback> &callback) {} 180 181 private: 182 183 #ifndef SURFACE_DISABLED 184 Surface* surface_; 185 SurfaceBuffer* curBuffer_; 186 #endif 187 }; 188 189 /** 190 * @brief Provides functions to implement source-related operations. 191 * 192 * @since 1.0 193 * @version 1.0 194 */ 195 class Source { 196 public: 197 /** 198 * @brief A constructor used to create a {@link Source} instance based on a specified URI. 199 * 200 * @param uri Indicates the media source URI, which can be a network URI or local file path. 201 * @since 1.0 202 * @version 1.0 203 */ 204 explicit Source(const std::string& uri); 205 206 /** 207 * @brief A constructor used to create a {@link Source} instance based on a specified URI and header. 208 * 209 * If the HTTP URL header does not carry valid information for network playback, this function is equivalent to 210 * {@link Source(const std::string& uri)}. 211 * 212 * @param uri Indicates the media source URI. 213 * @param header Indicates the header. 214 * @since 1.0 215 * @version 1.0 216 */ 217 Source(const std::string &uri, const std::map<std::string, std::string> &header); 218 219 /** 220 * @brief A constructor used to create a {@link Source} instance based on the stream source and format information. 221 * 222 * 223 * 224 * @param stream Indicates the media source stream. For details, see {@link StreamSource}. 225 * @param formats Indicates stream data information, which is subject to the stream type. For example, the key 226 * is {@link CODEC_MIME}, and the value is {@link MIME_AUDIO_AAC}. For details, see {@link Format}. This parameter 227 * can be null if no information needs to be passed. 228 * @since 1.0 229 * @version 1.0 230 */ 231 Source(const std::shared_ptr<StreamSource> &stream, const Format &formats); 232 233 /** 234 * @brief A constructor used to create a {@link Source} instance based on the data stream consumer. 235 * 236 * @param dataConsumer Indicates the data stream consumer. For details, see {@link DataConsumer}. 237 * @since 1.0 238 * @version 1.0 239 */ 240 explicit Source(const std::shared_ptr<DataConsumer> &dataConsumer); 241 242 ~Source() = default; 243 244 /** 245 * @brief Obtains the source type. 246 * 247 * @return Returns the source type. For details, see {@link SourceType}. 248 * @since 1.0 249 * @version 1.0 250 */ 251 SourceType GetSourceType() const; 252 253 /** 254 * @brief Obtains the media source URI. 255 * 256 * This function is called only when the {@link SourceType} is {@link SOURCE_TYPE_URI}. 257 * 258 * @return Returns the media source URI. 259 * @since 1.0 260 * @version 1.0 261 */ 262 const std::string &GetSourceUri() const; 263 264 /** 265 * @brief Obtains the HTTP header for the media source. 266 * 267 * This function is called only when the {@link SourceType} is {@link SOURCE_TYPE_URI}. 268 * 269 * @return Returns the media source header. 270 * @since 1.0 271 * @version 1.0 272 */ 273 const std::map<std::string, std::string> &GetSourceHeader() const; 274 275 /** 276 * @brief Obtains information about the media source stream. 277 * 278 * This function is called only when the {@link SourceType} is {@link SOURCE_TYPE_STREAM}. 279 * 280 * @return Returns information about the media source stream. For details, see {@link StreamSource}. 281 * @since 1.0 282 * @version 1.0 283 */ 284 const std::shared_ptr<StreamSource> &GetSourceStream() const; 285 286 /** 287 * @brief Obtains the media source stream format. 288 * 289 * @return Returns the media source stream format. For details, see {@link Format}. 290 * @since 1.0 291 * @version 1.0 292 */ 293 const Format &GetSourceStreamFormat() const; 294 295 /** 296 * @brief Obtains the data stream consumer interface. 297 * 298 * This function is called only when the {@link SourceType} is {@link SOURCE_TYPE_STREAM}. 299 * 300 * @return Returns the data stream consumer interface. For details, see {@link DataConsumer}. 301 * @since 1.0 302 * @version 1.0 303 */ 304 const std::shared_ptr<DataConsumer> &GetDataConsumer() const; 305 306 private: 307 std::string uri_; 308 SourceType sourceType_; 309 std::map<std::string, std::string> header_; 310 std::shared_ptr<StreamSource> stream_; 311 Format format_; 312 std::shared_ptr<DataConsumer> dataConsumer_; 313 }; 314 } // namespace Media 315 } // namespace OHOS 316 #endif // SOURCE_H 317