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 AUDIOPLAYER_ADAPTER_H
17 #define AUDIOPLAYER_ADAPTER_H
18 
19 #include <OpenSLES.h>
20 #include <OpenSLES_Platform.h>
21 #include <iostream>
22 #include <map>
23 #include <audio_renderer.h>
24 #include <audio_system_manager.h>
25 #include <readorwritecallback_adapter.h>
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 class AudioPlayerAdapter {
30 public:
31     static AudioPlayerAdapter* GetInstance();
32     AudioRenderer *GetAudioRenderById(SLuint32 id);
33     void EraseAudioRenderById(SLuint32 id);
34     SLresult CreateAudioPlayerAdapter
35         (SLuint32 id, SLDataSource *dataSource, SLDataSink *dataSink, AudioStreamType streamType);
36     SLresult SetPlayStateAdapter(SLuint32 id, SLuint32 state);
37     SLresult GetPlayStateAdapter(SLuint32 id, SLuint32 *state);
38     SLresult SetVolumeLevelAdapter(SLuint32 id, SLmillibel level);
39     SLresult GetVolumeLevelAdapter(SLuint32 id, SLmillibel *level);
40     SLresult GetMaxVolumeLevelAdapter(SLuint32 id, SLmillibel *level);
41     SLresult EnqueueAdapter(SLuint32 id, const void *buffer, SLuint32 size);
42     SLresult ClearAdapter(SLuint32 id);
43     SLresult GetStateAdapter(SLuint32 id, SLOHBufferQueueState *state);
44     SLresult GetBufferAdapter(SLuint32 id, SLuint8 **buffer, SLuint32 *size);
45     SLresult RegisterCallbackAdapter(SLOHBufferQueueItf itf, SlOHBufferQueueCallback callback, void *pContext);
46 
47 private:
48     AudioPlayerAdapter();
49     ~AudioPlayerAdapter();
50     const float MAGNIFICATION = 2000;
51     std::map<SLuint32, AudioRenderer*> renderMap_;
52     std::shared_ptr<ReadOrWriteCallbackAdapter> callbackPtr_;
53     std::map<SLuint32, std::shared_ptr<ReadOrWriteCallbackAdapter>> callbackMap_;
54 
55     void ConvertPcmFormat(SLDataFormat_PCM *slFormat, AudioRendererParams *rendererParams);
56     AudioSampleFormat SlToOhosSampelFormat(SLDataFormat_PCM *pcmFormat);
57     AudioSamplingRate SlToOhosSamplingRate(SLDataFormat_PCM *pcmFormat);
58     AudioChannel SlToOhosChannel(SLDataFormat_PCM *pcmFormat);
59 };
60 }  // namespace AudioStandard
61 }  // namespace OHOS
62 #endif // AUDIO_RENDERER_SINK_H
63