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