OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_BLUEZ_H_ | |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_BLUEZ_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/files/file.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/message_loop/message_loop.h" | |
18 #include "base/observer_list.h" | |
19 #include "dbus/file_descriptor.h" | |
20 #include "dbus/object_path.h" | |
21 #include "device/bluetooth/bluetooth_adapter.h" | |
22 #include "device/bluetooth/bluetooth_audio_sink.h" | |
23 #include "device/bluetooth/bluetooth_export.h" | |
24 #include "device/bluetooth/dbus/bluetooth_media_client.h" | |
25 #include "device/bluetooth/dbus/bluetooth_media_endpoint_service_provider.h" | |
26 #include "device/bluetooth/dbus/bluetooth_media_transport_client.h" | |
27 | |
28 namespace bluez { | |
29 | |
30 class BluetoothAudioSinkBlueZTest; | |
31 | |
32 class DEVICE_BLUETOOTH_EXPORT BluetoothAudioSinkBlueZ | |
33 : public device::BluetoothAudioSink, | |
34 public device::BluetoothAdapter::Observer, | |
35 public bluez::BluetoothMediaClient::Observer, | |
36 public bluez::BluetoothMediaTransportClient::Observer, | |
37 public bluez::BluetoothMediaEndpointServiceProvider::Delegate, | |
38 public base::MessageLoopForIO::Watcher { | |
39 public: | |
40 explicit BluetoothAudioSinkBlueZ( | |
41 scoped_refptr<device::BluetoothAdapter> adapter); | |
42 | |
43 // device::BluetoothAudioSink overrides. | |
44 // Unregisters a BluetoothAudioSink. |callback| should handle | |
45 // the clean-up after the audio sink is deleted successfully, otherwise | |
46 // |error_callback| will be called. | |
47 void Unregister( | |
48 const base::Closure& callback, | |
49 const device::BluetoothAudioSink::ErrorCallback& error_callback) override; | |
50 void AddObserver(BluetoothAudioSink::Observer* observer) override; | |
51 void RemoveObserver(BluetoothAudioSink::Observer* observer) override; | |
52 device::BluetoothAudioSink::State GetState() const override; | |
53 uint16_t GetVolume() const override; | |
54 | |
55 // Registers a BluetoothAudioSink. User applications can use |options| to | |
56 // configure the audio sink. |callback| will be executed if the audio sink is | |
57 // successfully registered, otherwise |error_callback| will be called. Called | |
58 // by BluetoothAdapterBlueZ. | |
59 void Register( | |
60 const device::BluetoothAudioSink::Options& options, | |
61 const base::Closure& callback, | |
62 const device::BluetoothAudioSink::ErrorCallback& error_callback); | |
63 | |
64 // Returns a pointer to the media endpoint object. This function should be | |
65 // used for testing purpose only. | |
66 bluez::BluetoothMediaEndpointServiceProvider* GetEndpointServiceProvider(); | |
67 | |
68 private: | |
69 ~BluetoothAudioSinkBlueZ() override; | |
70 | |
71 // device::BluetoothAdapter::Observer overrides. | |
72 void AdapterPresentChanged(device::BluetoothAdapter* adapter, | |
73 bool present) override; | |
74 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | |
75 bool powered) override; | |
76 | |
77 // bluez::BluetoothMediaClient::Observer overrides. | |
78 void MediaRemoved(const dbus::ObjectPath& object_path) override; | |
79 | |
80 // bluez::BluetoothMediaTransportClient::Observer overrides. | |
81 void MediaTransportRemoved(const dbus::ObjectPath& object_path) override; | |
82 void MediaTransportPropertyChanged(const dbus::ObjectPath& object_path, | |
83 const std::string& property_name) override; | |
84 | |
85 // bluez::BluetoothMediaEndpointServiceProvider::Delegate overrides. | |
86 void SetConfiguration(const dbus::ObjectPath& transport_path, | |
87 const TransportProperties& properties) override; | |
88 void SelectConfiguration( | |
89 const std::vector<uint8_t>& capabilities, | |
90 const SelectConfigurationCallback& callback) override; | |
91 void ClearConfiguration(const dbus::ObjectPath& transport_path) override; | |
92 void Released() override; | |
93 | |
94 // base::MessageLoopForIO::Watcher overrides. | |
95 void OnFileCanReadWithoutBlocking(int fd) override; | |
96 void OnFileCanWriteWithoutBlocking(int fd) override; | |
97 | |
98 // Acquires file descriptor via current transport object when the state change | |
99 // is triggered by MediaTransportPropertyChanged. | |
100 void AcquireFD(); | |
101 | |
102 // Watches if there is any available data from |fd_|. | |
103 void WatchFD(); | |
104 | |
105 // Stops watching |fd_| and resets |fd_|. | |
106 void StopWatchingFD(); | |
107 | |
108 // Reads from the file descriptor acquired via Media Transport object and | |
109 // notify |observer_| while the audio data is available. | |
110 void ReadFromFile(); | |
111 | |
112 // Called when the state property of BluetoothMediaTransport has been updated. | |
113 void StateChanged(device::BluetoothAudioSink::State state); | |
114 | |
115 // Called when the volume property of BluetoothMediaTransport has been | |
116 // updated. | |
117 void VolumeChanged(uint16_t volume); | |
118 | |
119 // Called when the registration of Media Endpoint has succeeded. | |
120 void OnRegisterSucceeded(const base::Closure& callback); | |
121 | |
122 // Called when the registration of Media Endpoint failed. | |
123 void OnRegisterFailed( | |
124 const device::BluetoothAudioSink::ErrorCallback& error_callback, | |
125 const std::string& error_name, | |
126 const std::string& error_message); | |
127 | |
128 // Called when the unregistration of Media Endpoint has succeeded. The | |
129 // clean-up of media, media transport and media endpoint will be handled here. | |
130 void OnUnregisterSucceeded(const base::Closure& callback); | |
131 | |
132 // Called when the unregistration of Media Endpoint failed. | |
133 void OnUnregisterFailed( | |
134 const device::BluetoothAudioSink::ErrorCallback& error_callback, | |
135 const std::string& error_name, | |
136 const std::string& error_message); | |
137 | |
138 // Called when the file descriptor, read MTU and write MTU are retrieved | |
139 // successfully using |transport_path_|. | |
140 void OnAcquireSucceeded(dbus::FileDescriptor* fd, | |
141 const uint16_t read_mtu, | |
142 const uint16_t write_mtu); | |
143 | |
144 // Called when acquiring the file descriptor, read MTU and write MTU failed. | |
145 void OnAcquireFailed(const std::string& error_name, | |
146 const std::string& error_message); | |
147 | |
148 // Called when the file descriptor is released successfully. | |
149 void OnReleaseFDSucceeded(); | |
150 | |
151 // Called when it failed to release file descriptor. | |
152 void OnReleaseFDFailed(const std::string& error_name, | |
153 const std::string& error_message); | |
154 | |
155 // Helper functions to clean up media, media transport and media endpoint. | |
156 // Called when the |state_| changes to either STATE_INVALID or | |
157 // STATE_DISCONNECTED. | |
158 void ResetMedia(); | |
159 void ResetTransport(); | |
160 void ResetEndpoint(); | |
161 | |
162 // The connection state between the BluetoothAudioSinkBlueZ and the remote | |
163 // device. | |
164 device::BluetoothAudioSink::State state_; | |
165 | |
166 // The volume control by the remote device during the streaming. The valid | |
167 // range of volume is 0-127, and 128 is used to represent invalid volume. | |
168 uint16_t volume_; | |
169 | |
170 // Read MTU of the file descriptor acquired via Media Transport object. | |
171 uint16_t read_mtu_; | |
172 | |
173 // Write MTU of the file descriptor acquired via Media Transport object. | |
174 uint16_t write_mtu_; | |
175 | |
176 // Flag for logging the read failure in ReadFromFD. | |
177 bool read_has_failed_; | |
178 | |
179 // The file which takes ownership of the file descriptor acquired via Media | |
180 // Transport object. | |
181 std::unique_ptr<base::File> file_; | |
182 | |
183 // To avoid reallocation of memory, data will be updated only when |read_mtu_| | |
184 // changes. | |
185 std::unique_ptr<char[]> data_; | |
186 | |
187 // File descriptor watcher for the file descriptor acquired via Media | |
188 // Transport object. | |
189 base::MessageLoopForIO::FileDescriptorWatcher fd_read_watcher_; | |
190 | |
191 // Object path of the media object being used. | |
192 dbus::ObjectPath media_path_; | |
193 | |
194 // Object path of the transport object being used. | |
195 dbus::ObjectPath transport_path_; | |
196 | |
197 // Object path of the media endpoint object being used. | |
198 dbus::ObjectPath endpoint_path_; | |
199 | |
200 // BT adapter which the audio sink binds to. |adapter_| should outlive | |
201 // a BluetoothAudioSinkBlueZ object. | |
202 scoped_refptr<device::BluetoothAdapter> adapter_; | |
203 | |
204 // Options used to initiate Media Endpoint and select configuration for the | |
205 // transport. | |
206 device::BluetoothAudioSink::Options options_; | |
207 | |
208 // Media Endpoint object owned by the audio sink object. | |
209 std::unique_ptr<bluez::BluetoothMediaEndpointServiceProvider> media_endpoint_; | |
210 | |
211 // List of observers interested in event notifications from us. Objects in | |
212 // |observers_| are expected to outlive a BluetoothAudioSinkBlueZ object. | |
213 base::ObserverList<BluetoothAudioSink::Observer> observers_; | |
214 | |
215 // Note: This should remain the last member so it'll be destroyed and | |
216 // invalidate its weak pointers before any other members are destroyed. | |
217 base::WeakPtrFactory<BluetoothAudioSinkBlueZ> weak_ptr_factory_; | |
218 | |
219 DISALLOW_COPY_AND_ASSIGN(BluetoothAudioSinkBlueZ); | |
220 }; | |
221 | |
222 } // namespace bluez | |
223 | |
224 #endif // DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_BLUEZ_H_ | |
OLD | NEW |