OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_H_ | |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/callback.h" | |
16 #include "base/macros.h" | |
17 #include "base/memory/ref_counted.h" | |
18 #include "device/bluetooth/bluetooth_export.h" | |
19 | |
20 namespace device { | |
21 | |
22 // TODO(mcchou): Define a BluetoothAudioSink specific IOBuffer abstraction. | |
23 | |
24 // BluetoothAudioSink represents a local A2DP audio sink where a remote device | |
25 // can stream audio data. Once a BluetoothAudioSink is successfully registered, | |
26 // user applications can obtain a pointer to a BluetoothAudioSink object via | |
27 // the interface provided by BluetoothAdapter. The validity of a | |
28 // BluetoothAudioSink depends on whether BluetoothAdapter is present and whether | |
29 // it is powered. | |
30 class DEVICE_BLUETOOTH_EXPORT BluetoothAudioSink | |
31 : public base::RefCounted<BluetoothAudioSink> { | |
32 public: | |
33 // Possible values indicating the connection states between the | |
34 // BluetoothAudioSink and the remote device. | |
35 enum State { | |
36 STATE_INVALID, // BluetoothAdapter not presented or not powered. | |
37 STATE_DISCONNECTED, // Not connected. | |
38 STATE_IDLE, // Connected but not streaming. | |
39 STATE_PENDING, // Connected, streaming but not acquired. | |
40 STATE_ACTIVE, // Connected, streaming and acquired. | |
41 }; | |
42 | |
43 // Possible types of error raised by Audio Sink object. | |
44 enum ErrorCode { | |
45 ERROR_UNSUPPORTED_PLATFORM, // A2DP sink not supported on current platform. | |
46 ERROR_INVALID_ADAPTER, // BluetoothAdapter not present/powered. | |
47 ERROR_NOT_REGISTERED, // BluetoothAudioSink not registered. | |
48 ERROR_NOT_UNREGISTERED, // BluetoothAudioSink not unregistered. | |
49 }; | |
50 | |
51 // Options to configure an A2DP audio sink. | |
52 struct Options { | |
53 Options(); | |
54 ~Options(); | |
55 | |
56 uint8_t codec; | |
57 std::vector<uint8_t> capabilities; | |
58 }; | |
59 | |
60 // Interface for observing changes from a BluetoothAudioSink. | |
61 class Observer { | |
62 public: | |
63 virtual ~Observer() {} | |
64 | |
65 // Called when the state of the BluetoothAudioSink object is changed. | |
66 // |audio_sink| indicates the object being changed, and |state| indicates | |
67 // the new state of that object. | |
68 virtual void BluetoothAudioSinkStateChanged( | |
69 BluetoothAudioSink* audio_sink, | |
70 BluetoothAudioSink::State state) = 0; | |
71 | |
72 // Called when the volume of the BluetoothAudioSink object is changed. | |
73 // |audio_sink| indicates the object being changed, and |volume| indicates | |
74 // the new volume level of that object. | |
75 virtual void BluetoothAudioSinkVolumeChanged( | |
76 BluetoothAudioSink* audio_sink, | |
77 uint16_t volume) = 0; | |
78 | |
79 // Called when there is audio data available. |audio_sink| indicates the | |
80 // object being changed. |data| is the pointer to the audio data and |size| | |
81 // is the number of bytes in |data|. |read_mtu| is the max size of the RTP | |
82 // packet. This method provides the raw audio data which hasn't been | |
83 // processed, so RTP assembling and SBC decoding need to be handled in order | |
84 // to get PCM data. | |
85 virtual void BluetoothAudioSinkDataAvailable(BluetoothAudioSink* audio_sink, | |
86 char* data, | |
87 size_t size, | |
88 uint16_t read_mtu) = 0; | |
89 }; | |
90 | |
91 // The ErrorCallback is used for the methods that can fail in which case it | |
92 // is called. | |
93 typedef base::Callback<void(ErrorCode)> ErrorCallback; | |
94 | |
95 // Possible volumes for media transport are 0-127, and 128 is used to | |
96 // represent invalid volume. | |
97 static const uint16_t kInvalidVolume; | |
98 | |
99 // Unregisters the audio sink. An audio sink will unregister itself | |
100 // automatically in its destructor, but calling Unregister is recommended, | |
101 // since user applications can be notified of an error returned by the call. | |
102 virtual void Unregister(const base::Closure& callback, | |
103 const ErrorCallback& error_callback) = 0; | |
104 | |
105 // Adds and removes an observer for events on the BluetoothAudioSink object. | |
106 // If monitoring multiple audio sinks, check the |audio_sink| parameter of | |
107 // observer methods to determine which audio sink is issuing the event. | |
108 virtual void AddObserver(Observer* observer) = 0; | |
109 virtual void RemoveObserver(Observer* observer) = 0; | |
110 | |
111 // Getters for state and volume. | |
112 virtual State GetState() const = 0; | |
113 | |
114 // Returns the current volume level of the audio sink. The valid volumes are | |
115 // 0-127, and |kInvalidVolume| is returned instead if the volume is unknown. | |
116 virtual uint16_t GetVolume() const = 0; | |
117 | |
118 protected: | |
119 friend class base::RefCounted<BluetoothAudioSink>; | |
120 BluetoothAudioSink(); | |
121 | |
122 // The destructor invokes Unregister() to ensure the audio sink will be | |
123 // unregistered even if the user applications fail to do so. | |
124 virtual ~BluetoothAudioSink(); | |
125 | |
126 private: | |
127 DISALLOW_COPY_AND_ASSIGN(BluetoothAudioSink); | |
128 }; | |
129 | |
130 } // namespace device | |
131 | |
132 #endif // DEVICE_BLUETOOTH_BLUETOOTH_AUDIO_SINK_H_ | |
OLD | NEW |