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