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