1# native_avmuxer.h 2 3## Overview 4 5The file declares the native APIs used for audio and video multiplexing. 6 7**File to include**: <multimedia/player_framework/native_avmuxer.h> 8 9**Library**: libnative_media_avmuxer.so 10 11**System capability**: SystemCapability.Multimedia.Media.Muxer 12 13**Since**: 10 14 15**Related module**: [AVMuxer](capi-avmuxer.md) 16 17**Sample**: [AVCodec](https://gitcode.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Media/AVCodec) 18 19## Summary 20 21### Structs 22 23| Name| typedef Keyword| Description| 24| -- | -- | -- | 25| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) | OH_AVMuxer | Describes a native object for the muxer interface.| 26 27### Functions 28 29| Name| Description| 30| -- | -- | 31| [OH_AVMuxer *OH_AVMuxer_Create(int32_t fd, OH_AVOutputFormat format)](#oh_avmuxer_create) | Creates an OH_AVMuxer instance by using the file descriptor and container format.| 32| [OH_AVErrCode OH_AVMuxer_SetRotation(OH_AVMuxer *muxer, int32_t rotation)](#oh_avmuxer_setrotation) | Sets the rotation angle (clockwise) of an output video. Note: This function must be called before [OH_AVMuxer_Start](#oh_avmuxer_start).| 33| [OH_AVErrCode OH_AVMuxer_SetFormat(OH_AVMuxer *muxer, OH_AVFormat *format)](#oh_avmuxer_setformat) | Sets format data to the muxer. Note: Currently, only **OH_MD_KEY_CREATION_TIME** can be set. 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.| 34| [OH_AVErrCode OH_AVMuxer_AddTrack(OH_AVMuxer *muxer, int32_t *trackIndex, OH_AVFormat *trackFormat)](#oh_avmuxer_addtrack) | Adds an audio or video track to a muxer. Note: This function must be called before [OH_AVMuxer_Start](#oh_avmuxer_start).| 35| [OH_AVErrCode OH_AVMuxer_Start(OH_AVMuxer *muxer)](#oh_avmuxer_start) | Starts a muxer. Note: This function must be called after **OH_AVMuxer_AddTrack** and before **OH_AVMuxer_WriteSample**.| 36| [OH_AVErrCode OH_AVMuxer_WriteSample(OH_AVMuxer *muxer, uint32_t trackIndex, OH_AVMemory *sample, OH_AVCodecBufferAttr info)](#oh_avmuxer_writesample) | Writes a sample to a muxer. Note: This function must be called after **OH_AVMuxer_Start** and before **OH_AVMuxer_Stop**. You must ensure that the data is written to the correct media track in ascending order by time.| 37| [OH_AVErrCode OH_AVMuxer_WriteSampleBuffer(OH_AVMuxer *muxer, uint32_t trackIndex, const OH_AVBuffer *sample)](#oh_avmuxer_writesamplebuffer) | Writes a sample to a muxer. Note: This function must be called after **OH_AVMuxer_Start** and before **OH_AVMuxer_Stop**. You must ensure that the data is written to the correct media track in ascending order by time.| 38| [OH_AVErrCode OH_AVMuxer_Stop(OH_AVMuxer *muxer)](#oh_avmuxer_stop) | Stops a muxer. Note: Once the muxer is stopped, it cannot be restarted.| 39| [OH_AVErrCode OH_AVMuxer_Destroy(OH_AVMuxer *muxer)](#oh_avmuxer_destroy) | Clears internal resources and destroys an OH_AVMuxer instance.| 40 41## Function Description 42 43### OH_AVMuxer_Create() 44 45``` 46OH_AVMuxer *OH_AVMuxer_Create(int32_t fd, OH_AVOutputFormat format) 47``` 48 49**Description** 50 51Creates an OH_AVMuxer instance by using the file descriptor and container format. 52 53**System capability**: SystemCapability.Multimedia.Media.Muxer 54 55**Since**: 10 56 57 58**Parameters** 59 60| Name| Description| 61| -- | -- | 62| int32_t fd | File descriptor (FD). You must open the file in read/write mode (O_RDWR) and close the file after using it.| 63| [OH_AVOutputFormat](capi-native-avcodec-base-h.md#oh_avoutputformat) format | Format of the multiplexed output file.| 64 65**Returns** 66 67| Type| Description| 68| -- | -- | 69| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) * | Pointer to the **OH_AVMuxer** instance created. You must call **OH_AVMuxer_Destroy** to destroy the instance when it is no longer needed.| 70 71### OH_AVMuxer_SetRotation() 72 73``` 74OH_AVErrCode OH_AVMuxer_SetRotation(OH_AVMuxer *muxer, int32_t rotation) 75``` 76 77**Description** 78 79Sets the rotation angle (clockwise), which must be 0, 90, 180, or 270, of an output video. This function must be called before [OH_AVMuxer_Start](#oh_avmuxer_start). 80 81**System capability**: SystemCapability.Multimedia.Media.Muxer 82 83**Since**: 10 84 85 86**Parameters** 87 88| Name| Description| 89| -- | -- | 90| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) *muxer | Pointer to an OH_AVMuxer instance.| 91| int32_t rotation | Angle to set. The value must be 0, 90, 180, or 270.| 92 93**Returns** 94 95| Type| Description| 96| -- | -- | 97| [OH_AVErrCode](_core.md#oh_averrcode-1) | **AV_ERR_OK**: The operation is successful.<br>**AV_ERR_INVALID_VAL**: The muxer pointer is null or the value of **rotation** is invalid.<br>**AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence.| 98 99### OH_AVMuxer_SetFormat() 100 101``` 102OH_AVErrCode OH_AVMuxer_SetFormat(OH_AVMuxer *muxer, OH_AVFormat *format) 103``` 104 105**Description** 106 107Sets format data to the muxer. 108 109Starting 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. 110 111Starting from API version 20, the following operations are supported: 112 113- 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]. 114- Setting the position of the MP4 moov via **OH_MD_KEYENABLE_MOOV_FRONT**. If **OH_MD_KEYENABLE_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. 115 116**System capability**: SystemCapability.Multimedia.Media.Muxer 117 118**Since**: 14 119 120 121**Parameters** 122 123| Name| Description| 124| -- | -- | 125| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) *muxer | Pointer to an OH_AVMuxer instance.| 126| [OH_AVFormat](_core.md#oh_avformat) *format | Pointer to an OH_AVFormat instance. It is a file-level metadata set.| 127 128**Returns** 129 130| Type| Description| 131| -- | -- | 132| [OH_AVErrCode](_core.md#oh_averrcode-1) | **AV_ERR_OK**: The **format** parameter is correctly set.<br>**AV_ERR_INVALID_VAL**: The muxer pointer is null or the value of **format** is invalid.<br>**AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence.| 133 134### OH_AVMuxer_AddTrack() 135 136``` 137OH_AVErrCode OH_AVMuxer_AddTrack(OH_AVMuxer *muxer, int32_t *trackIndex, OH_AVFormat *trackFormat) 138``` 139 140**Description** 141 142Adds an audio or video track to a muxer. Each time this function is called, an audio or video track is added to the muxer. This function must be called before [OH_AVMuxer_Start](#oh_avmuxer_start). 143 144**System capability**: SystemCapability.Multimedia.Media.Muxer 145 146**Since**: 10 147 148 149**Parameters** 150 151| Name| Description| 152| -- | -- | 153| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) *muxer | Pointer to an OH_AVMuxer instance.| 154| int32_t *trackIndex | Pointer to the index of the media track. The index will be used in the [OH_AVMuxer_WriteSample](#oh_avmuxer_writesample) function. If the media track is added, the index value is greater than or equal to 0; otherwise, the value is less than 0.| 155| [OH_AVFormat](_core.md#oh_avformat) *trackFormat | Pointer to an OH_AVFormat instance.| 156 157**Returns** 158 159| Type| Description| 160| -- | -- | 161| [OH_AVErrCode](_core.md#oh_averrcode-1) | **AV_ERR_OK**: The operation is successful.<br>**AV_ERR_INVALID_VAL**: The muxer pointer is null, or the track index or track format is invalid.<br>**AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence.<br>**AV_ERR_UNSUPPORT**: The MIME type is not supported.<br>**AV_ERR_NO_MEMORY**: Memory allocation fails.<br>**AV_ERR_UNKNOWN**: An unknown error occurs.| 162 163### OH_AVMuxer_Start() 164 165``` 166OH_AVErrCode OH_AVMuxer_Start(OH_AVMuxer *muxer) 167``` 168 169**Description** 170 171Starts a muxer. This function must be called after [OH_AVMuxer_AddTrack](#oh_avmuxer_addtrack) and before [OH_AVMuxer_WriteSample](#oh_avmuxer_writesample). 172 173**System capability**: SystemCapability.Multimedia.Media.Muxer 174 175**Since**: 10 176 177 178**Parameters** 179 180| Name| Description| 181| -- | -- | 182| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) *muxer | Pointer to an OH_AVMuxer instance.| 183 184**Returns** 185 186| Type| Description| 187| -- | -- | 188| [OH_AVErrCode](_core.md#oh_averrcode-1) | **AV_ERR_OK**: The operation is successful.<br>**AV_ERR_INVALID_VAL**: The muxer pointer is null.<br>**AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence.<br>**AV_ERR_UNKNOWN**: An unknown error occurs.| 189 190### OH_AVMuxer_WriteSample() 191 192``` 193OH_AVErrCode OH_AVMuxer_WriteSample(OH_AVMuxer *muxer, uint32_t trackIndex, OH_AVMemory *sample, OH_AVCodecBufferAttr info) 194``` 195 196**Description** 197 198Writes a sample to a muxer. This function must be called after [OH_AVMuxer_Start](#oh_avmuxer_start) and before [OH_AVMuxer_Stop](#oh_avmuxer_stop). The caller must write the sample to the correct audio or video track based on the timing in **info**. 199 200**System capability**: SystemCapability.Multimedia.Media.Muxer 201 202**Since**: 10 203 204**Deprecated from**: 11 205 206**Substitute**: [OH_AVMuxer_WriteSampleBuffer](capi-native-avmuxer-h.md#oh_avmuxer_writesamplebuffer) 207 208 209**Parameters** 210 211| Name| Description| 212| -- | -- | 213| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) *muxer | Pointer to an OH_AVMuxer instance.| 214| uint32_t trackIndex | Index of the audio or video track corresponding to the data.| 215| [OH_AVMemory](_core.md#oh_avmemory) *sample | Pointer to the data obtained after encoding or demultiplexing.| 216| [OH_AVCodecBufferAttr](_o_h___a_v_codec_buffer_attr.md) info | Sample description.| 217 218**Returns** 219 220| Type| Description| 221| -- | -- | 222| [OH_AVErrCode](_core.md#oh_averrcode-1) | **AV_ERR_OK**: The operation is successful.<br>**AV_ERR_INVALID_VAL**: The muxer pointer is null, or the track index, sample, or info is invalid.<br>**AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence.<br>**AV_ERR_NO_MEMORY**: Memory allocation fails.<br>**AV_ERR_UNKNOWN**: An unknown error occurs.| 223 224### OH_AVMuxer_WriteSampleBuffer() 225 226``` 227OH_AVErrCode OH_AVMuxer_WriteSampleBuffer(OH_AVMuxer *muxer, uint32_t trackIndex, const OH_AVBuffer *sample) 228``` 229 230**Description** 231 232Writes a sample to a muxer. This function must be called after [OH_AVMuxer_Start](#oh_avmuxer_start) and before [OH_AVMuxer_Stop](#oh_avmuxer_stop). The caller must write the sample to the correct audio or video track based on the timing in **sample**. 233 234**System capability**: SystemCapability.Multimedia.Media.Muxer 235 236**Since**: 11 237 238 239**Parameters** 240 241| Name| Description| 242| -- | -- | 243| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) *muxer | Pointer to an OH_AVMuxer instance.| 244| uint32_t trackIndex | Index of the audio or video track corresponding to the data.| 245| const [OH_AVBuffer](_core.md#oh_avbuffer) *sample | Pointer to the data and properties obtained after encoding or demultiplexing.| 246 247**Returns** 248 249| Type| Description| 250| -- | -- | 251| [OH_AVErrCode](_core.md#oh_averrcode-1) | **AV_ERR_OK**: The operation is successful.<br>**AV_ERR_INVALID_VAL**: The muxer pointer is null, or the track index or sample is invalid.<br>**AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence.<br>**AV_ERR_NO_MEMORY**: Memory allocation fails.<br>**AV_ERR_UNKNOWN**: An unknown error occurs.| 252 253### OH_AVMuxer_Stop() 254 255``` 256OH_AVErrCode OH_AVMuxer_Stop(OH_AVMuxer *muxer) 257``` 258 259**Description** 260 261Stops a muxer. Once the muxer is stopped, it cannot be restarted. 262 263**System capability**: SystemCapability.Multimedia.Media.Muxer 264 265**Since**: 10 266 267 268**Parameters** 269 270| Name| Description| 271| -- | -- | 272| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) *muxer | Pointer to an OH_AVMuxer instance.| 273 274**Returns** 275 276| Type| Description| 277| -- | -- | 278| [OH_AVErrCode](_core.md#oh_averrcode-1) | **AV_ERR_OK**: The operation is successful.<br>**AV_ERR_INVALID_VAL**: The muxer pointer is null.<br>**AV_ERR_OPERATE_NOT_PERMIT**: The function is called out of sequence.| 279 280### OH_AVMuxer_Destroy() 281 282``` 283OH_AVErrCode OH_AVMuxer_Destroy(OH_AVMuxer *muxer) 284``` 285 286**Description** 287 288Clears internal resources and destroys an OH_AVMuxer instance. 289 290Do not repeatedly destroy the instance. Otherwise, the program may crash. 291 292**System capability**: SystemCapability.Multimedia.Media.Muxer 293 294**Since**: 10 295 296**Parameters** 297 298| Name| Description| 299| -- | -- | 300| [OH_AVMuxer](capi-avmuxer-oh-avmuxer.md) *muxer | Pointer to an OH_AVMuxer instance.| 301 302**Returns** 303 304| Type| Description| 305| -- | -- | 306| [OH_AVErrCode](_core.md#oh_averrcode-1) | **AV_ERR_OK** if the operation is successful, and you must set the muxer to null; one of the following error codes otherwise:<br>**AV_ERR_INVALID_VAL**: The muxer pointer is null.| 307