1# AVDemuxer 2 3 4## Overview 5 6The AVDemuxer module provides the APIs for extracting [samples](../../media/avcodec/audio-video-demuxer.md) from media streams. 7 8For details about the development guide and sample, see [Media Data Demuxing](../../media/avcodec/audio-video-demuxer.md). 9 10**System capability**: SystemCapability.Multimedia.Media.Spliter 11 12**Since**: 10 13 14 15## Summary 16 17 18### File 19 20| Name| Description| 21| -------- | -------- | 22| [native_avdemuxer.h](native__avdemuxer_8h.md) | Declares the APIs for parsing audio and video media data.| 23 24### Types 25 26| Name| Description| 27| -------- | -------- | 28| typedef void(* [DRM_MediaKeySystemInfoCallback](#drm_mediakeysysteminfocallback)) (DRM_MediaKeySystemInfo *mediaKeySystemInfo) | Defines a pointer to the callback function for [DRM_MediaKeySystemInfo](../apis-drm-kit/_d_r_m___media_key_system_info.md). No demuxer instance is returned. This callback function applies to the scenario where a single demuxer instance is used. You need to call [OH_AVDemuxer_SetMediaKeySystemInfoCallback](#oh_avdemuxer_setmediakeysysteminfocallback) to set the callback function as a callback. (This type is deprecated from API version 14.) | 29| typedef void(* [Demuxer_MediaKeySystemInfoCallback](#demuxer_mediakeysysteminfocallback)) (OH_AVDemuxer *demuxer, DRM_MediaKeySystemInfo *mediaKeySystemInfo)| Defines a pointer to the callback function for [DRM_MediaKeySystemInfo](../apis-drm-kit/_d_r_m___media_key_system_info.md). A demuxer instance is returned. This callback function applies to the scenario where multiple demuxer instances are used. You need to call [OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback](#oh_avdemuxer_setdemuxermediakeysysteminfocallback) to set the callback function as a callback. This callback function is recommended. | 30 31 32### Functions 33 34| Name| Description| 35| -------- | -------- | 36| OH_AVDemuxer \* [OH_AVDemuxer_CreateWithSource](#oh_avdemuxer_createwithsource) ([OH_AVSource](_a_v_source.md#oh_avsource) \*source) | Creates an **OH_AVDemuxer** instance based on a source instance.| 37| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_Destroy](#oh_avdemuxer_destroy) (OH_AVDemuxer \*demuxer) | Destroys an **OH_AVDemuxer** instance and clears internal resources.| 38| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_SelectTrackByID](#oh_avdemuxer_selecttrackbyid) (OH_AVDemuxer \*demuxer, uint32_t trackIndex) | Selects a track from which the demuxer reads sample data.| 39| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_UnselectTrackByID](#oh_avdemuxer_unselecttrackbyid) (OH_AVDemuxer \*demuxer, uint32_t trackIndex) | Deselects a track. The demuxer no longer reads sample data from a track after it is deselected.| 40| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_ReadSample](#oh_avdemuxer_readsample) (OH_AVDemuxer \*demuxer, uint32_t trackIndex, [OH_AVMemory](_core.md#oh_avmemory) \*sample, [OH_AVCodecBufferAttr](_o_h___a_v_codec_buffer_attr.md) \*info) | Reads the sample and related information from the specified track. (This function is deprecated from API version 11.) | 41| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_ReadSampleBuffer](#oh_avdemuxer_readsamplebuffer) (OH_AVDemuxer \*demuxer, uint32_t trackIndex, [OH_AVBuffer](_core.md#oh_avbuffer) \*sample) | Reads the sample of the specified track.| 42| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_SeekToTime](#oh_avdemuxer_seektotime) (OH_AVDemuxer \*demuxer, int64_t millisecond, [OH_AVSeekMode](_codec_base.md#oh_avseekmode) mode) | Seeks to the specified time for all the selected tracks based on a [seek mode](_codec_base.md#oh_avseekmode-1). | 43| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_SetMediaKeySystemInfoCallback](#oh_avdemuxer_setmediakeysysteminfocallback) (OH_AVDemuxer \*demuxer, DRM_MediaKeySystemInfoCallback callback) | Sets an asynchronous callback for obtaining the media key system information. (This function is deprecated from API version 14.) | 44| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback](#oh_avdemuxer_setdemuxermediakeysysteminfocallback) (OH_AVDemuxer \*demuxer, Demuxer_MediaKeySystemInfoCallback callback) | Sets an asynchronous callback for obtaining the media key system information. | 45| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVDemuxer_GetMediaKeySystemInfo](#oh_avdemuxer_getmediakeysysteminfo) (OH_AVDemuxer \*demuxer, DRM_MediaKeySystemInfo \*mediaKeySystemInfo) | Obtains the media key system information. | 46 47 48## Type Description 49 50### Demuxer_MediaKeySystemInfoCallback 51 52``` 53typedef void(* Demuxer_MediaKeySystemInfoCallback) (OH_AVDemuxer *demuxer, DRM_MediaKeySystemInfo *mediaKeySystemInfo) 54``` 55 56**Description** 57 58Defines a pointer to the callback function for the media key system information. 59 60**System capability**: SystemCapability.Multimedia.Media.Spliter 61 62**Since**: 12 63 64 65### DRM_MediaKeySystemInfoCallback 66 67``` 68typedef void(* DRM_MediaKeySystemInfoCallback) (DRM_MediaKeySystemInfo *mediaKeySystemInfo) 69``` 70 71**Description** 72 73Defines a pointer to the callback function for the media key system information. 74 75**System capability**: SystemCapability.Multimedia.Media.Spliter 76 77**Since**: 11 78 79**Deprecated from**: 14 80 81**Substitute**: [Demuxer_MediaKeySystemInfoCallback](#demuxer_mediakeysysteminfocallback) 82 83 84## Function Description 85 86 87### OH_AVDemuxer_CreateWithSource() 88 89``` 90OH_AVDemuxer* OH_AVDemuxer_CreateWithSource (OH_AVSource *source) 91``` 92 93**Description** 94 95Creates an **OH_AVDemuxer** instance based on a source instance. 96 97For details about how to create, destroy, and use a source instance, see [OH_AVSource](_a_v_source.md). 98 99**System capability**: SystemCapability.Multimedia.Media.Spliter 100 101**Since**: 10 102 103**Parameters** 104 105| Name| Description| 106| -------- | -------- | 107| source | Pointer to an **OH_AVSource** instance. | 108 109**Returns** 110 111Returns the pointer to an **OH_AVDemuxer** instance if the operation is successful; returns NULL otherwise. 112 113The possible causes of an operation failure are as follows: 1141. The value of **source** is invalid, that is, a null pointer. 1152. The value of **source** does not point to an **OH_AVSource** instance. 116 117 118### OH_AVDemuxer_Destroy() 119 120``` 121OH_AVErrCode OH_AVDemuxer_Destroy (OH_AVDemuxer *demuxer) 122``` 123 124**Description** 125 126Destroys an **OH_AVDemuxer** instance and clears internal resources. An instance can be destroyed only once. 127 128The destroyed instance cannot be used until it is re-created. You are advised to set the pointer to **NULL** after the instance is destroyed. 129 130**System capability**: SystemCapability.Multimedia.Media.Spliter 131 132**Since**: 10 133 134**Parameters** 135 136| Name| Description| 137| -------- | -------- | 138| demuxer | Pointer to an **OH_AVDemuxer** instance. | 139 140**Returns** 141 142Returns one of the following result codes: 143 144**AV_ERR_OK**: The operation is successful. 145 146**AV_ERR_INVALID_VAL**: The value of **demuxer** is a null pointer or does not point to a demuxer instance. 147 148 149### OH_AVDemuxer_GetMediaKeySystemInfo() 150 151``` 152OH_AVErrCode OH_AVDemuxer_GetMediaKeySystemInfo (OH_AVDemuxer *demuxer, DRM_MediaKeySystemInfo *mediaKeySystemInfo) 153``` 154**Description** 155 156Obtains the media key system information. 157 158The media key system information can be obtained only after [Demuxer_MediaKeySystemInfoCallback](#demuxer_mediakeysysteminfocallback) or [DRM_MediaKeySystemInfoCallback](#drm_mediakeysysteminfocallback) is successfully invoked. 159 160**System capability**: SystemCapability.Multimedia.Media.Spliter 161 162**Since**: 11 163 164**Parameters** 165 166| Name| Description| 167| -------- | -------- | 168| demuxer | Pointer to an **OH_AVDemuxer** instance. | 169| mediaKeySystemInfo | Pointer to the media key system information. For details, see [DRM_MediaKeySystemInfo](../apis-drm-kit/_d_r_m___media_key_system_info.md). | 170 171**Returns** 172 173Returns one of the following result codes: 174 175**AV_ERR_OK**: The operation is successful. 176 177**AV_ERR_INVALID_VAL**: 178 179 1. The value of **demuxer** is a null pointer or does not point to a demuxer instance. 180 2. The value of **mediaKeySystemInfo** is a null pointer. 181 182 183### OH_AVDemuxer_ReadSample() 184 185``` 186OH_AVErrCode OH_AVDemuxer_ReadSample (OH_AVDemuxer *demuxer, uint32_t trackIndex, OH_AVMemory *sample, OH_AVCodecBufferAttr *info) 187``` 188 189**Description** 190 191Reads the sample and related information from the specified track. 192 193You must select a track before reading the sample. After this API is called, the demuxer automatically proceeds to the next frame. 194 195**System capability**: SystemCapability.Multimedia.Media.Spliter 196 197**Since**: 10 198 199**Deprecated from**: 11 200 201**Substitute API**: [OH_AVDemuxer_ReadSampleBuffer](#oh_avdemuxer_readsamplebuffer) 202 203**Parameters** 204 205| Name| Description| 206| -------- | -------- | 207| demuxer | Pointer to an **OH_AVDemuxer** instance. | 208| trackIndex | Index of the track from which the compressed frame is to be read. | 209| sample | Pointer to the **OH_AVMemory** instance for storing the compressed frame data. | 210| info | Pointer to the **OH_AVCodecBufferAttr** instance for storing the compressed frame information. | 211 212**Returns** 213 214Returns one of the following result codes: 215 216**AV_ERR_OK**: The operation is successful. 217 218**AV_ERR_INVALID_VAL**: 219 220 1. The value of **demuxer** is a null pointer or does not point to a demuxer instance. 221 2. The track index is out of range. 222 3. Track reading is not supported. 223 4. The value of **sample** is null. 224 5. The value of **info** is null. 225 226**AV_ERR_OPERATE_NOT_PERMIT**: 227 228 1. The track with the specified index is not selected. 229 2. The demuxer is not correctly initialized. 230 231**AV_ERR_NO_MEMORY**: The sample capacity is insufficient to store all frame data. 232 233**AV_ERR_UNKNOWN**: Failed to read or parse the frame from the file. 234 235 236### OH_AVDemuxer_ReadSampleBuffer() 237 238``` 239OH_AVErrCode OH_AVDemuxer_ReadSampleBuffer (OH_AVDemuxer *demuxer, uint32_t trackIndex, OH_AVBuffer *sample) 240``` 241 242**Description** 243 244Reads the sample and related information from the specified track. 245 246You can use [OH_AVDemuxer_SelectTrackByID](#oh_avdemuxer_selecttrackbyid) to select a track before reading the sample. After this API is called, the demuxer automatically proceeds to the next frame. 247 248**System capability**: SystemCapability.Multimedia.Media.Spliter 249 250**Since**: 11 251 252**Parameters** 253 254| Name| Description| 255| -------- | -------- | 256| demuxer | Pointer to an **OH_AVDemuxer** instance. | 257| trackIndex | Index of the track from which the compressed frame is to be read. | 258| sample | Pointer to the **OH_AVBuffer** instance for storing the compressed frame data and related information. | 259 260**Returns** 261 262Returns one of the following result codes: 263 264**AV_ERR_OK**: The operation is successful. 265 266**AV_ERR_INVALID_VAL**: 267 268 1. The value of **demuxer** is a null pointer or does not point to a demuxer instance. 269 2. The value of **sample** is a null pointer. 270 3. The track index is out of range. 271 4. The value of **sample** is null. 272 273**AV_ERR_OPERATE_NOT_PERMIT**: 274 275 1. The track with the specified index is not selected. 276 2. The demuxer is not correctly initialized. 277 278**AV_ERR_NO_MEMORY**: The sample capacity is insufficient to store all frame data. 279 280**AV_ERR_UNKNOWN**: Failed to read or parse the frame from the file. 281 282 283### OH_AVDemuxer_SeekToTime() 284 285``` 286OH_AVErrCode OH_AVDemuxer_SeekToTime (OH_AVDemuxer *demuxer, int64_t millisecond, OH_AVSeekMode mode) 287``` 288 289**Description** 290 291Seeks to the specified time for all the selected tracks based on a [seek mode](_codec_base.md#oh_avseekmode-1). 292 293**System capability**: SystemCapability.Multimedia.Media.Spliter 294 295**Since**: 10 296 297**Parameters** 298 299| Name| Description| 300| -------- | -------- | 301| demuxer | Pointer to an **OH_AVDemuxer** instance. | 302| millisecond | Time to seek to, in milliseconds. The timestamp is relative to the start position of the file. | 303| mode | Seek mode. For details, see [OH_AVSeekMode](_codec_base.md#oh_avseekmode). | 304 305**Returns** 306 307Returns one of the following result codes: 308 309**AV_ERR_OK**: The operation is successful. 310 311**AV_ERR_INVALID_VAL**: 312 313 1. The value of **demuxer** is a null pointer or does not point to a demuxer instance. 314 2. The value of **millisecond** is out of range. 315 316**AV_ERR_OPERATE_NOT_PERMIT**: 317 318 1. The track with the specified index is not selected. 319 2. The demuxer is not correctly initialized. 320 3. The seek operation cannot be performed on the resource. 321 322**AV_ERR_UNKNOWN**: 323 324 1. The seek operation fails. 325 2. **OH_AVSeekMode** is set to **SEEK_MODE_NEXT_SYNC** and there is no I-frame following the specified position. 326 327 328### OH_AVDemuxer_SelectTrackByID() 329 330``` 331OH_AVErrCode OH_AVDemuxer_SelectTrackByID (OH_AVDemuxer *demuxer, uint32_t trackIndex) 332``` 333 334**Description** 335 336Selects a track from which the demuxer reads sample data. 337 338You can select multiple tracks by calling this API multiple times, with a different track index passed in each time. 339 340When **OH_AVDemuxer_ReadSample** is called, only the data in the selected track is read. If the same track is selected multiple times, **AV_ERR_OK** is returned and the API call takes effect only once. 341 342**System capability**: SystemCapability.Multimedia.Media.Spliter 343 344**Since**: 10 345 346**Parameters** 347 348| Name| Description| 349| -------- | -------- | 350| demuxer | Pointer to an **OH_AVDemuxer** instance. | 351| trackIndex | Index of the track. | 352 353**Returns** 354 355Returns one of the following result codes: 356 357**AV_ERR_OK**: The operation is successful. 358 359**AV_ERR_OPERATE_NOT_PERMIT**: The demuxer is not correctly initialized. 360 361**AV_ERR_INVALID_VAL**: 362 363 1. The value of **demuxer** is a null pointer or does not point to a demuxer instance. 364 2. The track index is out of range. 365 3. Track reading is not supported. 366 367 368### OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback() 369 370``` 371OH_AVErrCode OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback (OH_AVDemuxer *demuxer, Demuxer_MediaKeySystemInfoCallback callback) 372``` 373 374**Description** 375 376Sets a callback for obtaining the media key system information. 377 378**System capability**: SystemCapability.Multimedia.Media.Spliter 379 380**Since**: 12 381 382**Parameters** 383 384| Name| Description| 385| -------- | -------- | 386| demuxer | Pointer to an **OH_AVDemuxer** instance. | 387| callback | Callback function. For details, see [Demuxer_MediaKeySystemInfoCallback](#demuxer_mediakeysysteminfocallback). | 388 389**Returns** 390 391Returns one of the following result codes: 392 393**AV_ERR_OK**: The operation is successful. 394 395**AV_ERR_OPERATE_NOT_PERMIT**: The demuxer is not correctly initialized. 396 397**AV_ERR_INVALID_VAL**: The value of **demuxer** is a null pointer or does not point to a demuxer instance. 398 399 400### OH_AVDemuxer_SetMediaKeySystemInfoCallback() 401 402``` 403OH_AVErrCode OH_AVDemuxer_SetMediaKeySystemInfoCallback (OH_AVDemuxer *demuxer, DRM_MediaKeySystemInfoCallback callback) 404``` 405**Description** 406 407Sets a callback for obtaining the media key system information. 408 409**System capability**: SystemCapability.Multimedia.Media.Spliter 410 411**Since**: 11 412 413**Deprecated from**: 14 414 415**Substitute**: [OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback](#oh_avdemuxer_setdemuxermediakeysysteminfocallback) 416 417**Parameters** 418 419| Name| Description| 420| -------- | -------- | 421| demuxer | Pointer to an **OH_AVDemuxer** instance. | 422| callback | Callback function. For details, see [DRM_MediaKeySystemInfoCallback](#drm_mediakeysysteminfocallback). | 423 424**Returns** 425 426Returns one of the following result codes: 427 428**AV_ERR_OK**: The operation is successful. 429 430**AV_ERR_OPERATE_NOT_PERMIT**: The demuxer is not correctly initialized. 431 432**AV_ERR_INVALID_VAL**: The value of **demuxer** is a null pointer or does not point to a demuxer instance. 433 434 435### OH_AVDemuxer_UnselectTrackByID() 436 437``` 438OH_AVErrCode OH_AVDemuxer_UnselectTrackByID (OH_AVDemuxer *demuxer, uint32_t trackIndex) 439``` 440 441**Description** 442 443Deselects a track. The demuxer no longer reads sample data from a track after it is deselected. 444 445You can deselect multiple tracks by calling this API multiple times, with a different track index passed in each time. 446 447If the same track is deselected multiple times, **AV_ERR_OK** is returned and the API call takes effect only once. 448 449**System capability**: SystemCapability.Multimedia.Media.Spliter 450 451**Since**: 10 452 453**Parameters** 454 455| Name| Description| 456| -------- | -------- | 457| demuxer | Pointer to an **OH_AVDemuxer** instance. | 458| trackIndex | Index of the track. | 459 460**Returns** 461 462Returns one of the following result codes: 463 464**AV_ERR_OK**: The operation is successful. 465 466**AV_ERR_OPERATE_NOT_PERMIT**: The demuxer is not correctly initialized. 467 468**AV_ERR_INVALID_VAL**: The value of **demuxer** is a null pointer or does not point to a demuxer instance. 469