1# AVMuxer
2
3
4## Overview
5
6The AVMuxer module provides the APIs for audio and video muxing.
7
8For details about the development guide and sample, see [Media Data Muxing](../../media/avcodec/audio-video-muxer.md).
9
10**System capability**: SystemCapability.Multimedia.Media.Muxer
11
12**Since**: 10
13
14
15## Summary
16
17
18### Files
19
20| Name| Description|
21| -------- | -------- |
22| [native_avmuxer.h](native__avmuxer_8h.md) | Declares the native APIs used for audio and video muxing. |
23
24
25### Types
26
27| Name| Description|
28| -------- | -------- |
29| typedef struct [OH_AVMuxer](#oh_avmuxer) [OH_AVMuxer](#oh_avmuxer) | Defines a struct that describes a native object for the muxer interface. |
30
31
32### Functions
33
34| Name| Description|
35| -------- | -------- |
36| [OH_AVMuxer](#oh_avmuxer) \* [OH_AVMuxer_Create](#oh_avmuxer_create) (int32_t fd, [OH_AVOutputFormat](_codec_base.md#oh_avoutputformat) format) | Creates an **OH_AVMuxer** instance by using the file descriptor and container format. |
37| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_SetRotation](#oh_avmuxer_setrotation) ([OH_AVMuxer](#oh_avmuxer) \*muxer, int32_t rotation) | Sets the rotation angle (clockwise), which must be 0, 90, 180, or 270, of an output video. |
38| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_AddTrack](#oh_avmuxer_addtrack) ([OH_AVMuxer](#oh_avmuxer) \*muxer, int32_t \*trackIndex, [OH_AVFormat](_core.md#oh_avformat) \*trackFormat) | Adds an audio or video track to a muxer.|
39| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_Start](#oh_avmuxer_start) ([OH_AVMuxer](#oh_avmuxer) \*muxer) | Starts a muxer.|
40| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_WriteSample](#oh_avmuxer_writesample) ([OH_AVMuxer](#oh_avmuxer) \*muxer, uint32_t trackIndex, [OH_AVMemory](_core.md#oh_avmemory) \*sample, [OH_AVCodecBufferAttr](_o_h___a_v_codec_buffer_attr.md) info) | Writes a sample to a muxer. (This function is deprecated from API version 11.)|
41| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_WriteSampleBuffer](#oh_avmuxer_writesamplebuffer) ([OH_AVMuxer](#oh_avmuxer) \*muxer, uint32_t trackIndex, const [OH_AVBuffer](_core.md#oh_avbuffer) \*sample) | Writes a sample to a muxer. |
42| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_Stop](#oh_avmuxer_stop) ([OH_AVMuxer](#oh_avmuxer) \*muxer) | Stops a muxer. |
43| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_Destroy](#oh_avmuxer_destroy) ([OH_AVMuxer](#oh_avmuxer) \*muxer) | Clears internal resources and destroys an **OH_AVMuxer** instance. |
44
45
46## Type Description
47
48
49### OH_AVMuxer
50
51```
52typedef struct OH_AVMuxer OH_AVMuxer
53```
54**Description**
55
56Defines a struct that describes a native object for the muxer interface.
57
58**Since**: 10
59
60
61## Function Description
62
63
64### OH_AVMuxer_AddTrack()
65
66```
67OH_AVErrCode OH_AVMuxer_AddTrack (OH_AVMuxer *muxer, int32_t *trackIndex, OH_AVFormat *trackFormat)
68```
69
70**Description**
71
72Adds an audio or video track to a muxer.
73
74Each time this function is called, an audio or video track is added to the muxer. This function must be called before **OH_AVMuxer_Start**.
75
76**System capability**: SystemCapability.Multimedia.Media.Muxer
77
78**Since**: 10
79
80**Parameters**
81
82| Name| Description|
83| -------- | -------- |
84| muxer | Pointer to an **OH_AVMuxer** instance. |
85| trackIndex | Pointer to the index of the media track. The index will be used in the **OH_AVMuxer_WriteSample** function. If the audio or video track is added, the index value is greater than or equal to 0; otherwise, the value is less than 0. |
86| trackFormat | Pointer to an **OH_AVFormat** instance. |
87
88**Returns**
89
90Returns **AV_ERR_OK** if the operation is successful; returns an error code defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise.
91
92- Returns **AV_ERR_INVALID_VAL** if the muxer pointer is null, or the track index or track format is invalid.
93
94- Returns **AV_ERR_OPERATE_NOT_PERMIT** if the function is called in an invalid state.
95
96- Returns **AV_ERR_UNSUPPORT** if the MIME type is not supported. Returns **AV_ERR_NO_MEMORY** if memory allocation fails.
97
98- Returns **AV_ERR_UNKNOWN** in the case of an unknown error.
99
100
101### OH_AVMuxer_Create()
102
103```
104OH_AVMuxer* OH_AVMuxer_Create (int32_t fd, OH_AVOutputFormat format)
105```
106
107**Description**
108
109Creates an **OH_AVMuxer** instance by using the file descriptor and container format.
110
111**System capability**: SystemCapability.Multimedia.Media.Muxer
112
113**Since**: 10
114
115**Parameters**
116
117| Name| Description|
118| -------- | -------- |
119| fd | File descriptor (FD). You must open the file in read/write mode (O_RDWR) and close the file after using it. |
120| format | Format of the encapsulated output file. For details, see [OH_AVOutputFormat](_codec_base.md#oh_avoutputformat). |
121
122**Returns**
123
124Returns the pointer to the **OH_AVMuxer** instance created. You must call **OH_AVMuxer_Destroy** to destroy the instance when it is no longer needed.
125
126
127### OH_AVMuxer_Destroy()
128
129```
130OH_AVErrCode OH_AVMuxer_Destroy (OH_AVMuxer *muxer)
131```
132
133**Description**
134
135Clears internal resources and destroys an **OH_AVMuxer** instance.
136
137**System capability**: SystemCapability.Multimedia.Media.Muxer
138
139**Since**: 10
140
141**Parameters**
142
143| Name| Description|
144| -------- | -------- |
145| muxer | Pointer to an **OH_AVMuxer** instance. |
146
147**Returns**
148
149Returns **AV_ERR_OK** if the operation is successful; returns an error code defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise. You must set the muxer to null.
150
151- Returns **AV_ERR_INVALID_VAL** if the muxer pointer is null.
152
153
154### OH_AVMuxer_SetRotation()
155
156```
157OH_AVErrCode OH_AVMuxer_SetRotation (OH_AVMuxer *muxer, int32_t rotation)
158```
159
160**Description**
161
162Sets the rotation angle (clockwise), which must be 0, 90, 180, or 270, of an output video.
163
164This function must be called before **OH_AVMuxer_Start**.
165
166**System capability**: SystemCapability.Multimedia.Media.Muxer
167
168**Since**: 10
169
170**Parameters**
171
172| Name| Description|
173| -------- | -------- |
174| muxer | Pointer to an **OH_AVMuxer** instance. |
175| rotation | Angle to set. The value must be 0, 90, 180, or 270. |
176
177**Returns**
178
179Returns **AV_ERR_OK** if the operation is successful; returns an error code defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise.
180
181- Returns **AV_ERR_INVALID_VAL** if the muxer pointer is null or the value of **rotation** is invalid.
182
183- Returns **AV_ERR_OPERATE_NOT_PERMIT** if the function is called in an invalid state.
184
185
186
187**Parameters**
188
189| Name| Description|
190| -------- | -------- |
191| muxer | Pointer to an **OH_AVMuxer** instance. |
192| format | Pointer to an **OH_AVFormat** instance. It is a file-level metadata set. |
193
194**Returns**
195
196Returns **AV_ERR_OK** if the **format** parameter is correctly set.
197
198Returns **AV_ERR_INVALID_VAL** if the muxer pointer is null or the value of **format** is invalid.
199
200Returns **AV_ERR_OPERATE_NOT_PERMIT** if the function is called in an invalid state.
201
202
203### OH_AVMuxer_Start()
204
205```
206OH_AVErrCode OH_AVMuxer_Start (OH_AVMuxer *muxer)
207```
208
209**Description**
210
211Starts a muxer.
212
213This function must be called after **OH_AVMuxer_AddTrack** and before **OH_AVMuxer_WriteSample**.
214
215**System capability**: SystemCapability.Multimedia.Media.Muxer
216
217**Since**: 10
218
219**Parameters**
220
221| Name| Description|
222| -------- | -------- |
223| muxer | Pointer to an **OH_AVMuxer** instance. |
224
225**Returns**
226
227Returns **AV_ERR_OK** if the operation is successful; returns an error code defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise.
228
229- Returns **AV_ERR_INVALID_VAL** if the muxer pointer is null.
230
231- Returns **AV_ERR_OPERATE_NOT_PERMIT** if the function is called in an invalid state.
232
233- Returns **AV_ERR_UNKNOWN** in the case of an unknown error.
234
235
236### OH_AVMuxer_Stop()
237
238```
239OH_AVErrCode OH_AVMuxer_Stop (OH_AVMuxer *muxer)
240```
241
242**Description**
243
244Stops a muxer.
245
246Once the muxer is stopped, it cannot be restarted.
247
248**System capability**: SystemCapability.Multimedia.Media.Muxer
249
250**Since**: 10
251
252**Parameters**
253
254| Name| Description|
255| -------- | -------- |
256| muxer | Pointer to an **OH_AVMuxer** instance. |
257
258**Returns**
259
260Returns **AV_ERR_OK** if the operation is successful; returns an error code defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise.
261
262- Returns **AV_ERR_INVALID_VAL** if the muxer pointer is null.
263
264- Returns **AV_ERR_OPERATE_NOT_PERMIT** if the function is called in an invalid state.
265
266
267### OH_AVMuxer_WriteSample()
268
269```
270OH_AVErrCode OH_AVMuxer_WriteSample (OH_AVMuxer *muxer, uint32_t trackIndex, OH_AVMemory *sample, OH_AVCodecBufferAttr info)
271```
272
273**Description**
274
275Writes a sample to a muxer.
276
277This function must be called after **OH_AVMuxer_Start** and before **OH_AVMuxer_Stop**. The caller must write the sample to the correct audio or video track based on the timing in **info**.
278
279**System capability**: SystemCapability.Multimedia.Media.Muxer
280
281**Since**: 10
282
283**Deprecated from**: 11
284
285**Substitute API**: [OH_AVMuxer_WriteSampleBuffer](#oh_avmuxer_writesamplebuffer)
286
287**Parameters**
288
289| Name| Description|
290| -------- | -------- |
291| muxer | Pointer to an **OH_AVMuxer** instance. |
292| trackIndex | Index of the audio or video track corresponding to the data. |
293| sample | Pointer to the data obtained after encoding or demuxing. |
294| info | Sample description. For details, see **OH_AVCodecBufferAttr**. |
295
296**Returns**
297
298Returns **AV_ERR_OK** if the operation is successful; returns an error code defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise.
299
300- Returns **AV_ERR_INVALID_VAL** if the muxer pointer is null, or the track index, sample, or info is invalid.
301
302- Returns **AV_ERR_OPERATE_NOT_PERMIT** if the function is called in an invalid state.
303
304- Returns **AV_ERR_NO_MEMORY** if memory allocation fails. Returns **AV_ERR_UNKNOWN** in the case of an unknown error.
305
306
307### OH_AVMuxer_WriteSampleBuffer()
308
309```
310OH_AVErrCode OH_AVMuxer_WriteSampleBuffer (OH_AVMuxer *muxer, uint32_t trackIndex, const OH_AVBuffer *sample)
311```
312
313**Description**
314
315Writes a sample to a muxer.
316
317This function must be called after **OH_AVMuxer_Start** and before **OH_AVMuxer_Stop**. The caller must write the sample to the correct audio or video track based on the timing in **sample**.
318
319**System capability**: SystemCapability.Multimedia.Media.Muxer
320
321**Since**: 11
322
323**Parameters**
324
325| Name| Description|
326| -------- | -------- |
327| muxer | Pointer to an **OH_AVMuxer** instance. |
328| trackIndex | Index of the audio or video track corresponding to the data. |
329| sample | Pointer to the data obtained after encoding or demuxing. Data and data attributes are included. |
330
331**Returns**
332
333Returns **AV_ERR_OK** if the operation is successful; returns an error code defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise.
334
335- Returns **AV_ERR_INVALID_VAL** if the muxer pointer is null, or the track index or sample is invalid.
336
337- Returns **AV_ERR_OPERATE_NOT_PERMIT** if the function is called in an invalid state.
338
339- Returns **AV_ERR_NO_MEMORY** if memory allocation fails. Returns **AV_ERR_UNKNOWN** in the case of an unknown error.
340