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