1# AVMuxer 2 3<!--Kit: AVCodec Kit--> 4<!--Subsystem: Multimedia--> 5<!--Owner: @mr-chencxy--> 6<!--Designer: @dpy2650---> 7<!--Tester: @baotianhao--> 8<!--Adviser: @zengyawen--> 9 10## Overview 11 12The AVMuxer module provides the APIs for audio and video multiplexing. 13 14For details about the development guide and sample, see [Media Data Multiplexing](../../media/avcodec/audio-video-muxer.md). 15 16**System capability**: SystemCapability.Multimedia.Media.Muxer 17 18**Since**: 10 19 20 21## Summary 22 23 24### Files 25 26| Name| Description| 27| -------- | -------- | 28| [native_avmuxer.h](native__avmuxer_8h.md) | Declares the native APIs used for audio and video multiplexing. | 29 30 31### Types 32 33| Name| Description| 34| -------- | -------- | 35| typedef struct [OH_AVMuxer](#oh_avmuxer) [OH_AVMuxer](#oh_avmuxer) | Defines a struct for the native object for the muxer interface. | 36 37 38### Functions 39 40| Name| Description| 41| -------- | -------- | 42| [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. | 43| [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. | 44| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_SetFormat](#oh_avmuxer_setformat) ([OH_AVMuxer](#oh_avmuxer) \*muxer, [OH_AVFormat](_core.md#oh_avformat) \*format) | Sets format data to the muxer.| 45| [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.| 46| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_Start](#oh_avmuxer_start) ([OH_AVMuxer](#oh_avmuxer) \*muxer) | Starts a muxer.| 47| [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. (It is deprecated from API version 11.)| 48| [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. | 49| [OH_AVErrCode](_core.md#oh_averrcode) [OH_AVMuxer_Stop](#oh_avmuxer_stop) ([OH_AVMuxer](#oh_avmuxer) \*muxer) | Stops a muxer. | 50| [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. | 51 52 53## Type Description 54 55 56### OH_AVMuxer 57 58``` 59typedef struct OH_AVMuxer OH_AVMuxer 60``` 61**Description** 62 63Defines a struct for the native object for the muxer interface. 64 65**Since**: 10 66 67 68## Function Description 69 70 71### OH_AVMuxer_AddTrack() 72 73``` 74OH_AVErrCode OH_AVMuxer_AddTrack (OH_AVMuxer *muxer, int32_t *trackIndex, OH_AVFormat *trackFormat) 75``` 76 77**Description** 78 79Adds an audio or video track to a muxer. 80 81Each time this function is called, an audio or video track is added to the muxer. This function must be called before **OH_AVMuxer_Start**. 82 83**System capability**: SystemCapability.Multimedia.Media.Muxer 84 85**Since**: 10 86 87**Parameters** 88 89| Name| Description| 90| -------- | -------- | 91| muxer | Pointer to an OH_AVMuxer instance. | 92| 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. | 93| trackFormat | Pointer to an OH_AVFormat instance. | 94 95**Returns** 96 97**AV_ERR_OK** if the operation is successful; one of the following error codes defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise: 98 99- **AV_ERR_INVALID_VAL**: The muxer pointer is null, or the track index or track format is invalid. 100 101- **AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence. 102 103- **AV_ERR_UNSUPPORT**: The MIME type is not supported. 104 105- **AV_ERR_NO_MEMORY**: Memory allocation fails. 106 107- **AV_ERR_UNKNOWN**: Unknown error. 108 109 110### OH_AVMuxer_Create() 111 112``` 113OH_AVMuxer* OH_AVMuxer_Create (int32_t fd, OH_AVOutputFormat format) 114``` 115 116**Description** 117 118Creates an OH_AVMuxer instance by using the file descriptor and container format. 119 120**System capability**: SystemCapability.Multimedia.Media.Muxer 121 122**Since**: 10 123 124**Parameters** 125 126| Name| Description| 127| -------- | -------- | 128| fd | File descriptor (FD). You must open the file in read/write mode (O_RDWR) and close the file after using it. | 129| format | Format of the multiplexed output file. For details, see [OH_AVOutputFormat](_codec_base.md#oh_avoutputformat-1). | 130 131**Returns** 132 133Pointer to the OH_AVMuxer instance created. After the multiplexing process is complete, call **OH_AVMuxer_Destroy** to destroy the instance. 134 135 136### OH_AVMuxer_Destroy() 137 138``` 139OH_AVErrCode OH_AVMuxer_Destroy (OH_AVMuxer *muxer) 140``` 141 142**Description** 143 144Clears internal resources and destroys an OH_AVMuxer instance. 145 146Do not repeatedly destroy the instance. Otherwise, the program may crash. 147 148**System capability**: SystemCapability.Multimedia.Media.Muxer 149 150**Since**: 10 151 152**Parameters** 153 154| Name| Description| 155| -------- | -------- | 156| muxer | Pointer to an OH_AVMuxer instance. | 157 158**Returns** 159 160**AV_ERR_OK** if the operation is successful, and you must set the muxer to null; one of the following error codes defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise: 161 162- **AV_ERR_INVALID_VAL**: The muxer pointer is null. 163 164 165### OH_AVMuxer_SetRotation() 166 167``` 168OH_AVErrCode OH_AVMuxer_SetRotation (OH_AVMuxer *muxer, int32_t rotation) 169``` 170 171**Description** 172 173Sets the rotation angle (clockwise), which must be 0, 90, 180, or 270, of an output video. 174 175This function must be called before **OH_AVMuxer_Start**. 176 177**System capability**: SystemCapability.Multimedia.Media.Muxer 178 179**Since**: 10 180 181**Parameters** 182 183| Name| Description| 184| -------- | -------- | 185| muxer | Pointer to an OH_AVMuxer instance. | 186| rotation | Angle to set. The value must be 0, 90, 180, or 270. | 187 188**Returns** 189 190**AV_ERR_OK** if the operation is successful; one of the following error codes defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise: 191 192- **AV_ERR_INVALID_VAL**: The muxer pointer is null or the value of **rotation** is invalid. 193 194- **AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence. 195 196 197 198### OH_AVMuxer_SetFormat() 199 200``` 201OH_AVErrCode OH_AVMuxer_SetFormat(OH_AVMuxer *muxer, OH_AVFormat *format) 202``` 203**Description** 204 205Sets format data to the muxer. 206 207Starting from API version 14, setting the creation time via **OH_MD_KEY_CREATION_TIME** is supported. If the setting fails, check whether the value of **OH_MD_KEY_CREATION_TIME** complies with the ISO 8601 standard and whether the UTC time is used. 208 209Starting from API version 20, the following operations are supported: 210- Setting the file description via **OH_MD_KEY_COMMENT** is supported. If the setting fails, check whether **OH_MD_KEY_COMMENT** is a string within the range [1, 256]. 211- Setting the position of the MP4 moov via **OH_MD_KEY_ENABLE_MOOV_FRONT**. If **OH_MD_KEY_ENABLE_MOOV_FRONT** is set to **0**, moov is placed at the end. If it is set to **1**, moov is placed at the front. By default, moov is placed at the end. 212 213**System capability**: SystemCapability.Multimedia.Media.Muxer 214 215**Since**: 14 216 217**Parameters** 218 219| Name| Description| 220| -------- | -------- | 221| muxer | Pointer to an OH_AVMuxer instance. | 222| format | Pointer to an OH_AVFormat instance. It is a file-level metadata set. | 223 224**Returns** 225 226- **AV_ERR_OK**: The **format** parameter is correctly set. 227 228- **AV_ERR_INVALID_VAL**: The muxer pointer is null or the value of **format** is invalid. 229 230- **AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence. 231 232### OH_AVMuxer_Start() 233 234``` 235OH_AVErrCode OH_AVMuxer_Start (OH_AVMuxer *muxer) 236``` 237 238**Description** 239 240Starts a muxer. 241 242This function must be called after **OH_AVMuxer_AddTrack** and before **OH_AVMuxer_WriteSample**. 243 244**System capability**: SystemCapability.Multimedia.Media.Muxer 245 246**Since**: 10 247 248**Parameters** 249 250| Name| Description| 251| -------- | -------- | 252| muxer | Pointer to an OH_AVMuxer instance. | 253 254**Returns** 255 256**AV_ERR_OK** if the operation is successful; one of the following error codes defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise: 257 258- **AV_ERR_INVALID_VAL**: The muxer pointer is null. 259 260- **AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence. 261 262- **AV_ERR_UNKNOWN**: Unknown error. 263 264### OH_AVMuxer_Stop() 265 266``` 267OH_AVErrCode OH_AVMuxer_Stop (OH_AVMuxer *muxer) 268``` 269 270**Description** 271 272Stops a muxer. 273 274Once the muxer is stopped, it cannot be restarted. 275 276**System capability**: SystemCapability.Multimedia.Media.Muxer 277 278**Since**: 10 279 280**Parameters** 281 282| Name| Description| 283| -------- | -------- | 284| muxer | Pointer to an OH_AVMuxer instance. | 285 286**Returns** 287 288**AV_ERR_OK** if the operation is successful; one of the following error codes defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise: 289 290- **AV_ERR_INVALID_VAL**: The muxer pointer is null. 291 292- **AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence. 293 294 295### OH_AVMuxer_WriteSample() 296 297``` 298OH_AVErrCode OH_AVMuxer_WriteSample (OH_AVMuxer *muxer, uint32_t trackIndex, OH_AVMemory *sample, OH_AVCodecBufferAttr info) 299``` 300 301**Description** 302 303Writes a sample to a muxer. 304 305This 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**. 306 307**System capability**: SystemCapability.Multimedia.Media.Muxer 308 309**Since**: 10 310 311**Deprecated from**: 11 312 313**Substitute API**: [OH_AVMuxer_WriteSampleBuffer](#oh_avmuxer_writesamplebuffer) 314 315**Parameters** 316 317| Name| Description| 318| -------- | -------- | 319| muxer | Pointer to an OH_AVMuxer instance. | 320| trackIndex | Index of the audio or video track corresponding to the data. | 321| sample | Pointer to the data obtained after encoding or demultiplexing. | 322| info | Sample description. For details, see [OH_AVCodecBufferAttr](_o_h___a_v_codec_buffer_attr.md). | 323 324**Returns** 325 326**AV_ERR_OK** if the operation is successful; one of the following error codes defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise: 327 328- **AV_ERR_INVALID_VAL**: The muxer pointer is null, or the track index, sample, or info is invalid. 329 330- **AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence. 331 332- **AV_ERR_NO_MEMORY**: Memory allocation fails. 333 334- **AV_ERR_UNKNOWN**: Unknown error. 335 336 337### OH_AVMuxer_WriteSampleBuffer() 338 339``` 340OH_AVErrCode OH_AVMuxer_WriteSampleBuffer (OH_AVMuxer *muxer, uint32_t trackIndex, const OH_AVBuffer *sample) 341``` 342 343**Description** 344 345Writes a sample to a muxer. 346 347This 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**. 348 349**System capability**: SystemCapability.Multimedia.Media.Muxer 350 351**Since**: 11 352 353**Parameters** 354 355| Name| Description| 356| -------- | -------- | 357| muxer | Pointer to an OH_AVMuxer instance. | 358| trackIndex | Index of the audio or video track corresponding to the data. | 359| sample | Pointer to the data and properties obtained after encoding or demultiplexing. | 360 361**Returns** 362 363**AV_ERR_OK** if the operation is successful; one of the following error codes defined in [OH_AVErrCode](_core.md#oh_averrcode) otherwise: 364 365- **AV_ERR_INVALID_VAL**: The muxer pointer is null, or the track index or sample is invalid. 366 367- **AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence. 368 369- **AV_ERR_NO_MEMORY**: Memory allocation fails. 370 371- **AV_ERR_UNKNOWN**: Unknown error. 372