1 /*
2  * Copyright (c) 2022 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 AUDIOCAPTURER_ADAPTER_H
17 #define AUDIOCAPTURER_ADAPTER_H
18 
19 #include <OpenSLES.h>
20 #include <OpenSLES_Platform.h>
21 #include <iostream>
22 #include <map>
23 #include <audio_capturer.h>
24 #include <audio_system_manager.h>
25 #include <readorwritecallback_adapter.h>
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 class AudioCapturerAdapter {
30 public:
31     static AudioCapturerAdapter* GetInstance();
32     AudioCapturer *GetAudioCapturerById(SLuint32 id);
33     void EraseAudioCapturerById(SLuint32 id);
34     SLresult CreateAudioCapturerAdapter
35         (SLuint32 id, SLDataSource *dataSource, SLDataSink *dataSink, AudioStreamType streamType);
36     SLresult SetCaptureStateAdapter(SLuint32 id, SLuint32 state);
37     SLresult GetCaptureStateAdapter(SLuint32 id, SLuint32 *state);
38     SLresult EnqueueAdapter(SLuint32 id, const void *buffer, SLuint32 size);
39     SLresult ClearAdapter(SLuint32 id);
40     SLresult GetStateAdapter(SLuint32 id, SLOHBufferQueueState *state);
41     SLresult GetBufferAdapter(SLuint32 id, SLuint8 **buffer, SLuint32 *size);
42     SLresult RegisterCallbackAdapter(SLOHBufferQueueItf itf, SlOHBufferQueueCallback callback, void *pContext);
43 
44 private:
45     AudioCapturerAdapter();
46     ~AudioCapturerAdapter();
47     std::map<SLuint32, AudioCapturer*> captureMap_;
48     std::shared_ptr<ReadOrWriteCallbackAdapter> callbackPtr_;
49     std::map<SLuint32, std::shared_ptr<ReadOrWriteCallbackAdapter>> callbackMap_;
50 
51     void ConvertPcmFormat(SLDataFormat_PCM *slFormat, AudioCapturerParams *capturerParams);
52     AudioSampleFormat SlToOhosSampelFormat(SLDataFormat_PCM *pcmFormat);
53     AudioSamplingRate SlToOhosSamplingRate(SLDataFormat_PCM *pcmFormat);
54     AudioChannel SlToOhosChannel(SLDataFormat_PCM *pcmFormat);
55 };
56 }
57 }
58 #endif