aboutsummaryrefslogtreecommitdiff
path: root/system/audio_hal_interface/aidl/bluetooth_audio_port_impl.cc
blob: 813648acb5acc173233ba9ee5089f2a6d324fa85 (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
/*
 * Copyright 2022 The Android Open Source Project
 *
 * 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 "bluetooth_audio_port_impl.h"

#include "btif/include/btif_common.h"
#include "common/stop_watch_legacy.h"

namespace bluetooth {
namespace audio {
namespace aidl {

using ::bluetooth::common::StopWatchLegacy;

BluetoothAudioPortImpl::BluetoothAudioPortImpl(
    IBluetoothTransportInstance* transport_instance,
    const std::shared_ptr<IBluetoothAudioProvider>& provider)
    : transport_instance_(transport_instance), provider_(provider) {}

BluetoothAudioPortImpl::~BluetoothAudioPortImpl() {}

ndk::ScopedAStatus BluetoothAudioPortImpl::startStream(bool is_low_latency) {
  StopWatchLegacy stop_watch(__func__);
  BluetoothAudioCtrlAck ack = transport_instance_->StartRequest(is_low_latency);
  if (ack != BluetoothAudioCtrlAck::PENDING) {
    auto aidl_retval =
        provider_->streamStarted(BluetoothAudioCtrlAckToHalStatus(ack));
    if (!aidl_retval.isOk()) {
      LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: "
                 << aidl_retval.getDescription();
    }
  }
  return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus BluetoothAudioPortImpl::suspendStream() {
  StopWatchLegacy stop_watch(__func__);
  BluetoothAudioCtrlAck ack = transport_instance_->SuspendRequest();
  if (ack != BluetoothAudioCtrlAck::PENDING) {
    auto aidl_retval =
        provider_->streamSuspended(BluetoothAudioCtrlAckToHalStatus(ack));
    if (!aidl_retval.isOk()) {
      LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: "
                 << aidl_retval.getDescription();
    }
  }
  return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus BluetoothAudioPortImpl::stopStream() {
  StopWatchLegacy stop_watch(__func__);
  transport_instance_->StopRequest();
  return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus BluetoothAudioPortImpl::getPresentationPosition(
    PresentationPosition* _aidl_return) {
  StopWatchLegacy stop_watch(__func__);
  uint64_t remote_delay_report_ns;
  uint64_t total_bytes_read;
  timespec data_position;
  bool retval = transport_instance_->GetPresentationPosition(
      &remote_delay_report_ns, &total_bytes_read, &data_position);

  PresentationPosition::TimeSpec transmittedOctetsTimeStamp;
  if (retval) {
    transmittedOctetsTimeStamp = timespec_convert_to_hal(data_position);
  } else {
    remote_delay_report_ns = 0;
    total_bytes_read = 0;
    transmittedOctetsTimeStamp = {};
  }
  VLOG(2) << __func__ << ": result=" << retval
          << ", delay=" << remote_delay_report_ns
          << ", data=" << total_bytes_read
          << " byte(s), timestamp=" << transmittedOctetsTimeStamp.toString();
  _aidl_return->remoteDeviceAudioDelayNanos =
      static_cast<int64_t>(remote_delay_report_ns);
  _aidl_return->transmittedOctets = static_cast<int64_t>(total_bytes_read);
  _aidl_return->transmittedOctetsTimestamp = transmittedOctetsTimeStamp;
  return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus BluetoothAudioPortImpl::updateSourceMetadata(
    const SourceMetadata& source_metadata) {
  StopWatchLegacy stop_watch(__func__);
  LOG(INFO) << __func__ << ": " << source_metadata.tracks.size() << "track(s)";

  std::vector<playback_track_metadata> metadata_vec;
  metadata_vec.reserve(source_metadata.tracks.size());
  for (const auto& metadata : source_metadata.tracks) {
    metadata_vec.push_back({
        .usage = static_cast<audio_usage_t>(metadata.usage),
        .content_type = static_cast<audio_content_type_t>(metadata.contentType),
        .gain = metadata.gain,
    });
  }
  const source_metadata_t legacy_source_metadata = {
      .track_count = metadata_vec.size(), .tracks = metadata_vec.data()};
  transport_instance_->SourceMetadataChanged(legacy_source_metadata);
  return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus BluetoothAudioPortImpl::updateSinkMetadata(
    const SinkMetadata& sink_metadata) {
  StopWatchLegacy stop_watch(__func__);
  LOG(INFO) << __func__ << ": " << sink_metadata.tracks.size() << " track(s)";

  std::vector<record_track_metadata> metadata_vec;
  metadata_vec.reserve(sink_metadata.tracks.size());
  for (const auto& metadata : sink_metadata.tracks) {
    metadata_vec.push_back({
        .source = static_cast<audio_source_t>(metadata.source),
        .gain = metadata.gain,
    });
  }
  const sink_metadata_t legacy_sink_metadata = {
      .track_count = metadata_vec.size(), .tracks = metadata_vec.data()};
  transport_instance_->SinkMetadataChanged(legacy_sink_metadata);
  return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus BluetoothAudioPortImpl::setLatencyMode(
    LatencyMode latency_mode) {
  bool is_low_latency = latency_mode == LatencyMode::LOW_LATENCY ? true : false;
  invoke_switch_buffer_size_cb(is_low_latency);
  transport_instance_->SetLowLatency(is_low_latency);
  return ndk::ScopedAStatus::ok();
}

PresentationPosition::TimeSpec BluetoothAudioPortImpl::timespec_convert_to_hal(
    const timespec& ts) {
  return {.tvSec = static_cast<int64_t>(ts.tv_sec),
          .tvNSec = static_cast<int64_t>(ts.tv_nsec)};
}

}  // namespace aidl
}  // namespace audio
}  // namespace bluetooth