OLD | NEW |
---|---|
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, |
(...skipping 28 matching lines...) Expand all Loading... | |
72 | 75 |
73 // Registers a BluetoothAudioSink. User applications can use |options| to | 76 // Registers a BluetoothAudioSink. User applications can use |options| to |
74 // configure the audio sink. |callback| will be executed if the audio sink is | 77 // configure the audio sink. |callback| will be executed if the audio sink is |
75 // successfully registered, otherwise |error_callback| will be called. Called | 78 // successfully registered, otherwise |error_callback| will be called. Called |
76 // by BluetoothAdapterChromeOS. | 79 // by BluetoothAdapterChromeOS. |
77 void Register( | 80 void Register( |
78 const device::BluetoothAudioSink::Options& options, | 81 const device::BluetoothAudioSink::Options& options, |
79 const base::Closure& callback, | 82 const base::Closure& callback, |
80 const device::BluetoothAudioSink::ErrorCallback& error_callback); | 83 const device::BluetoothAudioSink::ErrorCallback& error_callback); |
81 | 84 |
85 // base::MessageLoopForIO::Watcher overrides. | |
86 void OnFileCanReadWithoutBlocking(int fd) override; | |
87 void OnFileCanWriteWithoutBlocking(int fd) override; | |
armansito
2015/03/12 03:42:55
These can be private.
Miao
2015/03/12 22:33:32
Done.
| |
88 | |
82 // Returns a pointer to the media endpoint object. This function should be | 89 // Returns a pointer to the media endpoint object. This function should be |
83 // used for testing purpose only. | 90 // used for testing purpose only. |
84 BluetoothMediaEndpointServiceProvider* GetEndpointServiceProvider(); | 91 BluetoothMediaEndpointServiceProvider* GetEndpointServiceProvider(); |
85 | 92 |
86 private: | 93 private: |
87 ~BluetoothAudioSinkChromeOS() override; | 94 ~BluetoothAudioSinkChromeOS() override; |
88 | 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 | |
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(const int 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_failed_logged_; |
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 Loading... | |
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_ |
OLD | NEW |