1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef AUDIO_CAPTURER_ADAPTER_IMPL_H
17 #define AUDIO_CAPTURER_ADAPTER_IMPL_H
18 
19 #include "audio_capturer_adapter.h"
20 
21 #include <unordered_set>
22 #if defined(NWEB_AUDIO_ENABLE)
23 #include "audio_capturer.h"
24 #endif
25 
26 namespace OHOS::NWeb {
27 #if defined(NWEB_AUDIO_ENABLE)
28 using namespace OHOS::AudioStandard;
29 
30 class AudioCapturerReadCallbackImpl : public AudioCapturerReadCallback {
31 public:
32     AudioCapturerReadCallbackImpl(std::shared_ptr<AudioCapturerReadCallbackAdapter> cb);
33 
34     ~AudioCapturerReadCallbackImpl() override = default;
35 
36     void OnReadData(size_t length) override;
37 
38 private:
39     std::shared_ptr<AudioCapturerReadCallbackAdapter> cb_ = nullptr;
40 };
41 #endif
42 
43 class AudioCapturerAdapterImpl : public AudioCapturerAdapter {
44 public:
45     AudioCapturerAdapterImpl() = default;
46 
47     ~AudioCapturerAdapterImpl() override = default;
48 
49     int32_t Create(const std::shared_ptr<AudioCapturerOptionsAdapter> capturerOptions,
50         std::string cachePath = std::string()) override;
51 
52     bool Start() override;
53 
54     bool Stop() override;
55 
56     bool Release() override;
57 
58     int32_t SetCapturerReadCallback(
59         std::shared_ptr<AudioCapturerReadCallbackAdapter> callbck) override;
60 
61     int32_t GetBufferDesc(std::shared_ptr<BufferDescAdapter> bufferDesc) override;
62 
63     int32_t Enqueue(const std::shared_ptr<BufferDescAdapter> bufferDesc) override;
64 
65     int32_t GetFrameCount(uint32_t &frameCount) override;
66 
67     int64_t GetAudioTime() override;
68 
69 #if defined(NWEB_AUDIO_ENABLE)
70     static AudioSamplingRate GetAudioSamplingRate(AudioAdapterSamplingRate samplingRate);
71 
72     static AudioEncodingType GetAudioEncodingType(AudioAdapterEncodingType encodingType);
73 
74     static AudioSampleFormat GetAudioSampleFormat(AudioAdapterSampleFormat sampleFormat);
75 
76     static AudioChannel GetAudioChannel(AudioAdapterChannel channel);
77 
78     static SourceType GetAudioSourceType(AudioAdapterSourceType SourceType);
79 
80 private:
81     std::unique_ptr<AudioCapturer> audio_capturer_;
82 #endif
83 };
84 }  // namespace OHOS::NWeb
85 
86 #endif // AUDIO_CAPTURER_ADAPTER_IMPL_H
87