blob: 4983b9ac4236e65f28d00c41e18395ddfa41b544 [file] [log] [blame]
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// cribbed from samples/native-audio
19
20#include "audioplay.h"
21
22#define CHATTY ALOGD
Eric Laurent197e4792016-07-21 18:17:15 -070023#define LOG_TAG "audioplay"
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070024
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070025#include <string.h>
26
27#include <utils/Log.h>
28
29// for native audio
30#include <SLES/OpenSLES.h>
31#include <SLES/OpenSLES_Android.h>
32
33namespace audioplay {
34namespace {
35
36// engine interfaces
37static SLObjectItf engineObject = NULL;
38static SLEngineItf engineEngine;
39
40// output mix interfaces
41static SLObjectItf outputMixObject = NULL;
42
43// buffer queue player interfaces
44static SLObjectItf bqPlayerObject = NULL;
45static SLPlayItf bqPlayerPlay;
46static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
47static SLMuteSoloItf bqPlayerMuteSolo;
48static SLVolumeItf bqPlayerVolume;
49
50// pointer and size of the next player buffer to enqueue, and number of remaining buffers
51static const uint8_t* nextBuffer;
52static unsigned nextSize;
53
54static const uint32_t ID_RIFF = 0x46464952;
55static const uint32_t ID_WAVE = 0x45564157;
56static const uint32_t ID_FMT = 0x20746d66;
57static const uint32_t ID_DATA = 0x61746164;
58
59struct RiffWaveHeader {
60 uint32_t riff_id;
61 uint32_t riff_sz;
62 uint32_t wave_id;
63};
64
65struct ChunkHeader {
66 uint32_t id;
67 uint32_t sz;
68};
69
70struct ChunkFormat {
71 uint16_t audio_format;
72 uint16_t num_channels;
73 uint32_t sample_rate;
74 uint32_t byte_rate;
75 uint16_t block_align;
76 uint16_t bits_per_sample;
77};
78
79// this callback handler is called every time a buffer finishes playing
80void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context) {
81 (void)bq;
82 (void)context;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070083 audioplay::setPlaying(false);
84}
85
86bool hasPlayer() {
87 return (engineObject != NULL && bqPlayerObject != NULL);
88}
89
90// create the engine and output mix objects
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -040091bool createEngine() {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070092 SLresult result;
93
94 // create engine
95 result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -040096 if (result != SL_RESULT_SUCCESS) {
97 ALOGE("slCreateEngine failed with result %d", result);
98 return false;
99 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700100 (void)result;
101
102 // realize the engine
103 result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400104 if (result != SL_RESULT_SUCCESS) {
105 ALOGE("sl engine Realize failed with result %d", result);
106 return false;
107 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700108 (void)result;
109
110 // get the engine interface, which is needed in order to create other objects
111 result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400112 if (result != SL_RESULT_SUCCESS) {
113 ALOGE("sl engine GetInterface failed with result %d", result);
114 return false;
115 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700116 (void)result;
117
Eric Laurent197e4792016-07-21 18:17:15 -0700118 // create output mix
119 result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400120 if (result != SL_RESULT_SUCCESS) {
121 ALOGE("sl engine CreateOutputMix failed with result %d", result);
122 return false;
123 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700124 (void)result;
125
126 // realize the output mix
127 result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400128 if (result != SL_RESULT_SUCCESS) {
129 ALOGE("sl outputMix Realize failed with result %d", result);
130 return false;
131 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700132 (void)result;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400133
134 return true;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700135}
136
137// create buffer queue audio player
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400138bool createBufferQueueAudioPlayer(const ChunkFormat* chunkFormat) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700139 SLresult result;
140
141 // configure audio source
142 SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 1};
143
144 SLDataFormat_PCM format_pcm = {
145 SL_DATAFORMAT_PCM,
146 chunkFormat->num_channels,
147 chunkFormat->sample_rate * 1000, // convert to milliHz
148 chunkFormat->bits_per_sample,
149 16,
150 SL_SPEAKER_FRONT_CENTER,
151 SL_BYTEORDER_LITTLEENDIAN
152 };
153 SLDataSource audioSrc = {&loc_bufq, &format_pcm};
154
155 // configure audio sink
156 SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
157 SLDataSink audioSnk = {&loc_outmix, NULL};
158
159 // create audio player
Geoffrey Pitsch56133132016-07-15 10:50:04 -0400160 const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE, SL_IID_VOLUME, SL_IID_ANDROIDCONFIGURATION};
161 const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700162 result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk,
Geoffrey Pitsch56133132016-07-15 10:50:04 -0400163 3, ids, req);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400164 if (result != SL_RESULT_SUCCESS) {
165 ALOGE("sl CreateAudioPlayer failed with result %d", result);
166 return false;
167 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700168 (void)result;
169
Geoffrey Pitsch56133132016-07-15 10:50:04 -0400170 // Use the System stream for boot sound playback.
171 SLAndroidConfigurationItf playerConfig;
172 result = (*bqPlayerObject)->GetInterface(bqPlayerObject,
173 SL_IID_ANDROIDCONFIGURATION, &playerConfig);
174 if (result != SL_RESULT_SUCCESS) {
175 ALOGE("config GetInterface failed with result %d", result);
176 return false;
177 }
178 SLint32 streamType = SL_ANDROID_STREAM_SYSTEM;
179 result = (*playerConfig)->SetConfiguration(playerConfig,
180 SL_ANDROID_KEY_STREAM_TYPE, &streamType, sizeof(SLint32));
181 if (result != SL_RESULT_SUCCESS) {
182 ALOGE("SetConfiguration failed with result %d", result);
183 return false;
184 }
Eric Laurent197e4792016-07-21 18:17:15 -0700185 // use normal performance mode as low latency is not needed. This is not mandatory so
186 // do not bail if we fail
187 SLuint32 performanceMode = SL_ANDROID_PERFORMANCE_NONE;
188 result = (*playerConfig)->SetConfiguration(
189 playerConfig, SL_ANDROID_KEY_PERFORMANCE_MODE, &performanceMode, sizeof(SLuint32));
190 ALOGW_IF(result != SL_RESULT_SUCCESS,
191 "could not set performance mode on player, error %d", result);
192 (void)result;
Geoffrey Pitsch56133132016-07-15 10:50:04 -0400193
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700194 // realize the player
195 result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400196 if (result != SL_RESULT_SUCCESS) {
197 ALOGE("sl player Realize failed with result %d", result);
198 return false;
199 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700200 (void)result;
201
202 // get the play interface
203 result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400204 if (result != SL_RESULT_SUCCESS) {
205 ALOGE("sl player GetInterface failed with result %d", result);
206 return false;
207 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700208 (void)result;
209
210 // get the buffer queue interface
211 result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_BUFFERQUEUE,
212 &bqPlayerBufferQueue);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400213 if (result != SL_RESULT_SUCCESS) {
214 ALOGE("sl playberBufferQueue GetInterface failed with result %d", result);
215 return false;
216 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700217 (void)result;
218
219 // register callback on the buffer queue
220 result = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, NULL);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400221 if (result != SL_RESULT_SUCCESS) {
222 ALOGE("sl bqPlayerBufferQueue RegisterCallback failed with result %d", result);
223 return false;
224 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700225 (void)result;
226
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700227 // get the volume interface
228 result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400229 if (result != SL_RESULT_SUCCESS) {
230 ALOGE("sl volume GetInterface failed with result %d", result);
231 return false;
232 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700233 (void)result;
234
235 // set the player's state to playing
236 audioplay::setPlaying(true);
237 CHATTY("Created buffer queue player: %p", bqPlayerBufferQueue);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400238 return true;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700239}
240
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400241bool parseClipBuf(const uint8_t* clipBuf, int clipBufSize, const ChunkFormat** oChunkFormat,
242 const uint8_t** oSoundBuf, unsigned* oSoundBufSize) {
243 *oSoundBuf = clipBuf;
244 *oSoundBufSize = clipBufSize;
245 *oChunkFormat = NULL;
246 const RiffWaveHeader* wavHeader = (const RiffWaveHeader*)*oSoundBuf;
247 if (*oSoundBufSize < sizeof(*wavHeader) || (wavHeader->riff_id != ID_RIFF) ||
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700248 (wavHeader->wave_id != ID_WAVE)) {
249 ALOGE("Error: audio file is not a riff/wave file\n");
250 return false;
251 }
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400252 *oSoundBuf += sizeof(*wavHeader);
253 *oSoundBufSize -= sizeof(*wavHeader);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700254
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700255 while (true) {
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400256 const ChunkHeader* chunkHeader = (const ChunkHeader*)*oSoundBuf;
257 if (*oSoundBufSize < sizeof(*chunkHeader)) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700258 ALOGE("EOF reading chunk headers");
259 return false;
260 }
261
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400262 *oSoundBuf += sizeof(*chunkHeader);
263 *oSoundBufSize -= sizeof(*chunkHeader);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700264
265 bool endLoop = false;
266 switch (chunkHeader->id) {
267 case ID_FMT:
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400268 *oChunkFormat = (const ChunkFormat*)*oSoundBuf;
269 *oSoundBuf += chunkHeader->sz;
270 *oSoundBufSize -= chunkHeader->sz;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700271 break;
272 case ID_DATA:
273 /* Stop looking for chunks */
Eric Laurent197e4792016-07-21 18:17:15 -0700274 *oSoundBufSize = chunkHeader->sz;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700275 endLoop = true;
276 break;
277 default:
278 /* Unknown chunk, skip bytes */
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400279 *oSoundBuf += chunkHeader->sz;
280 *oSoundBufSize -= chunkHeader->sz;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700281 }
282 if (endLoop) {
283 break;
284 }
285 }
286
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400287 if (*oChunkFormat == NULL) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700288 ALOGE("format not found in WAV file");
289 return false;
290 }
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400291 return true;
292}
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700293
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400294} // namespace
295
296bool create(const uint8_t* exampleClipBuf, int exampleClipBufSize) {
297 if (!createEngine()) {
298 return false;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700299 }
300
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400301 // Parse the example clip.
302 const ChunkFormat* chunkFormat;
303 const uint8_t* soundBuf;
304 unsigned soundBufSize;
305 if (!parseClipBuf(exampleClipBuf, exampleClipBufSize, &chunkFormat, &soundBuf, &soundBufSize)) {
306 return false;
307 }
308
309 // Initialize the BufferQueue based on this clip's format.
310 if (!createBufferQueueAudioPlayer(chunkFormat)) {
311 return false;
312 }
313 return true;
314}
315
316bool playClip(const uint8_t* buf, int size) {
317 // Parse the WAV header
318 const ChunkFormat* chunkFormat;
319 if (!parseClipBuf(buf, size, &chunkFormat, &nextBuffer, &nextSize)) {
320 return false;
321 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700322
323 if (!hasPlayer()) {
324 ALOGD("cannot play clip %p without a player", buf);
325 return false;
326 }
327
Eric Laurent197e4792016-07-21 18:17:15 -0700328 CHATTY("playClip on player %p: buf=%p size=%d nextSize %d",
329 bqPlayerBufferQueue, buf, size, nextSize);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700330
331 if (nextSize > 0) {
332 // here we only enqueue one buffer because it is a long clip,
333 // but for streaming playback we would typically enqueue at least 2 buffers to start
334 SLresult result;
335 result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, nextBuffer, nextSize);
336 if (SL_RESULT_SUCCESS != result) {
337 return false;
338 }
339 audioplay::setPlaying(true);
340 }
341
342 return true;
343}
344
345// set the playing state for the buffer queue audio player
346void setPlaying(bool isPlaying) {
347 if (!hasPlayer()) return;
348
349 SLresult result;
350
351 if (NULL != bqPlayerPlay) {
352 // set the player's state
353 result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay,
354 isPlaying ? SL_PLAYSTATE_PLAYING : SL_PLAYSTATE_STOPPED);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700355 }
356
357}
358
359void destroy() {
360 // destroy buffer queue audio player object, and invalidate all associated interfaces
361 if (bqPlayerObject != NULL) {
362 CHATTY("destroying audio player");
363 (*bqPlayerObject)->Destroy(bqPlayerObject);
364 bqPlayerObject = NULL;
365 bqPlayerPlay = NULL;
366 bqPlayerBufferQueue = NULL;
367 bqPlayerMuteSolo = NULL;
368 bqPlayerVolume = NULL;
369 }
370
371 // destroy output mix object, and invalidate all associated interfaces
372 if (outputMixObject != NULL) {
373 (*outputMixObject)->Destroy(outputMixObject);
374 outputMixObject = NULL;
375 }
376
377 // destroy engine object, and invalidate all associated interfaces
378 if (engineObject != NULL) {
379 CHATTY("destroying audio engine");
380 (*engineObject)->Destroy(engineObject);
381 engineObject = NULL;
382 engineEngine = NULL;
383 }
384}
385
386} // namespace audioplay