summaryrefslogtreecommitdiff
path: root/bluetooth/audio/utils/session/BluetoothAudioSession_2_2.cpp
blob: 4613ddc05a5f52cfb1b4f04761a76dd0a0ce7f97 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
 * Copyright 2018 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.
 */

#define LOG_TAG "BTAudioProviderSession_2_2"

#include "BluetoothAudioSession_2_2.h"

#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android/hardware/bluetooth/audio/2.2/IBluetoothAudioPort.h>

#include "../aidl_session/HidlToAidlMiddleware_2_0.h"
#include "../aidl_session/HidlToAidlMiddleware_2_2.h"

namespace android {
namespace bluetooth {
namespace audio {

using ::aidl::android::hardware::bluetooth::audio::HidlToAidlMiddleware_2_0;
using ::aidl::android::hardware::bluetooth::audio::HidlToAidlMiddleware_2_2;
using ::android::hardware::audio::common::V5_0::AudioSource;
using ::android::hardware::audio::common::V5_0::RecordTrackMetadata;
using ::android::hardware::audio::common::V5_0::SinkMetadata;
using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
using ::android::hardware::bluetooth::audio::V2_2::LeAudioConfiguration;
using ::android::hardware::bluetooth::audio::V2_2::LeAudioMode;
using PcmParameters_2_1 =
    ::android::hardware::bluetooth::audio::V2_1::PcmParameters;
using SampleRate_2_1 = ::android::hardware::bluetooth::audio::V2_1::SampleRate;

using SessionType_2_1 =
    ::android::hardware::bluetooth::audio::V2_1::SessionType;
using SessionType_2_0 =
    ::android::hardware::bluetooth::audio::V2_0::SessionType;

using AudioConfiguration_2_1 =
    ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration;

static constexpr PcmParameters_2_1 kInvalidPcmParameters = {
    .sampleRate = SampleRate_2_1::RATE_UNKNOWN,
    .channelMode = ChannelMode::UNKNOWN,
    .bitsPerSample = BitsPerSample::BITS_UNKNOWN,
    .dataIntervalUs = 0,
};

static LeAudioConfiguration kInvalidLeAudioConfig = {
    .mode = LeAudioMode::UNKNOWN,
    .config = {},
};

::android::hardware::bluetooth::audio::V2_2::AudioConfiguration
    BluetoothAudioSession_2_2::invalidSoftwareAudioConfiguration = {};
::android::hardware::bluetooth::audio::V2_2::AudioConfiguration
    BluetoothAudioSession_2_2::invalidOffloadAudioConfiguration = {};
::android::hardware::bluetooth::audio::V2_2::AudioConfiguration
    BluetoothAudioSession_2_2::invalidLeOffloadAudioConfiguration = {};

using IBluetoothAudioPort_2_2 =
    ::android::hardware::bluetooth::audio::V2_2::IBluetoothAudioPort;

namespace {
bool is_2_0_session_type(
    const ::android::hardware::bluetooth::audio::V2_1::SessionType&
        session_type) {
  if (session_type == SessionType_2_1::A2DP_SOFTWARE_ENCODING_DATAPATH ||
      session_type == SessionType_2_1::A2DP_HARDWARE_OFFLOAD_DATAPATH ||
      session_type == SessionType_2_1::HEARING_AID_SOFTWARE_ENCODING_DATAPATH) {
    return true;
  } else {
    return false;
  }
}
}  // namespace

BluetoothAudioSession_2_2::BluetoothAudioSession_2_2(
    const ::android::hardware::bluetooth::audio::V2_1::SessionType&
        session_type)
    : audio_session(BluetoothAudioSessionInstance::GetSessionInstance(
          static_cast<SessionType_2_0>(session_type))),
      audio_session_2_1(
          BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type)) {
  if (is_2_0_session_type(session_type)) {
    session_type_2_1_ = (SessionType_2_1::UNKNOWN);
  } else {
    session_type_2_1_ = (session_type);
  }
  raw_session_type_ = session_type;
  invalidSoftwareAudioConfiguration.pcmConfig(kInvalidPcmParameters);
  invalidOffloadAudioConfiguration.codecConfig(
      audio_session->kInvalidCodecConfiguration);
  invalidLeOffloadAudioConfiguration.leAudioConfig(kInvalidLeAudioConfig);
}

bool BluetoothAudioSession_2_2::IsSessionReady() {
  if (HidlToAidlMiddleware_2_0::IsAidlAvailable())
    return HidlToAidlMiddleware_2_2::IsSessionReady(raw_session_type_);
  if (session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH &&
      session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
    return audio_session->IsSessionReady();
  }

  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  return audio_session->stack_iface_ != nullptr;
}

std::shared_ptr<BluetoothAudioSession>
BluetoothAudioSession_2_2::GetAudioSession() {
  return audio_session;
}
std::shared_ptr<BluetoothAudioSession_2_1>
BluetoothAudioSession_2_2::GetAudioSession_2_1() {
  return audio_session_2_1;
}

void BluetoothAudioSession_2_2::UpdateSinkMetadata(
    const struct sink_metadata* sink_metadata) {
  if (HidlToAidlMiddleware_2_0::IsAidlAvailable())
    return HidlToAidlMiddleware_2_2::UpdateSinkMetadata(raw_session_type_,
                                                        sink_metadata);
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  if (!IsSessionReady()) {
    LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_2_1_)
               << " has NO session";
    return;
  }

