1# Using OpenSL ES for Audio Recording (C/C++) 2 3OpenSL ES, short for Open Sound Library for Embedded Systems, is an embedded, cross-platform audio processing library that is free of charge. It provides high-performance and low-latency APIs for you to develop applications running on embedded mobile multimedia devices. OpenHarmony has implemented certain native APIs based on [OpenSL ES](https://www.khronos.org/opensles/) 1.0.1 API specifications developed by the [Khronos Group](https://www.khronos.org/). You can use these APIs through <OpenSLES.h> and <OpenSLES_OpenHarmony.h>. 4 5## Using OHAudio to Replace OpenSL ES 6OpenHarmony provides the OpenSL ES APIs for audio development at the native layer since SDK8. As the version evolves, these APIs fail to meet the capability expansion requirements of the audio system and therefore are no longer recommended. 7 8In SDK 10, OpenHarmony provides the **OHAudio** APIs, which open up all audio functions of the system. The **OHAudio** APIs cover all the capabilities provided by OpenSL ES in OpenHarmony. They also support new features such as audio focus events and low latency. 9 10For details about how to use the **OHAudio** APIs for audio development, see [Using OHAudio for Audio Recording (C/C++)](using-ohaudio-for-recording.md). 11 12If you have used the OpenSL ES APIs in your code, you can switch them to the **OHAudio** APIs. For details, see [Switching from OpenSL ES to OHAudio (C/C++)](replace-opensles-by-ohaudio.md). 13 14## OpenSL ES on OpenHarmony 15 16Currently, OpenHarmony implements parts of [OpenSL ES APIs](https://gitee.com/openharmony/third_party_opensles/blob/master/api/1.0.1/OpenSLES.h) to implement basic audio recording functionalities. 17 18If an API that has not been implemented on OpenHarmony is called, **SL_RESULT_FEATURE_UNSUPPORTED** is returned. 19 20The following lists the OpenSL ES APIs that have been implemented on OpenHarmony. For details, see the [OpenSL ES](https://www.khronos.org/opensles/) specifications. 21 22- **SLInterfaceID implemented on OpenHarmony** 23 24 | SLInterfaceID | Description| 25 | -------- | -------- | 26 | SL_IID_ENGINE | Universal engine, which provides the interface for creating capturer objects.| 27 | SL_IID_RECORD | Provides the capturer status interface.| 28 | SL_IID_OH_BUFFERQUEUE | Provides the callback registration interface for audio recording stream data.| 29 30- **Engine APIs implemented on OpenHarmony** 31 - SLresult (\*CreateAudioPlayer) (SLEngineItf self, SLObjectItf \* pPlayer, SLDataSource \*pAudioSrc, SLDataSink \*pAudioSnk, SLuint32 numInterfaces, const SLInterfaceID \* pInterfaceIds, const SLboolean \* pInterfaceRequired) 32 - SLresult (\*CreateAudioRecorder) (SLEngineItf self, SLObjectItf \* pRecorder, SLDataSource \*pAudioSrc, SLDataSink \*pAudioSnk, SLuint32 numInterfaces, const SLInterfaceID \* pInterfaceIds, const SLboolean \* pInterfaceRequired) 33 - SLresult (\*CreateOutputMix) (SLEngineItf self, SLObjectItf \* pMix, SLuint32 numInterfaces, const SLInterfaceID \* pInterfaceIds, const SLboolean \* pInterfaceRequired) 34 35- **Object APIs implemented on OpenHarmony** 36 - SLresult (\*Realize) (SLObjectItf self, SLboolean async) 37 - SLresult (\*GetState) (SLObjectItf self, SLuint32 \* pState) 38 - SLresult (\*GetInterface) (SLObjectItf self, const SLInterfaceID iid, void \* pInterface) 39 - void (\*Destroy) (SLObjectItf self) 40 41- **Recorder APIs implemented on OpenHarmony** 42 - SLresult (\*SetRecordState) (SLRecordItf self, SLuint32 state) 43 - SLresult (\*GetRecordState) (SLRecordItf self,SLuint32 \*pState) 44 45- **BufferQueue APIs implemented on OpenHarmony** 46 47 The APIs listed below can be used only after <OpenSLES_OpenHarmony.h> is introduced. 48 49 | API| Description| 50 | -------- | -------- | 51 | SLresult (\*Enqueue) (SLOHBufferQueueItf self, const void \*buffer, SLuint32 size) | Adds a buffer to the corresponding queue.<br>For an audio playback operation, this API adds the buffer with audio data to the **filledBufferQ_** queue. For an audio recording operation, this API adds the idle buffer after recording data storage to the **freeBufferQ_** queue.<br>The **self** parameter indicates the **BufferQueue** object that calls this API.<br>The **buffer** parameter indicates the pointer to the buffer with audio data or the pointer to the idle buffer after the recording data is stored.<br>The **size** parameter indicates the size of the buffer.| 52 | SLresult (\*Clear) (SLOHBufferQueueItf self) | Releases a **BufferQueue** object.<br>The **self** parameter indicates the **BufferQueue** object that calls this API.| 53 | SLresult (\*GetState) (SLOHBufferQueueItf self, SLOHBufferQueueState \*state) | Obtains the state of a **BufferQueue** object.<br>The **self** parameter indicates the **BufferQueue** object that calls this API.<br>The **state** parameter indicates the pointer to the state of the **BufferQueue** object.| 54 | SLresult (\*RegisterCallback) (SLOHBufferQueueItf self, SlOHBufferQueueCallback callback, void\* pContext) | Registers a callback.<br>The **self** parameter indicates the **BufferQueue** object that calls this API.<br>The **callback** parameter indicates the callback to be registered for the audio playback or recording operation.<br>The **pContext** parameter indicates the pointer to the audio file to be played for an audio playback operation or the pointer to the audio file to be recorded for an audio recording operation.| 55 | SLresult (\*GetBuffer) (SLOHBufferQueueItf self, SLuint8\*\* buffer, SLuint32\* size) | Obtains a buffer.<br>For an audio playback operation, this API obtains an idle buffer from the **freeBufferQ_** queue. For an audio recording operation, this API obtains the buffer that carries recording data from the **filledBufferQ_** queue.<br>The **self** parameter indicates the **BufferQueue** object that calls this API.<br>The **buffer** parameter indicates the double pointer to the idle buffer or the buffer carrying recording data.<br>The **size** parameter indicates the size of the buffer.| 56 57## Sample Code 58 59### Linking the Dynamic Library in the CMake Script 60 61``` cmake 62target_link_libraries(sample PUBLIC libOpenSLES.so) 63``` 64 65Refer to the sample code below to record an audio file. 66 671. Add the header files. 68 69 ```c++ 70 #include "SLES/OpenSLES.h" 71 #include "SLES/OpenSLES_OpenHarmony.h" 72 #include "SLES/OpenSLES_Platform.h" 73 ``` 74 752. Use the **slCreateEngine** API to create and instantiate an **engine** object. 76 77 ```c++ 78 SLObjectItf engineObject = nullptr; 79 slCreateEngine(&engineObject, 0, nullptr, 0, nullptr, nullptr); 80 (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); 81 ``` 82 833. Obtain the **engineEngine** instance of the **SL_IID_ENGINE** API. 84 85 ```c++ 86 SLEngineItf engineItf = nullptr; 87 (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineItf); 88 ``` 89 904. Configure the recorder information (including the input source **audiosource** and output source **audiosink**), and create a **pcmCapturerObject** instance. 91 92 ```c++ 93 SLDataLocator_IODevice io_device = { 94 SL_DATALOCATOR_IODEVICE, 95 SL_IODEVICE_AUDIOINPUT, 96 SL_DEFAULTDEVICEID_AUDIOINPUT, 97 NULL 98 }; 99 SLDataSource audioSource = { 100 &io_device, 101 NULL 102 }; 103 SLDataLocator_BufferQueue buffer_queue = { 104 SL_DATALOCATOR_BUFFERQUEUE, 105 3 106 }; 107 // Configure the parameters based on the audio file format. 108 SLDataFormat_PCM format_pcm = { 109 SL_DATAFORMAT_PCM, // Input audio format. 110 1, // Mono channel. 111 SL_SAMPLINGRATE_44_1, // Sampling rate, 44100 Hz. 112 SL_PCMSAMPLEFORMAT_FIXED_16, // Audio sampling format, a signed 16-bit integer in little-endian format. 113 16, 114 SL_SPEAKER_FRONT_LEFT, 115 SL_BYTEORDER_LITTLEENDIAN 116 }; 117 SLDataSink audioSink = { 118 &buffer_queue, 119 &format_pcm 120 }; 121 122 SLObjectItf pcmCapturerObject = nullptr; 123 (*engineItf)->CreateAudioRecorder(engineItf, &pcmCapturerObject, 124 &audioSource, &audioSink, 0, nullptr, nullptr); 125 (*pcmCapturerObject)->Realize(pcmCapturerObject, SL_BOOLEAN_FALSE); 126 127 ``` 128 1295. Obtain the **recordItf** instance of the **SL_IID_RECORD** API. 130 131 ```c++ 132 SLRecordItf recordItf; 133 (*pcmCapturerObject)->GetInterface(pcmCapturerObject, SL_IID_RECORD, &recordItf); 134 ``` 135 1366. Obtain the **bufferQueueItf** instance of the **SL_IID_OH_BUFFERQUEUE** API. 137 138 ```c++ 139 SLOHBufferQueueItf bufferQueueItf; 140 (*pcmCapturerObject)->GetInterface(pcmCapturerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf); 141 ``` 142 1437. Register the **BufferQueueCallback** function. 144 145 ```c++ 146 static void BufferQueueCallback(SLOHBufferQueueItf bufferQueueItf, void *pContext, SLuint32 size) 147 { 148 // Obtain the user information passed in during the registration from pContext. 149 SLuint8 *buffer = nullptr; 150 SLuint32 pSize = 0; 151 (*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, &pSize); 152 if (buffer != nullptr) { 153 // The recording data can be read from the buffer for subsequent processing. 154 (*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, size); 155 } 156 } 157 void *pContext; // This callback can be used to obtain the custom context information passed in. 158 (*bufferQueueItf)->RegisterCallback(bufferQueueItf, BufferQueueCallback, pContext); 159 ``` 160 1618. Start audio recording. 162 163 ```c++ 164 (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING); 165 ``` 166 1679. Stop audio recording. 168 169 ```c++ 170 (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED); 171 (*pcmCapturerObject)->Destroy(pcmCapturerObject); 172 (*engineObject)->Destroy(engineObject); 173 ``` 174 175 <!--no_check-->