• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #include <system/audio.h>
21 
22 #include <list>
23 
24 #include "device_port_proxy.h"
25 #include "device_port_proxy_hidl.h"
26 
27 constexpr unsigned int kBluetoothDefaultSampleRate = 44100;
28 constexpr audio_format_t kBluetoothDefaultAudioFormatBitsPerSample = AUDIO_FORMAT_PCM_16_BIT;
29 
30 constexpr unsigned int kBluetoothDefaultInputBufferMs = 20;
31 constexpr unsigned int kBluetoothDefaultInputStateTimeoutMs = 20;
32 
33 constexpr unsigned int kBluetoothDefaultOutputBufferMs = 10;
34 constexpr unsigned int kBluetoothSpatializerOutputBufferMs = 10;
35 
36 constexpr audio_channel_mask_t kBluetoothDefaultOutputChannelModeMask = AUDIO_CHANNEL_OUT_STEREO;
37 constexpr audio_channel_mask_t kBluetoothDefaultInputChannelModeMask = AUDIO_CHANNEL_IN_MONO;
38 
39 enum class BluetoothStreamState : uint8_t {
40   DISABLED = 0,  // This stream is closing or set param "suspend=true"
41   STANDBY,
42   STARTING,
43   STARTED,
44   SUSPENDING,
45   UNKNOWN,
46 };
47 
48 std::ostream& operator<<(std::ostream& os, const BluetoothStreamState& state);
49 
50 struct BluetoothAudioDevice;
51 
52 struct BluetoothStreamOut {
53   // Must be the first member so it can be cast from audio_stream
54   // or audio_stream_out pointer
55   audio_stream_out stream_out_{};
56   std::unique_ptr<::android::bluetooth::audio::BluetoothAudioPort> bluetooth_output_;
57   bool is_aidl;
58   int64_t last_write_time_us_;
59   // Audio PCM Configs
60   uint32_t sample_rate_;
61   audio_channel_mask_t channel_mask_;
62   audio_format_t format_;
63   size_t preferred_data_interval_us;
64   // frame is the number of samples per channel
65   // frames count per tick
66   size_t frames_count_;
67   // total frames written, reset on standby
68   uint64_t frames_rendered_;
69   // total frames written after opened, never reset
70   uint64_t frames_presented_;
71   bool is_low_latency_ = false;
72   BluetoothAudioDevice* bt_dev_;
73   mutable std::mutex mutex_;
74 };
75 
76 struct BluetoothAudioDevice {
77   // Important: device must be first as an audio_hw_device* may be cast to
78   // BluetoothAudioDevice* when the type is implicitly known.
79   audio_hw_device audio_device_{};
80   // protect against device->output and stream_out from being inconsistent
81   std::mutex mutex_;
82   std::list<BluetoothStreamOut*> opened_stream_outs_ = std::list<BluetoothStreamOut*>(0);
83   uint32_t next_unique_id = 1;
84   bool support_low_latency_ = false;
85 };
86 
87 struct BluetoothStreamIn {
88   // Must be the first member so it can be cast from audio_stream
89   // or audio_stream_in pointer
90   audio_stream_in stream_in_;
91   std::unique_ptr<::android::bluetooth::audio::BluetoothAudioPort> bluetooth_input_;
92   bool is_aidl;
93   int64_t last_read_time_us_;
94   // Audio PCM Configs
95   uint32_t sample_rate_;
96   audio_channel_mask_t channel_mask_;
97   audio_format_t format_;
98   size_t preferred_data_interval_us;
99   // frame is the number of samples per channel
100   // frames count per tick
101   size_t frames_count_;
102   // total frames read after opened, never reset
103   uint64_t frames_presented_;
104   mutable std::mutex mutex_;
105 };
106 
107 int adev_open_output_stream(struct audio_hw_device* dev, audio_io_handle_t handle,
108                             audio_devices_t devices, audio_output_flags_t flags,
109                             struct audio_config* config, struct audio_stream_out** stream_out,
110                             const char* address __unused);
111 
112 void adev_close_output_stream(struct audio_hw_device* dev, struct audio_stream_out* stream);
113 
114 size_t adev_get_input_buffer_size(const struct audio_hw_device* dev,
115                                   const struct audio_config* config);
116 
117 int adev_open_input_stream(struct audio_hw_device* dev, audio_io_handle_t handle,
118                            audio_devices_t devices, struct audio_config* config,
119                            struct audio_stream_in** stream_in, audio_input_flags_t flags __unused,
120                            const char* address __unused, audio_source_t source __unused);
121 
122 void adev_close_input_stream(struct audio_hw_device* dev, struct audio_stream_in* in);
123