1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License") = 0;
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 I_AUDIO_CAPTURER_SOURCE_H
17 #define I_AUDIO_CAPTURER_SOURCE_H
18 
19 #include <cstdint>
20 
21 #include "audio_info.h"
22 #include "audio_hdiadapter_info.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 typedef struct IAudioSourceAttr {
27     const char *adapterName = NULL;
28     uint32_t openMicSpeaker = 0;
29     HdiAdapterFormat format = HdiAdapterFormat::INVALID_WIDTH;
30     uint32_t sampleRate = 0;
31     uint32_t channel = 0;
32     float volume = 0.0f;
33     uint32_t bufferSize = 0;
34     bool isBigEndian = false;
35     const char *filePath = NULL;
36     const char *deviceNetworkId = NULL;
37     int32_t deviceType = 0;
38     int32_t sourceType = 0;
39     uint64_t channelLayout = 0;
40     int32_t audioStreamFlag = 0;
41 } IAudioSourceAttr;
42 
43 class IAudioSourceCallback {
44 public:
45     virtual void OnWakeupClose() = 0;
46     virtual void OnAudioSourceParamChange(const std::string &netWorkId, const AudioParamKey key,
47         const std::string &condition, const std::string &value) = 0;
48 };
49 
50 class ICapturerStateCallback {
51 public:
52     virtual void OnCapturerState(bool isActive) = 0;
53     virtual ~ICapturerStateCallback() = default;
54 };
55 
56 class IAudioCapturerSource {
57 public:
58     static IAudioCapturerSource *GetInstance(const char *deviceClass, const char *deviceNetworkId,
59            const SourceType sourceType = SourceType::SOURCE_TYPE_MIC, const char *sourceName = "Built_in_wakeup");
60     static void GetAllInstance(std::vector<IAudioCapturerSource *> &allInstance);
61     virtual ~IAudioCapturerSource() = default;
62 
63     virtual int32_t Init(const IAudioSourceAttr &attr) = 0;
64     virtual bool IsInited(void) = 0;
65     virtual void DeInit(void) = 0;
66 
67     virtual int32_t Start(void) = 0;
68     virtual int32_t Stop(void) = 0;
69     virtual int32_t Flush(void) = 0;
70     virtual int32_t Reset(void) = 0;
71     virtual int32_t Pause(void) = 0;
72     virtual int32_t Resume(void) = 0;
73 
74     virtual int32_t CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes) = 0;
75 
76     virtual int32_t SetVolume(float left, float right) = 0;
77     virtual int32_t GetVolume(float &left, float &right) = 0;
78     virtual int32_t SetMute(bool isMute) = 0;
79     virtual int32_t GetMute(bool &isMute) = 0;
80     virtual int32_t SetAudioScene(AudioScene audioScene, DeviceType activeDevice) = 0;
81     virtual int32_t SetInputRoute(DeviceType deviceType) = 0;
82     virtual uint64_t GetTransactionId() = 0;
83     virtual int32_t GetPresentationPosition(uint64_t& frames, int64_t& timeSec, int64_t& timeNanoSec) = 0;
84     virtual std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) = 0;
85 
86     virtual void RegisterWakeupCloseCallback(IAudioSourceCallback *callback) = 0;
87     virtual void RegisterAudioCapturerSourceCallback(std::unique_ptr<ICapturerStateCallback> callback) = 0;
88     virtual void RegisterParameterCallback(IAudioSourceCallback *callback) = 0;
89     virtual float GetMaxAmplitude() = 0;
90 
91     virtual int32_t UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE],
92         const size_t size) = 0;
93     virtual int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) = 0;
94 
Preload(const std::string & usbInfoStr)95     virtual int32_t Preload(const std::string &usbInfoStr)
96     {
97         return 0;
98     }
99 
UpdateSourceType(SourceType souceType)100     virtual int32_t UpdateSourceType(SourceType souceType)
101     {
102         return 0;
103     }
104 };
105 
106 class IMmapAudioCapturerSource : public IAudioCapturerSource {
107 public:
108     IMmapAudioCapturerSource() = default;
109     virtual ~IMmapAudioCapturerSource() = default;
110     virtual int32_t GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe,
111         uint32_t &byteSizePerFrame) = 0;
112     virtual int32_t GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) = 0;
113 };
114 }  // namespace AudioStandard
115 }  // namespace OHOS
116 #endif  // AUDIO_CAPTURER_SOURCE_H
117