  ssize_t track_count = sink_metadata->track_count;
  LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_2_1_)
            << ", " << track_count << " track(s)";
  if (session_type_2_1_ == SessionType_2_1::A2DP_SOFTWARE_ENCODING_DATAPATH ||
      session_type_2_1_ == SessionType_2_1::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
    return;
  }

  struct record_track_metadata* track = sink_metadata->tracks;
  SinkMetadata sinkMetadata;
  RecordTrackMetadata* halMetadata;

  sinkMetadata.tracks.resize(track_count);
  halMetadata = sinkMetadata.tracks.data();
  while (track_count && track) {
    halMetadata->source = static_cast<AudioSource>(track->source);
    halMetadata->gain = track->gain;
    // halMetadata->destination leave unspecified
    LOG(INFO) << __func__
              << " - SessionType=" << toString(GetAudioSession()->session_type_)
              << ", source=" << track->source
              << ", dest_device=" << track->dest_device
              << ", gain=" << track->gain
              << ", dest_device_address=" << track->dest_device_address;
    --track_count;
    ++track;
    ++halMetadata;
  }

  /* This is called just for 2.2 sessions, so it's safe to do this casting*/
  IBluetoothAudioPort_2_2* stack_iface_2_2_ =
      static_cast<IBluetoothAudioPort_2_2*>(audio_session->stack_iface_.get());
  auto hal_retval = stack_iface_2_2_->updateSinkMetadata(sinkMetadata);
  if (!hal_retval.isOk()) {
    LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
                 << toString(session_type_2_1_) << " failed";
  }
}

// The control function is for the bluetooth_audio module to get the current
// AudioConfiguration
const ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration
BluetoothAudioSession_2_2::GetAudioConfig() {
  if (HidlToAidlMiddleware_2_0::IsAidlAvailable())
    return HidlToAidlMiddleware_2_2::GetAudioConfig(raw_session_type_);
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  if (IsSessionReady()) {
    auto audio_config_discriminator = audio_config_2_2_.getDiscriminator();
    // If session is unknown it means it should be 2.0 type
    if (session_type_2_1_ != SessionType_2_1::UNKNOWN) {
      if ((audio_config_discriminator ==
               ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration::
                   hidl_discriminator::pcmConfig &&
           audio_config_2_2_ != kInvalidSoftwareAudioConfiguration) ||
          (audio_config_discriminator ==
               ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration::
                   hidl_discriminator::leAudioConfig &&
           audio_config_2_2_ != kInvalidLeOffloadAudioConfiguration))
        return audio_config_2_2_;

      ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration toConf;
      const AudioConfiguration_2_1 fromConf =
          GetAudioSession_2_1()->GetAudioConfig();
      if (fromConf.getDiscriminator() ==
          AudioConfiguration_2_1::hidl_discriminator::pcmConfig) {
        toConf.pcmConfig(fromConf.pcmConfig());
        return toConf;
      }
    }

    ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration toConf;
    const AudioConfiguration fromConf = GetAudioSession()->GetAudioConfig();
    // pcmConfig only differs between 2.0 and 2.1 in AudioConfiguration
    if (fromConf.getDiscriminator() ==
        AudioConfiguration::hidl_discriminator::codecConfig) {
      toConf.codecConfig(fromConf.codecConfig());
    } else {
      toConf.pcmConfig() = {
          .sampleRate = static_cast<
              ::android::hardware::bluetooth::audio::V2_1::SampleRate>(
              fromConf.pcmConfig().sampleRate),
          .channelMode = fromConf.pcmConfig().channelMode,
          .bitsPerSample = fromConf.pcmConfig().bitsPerSample,
          .dataIntervalUs = 0};
    }
    return toConf;
  } else if (session_type_2_1_ ==
                 SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
             session_type_2_1_ ==
                 SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
    return kInvalidLeOffloadAudioConfiguration;
  } else {
    return kInvalidSoftwareAudioConfiguration;
  }
}

