Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: device/bluetooth/bluetooth_audio_sink_chromeos.h

Issue 993273002: device/bluetooth: Add I/O watcher for audio data retrieval triggered by state change. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_ 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_ 6 #define DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/files/file.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
14 #include "base/observer_list.h" 16 #include "base/observer_list.h"
15 #include "chromeos/dbus/bluetooth_media_client.h" 17 #include "chromeos/dbus/bluetooth_media_client.h"
16 #include "chromeos/dbus/bluetooth_media_endpoint_service_provider.h" 18 #include "chromeos/dbus/bluetooth_media_endpoint_service_provider.h"
17 #include "chromeos/dbus/bluetooth_media_transport_client.h" 19 #include "chromeos/dbus/bluetooth_media_transport_client.h"
18 #include "dbus/file_descriptor.h" 20 #include "dbus/file_descriptor.h"
19 #include "dbus/object_path.h" 21 #include "dbus/object_path.h"
20 #include "device/bluetooth/bluetooth_adapter.h" 22 #include "device/bluetooth/bluetooth_adapter.h"
21 #include "device/bluetooth/bluetooth_audio_sink.h" 23 #include "device/bluetooth/bluetooth_audio_sink.h"
22 #include "device/bluetooth/bluetooth_export.h" 24 #include "device/bluetooth/bluetooth_export.h"
23 25
24 namespace chromeos { 26 namespace chromeos {
25 27
26 class BluetoothAudioSinkChromeOSTest; 28 class BluetoothAudioSinkChromeOSTest;
27 29
28 class DEVICE_BLUETOOTH_EXPORT BluetoothAudioSinkChromeOS 30 class DEVICE_BLUETOOTH_EXPORT BluetoothAudioSinkChromeOS
29 : public device::BluetoothAudioSink, 31 : public device::BluetoothAudioSink,
30 public device::BluetoothAdapter::Observer, 32 public device::BluetoothAdapter::Observer,
31 public BluetoothMediaClient::Observer, 33 public BluetoothMediaClient::Observer,
32 public BluetoothMediaTransportClient::Observer, 34 public BluetoothMediaTransportClient::Observer,
33 public BluetoothMediaEndpointServiceProvider::Delegate { 35 public BluetoothMediaEndpointServiceProvider::Delegate,
36 public base::MessageLoopForIO::Watcher {
34 public: 37 public:
35 explicit BluetoothAudioSinkChromeOS( 38 explicit BluetoothAudioSinkChromeOS(
36 scoped_refptr<device::BluetoothAdapter> adapter); 39 scoped_refptr<device::BluetoothAdapter> adapter);
37 40
38 // device::BluetoothAudioSink overrides. 41 // device::BluetoothAudioSink overrides.
39 // Unregisters a BluetoothAudioSink. |callback| should handle 42 // Unregisters a BluetoothAudioSink. |callback| should handle
40 // the clean-up after the audio sink is deleted successfully, otherwise 43 // the clean-up after the audio sink is deleted successfully, otherwise
41 // |error_callback| will be called. 44 // |error_callback| will be called.
42 void Unregister( 45 void Unregister(
43 const base::Closure& callback, 46 const base::Closure& callback,
44 const device::BluetoothAudioSink::ErrorCallback& error_callback) override; 47 const device::BluetoothAudioSink::ErrorCallback& error_callback) override;
45 void AddObserver(BluetoothAudioSink::Observer* observer) override; 48 void AddObserver(BluetoothAudioSink::Observer* observer) override;
46 void RemoveObserver(BluetoothAudioSink::Observer* observer) override; 49 void RemoveObserver(BluetoothAudioSink::Observer* observer) override;
47 device::BluetoothAudioSink::State GetState() const override; 50 device::BluetoothAudioSink::State GetState() const override;
48 uint16_t GetVolume() const override; 51 uint16_t GetVolume() const override;
49 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 BluetoothMediaEndpointServiceProvider* GetEndpointServiceProvider();
65
66 private:
67 ~BluetoothAudioSinkChromeOS() override;
68
50 // device::BluetoothAdapter::Observer overrides. 69 // device::BluetoothAdapter::Observer overrides.
51 void AdapterPresentChanged(device::BluetoothAdapter* adapter, 70 void AdapterPresentChanged(device::BluetoothAdapter* adapter,
52 bool present) override; 71 bool present) override;
53 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, 72 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
54 bool powered) override; 73 bool powered) override;
55 74
56 // BluetoothMediaClient::Observer overrides. 75 // BluetoothMediaClient::Observer overrides.
57 void MediaRemoved(const dbus::ObjectPath& object_path) override; 76 void MediaRemoved(const dbus::ObjectPath& object_path) override;
58 77
59 // BluetoothMediaTransportClient::Observer overrides. 78 // BluetoothMediaTransportClient::Observer overrides.
60 void MediaTransportRemoved(const dbus::ObjectPath& object_path) override; 79 void MediaTransportRemoved(const dbus::ObjectPath& object_path) override;
61 void MediaTransportPropertyChanged(const dbus::ObjectPath& object_path, 80 void MediaTransportPropertyChanged(const dbus::ObjectPath& object_path,
62 const std::string& property_name) override; 81 const std::string& property_name) override;
63 82
64 // BluetoothMediaEndpointServiceProvider::Delegate overrides. 83 // BluetoothMediaEndpointServiceProvider::Delegate overrides.
65 void SetConfiguration(const dbus::ObjectPath& transport_path, 84 void SetConfiguration(const dbus::ObjectPath& transport_path,
66 const TransportProperties& properties) override; 85 const TransportProperties& properties) override;
67 void SelectConfiguration( 86 void SelectConfiguration(
68 const std::vector<uint8_t>& capabilities, 87 const std::vector<uint8_t>& capabilities,
69 const SelectConfigurationCallback& callback) override; 88 const SelectConfigurationCallback& callback) override;
70 void ClearConfiguration(const dbus::ObjectPath& transport_path) override; 89 void ClearConfiguration(const dbus::ObjectPath& transport_path) override;
71 void Released() override; 90 void Released() override;
72 91
73 // Registers a BluetoothAudioSink. User applications can use |options| to 92 // base::MessageLoopForIO::Watcher overrides.
74 // configure the audio sink. |callback| will be executed if the audio sink is 93 void OnFileCanReadWithoutBlocking(int fd) override;
75 // successfully registered, otherwise |error_callback| will be called. Called 94 void OnFileCanWriteWithoutBlocking(int fd) override;
76 // by BluetoothAdapterChromeOS.
77 void Register(
78 const device::BluetoothAudioSink::Options& options,
79 const base::Closure& callback,
80 const device::BluetoothAudioSink::ErrorCallback& error_callback);
81 95
82 // Returns a pointer to the media endpoint object. This function should be 96 // Acquires file descriptor via current transport object when the state change
83 // used for testing purpose only. 97 // is triggered by MediaTransportPropertyChanged.
84 BluetoothMediaEndpointServiceProvider* GetEndpointServiceProvider(); 98 void AcquireFD();
85 99
86 private: 100 // Watches if there is any available data from |fd_|.
87 ~BluetoothAudioSinkChromeOS() override; 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();
88 109
89 // Called when the state property of BluetoothMediaTransport has been updated. 110 // Called when the state property of BluetoothMediaTransport has been updated.
90 void StateChanged(device::BluetoothAudioSink::State state); 111 void StateChanged(device::BluetoothAudioSink::State state);
91 112
92 // Called when the volume property of BluetoothMediaTransport has been 113 // Called when the volume property of BluetoothMediaTransport has been
93 // updated. 114 // updated.
94 void VolumeChanged(uint16_t volume); 115 void VolumeChanged(uint16_t volume);
95 116
96 // Called when the registration of Media Endpoint has succeeded. 117 // Called when the registration of Media Endpoint has succeeded.
97 void OnRegisterSucceeded(const base::Closure& callback); 118 void OnRegisterSucceeded(const base::Closure& callback);
98 119
99 // Called when the registration of Media Endpoint failed. 120 // Called when the registration of Media Endpoint failed.
100 void OnRegisterFailed( 121 void OnRegisterFailed(
101 const device::BluetoothAudioSink::ErrorCallback& error_callback, 122 const device::BluetoothAudioSink::ErrorCallback& error_callback,
102 const std::string& error_name, 123 const std::string& error_name,
103 const std::string& error_message); 124 const std::string& error_message);
104 125
105 // Called when the unregistration of Media Endpoint has succeeded. The 126 // Called when the unregistration of Media Endpoint has succeeded. The
106 // clean-up of media, media transport and media endpoint will be handled here. 127 // clean-up of media, media transport and media endpoint will be handled here.
107 void OnUnregisterSucceeded(const base::Closure& callback); 128 void OnUnregisterSucceeded(const base::Closure& callback);
108 129
109 // Called when the unregistration of Media Endpoint failed. 130 // Called when the unregistration of Media Endpoint failed.
110 void OnUnregisterFailed( 131 void OnUnregisterFailed(
111 const device::BluetoothAudioSink::ErrorCallback& error_callback, 132 const device::BluetoothAudioSink::ErrorCallback& error_callback,
112 const std::string& error_name, 133 const std::string& error_name,
113 const std::string& error_message); 134 const std::string& error_message);
114 135
115 // Reads from the file descriptor acquired via Media Transport object and 136 // Called when the file descriptor, read MTU and write MTU are retrieved
116 // notify |observer_| while the audio data is available. 137 // successfully using |transport_path_|.
117 void ReadFromFD(); 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);
118 152
119 // Helper functions to clean up media, media transport and media endpoint. 153 // Helper functions to clean up media, media transport and media endpoint.
120 // Called when the |state_| changes to either STATE_INVALID or 154 // Called when the |state_| changes to either STATE_INVALID or
121 // STATE_DISCONNECTED. 155 // STATE_DISCONNECTED.
122 void ResetMedia(); 156 void ResetMedia();
123 void ResetTransport(); 157 void ResetTransport();
124 void ResetEndpoint(); 158 void ResetEndpoint();
125 159
126 // The connection state between the BluetoothAudioSinkChromeOS and the remote 160 // The connection state between the BluetoothAudioSinkChromeOS and the remote
127 // device. 161 // device.
128 device::BluetoothAudioSink::State state_; 162 device::BluetoothAudioSink::State state_;
129 163
130 // The volume control by the remote device during the streaming. The valid 164 // The volume control by the remote device during the streaming. The valid
131 // range of volume is 0-127, and 128 is used to represent invalid volume. 165 // range of volume is 0-127, and 128 is used to represent invalid volume.
132 uint16_t volume_; 166 uint16_t volume_;
133 167
134 // Read MTU of the file descriptor acquired via Media Transport object. 168 // Read MTU of the file descriptor acquired via Media Transport object.
135 scoped_ptr<uint16_t> read_mtu_; 169 uint16_t read_mtu_;
136 170
137 // Write MTU of the file descriptor acquired via Media Transport object. 171 // Write MTU of the file descriptor acquired via Media Transport object.
138 scoped_ptr<uint16_t> write_mtu_; 172 uint16_t write_mtu_;
139 173
140 // File descriptor acquired via Media Transport object. 174 // Flag for logging the read failure in ReadFromFD.
141 dbus::FileDescriptor fd_; 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_;
142 188
143 // Object path of the media object being used. 189 // Object path of the media object being used.
144 dbus::ObjectPath media_path_; 190 dbus::ObjectPath media_path_;
145 191
146 // Object path of the transport object being used. 192 // Object path of the transport object being used.
147 dbus::ObjectPath transport_path_; 193 dbus::ObjectPath transport_path_;
148 194
149 // Object path of the media endpoint object being used. 195 // Object path of the media endpoint object being used.
150 dbus::ObjectPath endpoint_path_; 196 dbus::ObjectPath endpoint_path_;
151 197
(...skipping 15 matching lines...) Expand all
167 // Note: This should remain the last member so it'll be destroyed and 213 // Note: This should remain the last member so it'll be destroyed and
168 // invalidate its weak pointers before any other members are destroyed. 214 // invalidate its weak pointers before any other members are destroyed.
169 base::WeakPtrFactory<BluetoothAudioSinkChromeOS> weak_ptr_factory_; 215 base::WeakPtrFactory<BluetoothAudioSinkChromeOS> weak_ptr_factory_;
170 216
171 DISALLOW_COPY_AND_ASSIGN(BluetoothAudioSinkChromeOS); 217 DISALLOW_COPY_AND_ASSIGN(BluetoothAudioSinkChromeOS);
172 }; 218 };
173 219
174 } // namespace chromeos 220 } // namespace chromeos
175 221
176 #endif // DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_ 222 #endif // DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_CHROMEOS_H_
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_audio_sink.h ('k') | device/bluetooth/bluetooth_audio_sink_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698