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