// Those control functions are for the bluetooth_audio module to start, suspend,
// stop stream, to check position, and to update metadata.
bool BluetoothAudioSession_2_2::StartStream() {
  if (HidlToAidlMiddleware_2_0::IsAidlAvailable())
    return HidlToAidlMiddleware_2_2::StartStream(raw_session_type_);
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  if (!IsSessionReady()) {
    LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_2_1_)
               << " has NO session";
    return false;
  }
  auto hal_retval = audio_session->stack_iface_->startStream();
  if (!hal_retval.isOk()) {
    LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
                 << toString(session_type_2_1_) << " failed";
    return false;
  }
  return true;
}

bool BluetoothAudioSession_2_2::SuspendStream() {
  if (HidlToAidlMiddleware_2_0::IsAidlAvailable())
    return HidlToAidlMiddleware_2_2::SuspendStream(raw_session_type_);
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  if (!IsSessionReady()) {
    LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_2_1_)
               << " has NO session";
    return false;
  }
  auto hal_retval = audio_session->stack_iface_->suspendStream();
  if (!hal_retval.isOk()) {
    LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
                 << toString(session_type_2_1_) << " failed";
    return false;
  }
  return true;
}

void BluetoothAudioSession_2_2::StopStream() {
  if (HidlToAidlMiddleware_2_0::IsAidlAvailable())
    return HidlToAidlMiddleware_2_2::StopStream(raw_session_type_);
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  if (!IsSessionReady()) {
    return;
  }
  auto hal_retval = audio_session->stack_iface_->stopStream();
  if (!hal_retval.isOk()) {
    LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
                 << toString(session_type_2_1_) << " failed";
  }
}

bool BluetoothAudioSession_2_2::UpdateAudioConfig(
    const ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration&
        audio_config) {
  bool is_software_session =
      (session_type_2_1_ == SessionType_2_1::A2DP_SOFTWARE_ENCODING_DATAPATH ||
       session_type_2_1_ ==
           SessionType_2_1::HEARING_AID_SOFTWARE_ENCODING_DATAPATH ||
       session_type_2_1_ ==
           SessionType_2_1::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH ||
       session_type_2_1_ ==
           SessionType_2_1::LE_AUDIO_SOFTWARE_DECODED_DATAPATH);
  bool is_offload_a2dp_session =
      (session_type_2_1_ == SessionType_2_1::A2DP_HARDWARE_OFFLOAD_DATAPATH);
  bool is_offload_le_audio_session =
      (session_type_2_1_ ==
           SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
       session_type_2_1_ ==
           SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH);
  auto audio_config_discriminator = audio_config.getDiscriminator();
  bool is_software_audio_config =
      (is_software_session &&
       audio_config_discriminator ==
           ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration::
               hidl_discriminator::pcmConfig);
  bool is_a2dp_offload_audio_config =
      (is_offload_a2dp_session &&
       audio_config_discriminator ==
           ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration::
               hidl_discriminator::codecConfig);
  bool is_le_audio_offload_audio_config =
      (is_offload_le_audio_session &&
       audio_config_discriminator ==
           ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration::
               hidl_discriminator::leAudioConfig);
  if (!is_software_audio_config && !is_a2dp_offload_audio_config &&
      !is_le_audio_offload_audio_config) {
    return false;
  }
  audio_config_2_2_ = audio_config;
  return true;
}

