• 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 #ifndef SDK_ANDROID_SRC_JNI_AUDIO_DEVICE_OPENSLES_COMMON_H_
12 #define SDK_ANDROID_SRC_JNI_AUDIO_DEVICE_OPENSLES_COMMON_H_
13 
14 #include <SLES/OpenSLES.h>
15 #include <stddef.h>
16 
17 #include "api/ref_counted_base.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/logging.h"
20 #include "rtc_base/thread_checker.h"
21 
22 namespace webrtc {
23 
24 namespace jni {
25 
26 // Returns a string representation given an integer SL_RESULT_XXX code.
27 // The mapping can be found in <SLES/OpenSLES.h>.
28 const char* GetSLErrorString(size_t code);
29 
30 // Configures an SL_DATAFORMAT_PCM structure based on native audio parameters.
31 SLDataFormat_PCM CreatePCMConfiguration(size_t channels,
32                                         int sample_rate,
33                                         size_t bits_per_sample);
34 
35 // Helper class for using SLObjectItf interfaces.
36 template <typename SLType, typename SLDerefType>
37 class ScopedSLObject {
38  public:
ScopedSLObject()39   ScopedSLObject() : obj_(nullptr) {}
40 
~ScopedSLObject()41   ~ScopedSLObject() { Reset(); }
42 
Receive()43   SLType* Receive() {
44     RTC_DCHECK(!obj_);
45     return &obj_;
46   }
47 
48   SLDerefType operator->() { return *obj_; }
49 
Get()50   SLType Get() const { return obj_; }
51 
Reset()52   void Reset() {
53     if (obj_) {
54       (*obj_)->Destroy(obj_);
55       obj_ = nullptr;
56     }
57   }
58 
59  private:
60   SLType obj_;
61 };
62 
63 typedef ScopedSLObject<SLObjectItf, const SLObjectItf_*> ScopedSLObjectItf;
64 
65 // Creates and realizes the main (global) Open SL engine object and returns
66 // a reference to it. The engine object is only created at the first call
67 // since OpenSL ES for Android only supports a single engine per application.
68 // Subsequent calls returns the already created engine.
69 // Note: This class must be used single threaded and this is enforced by a
70 // thread checker.
71 class OpenSLEngineManager : public rtc::RefCountedBase {
72  public:
73   OpenSLEngineManager();
74   ~OpenSLEngineManager() override;
75   SLObjectItf GetOpenSLEngine();
76 
77  private:
78   rtc::ThreadChecker thread_checker_;
79   // This object is the global entry point of the OpenSL ES API.
80   // After creating the engine object, the application can obtain this object‘s
81   // SLEngineItf interface. This interface contains creation methods for all
82   // the other object types in the API. None of these interface are realized
83   // by this class. It only provides access to the global engine object.
84   ScopedSLObjectItf engine_object_;
85 };
86 
87 }  // namespace jni
88 
89 }  // namespace webrtc
90 
91 #endif  // SDK_ANDROID_SRC_JNI_AUDIO_DEVICE_OPENSLES_COMMON_H_
92