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