aboutsummaryrefslogtreecommitdiff
path: root/service/a2dp_sink.cc
blob: 4d59c7c31bc9f76f9a9d24d89ff2084163389b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
//  Copyright 2017 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#include "service/a2dp_sink.h"

#include <base/logging.h>
#include <base/memory/ptr_util.h>

#include "service/logging_helpers.h"

using bluetooth::hal::BluetoothAvInterface;

namespace bluetooth {

// static
const int A2dpSink::kSingletonInstanceId = 0;

A2dpSink::A2dpSink(const Uuid& uuid) : app_identifier_(uuid) {
  hal::BluetoothAvInterface::Get()->AddA2dpSinkObserver(this);
}

A2dpSink::~A2dpSink() {
  hal::BluetoothAvInterface::Get()->RemoveA2dpSinkObserver(this);
}

const Uuid& A2dpSink::GetAppIdentifier() const { return app_identifier_; }

int A2dpSink::GetInstanceId() const { return kSingletonInstanceId; }

void A2dpSink::SetDelegate(Delegate* delegate) {
  std::lock_guard<std::mutex> lock(delegate_mutex_);
  delegate_ = delegate;
}

bool A2dpSink::Enable() {
  std::lock_guard<std::mutex> lock(mutex_);
  return hal::BluetoothAvInterface::Get()->A2dpSinkEnable();
}

void A2dpSink::Disable() {
  std::lock_guard<std::mutex> lock(mutex_);
  hal::BluetoothAvInterface::Get()->A2dpSinkDisable();
}

bool A2dpSink::Connect(const std::string& device_address) {
  std::lock_guard<std::mutex> lock(mutex_);
  RawAddress addr;
  if (!RawAddress::FromString(device_address, addr)) {
    LOG(ERROR) << "Invalid device address given: " << device_address;
    return false;
  }

  bt_status_t status =
      hal::BluetoothAvInterface::Get()->GetA2dpSinkHALInterface()->connect(
          addr);
  if (status != BT_STATUS_SUCCESS) {
    LOG(ERROR) << "Failed to connect";
    return false;
  }

  return true;
}

bool A2dpSink::Disconnect(const std::string& device_address) {
  std::lock_guard<std::mutex> lock(mutex_);
  RawAddress addr;
  if (!RawAddress::FromString(device_address, addr)) {
    LOG(ERROR) << "Invalid device address given: " << device_address;
    return false;
  }

  bt_status_t status =
      hal::BluetoothAvInterface::Get()->GetA2dpSinkHALInterface()->disconnect(
          addr);
  if (status != BT_STATUS_SUCCESS) {
    LOG(ERROR) << "Failed to disconnect";
    return false;
  }

  return true;
}

void A2dpSink::SetAudioFocusState(int focus_state) {
  std::lock_guard<std::mutex> lock(mutex_);
  hal::BluetoothAvInterface::Get()
      ->GetA2dpSinkHALInterface()
      ->set_audio_focus_state(focus_state);
}

void A2dpSink::SetAudioTrackGain(float gain) {
  std::lock_guard<std::mutex> lock(mutex_);
  hal::BluetoothAvInterface::Get()
      ->GetA2dpSinkHALInterface()
      ->set_audio_track_gain(gain);
}

void A2dpSink::ConnectionStateCallback(BluetoothAvInterface* iface,
                                       const RawAddress& bd_addr,
                                       btav_connection_state_t state) {
  std::string device_address = BtAddrString(&bd_addr);
  std::lock_guard<std::mutex> lock(delegate_mutex_);

  if (delegate_)
    delegate_->OnConnectionState(device_address, static_cast<int>(state));
}

void A2dpSink::AudioStateCallback(BluetoothAvInterface* iface,
                                  const RawAddress& bd_addr,
                                  btav_audio_state_t state) {
  std::string device_address = BtAddrString(&bd_addr);
  std::lock_guard<std::mutex> lock(delegate_mutex_);

  if (delegate_)
    delegate_->OnAudioState(device_address, static_cast<int>(state));
}

void A2dpSink::AudioConfigCallback(BluetoothAvInterface* iface,
                                   const RawAddress& bd_addr,
                                   uint32_t sample_rate,
                                   uint8_t channel_count) {
  std::string device_address = BtAddrString(&bd_addr);
  std::lock_guard<std::mutex> lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnAudioConfig(device_address, sample_rate, channel_count);
}

// A2dpSinkFactory implementation
// ========================================================
A2dpSinkFactory::A2dpSinkFactory() = default;
A2dpSinkFactory::~A2dpSinkFactory() = default;

bool A2dpSinkFactory::RegisterInstance(const Uuid& uuid,
                                       const RegisterCallback& callback) {
  VLOG(1) << __func__ << " - Uuid: " << uuid.ToString();

  auto a2dp_sink = base::WrapUnique(new A2dpSink(uuid));
  callback(BLE_STATUS_SUCCESS, uuid, std::move(a2dp_sink));
  return true;
}

}  // namespace bluetooth