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.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_
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 "chromeos/dbus/bluetooth_media_client.h"
18 #include "chromeos/dbus/bluetooth_media_endpoint_service_provider.h"
19 #include "chromeos/dbus/bluetooth_media_transport_client.h"
20 #include "dbus/file_descriptor.h"
21 #include "dbus/object_path.h"
22 #include "device/bluetooth/bluetooth_adapter.h"
23 #include "device/bluetooth/bluetooth_audio_sink.h"
24 #include "device/bluetooth/bluetooth_export.h"
28 class BluetoothAudioSinkChromeOSTest
;
30 class DEVICE_BLUETOOTH_EXPORT BluetoothAudioSinkChromeOS
31 : public device::BluetoothAudioSink
,
32 public device::BluetoothAdapter::Observer
,
33 public BluetoothMediaClient::Observer
,
34 public BluetoothMediaTransportClient::Observer
,
35 public BluetoothMediaEndpointServiceProvider::Delegate
,
36 public base::MessageLoopForIO::Watcher
{
38 explicit BluetoothAudioSinkChromeOS(
39 scoped_refptr
<device::BluetoothAdapter
> adapter
);
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.
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
;
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.
58 const device::BluetoothAudioSink::Options
& options
,
59 const base::Closure
& callback
,
60 const device::BluetoothAudioSink::ErrorCallback
& error_callback
);
62 // Returns a pointer to the media endpoint object. This function should be
63 // used for testing purpose only.
64 BluetoothMediaEndpointServiceProvider
* GetEndpointServiceProvider();
67 ~BluetoothAudioSinkChromeOS() override
;
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
;
75 // BluetoothMediaClient::Observer overrides.
76 void MediaRemoved(const dbus::ObjectPath
& object_path
) override
;
78 // 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
;
83 // 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
;
92 // base::MessageLoopForIO::Watcher overrides.
93 void OnFileCanReadWithoutBlocking(int fd
) override
;
94 void OnFileCanWriteWithoutBlocking(int fd
) override
;
96 // Acquires file descriptor via current transport object when the state change
97 // is triggered by MediaTransportPropertyChanged.
100 // Watches if there is any available data from |fd_|.
103 // Stops watching |fd_| and resets |fd_|.
104 void StopWatchingFD();
106 // Reads from the file descriptor acquired via Media Transport object and
107 // notify |observer_| while the audio data is available.
110 // Called when the state property of BluetoothMediaTransport has been updated.
111 void StateChanged(device::BluetoothAudioSink::State state
);
113 // Called when the volume property of BluetoothMediaTransport has been
115 void VolumeChanged(uint16_t volume
);
117 // Called when the registration of Media Endpoint has succeeded.
118 void OnRegisterSucceeded(const base::Closure
& callback
);
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
);
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
);
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
);
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
);
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
);
146 // Called when the file descriptor is released successfully.
147 void OnReleaseFDSucceeded();
149 // Called when it failed to release file descriptor.
150 void OnReleaseFDFailed(const std::string
& error_name
,
151 const std::string
& error_message
);
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.
157 void ResetTransport();
158 void ResetEndpoint();
160 // The connection state between the BluetoothAudioSinkChromeOS and the remote
162 device::BluetoothAudioSink::State state_
;
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.
168 // Read MTU of the file descriptor acquired via Media Transport object.
171 // Write MTU of the file descriptor acquired via Media Transport object.
174 // Flag for logging the read failure in ReadFromFD.
175 bool read_has_failed_
;
177 // The file which takes ownership of the file descriptor acquired via Media
179 scoped_ptr
<base::File
> file_
;
181 // To avoid reallocation of memory, data will be updated only when |read_mtu_|
183 scoped_ptr
<char[]> data_
;
185 // File descriptor watcher for the file descriptor acquired via Media
187 base::MessageLoopForIO::FileDescriptorWatcher fd_read_watcher_
;
189 // Object path of the media object being used.
190 dbus::ObjectPath media_path_
;
192 // Object path of the transport object being used.
193 dbus::ObjectPath transport_path_
;
195 // Object path of the media endpoint object being used.
196 dbus::ObjectPath endpoint_path_
;
198 // BT adapter which the audio sink binds to. |adapter_| should outlive
199 // a BluetoothAudioSinkChromeOS object.
200 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
202 // Options used to initiate Media Endpoint and select configuration for the
204 device::BluetoothAudioSink::Options options_
;
206 // Media Endpoint object owned by the audio sink object.
207 scoped_ptr
<BluetoothMediaEndpointServiceProvider
> media_endpoint_
;
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_
;
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_
;
217 DISALLOW_COPY_AND_ASSIGN(BluetoothAudioSinkChromeOS
);
220 } // namespace chromeos
222 #endif // DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_