summaryrefslogtreecommitdiff
path: root/bluetooth/audio/utils/session/BluetoothAudioSupportedCodecsDB_2_1.cpp
blob: 8b0b0f743a5faefa13dd361c66d9de5c751b9692 (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
/*
 * 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 "BTAudioProviderSessionCodecsDB_2_1"

#include "BluetoothAudioSupportedCodecsDB_2_1.h"

#include <android-base/logging.h>

namespace android {
namespace bluetooth {
namespace audio {

using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;

using SampleRate_2_0 = ::android::hardware::bluetooth::audio::V2_0::SampleRate;
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;

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

static const ::android::hardware::bluetooth::audio::V2_1::PcmParameters
    kDefaultSoftwarePcmCapabilities_2_1 = {
        .sampleRate = static_cast<SampleRate_2_1>(
            SampleRate_2_1::RATE_44100 | SampleRate_2_1::RATE_48000 |
            SampleRate_2_1::RATE_88200 | SampleRate_2_1::RATE_96000 |
            SampleRate_2_1::RATE_16000 | SampleRate_2_1::RATE_24000),
        .channelMode =
            static_cast<ChannelMode>(ChannelMode::MONO | ChannelMode::STEREO),
        .bitsPerSample = static_cast<BitsPerSample>(BitsPerSample::BITS_16 |
                                                    BitsPerSample::BITS_24 |
                                                    BitsPerSample::BITS_32)};

std::vector<::android::hardware::bluetooth::audio::V2_1::PcmParameters>
GetSoftwarePcmCapabilities_2_1() {
  return std::vector<
      ::android::hardware::bluetooth::audio::V2_1::PcmParameters>(
      1, kDefaultSoftwarePcmCapabilities_2_1);
}

std::vector<CodecCapabilities> GetOffloadCodecCapabilities(
    const ::android::hardware::bluetooth::audio::V2_1::SessionType&
        session_type) {
  if (is_2_0_session_type(session_type)) {
    return GetOffloadCodecCapabilities(
        static_cast<SessionType_2_0>(session_type));
  }
  return std::vector<CodecCapabilities>(0);
}

bool IsSoftwarePcmConfigurationValid_2_1(
    const ::android::hardware::bluetooth::audio::V2_1::PcmParameters&
        pcm_config) {
  if ((pcm_config.sampleRate != SampleRate_2_1::RATE_44100 &&
       pcm_config.sampleRate != SampleRate_2_1::RATE_48000 &&
       pcm_config.sampleRate != SampleRate_2_1::RATE_88200 &&
       pcm_config.sampleRate != SampleRate_2_1::RATE_96000 &&
       pcm_config.sampleRate != SampleRate_2_1::RATE_16000 &&
       pcm_config.sampleRate != SampleRate_2_1::RATE_24000) ||
      (pcm_config.bitsPerSample != BitsPerSample::BITS_16 &&
       pcm_config.bitsPerSample != BitsPerSample::BITS_24 &&
       pcm_config.bitsPerSample != BitsPerSample::BITS_32) ||
      (pcm_config.channelMode != ChannelMode::MONO &&
       pcm_config.channelMode != ChannelMode::STEREO)) {
    LOG(WARNING) << __func__
                 << ": Invalid PCM Configuration=" << toString(pcm_config);
    return false;
  } else if (pcm_config.sampleRate &
                 kDefaultSoftwarePcmCapabilities_2_1.sampleRate &&
             pcm_config.bitsPerSample &
                 kDefaultSoftwarePcmCapabilities_2_1.bitsPerSample &&
             pcm_config.channelMode &
                 kDefaultSoftwarePcmCapabilities_2_1.channelMode &&
             pcm_config.dataIntervalUs != 0) {
    return true;
  }
  LOG(WARNING) << __func__
               << ": Unsupported PCM Configuration=" << toString(pcm_config);
  return false;
}

bool IsOffloadCodecConfigurationValid(
    const ::android::hardware::bluetooth::audio::V2_1::SessionType&
        session_type,
    const ::android::hardware::bluetooth::audio::V2_0::CodecConfiguration&
        codec_config) {
  if (is_2_0_session_type(session_type)) {
    return IsOffloadCodecConfigurationValid(
        static_cast<SessionType_2_0>(session_type), codec_config);
  }

  return false;
}

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