OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_device/android/opensles_common.h" | 11 #include "webrtc/modules/audio_device/android/opensles_common.h" |
12 | 12 |
13 #include <assert.h> | |
14 #include <SLES/OpenSLES.h> | 13 #include <SLES/OpenSLES.h> |
15 | 14 |
16 #include "webrtc/base/arraysize.h" | 15 #include "webrtc/base/arraysize.h" |
17 #include "webrtc/modules/audio_device/android/audio_common.h" | 16 #include "webrtc/base/checks.h" |
18 | |
19 using webrtc::kNumChannels; | |
20 | 17 |
21 namespace webrtc { | 18 namespace webrtc { |
22 | 19 |
23 // Returns a string representation given an integer SL_RESULT_XXX code. | 20 // Returns a string representation given an integer SL_RESULT_XXX code. |
24 // The mapping can be found in <SLES/OpenSLES.h>. | 21 // The mapping can be found in <SLES/OpenSLES.h>. |
25 const char* GetSLErrorString(size_t code) { | 22 const char* GetSLErrorString(size_t code) { |
26 static const char* sl_error_strings[] = { | 23 static const char* sl_error_strings[] = { |
27 "SL_RESULT_SUCCESS", // 0 | 24 "SL_RESULT_SUCCESS", // 0 |
28 "SL_RESULT_PRECONDITIONS_VIOLATED", // 1 | 25 "SL_RESULT_PRECONDITIONS_VIOLATED", // 1 |
29 "SL_RESULT_PARAMETER_INVALID", // 2 | 26 "SL_RESULT_PARAMETER_INVALID", // 2 |
(...skipping 12 matching lines...) Expand all Loading... | |
42 "SL_RESULT_OPERATION_ABORTED", // 15 | 39 "SL_RESULT_OPERATION_ABORTED", // 15 |
43 "SL_RESULT_CONTROL_LOST", // 16 | 40 "SL_RESULT_CONTROL_LOST", // 16 |
44 }; | 41 }; |
45 | 42 |
46 if (code >= arraysize(sl_error_strings)) { | 43 if (code >= arraysize(sl_error_strings)) { |
47 return "SL_RESULT_UNKNOWN_ERROR"; | 44 return "SL_RESULT_UNKNOWN_ERROR"; |
48 } | 45 } |
49 return sl_error_strings[code]; | 46 return sl_error_strings[code]; |
50 } | 47 } |
51 | 48 |
52 SLDataFormat_PCM CreatePcmConfiguration(int sample_rate) { | 49 SLDataFormat_PCM CreatePCMConfiguration(size_t channels, |
53 SLDataFormat_PCM configuration; | 50 int sample_rate, |
54 configuration.formatType = SL_DATAFORMAT_PCM; | 51 size_t bits_per_sample) { |
55 configuration.numChannels = kNumChannels; | 52 RTC_CHECK_EQ(bits_per_sample, SL_PCMSAMPLEFORMAT_FIXED_16); |
56 // According to the opensles documentation in the ndk: | 53 SLDataFormat_PCM format; |
57 // samplesPerSec is actually in units of milliHz, despite the misleading name. | 54 format.formatType = SL_DATAFORMAT_PCM; |
58 // It further recommends using constants. However, this would lead to a lot | 55 format.numChannels = static_cast<SLuint32>(channels); |
59 // of boilerplate code so it is not done here. | 56 // Note that, the unit of sample rate is actually in milliHertz and not Hertz. |
60 configuration.samplesPerSec = sample_rate * 1000; | 57 switch (sample_rate) { |
61 configuration.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; | 58 case 8000: |
62 configuration.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16; | 59 format.samplesPerSec = SL_SAMPLINGRATE_8; |
63 configuration.channelMask = SL_SPEAKER_FRONT_CENTER; | 60 break; |
64 if (2 == configuration.numChannels) { | 61 case 16000: |
65 configuration.channelMask = | 62 format.samplesPerSec = SL_SAMPLINGRATE_16; |
66 SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; | 63 break; |
64 case 22050: | |
65 format.samplesPerSec = SL_SAMPLINGRATE_22_05; | |
66 break; | |
67 case 32000: | |
68 format.samplesPerSec = SL_SAMPLINGRATE_32; | |
69 break; | |
70 case 44100: | |
71 format.samplesPerSec = SL_SAMPLINGRATE_44_1; | |
72 break; | |
73 case 48000: | |
74 format.samplesPerSec = SL_SAMPLINGRATE_48; | |
75 break; | |
76 default: | |
tommi
2016/09/15 09:34:13
I think I've read somewhere that 96kHz is supporte
henrika_webrtc
2016/09/16 13:30:47
Great feedback. You are correct. I have never seen
| |
77 RTC_CHECK(false) << "Unsupported sample rate: " << sample_rate; | |
tommi
2016/09/15 09:34:12
nit: also include a break; here for consistency.
henrika_webrtc
2016/09/16 13:30:47
Done.
| |
67 } | 78 } |
68 configuration.endianness = SL_BYTEORDER_LITTLEENDIAN; | 79 format.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; |
69 return configuration; | 80 format.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16; |
81 format.endianness = SL_BYTEORDER_LITTLEENDIAN; | |
82 if (format.numChannels == 1) | |
tommi
2016/09/15 09:34:13
{}
(see lines below)
henrika_webrtc
2016/09/16 13:30:47
Done.
| |
83 format.channelMask = SL_SPEAKER_FRONT_CENTER; | |
84 else if (format.numChannels == 2) | |
85 format.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; | |
86 else | |
87 RTC_CHECK(false) << "Unsupported number of channels: " | |
88 << format.numChannels; | |
89 return format; | |
70 } | 90 } |
71 | 91 |
72 } // namespace webrtc_opensl | 92 } // namespace webrtc |
OLD | NEW |