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/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "chromeos/dbus/bluetooth_media_client.h"
16 #include "chromeos/dbus/bluetooth_media_endpoint_service_provider.h"
17 #include "chromeos/dbus/bluetooth_media_transport_client.h"
18 #include "dbus/file_descriptor.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"
26 class BluetoothAudioSinkChromeOSTest
;
28 class DEVICE_BLUETOOTH_EXPORT BluetoothAudioSinkChromeOS
29 : public device::BluetoothAudioSink
,
30 public device::BluetoothAdapter::Observer
,
31 public BluetoothMediaClient::Observer
,
32 public BluetoothMediaTransportClient::Observer
,
33 public BluetoothMediaEndpointServiceProvider::Delegate
{
35 explicit BluetoothAudioSinkChromeOS(
36 scoped_refptr
<device::BluetoothAdapter
> adapter
);
38 // device::BluetoothAudioSink overrides.
39 // Unregisters a BluetoothAudioSink. |callback| should handle
40 // the clean-up after the audio sink is deleted successfully, otherwise
41 // |error_callback| will be called.
43 const base::Closure
& callback
,
44 const device::BluetoothAudioSink::ErrorCallback
& error_callback
) override
;
45 void AddObserver(BluetoothAudioSink::Observer
* observer
) override
;
46 void RemoveObserver(BluetoothAudioSink::Observer
* observer
) override
;
47 device::BluetoothAudioSink::State
GetState() const override
;
48 uint16_t GetVolume() const override
;
50 // device::BluetoothAdapter::Observer overrides.
51 void AdapterPresentChanged(device::BluetoothAdapter
* adapter
,
52 bool present
) override
;
54 // BluetoothMediaClient::Observer overrides.
55 void MediaRemoved(const dbus::ObjectPath
& object_path
) override
;
57 // BluetoothMediaTransportClient::Observer overrides.
58 void MediaTransportRemoved(const dbus::ObjectPath
& object_path
) override
;
59 void MediaTransportPropertyChanged(const dbus::ObjectPath
& object_path
,
60 const std::string
& property_name
) override
;
62 // BluetoothMediaEndpointServiceProvider::Delegate overrides.
63 void SetConfiguration(const dbus::ObjectPath
& transport_path
,
64 const TransportProperties
& properties
) override
;
65 void SelectConfiguration(
66 const std::vector
<uint8_t>& capabilities
,
67 const SelectConfigurationCallback
& callback
) override
;
68 void ClearConfiguration(const dbus::ObjectPath
& transport_path
) override
;
69 void Released() override
;
71 // Registers a BluetoothAudioSink. User applications can use |options| to
72 // configure the audio sink. |callback| will be executed if the audio sink is
73 // successfully registered, otherwise |error_callback| will be called. Called
74 // by BluetoothAdapterChromeOS.
76 const device::BluetoothAudioSink::Options
& options
,
77 const base::Closure
& callback
,
78 const device::BluetoothAudioSink::ErrorCallback
& error_callback
);
80 // Returns a pointer to the media endpoint object. This function should be
81 // used for testing purpose only.
82 BluetoothMediaEndpointServiceProvider
* GetEndpointServiceProvider();
85 ~BluetoothAudioSinkChromeOS() override
;
87 // Called when the state property of BluetoothMediaTransport has been updated.
88 void StateChanged(device::BluetoothAudioSink::State state
);
90 // Called when the volume property of BluetoothMediaTransport has been
92 void VolumeChanged(uint16_t volume
);
94 // Called when the registration of Media Endpoint has succeeded.
95 void OnRegisterSucceeded(const base::Closure
& callback
);
97 // Called when the registration of Media Endpoint failed.
98 void OnRegisterFailed(const BluetoothAudioSink::ErrorCallback
& error_callback
,
99 const std::string
& error_name
,
100 const std::string
& error_message
);
102 // Reads from the file descriptor acquired via Media Transport object and
103 // notify |observer_| while the audio data is available.
106 // Helper functions to clean up media, media transport and media endpoint.
107 // Called when the |state_| changes to either STATE_INVALID or
108 // STATE_DISCONNECTED.
110 void ResetTransport();
111 void ResetEndpoint();
113 // The connection state between the BluetoothAudioSinkChromeOS and the remote
115 device::BluetoothAudioSink::State state_
;
117 // The volume control by the remote device during the streaming.
120 // Read MTU of the file descriptor acquired via Media Transport object.
123 // Write MTU of the file descriptor acquired via Media Transport object.
126 // File descriptor acquired via Media Transport object.
127 dbus::FileDescriptor fd_
;
129 // Object path of the media object being used.
130 dbus::ObjectPath media_path_
;
132 // Object path of the transport object being used.
133 dbus::ObjectPath transport_path_
;
135 // Object path of the media endpoint object being used.
136 dbus::ObjectPath endpoint_path_
;
138 // BT adapter which the audio sink binds to. |adapter_| should outlive
139 // a BluetoothAudioSinkChromeOS object.
140 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
142 // Options used to initiate Media Endpoint and select configuration for the
144 device::BluetoothAudioSink::Options options_
;
146 // Media Endpoint object owned by the audio sink object.
147 scoped_ptr
<BluetoothMediaEndpointServiceProvider
> media_endpoint_
;
149 // List of observers interested in event notifications from us. Objects in
150 // |observers_| are expected to outlive a BluetoothAudioSinkChromeOS object.
151 ObserverList
<BluetoothAudioSink::Observer
> observers_
;
153 // Note: This should remain the last member so it'll be destroyed and
154 // invalidate its weak pointers before any other members are destroyed.
155 base::WeakPtrFactory
<BluetoothAudioSinkChromeOS
> weak_ptr_factory_
;
157 DISALLOW_COPY_AND_ASSIGN(BluetoothAudioSinkChromeOS
);
160 } // namespace chromeos
162 #endif // DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_