1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "mock_codec_interface.h"
18
19 #include "le_audio/codec_interface.h"
20
21 static std::list<std::function<void(MockCodecInterface*, bool)>> mock_life_listener_list;
22
23 namespace bluetooth::le_audio {
24
25 struct CodecInterface::Impl : public MockCodecInterface {
26 public:
Implbluetooth::le_audio::CodecInterface::Impl27 Impl(const types::LeAudioCodecId& /*codec_id*/) { output_channel_data_.resize(1); }
28 ~Impl() = default;
29
GetDecodedSamplesbluetooth::le_audio::CodecInterface::Impl30 std::vector<int16_t>& GetDecodedSamples() { return output_channel_data_; }
31 std::vector<int16_t> output_channel_data_;
32 };
33
CodecInterface(const types::LeAudioCodecId & codec_id)34 CodecInterface::CodecInterface(const types::LeAudioCodecId& codec_id) {
35 impl = new Impl(codec_id);
36 for (auto& foo : mock_life_listener_list) {
37 foo(impl, false);
38 }
39 }
~CodecInterface()40 CodecInterface::~CodecInterface() {
41 for (auto& foo : mock_life_listener_list) {
42 foo(impl, true);
43 }
44 delete impl;
45 }
IsReady()46 bool CodecInterface::IsReady() { return impl->IsReady(); }
InitEncoder(const LeAudioCodecConfiguration & pcm_config,const LeAudioCodecConfiguration & codec_config)47 CodecInterface::Status CodecInterface::InitEncoder(const LeAudioCodecConfiguration& pcm_config,
48 const LeAudioCodecConfiguration& codec_config) {
49 return impl->InitEncoder(pcm_config, codec_config);
50 }
InitDecoder(const LeAudioCodecConfiguration & codec_config,const LeAudioCodecConfiguration & pcm_config)51 CodecInterface::Status CodecInterface::InitDecoder(const LeAudioCodecConfiguration& codec_config,
52 const LeAudioCodecConfiguration& pcm_config) {
53 return impl->InitDecoder(codec_config, pcm_config);
54 }
GetDecodedSamples()55 std::vector<int16_t>& CodecInterface::GetDecodedSamples() { return impl->GetDecodedSamples(); }
Decode(uint8_t * data,uint16_t size)56 CodecInterface::Status CodecInterface::Decode(uint8_t* data, uint16_t size) {
57 return impl->Decode(data, size);
58 }
Encode(const uint8_t * data,int stride,uint16_t out_size,std::vector<int16_t> * out_buffer,uint16_t out_offset)59 CodecInterface::Status CodecInterface::Encode(const uint8_t* data, int stride, uint16_t out_size,
60 std::vector<int16_t>* out_buffer,
61 uint16_t out_offset) {
62 return impl->Encode(data, stride, out_size, out_buffer, out_offset);
63 }
Cleanup()64 void CodecInterface::Cleanup() { return impl->Cleanup(); }
65
GetNumOfSamplesPerChannel()66 uint16_t CodecInterface::GetNumOfSamplesPerChannel() { return impl->GetNumOfSamplesPerChannel(); }
GetNumOfBytesPerSample()67 uint8_t CodecInterface::GetNumOfBytesPerSample() { return impl->GetNumOfBytesPerSample(); }
68 } // namespace bluetooth::le_audio
69
RegisterMockInstanceHook(std::function<void (MockCodecInterface *,bool)> listener)70 void MockCodecInterface::RegisterMockInstanceHook(
71 std::function<void(MockCodecInterface*, bool)> listener) {
72 mock_life_listener_list.push_back(std::move(listener));
73 }
74
ClearMockInstanceHookList()75 void MockCodecInterface::ClearMockInstanceHookList() { mock_life_listener_list.clear(); }
76