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