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