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 <functional>
20 
21 #include "common/message_loop_thread.h"
22 
23 namespace bluetooth {
24 namespace audio {
25 namespace le_audio {
26 
27 struct StreamCallbacks {
28   std::function<bool(bool start_media_task)> on_resume_;
29   std::function<bool(void)> on_suspend_;
30 };
31 
32 class LeAudioClientInterface {
33  public:
34   struct PcmParameters {
35     uint32_t data_interval_us;
36     uint32_t sample_rate;
37     uint8_t bits_per_sample;
38     uint8_t channels_count;
39   };
40 
41  private:
42   class IClientInterfaceEndpoint {
43    public:
44     virtual ~IClientInterfaceEndpoint() = default;
45     virtual void Cleanup() = 0;
46     virtual void SetPcmParameters(const PcmParameters& params) = 0;
47     virtual void SetRemoteDelay(uint16_t delay_report_ms) = 0;
48     virtual void StartSession() = 0;
49     virtual void StopSession() = 0;
50   };
51 
52  public:
53   class Sink : public IClientInterfaceEndpoint {
54    public:
55     virtual ~Sink() = default;
56 
57     void Cleanup() override;
58     void SetPcmParameters(const PcmParameters& params) override;
59     void SetRemoteDelay(uint16_t delay_report_ms) override;
60     void StartSession() override;
61     void StopSession() override;
62 
63     // Read the stream of bytes sinked to us by the upper layers
64     size_t Read(uint8_t* p_buf, uint32_t len);
65   };
66   class Source : public IClientInterfaceEndpoint {
67    public:
68     virtual ~Source() = default;
69 
70     void Cleanup() override;
71     void SetPcmParameters(const PcmParameters& params) override;
72     void SetRemoteDelay(uint16_t delay_report_ms) override;
73     void StartSession() override;
74     void StopSession() override;
75 
76     // Source the given stream of bytes to be sinked into the upper layers
77     size_t Write(const uint8_t* p_buf, uint32_t len);
78   };
79 
80   // Get LE Audio sink client interface if it's not previously acquired and not
81   // yet released.
82   Sink* GetSink(StreamCallbacks stream_cb,
83                 bluetooth::common::MessageLoopThread* message_loop);
84   // This should be called before trying to get sink interface
85   bool IsSinkAcquired();
86   // Release sink interface if belongs to LE audio client interface
87   bool ReleaseSink(Sink* sink);
88 
89   // Get LE Audio source client interface if it's not previously acquired and
90   // not yet released.
91   Source* GetSource(StreamCallbacks stream_cb,
92                     bluetooth::common::MessageLoopThread* message_loop);
93   // This should be called before trying to get source interface
94   bool IsSourceAcquired();
95   // Release source interface if belongs to LE audio client interface
96   bool ReleaseSource(Source* source);
97 
98   // Get interface, if previously not initialized - it'll initialize singleton.
99   static LeAudioClientInterface* Get();
100 
101  private:
102   static LeAudioClientInterface* interface;
103   Sink* sink_ = nullptr;
104   Source* source_ = nullptr;
105 };
106 
107 }  // namespace le_audio
108 }  // namespace audio
109 }  // namespace bluetooth
110