1# AudioCodec 2 3 4## 概述 5 6AudioCodec模块提供用于音频编解码功能的函数。 7 8**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 9 10**起始版本:** 11 11 12 13## 汇总 14 15 16### 文件 17 18| 名称 | 描述 | 19| -------- | -------- | 20| [native_avcodec_audiocodec.h](native__avcodec__audiocodec_8h.md) | 声明用于音频编解码的Native API。 | 21 22 23### 函数 24 25| 名称 | 描述 | 26| -------- | -------- | 27| OH_AVCodec \* [OH_AudioCodec_CreateByMime](#oh_audiocodec_createbymime) (const char \*mime, bool isEncoder) | 根据MIME类型创建音频编解码器实例。 | 28| OH_AVCodec \* [OH_AudioCodec_CreateByName](#oh_audiocodec_createbyname) (const char \*name) | 通过音频编解码器名称创建音频编解码器实例。 | 29| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_Destroy](#oh_audiocodec_destroy) (OH_AVCodec \*codec) | 清理编解码器内部资源,销毁编解码器实例。 | 30| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_RegisterCallback](#oh_audiocodec_registercallback) (OH_AVCodec \*codec, [OH_AVCodecCallback](_o_h___a_v_codec_callback.md) callback, void \*userData) | 设置异步回调函数,使应用可以响应音频编解码器生成的事件。在调用Prepare之前,必须调用此接口。 | 31| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_Configure](#oh_audiocodec_configure) (OH_AVCodec \*codec, const OH_AVFormat \*format) | 要配置音频编解码器,通常需要配置音频描述信息。 | 32| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_Prepare](#oh_audiocodec_prepare) (OH_AVCodec \*codec) | 准备编解码器的内部资源。 | 33| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_Start](#oh_audiocodec_start) (OH_AVCodec \*codec) | Prepare成功后调用此接口启动编解码器。 | 34| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_Stop](#oh_audiocodec_stop) (OH_AVCodec \*codec) | 停止编解码器。 | 35| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_Flush](#oh_audiocodec_flush) (OH_AVCodec \*codec) | 清除编解码器中缓存的输入和输出数据。 | 36| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_Reset](#oh_audiocodec_reset) (OH_AVCodec \*codec) | 重置编解码器。如果要继续编解码,需要再次调用Configure接口配置编解码器实例。 | 37| OH_AVFormat \* [OH_AudioCodec_GetOutputDescription](#oh_audiocodec_getoutputdescription) (OH_AVCodec \*codec) | 获取编解码器输出数据的描述信息。 | 38| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_SetParameter](#oh_audiocodec_setparameter) (OH_AVCodec \*codec, const OH_AVFormat \*format) | 配置编解码器的动态参数。 | 39| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_PushInputBuffer](#oh_audiocodec_pushinputbuffer) (OH_AVCodec \*codec, uint32_t index) | 将填充有数据的输入缓冲区提交给音频编解码器。 | 40| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_FreeOutputBuffer](#oh_audiocodec_freeoutputbuffer) (OH_AVCodec \*codec, uint32_t index) | 将处理后的输出缓冲区返回给编解码器。 | 41| [OH_AVErrCode](_core.md#oh_averrcode)[OH_AudioCodec_IsValid](#oh_audiocodec_isvalid) (OH_AVCodec \*codec, bool \*isValid) | 检查当前编解码器实例是否有效,可用于后台故障恢复或应用程序从后台恢复时检测编解码器有效状态。 | 42 43 44## 函数说明 45 46 47### OH_AudioCodec_Configure() 48 49``` 50OH_AVErrCode OH_AudioCodec_Configure (OH_AVCodec *codec, const OH_AVFormat *format) 51``` 52 53**描述** 54 55要配置音频编解码器,通常需要配置音频描述信息。在调用Prepare之前,必须调用此接口。 56 57**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 58 59**起始版本:** 11 60 61**参数:** 62 63| 名称 | 描述 | 64| -------- | -------- | 65| codec | 指向OH_AVCodec实例的指针。 | 66| format | 指向OH_AVFormat的指针,给出要编解码的音频轨道的描述。 | 67 68**返回:** 69 70如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 71 72 73### OH_AudioCodec_CreateByMime() 74 75``` 76OH_AVCodec* OH_AudioCodec_CreateByMime (const char *mime, bool isEncoder) 77``` 78 79**描述** 80 81根据MIME类型创建音频编解码器实例,大多数场景下建议使用此方式。 82 83**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 84 85**起始版本:** 11 86 87**参数:** 88 89| 名称 | 描述 | 90| -------- | -------- | 91| mime | MIME类型描述字符串,请参阅[AVCODEC_MIMETYPE](_codec_base.md#变量)。 | 92| isEncoder | true表示需要创建编码器,false表示需要创建解码器。 | 93 94**返回:** 95 96返回OH_AVCodec实例的指针。 97 98 99### OH_AudioCodec_CreateByName() 100 101``` 102OH_AVCodec* OH_AudioCodec_CreateByName (const char *name) 103``` 104 105**描述** 106 107通过音频编解码器名称创建音频编解码器实例,使用此接口的前提是知道编解码器的确切名称。 108 109**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 110 111**起始版本:** 11 112 113**参数:** 114 115| 名称 | 描述 | 116| -------- | -------- | 117| name | 音频编解码器名称。 | 118 119**返回:** 120 121返回OH_AVCodec实例的指针。 122 123 124### OH_AudioCodec_Destroy() 125 126``` 127OH_AVErrCode OH_AudioCodec_Destroy (OH_AVCodec *codec) 128``` 129 130**描述** 131 132清理编解码器内部资源,销毁编解码器实例。 133 134**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 135 136**起始版本:** 11 137 138**参数:** 139 140| 名称 | 描述 | 141| -------- | -------- | 142| codec | 指向OH_AVCodec实例的指针。 | 143 144**返回:** 145 146如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 147 148 149### OH_AudioCodec_Flush() 150 151``` 152OH_AVErrCode OH_AudioCodec_Flush (OH_AVCodec *codec) 153``` 154 155**描述** 156 157清除编解码器中缓存的输入和输出数据。 158 159调用此接口后,以前通过异步回调上报的所有缓冲区索引都将失效,请确保不要访问这些索引对应的缓冲区。 160 161**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 162 163**起始版本:** 11 164 165**参数:** 166 167| 名称 | 描述 | 168| -------- | -------- | 169| codec | 指向OH_AVCodec实例的指针。 | 170 171**返回:** 172 173如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 174 175 176### OH_AudioCodec_FreeOutputBuffer() 177 178``` 179OH_AVErrCode OH_AudioCodec_FreeOutputBuffer (OH_AVCodec *codec, uint32_t index) 180``` 181 182**描述** 183 184将处理后的输出缓冲区返回给编解码器。 185 186**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 187 188**起始版本:** 11 189 190**参数:** 191 192| 名称 | 描述 | 193| -------- | -------- | 194| codec | 指向OH_AVCodec实例的指针。 | 195| index | 输出Buffer对应的索引值。 | 196 197**返回:** 198 199如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 200 201 202### OH_AudioCodec_GetOutputDescription() 203 204``` 205OH_AVFormat* OH_AudioCodec_GetOutputDescription (OH_AVCodec *codec) 206``` 207 208**描述** 209 210获取编解码器输出数据的描述信息。 211 212需要注意的是,返回值所指向的OH_AVFormat实例的生命周期需要调用[OH_AVFormat_Destroy](_core.md#oh_avformat_destroy)接口手动释放。 213 214**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 215 216**起始版本:** 11 217 218**参数:** 219 220| 名称 | 描述 | 221| -------- | -------- | 222| codec | 指向OH_AVCodec实例的指针。 | 223 224**返回:** 225 226返回OH_AVFormat句柄指针,生命周期将使用下一个GetOutputDescription刷新,或使用OH_AVCodec销毁。 227 228 229### OH_AudioCodec_IsValid() 230 231``` 232OH_AVErrCode OH_AudioCodec_IsValid (OH_AVCodec *codec, bool *isValid) 233``` 234 235**描述** 236 237检查当前编解码器实例是否有效,可用于后台故障恢复或应用程序从后台恢复时检测编解码器有效状态。 238 239**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 240 241**起始版本:** 11 242 243**参数:** 244 245| 名称 | 描述 | 246| -------- | -------- | 247| codec | 指向OH_AVCodec实例的指针。 | 248| isValid | 输出参数。指向布尔实例的指针,true:编解码器实例有效,false:编解码器实例无效。 | 249 250**返回:** 251 252如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 253 254 255### OH_AudioCodec_Prepare() 256 257``` 258OH_AVErrCode OH_AudioCodec_Prepare (OH_AVCodec *codec) 259``` 260 261**描述** 262 263准备编解码器的内部资源,在调用此接口之前必须调用Configure接口。 264 265**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 266 267**起始版本:** 11 268 269**参数:** 270 271| 名称 | 描述 | 272| -------- | -------- | 273| codec | 指向OH_AVCodec实例的指针。 | 274 275**返回:** 276 277如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 278 279 280### OH_AudioCodec_PushInputBuffer() 281 282``` 283OH_AVErrCode OH_AudioCodec_PushInputBuffer (OH_AVCodec *codec, uint32_t index) 284``` 285 286**描述** 287 288将填充有数据的输入缓冲区提交给音频编解码器。 289 290[OH_AVCodecOnNeedInputBuffer](_codec_base.md#oh_avcodeconneedinputbuffer)回调将报告可用的输入缓冲区和对应的索引值。一旦具有指定索引的缓冲区被提交给音频编解码器,该缓冲区将无法再次访问, 直到再次收到[OH_AVCodecOnNeedInputBuffer](_codec_base.md#oh_avcodeconneedinputbuffer)回调,收到相同索引时此缓冲区才可使用。 291 292此外,对于某些编解码器,需要在开始时向编解码器输入编解码特定配置数据(Codec-Specific-Data), 以初始化编解码器的编解码过程。 293 294**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 295 296**起始版本:** 11 297 298**参数:** 299 300| 名称 | 描述 | 301| -------- | -------- | 302| codec | 指向OH_AVCodec实例的指针。 | 303| index | 输入缓冲区Buffer对应的索引值。 | 304 305**返回:** 306 307如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 308 309 310### OH_AudioCodec_RegisterCallback() 311 312``` 313OH_AVErrCode OH_AudioCodec_RegisterCallback (OH_AVCodec *codec, OH_AVCodecCallback callback, void *userData) 314``` 315 316**描述** 317 318设置异步回调函数,使应用可以响应音频编解码器生成的事件。在调用Prepare之前,必须调用此接口。 319 320**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 321 322**起始版本:** 11 323 324**参数:** 325 326| 名称 | 描述 | 327| -------- | -------- | 328| codec | 指向OH_AVCodec实例的指针。 | 329| callback | 所有回调函数的集合,请参见 [OH_AVCodecCallback](_o_h___a_v_codec_callback.md)。 | 330| userData | 用户特定数据。 | 331 332**返回:** 333 334如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 335 336 337### OH_AudioCodec_Reset() 338 339``` 340OH_AVErrCode OH_AudioCodec_Reset (OH_AVCodec *codec) 341``` 342 343**描述** 344 345重置编解码器。如果要继续编解码,需要再次调用Configure接口配置编解码器实例。 346 347**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 348 349**起始版本:** 11 350 351**参数:** 352 353| 名称 | 描述 | 354| -------- | -------- | 355| codec | 指向OH_AVCodec实例的指针。 | 356 357**返回:** 358 359如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 360 361 362### OH_AudioCodec_SetParameter() 363 364``` 365OH_AVErrCode OH_AudioCodec_SetParameter (OH_AVCodec *codec, const OH_AVFormat *format) 366``` 367 368**描述** 369 370配置编解码器的动态参数。 371 372注意:该接口必须在编解码器启动后才能调用。另外,参数配置错误可能会导致编解码失败。 373 374**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 375 376**起始版本:** 11 377 378**参数:** 379 380| 名称 | 描述 | 381| -------- | -------- | 382| codec | 指向OH_AVCodec实例的指针。 | 383| format | OH_AVFormat句柄指针。 | 384 385**返回:** 386 387如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 388 389 390### OH_AudioCodec_Start() 391 392``` 393OH_AVErrCode OH_AudioCodec_Start (OH_AVCodec *codec) 394``` 395 396**描述** 397 398Prepare成功后调用此接口启动编解码器。启动后,编解码器将开始上报OH_AVCodecOnNeedInputBuffer事件。 399 400**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 401 402**起始版本:** 11 403 404**参数:** 405 406| 名称 | 描述 | 407| -------- | -------- | 408| codec | 指向OH_AVCodec实例的指针。 | 409 410**返回:** 411 412如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 413 414 415### OH_AudioCodec_Stop() 416 417``` 418OH_AVErrCode OH_AudioCodec_Stop (OH_AVCodec *codec) 419``` 420 421**描述** 422 423停止编解码器。 424 425停止后,可以通过Start重新进入已启动状态(started),但需要注意的是, 如果编解码器之前已输入数据,则需要重新输入编解码器数据。 426 427**系统能力:** SystemCapability.Multimedia.Media.AudioCodec 428 429**起始版本:** 11 430 431**参数:** 432 433| 名称 | 描述 | 434| -------- | -------- | 435| codec | 指向OH_AVCodec实例的指针。 | 436 437**返回:** 438 439如果执行成功,则返回AV_ERR_OK,否则返回特定错误代码,请参阅 [OH_AVErrCode](_core.md#oh_averrcode)。 440