• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "modules/audio_device/android/opensles_common.h"
12 
13 #include <SLES/OpenSLES.h>
14 
15 #include "rtc_base/arraysize.h"
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 
20 // Returns a string representation given an integer SL_RESULT_XXX code.
21 // The mapping can be found in <SLES/OpenSLES.h>.
GetSLErrorString(size_t code)22 const char* GetSLErrorString(size_t code) {
23   static const char* sl_error_strings[] = {
24       "SL_RESULT_SUCCESS",                 // 0
25       "SL_RESULT_PRECONDITIONS_VIOLATED",  // 1
26       "SL_RESULT_PARAMETER_INVALID",       // 2
27       "SL_RESULT_MEMORY_FAILURE",          // 3
28       "SL_RESULT_RESOURCE_ERROR",          // 4
29       "SL_RESULT_RESOURCE_LOST",           // 5
30       "SL_RESULT_IO_ERROR",                // 6
31       "SL_RESULT_BUFFER_INSUFFICIENT",     // 7
32       "SL_RESULT_CONTENT_CORRUPTED",       // 8
33       "SL_RESULT_CONTENT_UNSUPPORTED",     // 9
34       "SL_RESULT_CONTENT_NOT_FOUND",       // 10
35       "SL_RESULT_PERMISSION_DENIED",       // 11
36       "SL_RESULT_FEATURE_UNSUPPORTED",     // 12
37       "SL_RESULT_INTERNAL_ERROR",          // 13
38       "SL_RESULT_UNKNOWN_ERROR",           // 14
39       "SL_RESULT_OPERATION_ABORTED",       // 15
40       "SL_RESULT_CONTROL_LOST",            // 16
41   };
42 
43   if (code >= arraysize(sl_error_strings)) {
44     return "SL_RESULT_UNKNOWN_ERROR";
45   }
46   return sl_error_strings[code];
47 }
48 
CreatePCMConfiguration(size_t channels,int sample_rate,size_t bits_per_sample)49 SLDataFormat_PCM CreatePCMConfiguration(size_t channels,
50                                         int sample_rate,
51                                         size_t bits_per_sample) {
52   RTC_CHECK_EQ(bits_per_sample, SL_PCMSAMPLEFORMAT_FIXED_16);
53   SLDataFormat_PCM format;
54   format.formatType = SL_DATAFORMAT_PCM;
55   format.numChannels = static_cast<SLuint32>(channels);
56   // Note that, the unit of sample rate is actually in milliHertz and not Hertz.
57   switch (sample_rate) {
58     case 8000:
59       format.samplesPerSec = SL_SAMPLINGRATE_8;
60       break;
61     case 16000:
62       format.samplesPerSec = SL_SAMPLINGRATE_16;
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     case 64000:
77       format.samplesPerSec = SL_SAMPLINGRATE_64;
78       break;
79     case 88200:
80       format.samplesPerSec = SL_SAMPLINGRATE_88_2;
81       break;
82     case 96000:
83       format.samplesPerSec = SL_SAMPLINGRATE_96;
84       break;
85     default:
86       RTC_CHECK(false) << "Unsupported sample rate: " << sample_rate;
87       break;
88   }
89   format.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
90   format.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
91   format.endianness = SL_BYTEORDER_LITTLEENDIAN;
92   if (format.numChannels == 1) {
93     format.channelMask = SL_SPEAKER_FRONT_CENTER;
94   } else if (format.numChannels == 2) {
95     format.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
96   } else {
97     RTC_CHECK(false) << "Unsupported number of channels: "
98                      << format.numChannels;
99   }
100   return format;
101 }
102 
103 }  // namespace webrtc
104