xref: /aosp_15_r20/external/webrtc/sdk/android/src/jni/audio_device/opensles_common.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "api/sequence_checker.h"
19 #include "rtc_base/checks.h"
20 #include "rtc_base/logging.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
72     : public rtc::RefCountedNonVirtual<OpenSLEngineManager> {
73  public:
74   OpenSLEngineManager();
75   ~OpenSLEngineManager() = default;
76   SLObjectItf GetOpenSLEngine();
77 
78  private:
79   SequenceChecker thread_checker_;
80   // This object is the global entry point of the OpenSL ES API.
81   // After creating the engine object, the application can obtain this object‘s
82   // SLEngineItf interface. This interface contains creation methods for all
83   // the other object types in the API. None of these interface are realized
84   // by this class. It only provides access to the global engine object.
85   ScopedSLObjectItf engine_object_;
86 };
87 
88 }  // namespace jni
89 
90 }  // namespace webrtc
91 
92 #endif  // SDK_ANDROID_SRC_JNI_AUDIO_DEVICE_OPENSLES_COMMON_H_
93