// The report function is used to report that the Bluetooth stack has started
// this session without any failure, and will invoke session_changed_cb_ to
// notify those registered bluetooth_audio outputs
void BluetoothAudioSession_2_2::OnSessionStarted(
    const sp<IBluetoothAudioPort> stack_iface, const DataMQ::Descriptor* dataMQ,
    const ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration&
        audio_config) {
  if (session_type_2_1_ == SessionType_2_1::UNKNOWN) {
    ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration config;
    if (audio_config.getDiscriminator() ==
        ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration::
            hidl_discriminator::codecConfig) {
      config.codecConfig(audio_config.codecConfig());
    } else {
      auto& tmpPcm = audio_config.pcmConfig();
      config.pcmConfig(
          ::android::hardware::bluetooth::audio::V2_0::PcmParameters{
              .sampleRate = static_cast<SampleRate>(tmpPcm.sampleRate),
              .channelMode = tmpPcm.channelMode,
              .bitsPerSample = tmpPcm.bitsPerSample
              /*dataIntervalUs is not passed to 2.0 */
          });
    }

    audio_session->OnSessionStarted(stack_iface, dataMQ, config);
  } else {
    std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
    if (stack_iface == nullptr) {
      LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_2_1_)
                 << ", IBluetoothAudioPort Invalid";
    } else if (!UpdateAudioConfig(audio_config)) {
      LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_2_1_)
                 << ", AudioConfiguration=" << toString(audio_config)
                 << " Invalid";
    } else if (!audio_session->UpdateDataPath(dataMQ)) {
      LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_2_1_)
                 << " DataMQ Invalid";
      audio_config_2_2_ =
          ((session_type_2_1_ ==
                SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
            session_type_2_1_ ==
                SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH)
               ? kInvalidLeOffloadAudioConfiguration
               : kInvalidSoftwareAudioConfiguration);
    } else {
      audio_session->stack_iface_ = stack_iface;
      LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_2_1_)
                << ", AudioConfiguration=" << toString(audio_config);
      ReportSessionStatus();
    };
  }
}

// The report function is used to report that the Bluetooth stack has ended the
// session, and will invoke session_changed_cb_ to notify registered
// bluetooth_audio outputs
void BluetoothAudioSession_2_2::OnSessionEnded() {
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  bool toggled = IsSessionReady();
  LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_2_1_);
  if (session_type_2_1_ == SessionType_2_1::UNKNOWN) {
    audio_session->OnSessionEnded();
    return;
  }

  audio_config_2_2_ =
      ((session_type_2_1_ ==
            SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
        session_type_2_1_ ==
            SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH)
           ? kInvalidLeOffloadAudioConfiguration
           : kInvalidSoftwareAudioConfiguration);
  audio_session->stack_iface_ = nullptr;
  audio_session->UpdateDataPath(nullptr);
  if (toggled) {
    ReportSessionStatus();
  }
}

// The control function helps the bluetooth_audio module to register
// PortStatusCallbacks_2_2
// @return: cookie - the assigned number to this bluetooth_audio output
uint16_t BluetoothAudioSession_2_2::RegisterStatusCback(
    const PortStatusCallbacks_2_2& cbacks) {
  if (HidlToAidlMiddleware_2_0::IsAidlAvailable())
    return HidlToAidlMiddleware_2_2::RegisterControlResultCback(
        raw_session_type_, cbacks);
  if (session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH &&
      session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
    PortStatusCallbacks cb = {
        .control_result_cb_ = cbacks.control_result_cb_,
        .session_changed_cb_ = cbacks.session_changed_cb_};
    return audio_session->RegisterStatusCback(cb);
  }

  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  uint16_t cookie = ObserversCookieGetInitValue(session_type_2_1_);
  uint16_t cookie_upper_bound = ObserversCookieGetUpperBound(session_type_2_1_);

  while (cookie < cookie_upper_bound) {
    if (observers_.find(cookie) == observers_.end()) {
      break;
    }
    ++cookie;
  }
  if (cookie >= cookie_upper_bound) {
    LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_2_1_)
               << " has " << observers_.size()
               << " observers already (No Resource)";
    return kObserversCookieUndefined;
  }
  std::shared_ptr<struct PortStatusCallbacks_2_2> cb =
      std::make_shared<struct PortStatusCallbacks_2_2>();
  *cb = cbacks;
  observers_[cookie] = cb;
  return cookie;
}

// The control function helps the bluetooth_audio module to unregister
// PortStatusCallbacks_2_2
// @param: cookie - indicates which bluetooth_audio output is
void BluetoothAudioSession_2_2::UnregisterStatusCback(uint16_t cookie) {
  if (HidlToAidlMiddleware_2_0::IsAidlAvailable())
    return HidlToAidlMiddleware_2_2::UnregisterControlResultCback(
        raw_session_type_, cookie);
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  if (session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH &&
      session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
    audio_session->UnregisterStatusCback(cookie);
    return;
  }
  if (observers_.erase(cookie) != 1) {
    LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_2_1_)
                 << " no such provider=0x"
                 << android::base::StringPrintf("%04x", cookie);
  }
}

