• 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 "sdk/android/src/jni/audio_device/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 namespace jni {
21 
22 // Returns a string representation given an integer SL_RESULT_XXX code.
23 // The mapping can be found in <SLES/OpenSLES.h>.
GetSLErrorString(size_t code)24 const char* GetSLErrorString(size_t code) {
25   static const char* sl_error_strings[] = {
26       "SL_RESULT_SUCCESS",                 // 0
27       "SL_RESULT_PRECONDITIONS_VIOLATED",  // 1
28       "SL_RESULT_PARAMETER_INVALID",       // 2
29       "SL_RESULT_MEMORY_FAILURE",          // 3
30       "SL_RESULT_RESOURCE_ERROR",          // 4
31       "SL_RESULT_RESOURCE_LOST",           // 5
32       "SL_RESULT_IO_ERROR",                // 6
33       "SL_RESULT_BUFFER_INSUFFICIENT",     // 7
34       "SL_RESULT_CONTENT_CORRUPTED",       // 8
35       "SL_RESULT_CONTENT_UNSUPPORTED",     // 9
36       "SL_RESULT_CONTENT_NOT_FOUND",       // 10
37       "SL_RESULT_PERMISSION_DENIED",       // 11
38       "SL_RESULT_FEATURE_UNSUPPORTED",     // 12
39       "SL_RESULT_INTERNAL_ERROR",          // 13
40       "SL_RESULT_UNKNOWN_ERROR",           // 14
41       "SL_RESULT_OPERATION_ABORTED",       // 15
42       "SL_RESULT_CONTROL_LOST",            // 16
43   };
44 
45   if (code >= arraysize(sl_error_strings)) {
46     return "SL_RESULT_UNKNOWN_ERROR";
47   }
48   return sl_error_strings[code];
49 }
50 
CreatePCMConfiguration(size_t channels,int sample_rate,size_t bits_per_sample)51 SLDataFormat_PCM CreatePCMConfiguration(size_t channels,
52                                         int sample_rate,
53                                         size_t bits_per_sample) {
54   RTC_CHECK_EQ(bits_per_sample, SL_PCMSAMPLEFORMAT_FIXED_16);
55   SLDataFormat_PCM format;
56   format.formatType = SL_DATAFORMAT_PCM;
57   format.numChannels = static_cast<SLuint32>(channels);
58   // Note that, the unit of sample rate is actually in milliHertz and not Hertz.
59   switch (sample_rate) {
60     case 8000:
61       format.samplesPerSec = SL_SAMPLINGRATE_8;
62       break;
63     case 16000:
64       format.samplesPerSec = SL_SAMPLINGRATE_16;
65       break;
66     case 22050:
67       format.samplesPerSec = SL_SAMPLINGRATE_22_05;
68       break;
69     case 32000:
70       format.samplesPerSec = SL_SAMPLINGRATE_32;
71       break;
72     case 44100:
73       format.samplesPerSec = SL_SAMPLINGRATE_44_1;
74       break;
75     case 48000:
76       format.samplesPerSec = SL_SAMPLINGRATE_48;
77       break;
78     case 64000:
79       format.samplesPerSec = SL_SAMPLINGRATE_64;
80       break;
81     case 88200:
82       format.samplesPerSec = SL_SAMPLINGRATE_88_2;
83       break;
84     case 96000:
85       format.samplesPerSec = SL_SAMPLINGRATE_96;
86       break;
87     default:
88       RTC_CHECK(false) << "Unsupported sample rate: " << sample_rate;
89       break;
90   }
91   format.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
92   format.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
93   format.endianness = SL_BYTEORDER_LITTLEENDIAN;
94   if (format.numChannels == 1) {
95     format.channelMask = SL_SPEAKER_FRONT_CENTER;
96   } else if (format.numChannels == 2) {
97     format.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
98   } else {
99     RTC_CHECK(false) << "Unsupported number of channels: "
100                      << format.numChannels;
101   }
102   return format;
103 }
104 
OpenSLEngineManager()105 OpenSLEngineManager::OpenSLEngineManager() {
106   thread_checker_.Detach();
107 }
108 
109 OpenSLEngineManager::~OpenSLEngineManager() = default;
110 
GetOpenSLEngine()111 SLObjectItf OpenSLEngineManager::GetOpenSLEngine() {
112   RTC_LOG(INFO) << "GetOpenSLEngine";
113   RTC_DCHECK(thread_checker_.IsCurrent());
114   // OpenSL ES for Android only supports a single engine per application.
115   // If one already has been created, return existing object instead of
116   // creating a new.
117   if (engine_object_.Get() != nullptr) {
118     RTC_LOG(WARNING) << "The OpenSL ES engine object has already been created";
119     return engine_object_.Get();
120   }
121   // Create the engine object in thread safe mode.
122   const SLEngineOption option[] = {
123       {SL_ENGINEOPTION_THREADSAFE, static_cast<SLuint32>(SL_BOOLEAN_TRUE)}};
124   SLresult result =
125       slCreateEngine(engine_object_.Receive(), 1, option, 0, NULL, NULL);
126   if (result != SL_RESULT_SUCCESS) {
127     RTC_LOG(LS_ERROR) << "slCreateEngine() failed: "
128                       << GetSLErrorString(result);
129     engine_object_.Reset();
130     return nullptr;
131   }
132   // Realize the SL Engine in synchronous mode.
133   result = engine_object_->Realize(engine_object_.Get(), SL_BOOLEAN_FALSE);
134   if (result != SL_RESULT_SUCCESS) {
135     RTC_LOG(LS_ERROR) << "Realize() failed: " << GetSLErrorString(result);
136     engine_object_.Reset();
137     return nullptr;
138   }
139   // Finally return the SLObjectItf interface of the engine object.
140   return engine_object_.Get();
141 }
142 
143 }  // namespace jni
144 
145 }  // namespace webrtc
146