• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using OpenSL ES for Audio Recording
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 have 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## OpenSL ES on OpenHarmony
6
7Currently, 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.
8
9If an API that has not been implemented on OpenHarmony is called, **SL_RESULT_FEATURE_UNSUPPORTED** is returned.
10
11The following lists the OpenSL ES APIs that have been implemented on OpenHarmony. For details, see the [OpenSL ES](https://www.khronos.org/opensles/) specifications.
12
13- **Engine APIs implemented on OpenHarmony**
14  - SLresult (\*CreateAudioPlayer) (SLEngineItf self, SLObjectItf \* pPlayer, SLDataSource \*pAudioSrc, SLDataSink \*pAudioSnk, SLuint32 numInterfaces, const SLInterfaceID \* pInterfaceIds, const SLboolean \* pInterfaceRequired)
15  - SLresult (\*CreateAudioRecorder) (SLEngineItf self, SLObjectItf \* pRecorder, SLDataSource \*pAudioSrc, SLDataSink \*pAudioSnk, SLuint32 numInterfaces, const SLInterfaceID \* pInterfaceIds, const SLboolean \* pInterfaceRequired)
16  - SLresult (\*CreateOutputMix) (SLEngineItf self, SLObjectItf \* pMix, SLuint32 numInterfaces, const SLInterfaceID \* pInterfaceIds, const SLboolean \* pInterfaceRequired)
17
18- **Object APIs implemented on OpenHarmony**
19  - SLresult (\*Realize) (SLObjectItf self, SLboolean async)
20  - SLresult (\*GetState) (SLObjectItf self, SLuint32 \* pState)
21  - SLresult (\*GetInterface) (SLObjectItf self, const SLInterfaceID iid, void \* pInterface)
22  - void (\*Destroy) (SLObjectItf self)
23
24- **Recorder APIs implemented on OpenHarmony**
25  - SLresult (\*SetRecordState) (SLRecordItf self, SLuint32 state)
26  - SLresult (\*GetRecordState) (SLRecordItf self,SLuint32 \*pState)
27
28- **BufferQueue APIs implemented on OpenHarmony**
29
30  The APIs listed below can be used only after <OpenSLES_OpenHarmony.h\> is introduced.
31  | API| Description|
32  | -------- | -------- |
33  | 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.|
34  | SLresult (\*Clear) (SLOHBufferQueueItf self) | Releases a **BufferQueue** object.<br>The **self** parameter indicates the **BufferQueue** object that calls this API.|
35  | 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.|
36  | 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.|
37  | 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.|
38
39## Sample Code
40
41Refer to the sample code below to record an audio file.
42
431. Add the header files.
44
45   ```c++
46   #include <OpenSLES.h>
47   #include <OpenSLES_OpenHarmony.h>
48   #include <OpenSLES_Platform.h>
49   ```
50
512. Use the **slCreateEngine** API to create and instantiate an **engine** object.
52
53   ```c++
54   SLObjectItf engineObject = nullptr;
55   slCreateEngine(&engineObject, 0, nullptr, 0, nullptr, nullptr);
56   (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
57   ```
58
593. Obtain the **engineEngine** instance of the **SL_IID_ENGINE** API.
60
61   ```c++
62   SLEngineItf engineItf = nullptr;
63   (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineItf);
64   ```
65
664. Configure the recorder information (including the input source **audiosource** and output source **audiosink**), and create a **pcmCapturerObject** instance.
67
68   ```c++
69   SLDataLocator_IODevice io_device = {
70       SL_DATALOCATOR_IODEVICE,
71       SL_IODEVICE_AUDIOINPUT,
72       SL_DEFAULTDEVICEID_AUDIOINPUT,
73       NULL
74   };
75   SLDataSource audioSource = {
76       &io_device,
77       NULL
78   };
79   SLDataLocator_BufferQueue buffer_queue = {
80       SL_DATALOCATOR_BUFFERQUEUE,
81       3
82   };
83   // Configure the parameters based on the audio file format.
84   SLDataFormat_PCM format_pcm = {
85       SL_DATAFORMAT_PCM,           // Input audio format.
86       1,                                              // Mono channel.
87       SL_SAMPLINGRATE_44_1,        // Sampling rate, 44100 Hz.
88       SL_PCMSAMPLEFORMAT_FIXED_16, // Audio sampling format, a signed 16-bit integer in little-endian format.
89       0,
90       0,
91       0
92   };
93   SLDataSink audioSink = {
94       &buffer_queue,
95       &format_pcm
96   };
97
98   SLObjectItf pcmCapturerObject = nullptr;
99   (*engineItf)->CreateAudioRecorder(engineItf, &pcmCapturerObject,
100       &audioSource, &audioSink, 0, nullptr, nullptr);
101   (*pcmCapturerObject)->Realize(pcmCapturerObject, SL_BOOLEAN_FALSE);
102
103   ```
104
1055. Obtain the **recordItf** instance of the **SL_IID_RECORD** API.
106
107   ```c++
108   SLRecordItf  recordItf;
109   (*pcmCapturerObject)->GetInterface(pcmCapturerObject, SL_IID_RECORD, &recordItf);
110   ```
111
1126. Obtain the **bufferQueueItf** instance of the **SL_IID_OH_BUFFERQUEUE** API.
113
114   ```c++
115   SLOHBufferQueueItf bufferQueueItf;
116   (*pcmCapturerObject)->GetInterface(pcmCapturerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf);
117   ```
118
1197. Register the **BufferQueueCallback** function.
120
121   ```c++
122   static void BufferQueueCallback(SLOHBufferQueueItf bufferQueueItf, void *pContext, SLuint32 size)
123   {
124       // Obtain the user information passed in during the registration from pContext.
125       SLuint8 *buffer = nullptr;
126       SLuint32 pSize = 0;
127       (*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, &pSize);
128       if (buffer != nullptr) {
129           // The recording data can be read from the buffer for subsequent processing.
130           (*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, size);
131       }
132   }
133   void *pContext; // This callback can be used to obtain the custom context information passed in.
134   (*bufferQueueItf)->RegisterCallback(bufferQueueItf, BufferQueueCallback, pContext);
135   ```
136
1378. Start audio recording.
138
139   ```c++
140   (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING);
141   ```
142
1439. Stop audio recording.
144
145   ```c++
146   (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
147   (*pcmCapturerObject)->Destroy(pcmCapturerObject);
148   ```
149