• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <hardware/audio.h>
20 
21 #include <functional>
22 
23 #include "bta/le_audio/codec_manager.h"
24 #include "bta/le_audio/le_audio_types.h"
25 #include "common/message_loop_thread.h"
26 
27 namespace bluetooth {
28 namespace audio {
29 namespace le_audio {
30 
31 enum class StartRequestState {
32   IDLE = 0x00,
33   PENDING_BEFORE_RESUME,
34   PENDING_AFTER_RESUME,
35   CONFIRMED,
36   CANCELED,
37 };
38 
39 constexpr uint8_t kChannelNumberMono = 1;
40 constexpr uint8_t kChannelNumberStereo = 2;
41 
42 constexpr uint32_t kSampleRate48000 = 48000;
43 constexpr uint32_t kSampleRate44100 = 44100;
44 constexpr uint32_t kSampleRate32000 = 32000;
45 constexpr uint32_t kSampleRate24000 = 24000;
46 constexpr uint32_t kSampleRate16000 = 16000;
47 constexpr uint32_t kSampleRate8000 = 8000;
48 
49 constexpr uint8_t kBitsPerSample16 = 16;
50 constexpr uint8_t kBitsPerSample24 = 24;
51 constexpr uint8_t kBitsPerSample32 = 32;
52 
53 struct StreamCallbacks {
54   std::function<bool(bool start_media_task)> on_resume_;
55   std::function<bool(void)> on_suspend_;
56   std::function<bool(const source_metadata_t&)> on_metadata_update_;
57   std::function<bool(const sink_metadata_t&)> on_sink_metadata_update_;
58 };
59 
60 std::vector<::le_audio::set_configurations::AudioSetConfiguration>
61 get_offload_capabilities();
62 int GetAidlInterfaceVersion();
63 
64 class LeAudioClientInterface {
65  public:
66   struct PcmParameters {
67     uint32_t data_interval_us;
68     uint32_t sample_rate;
69     uint8_t bits_per_sample;
70     uint8_t channels_count;
71   };
72 
73  private:
74   class IClientInterfaceEndpoint {
75    public:
76     virtual ~IClientInterfaceEndpoint() = default;
77     virtual void Cleanup() = 0;
78     virtual void SetPcmParameters(const PcmParameters& params) = 0;
79     virtual void SetRemoteDelay(uint16_t delay_report_ms) = 0;
80     virtual void StartSession() = 0;
81     virtual void StopSession() = 0;
82     virtual void ConfirmStreamingRequest() = 0;
83     virtual void CancelStreamingRequest() = 0;
84     virtual void UpdateAudioConfigToHal(
85         const ::le_audio::offload_config& config) = 0;
86     virtual void SuspendedForReconfiguration() = 0;
87     virtual void ReconfigurationComplete() = 0;
88   };
89 
90  public:
91   class Sink : public IClientInterfaceEndpoint {
92    public:
is_broadcaster_(is_broadcaster)93     Sink(bool is_broadcaster = false) : is_broadcaster_(is_broadcaster){};
94     virtual ~Sink() = default;
95 
96     void Cleanup() override;
97     void SetPcmParameters(const PcmParameters& params) override;
98     void SetRemoteDelay(uint16_t delay_report_ms) override;
99     void StartSession() override;
100     void StopSession() override;
101     void ConfirmStreamingRequest() override;
102     void CancelStreamingRequest() override;
103     void UpdateAudioConfigToHal(
104         const ::le_audio::offload_config& config) override;
105     void UpdateBroadcastAudioConfigToHal(
106         const ::le_audio::broadcast_offload_config& config);
107     void SuspendedForReconfiguration() override;
108     void ReconfigurationComplete() override;
109     // Read the stream of bytes sinked to us by the upper layers
110     size_t Read(uint8_t* p_buf, uint32_t len);
IsBroadcaster()111     bool IsBroadcaster() { return is_broadcaster_; }
112 
113    private:
114     bool is_broadcaster_ = false;
115   };
116   class Source : public IClientInterfaceEndpoint {
117    public:
118     virtual ~Source() = default;
119 
120     void Cleanup() override;
121     void SetPcmParameters(const PcmParameters& params) override;
122     void SetRemoteDelay(uint16_t delay_report_ms) override;
123     void StartSession() override;
124     void StopSession() override;
125     void ConfirmStreamingRequest() override;
126     void CancelStreamingRequest() override;
127     void UpdateAudioConfigToHal(
128         const ::le_audio::offload_config& config) override;
129     void SuspendedForReconfiguration() override;
130     void ReconfigurationComplete() override;
131     // Source the given stream of bytes to be sinked into the upper layers
132     size_t Write(const uint8_t* p_buf, uint32_t len);
133   };
134 
135   // Get LE Audio sink client interface if it's not previously acquired and not
136   // yet released.
137   Sink* GetSink(StreamCallbacks stream_cb,
138                 bluetooth::common::MessageLoopThread* message_loop,
139                 bool is_broadcasting_session_type);
140   // This should be called before trying to get unicast sink interface
141   bool IsUnicastSinkAcquired();
142   // This should be called before trying to get broadcast sink interface
143   bool IsBroadcastSinkAcquired();
144   // Release sink interface if belongs to LE audio client interface
145   bool ReleaseSink(Sink* sink);
146 
147   // Get LE Audio source client interface if it's not previously acquired and
148   // not yet released.
149   Source* GetSource(StreamCallbacks stream_cb,
150                     bluetooth::common::MessageLoopThread* message_loop);
151   // This should be called before trying to get source interface
152   bool IsSourceAcquired();
153   // Release source interface if belongs to LE audio client interface
154   bool ReleaseSource(Source* source);
155 
156   // Get interface, if previously not initialized - it'll initialize singleton.
157   static LeAudioClientInterface* Get();
158 
159  private:
160   static LeAudioClientInterface* interface;
161   Sink* unicast_sink_ = nullptr;
162   Sink* broadcast_sink_ = nullptr;
163   Source* source_ = nullptr;
164 };
165 
166 }  // namespace le_audio
167 }  // namespace audio
168 }  // namespace bluetooth
169