henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/modules/audio_device/android/opensles_common.h" |
| 12 | |
| 13 | #include <assert.h> |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame^] | 14 | #include <SLES/OpenSLES.h> |
henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 15 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame^] | 16 | #include "webrtc/base/arraysize.h" |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/audio_device/android/audio_common.h" |
| 18 | |
| 19 | using webrtc::kNumChannels; |
| 20 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 21 | namespace webrtc { |
henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 22 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame^] | 23 | // Returns a string representation given an integer SL_RESULT_XXX code. |
| 24 | // The mapping can be found in <SLES/OpenSLES.h>. |
| 25 | const char* GetSLErrorString(size_t code) { |
| 26 | static const char* sl_error_strings[] = { |
| 27 | "SL_RESULT_SUCCESS", // 0 |
| 28 | "SL_RESULT_PRECONDITIONS_VIOLATED", // 1 |
| 29 | "SL_RESULT_PARAMETER_INVALID", // 2 |
| 30 | "SL_RESULT_MEMORY_FAILURE", // 3 |
| 31 | "SL_RESULT_RESOURCE_ERROR", // 4 |
| 32 | "SL_RESULT_RESOURCE_LOST", // 5 |
| 33 | "SL_RESULT_IO_ERROR", // 6 |
| 34 | "SL_RESULT_BUFFER_INSUFFICIENT", // 7 |
| 35 | "SL_RESULT_CONTENT_CORRUPTED", // 8 |
| 36 | "SL_RESULT_CONTENT_UNSUPPORTED", // 9 |
| 37 | "SL_RESULT_CONTENT_NOT_FOUND", // 10 |
| 38 | "SL_RESULT_PERMISSION_DENIED", // 11 |
| 39 | "SL_RESULT_FEATURE_UNSUPPORTED", // 12 |
| 40 | "SL_RESULT_INTERNAL_ERROR", // 13 |
| 41 | "SL_RESULT_UNKNOWN_ERROR", // 14 |
| 42 | "SL_RESULT_OPERATION_ABORTED", // 15 |
| 43 | "SL_RESULT_CONTROL_LOST", // 16 |
| 44 | }; |
| 45 | |
| 46 | if (code >= arraysize(sl_error_strings)) { |
| 47 | return "SL_RESULT_UNKNOWN_ERROR"; |
| 48 | } |
| 49 | return sl_error_strings[code]; |
| 50 | } |
| 51 | |
henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 52 | SLDataFormat_PCM CreatePcmConfiguration(int sample_rate) { |
| 53 | SLDataFormat_PCM configuration; |
| 54 | configuration.formatType = SL_DATAFORMAT_PCM; |
| 55 | configuration.numChannels = kNumChannels; |
| 56 | // According to the opensles documentation in the ndk: |
| 57 | // samplesPerSec is actually in units of milliHz, despite the misleading name. |
| 58 | // It further recommends using constants. However, this would lead to a lot |
| 59 | // of boilerplate code so it is not done here. |
| 60 | configuration.samplesPerSec = sample_rate * 1000; |
| 61 | configuration.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; |
| 62 | configuration.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16; |
| 63 | configuration.channelMask = SL_SPEAKER_FRONT_CENTER; |
| 64 | if (2 == configuration.numChannels) { |
| 65 | configuration.channelMask = |
| 66 | SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; |
| 67 | } |
| 68 | configuration.endianness = SL_BYTEORDER_LITTLEENDIAN; |
| 69 | return configuration; |
| 70 | } |
| 71 | |
| 72 | } // namespace webrtc_opensl |