// invoking the registered session_changed_cb_
void BluetoothAudioSession_2_2::ReportSessionStatus() {
  // This is locked already by OnSessionStarted / OnSessionEnded
  if (session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH &&
      session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
    audio_session->ReportSessionStatus();
    return;
  }
  if (observers_.empty()) {
    LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_2_1_)
              << " has NO port state observer";
    return;
  }
  for (auto& observer : observers_) {
    uint16_t cookie = observer.first;
    std::shared_ptr<struct PortStatusCallbacks_2_2> cb = observer.second;
    LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_2_1_)
              << " notify to bluetooth_audio=0x"
              << android::base::StringPrintf("%04x", cookie);
    cb->session_changed_cb_(cookie);
  }
}

// The report function is used to report that the Bluetooth stack has notified
// the result of startStream or suspendStream, and will invoke
// control_result_cb_ to notify registered bluetooth_audio outputs
void BluetoothAudioSession_2_2::ReportControlStatus(
    bool start_resp, const BluetoothAudioStatus& status) {
  if (session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH &&
      session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
    audio_session->ReportControlStatus(start_resp, status);
    return;
  }
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  if (observers_.empty()) {
    LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_2_1_)
                 << " has NO port state observer";
    return;
  }
  for (auto& observer : observers_) {
    uint16_t cookie = observer.first;
    std::shared_ptr<struct PortStatusCallbacks_2_2> cb = observer.second;
    LOG(INFO) << __func__ << " - status=" << toString(status)
              << " for SessionType=" << toString(session_type_2_1_)
              << ", bluetooth_audio=0x"
              << android::base::StringPrintf("%04x", cookie)
              << (start_resp ? " started" : " suspended");
    cb->control_result_cb_(cookie, start_resp, status);
  }
}

// The report function is used to report that the Bluetooth stack has notified
// the result of startStream or suspendStream, and will invoke
// control_result_cb_ to notify registered bluetooth_audio outputs
void BluetoothAudioSession_2_2::ReportAudioConfigChanged(
    const ::android::hardware::bluetooth::audio::V2_2::AudioConfiguration&
        audio_config) {
  if (session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH &&
      session_type_2_1_ !=
          SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
    return;
  }
  std::lock_guard<std::recursive_mutex> guard(audio_session->mutex_);
  audio_config_2_2_ = audio_config;
  if (observers_.empty()) {
    LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_2_1_)
                 << " has NO port state observer";
    return;
  }
  for (auto& observer : observers_) {
    uint16_t cookie = observer.first;
    std::shared_ptr<struct PortStatusCallbacks_2_2> cb = observer.second;
    LOG(INFO) << __func__ << " for SessionType=" << toString(session_type_2_1_)
              << ", bluetooth_audio=0x"
              << android::base::StringPrintf("%04x", cookie);
    if (cb->audio_configuration_changed_cb_ != nullptr) {
      cb->audio_configuration_changed_cb_(cookie);
    }
  }
}

std::unique_ptr<BluetoothAudioSessionInstance_2_2>
    BluetoothAudioSessionInstance_2_2::instance_ptr =
        std::unique_ptr<BluetoothAudioSessionInstance_2_2>(
            new BluetoothAudioSessionInstance_2_2());

// API to fetch the session of A2DP / Hearing Aid
std::shared_ptr<BluetoothAudioSession_2_2>
BluetoothAudioSessionInstance_2_2::GetSessionInstance(
    const SessionType_2_1& session_type) {
  std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
  if (!instance_ptr->sessions_map_.empty()) {
    auto entry = instance_ptr->sessions_map_.find(session_type);
    if (entry != instance_ptr->sessions_map_.end()) {
      return entry->second;
    }
  }
  std::shared_ptr<BluetoothAudioSession_2_2> session_ptr =
      std::make_shared<BluetoothAudioSession_2_2>(session_type);
  instance_ptr->sessions_map_[session_type] = session_ptr;
  return session_ptr;
}

}  // namespace audio
}  // namespace bluetooth
}  // namespace android