• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 REMOTE_FAST_AUDIO_CAPTURE_SOURCE_H
17 #define REMOTE_FAST_AUDIO_CAPTURE_SOURCE_H
18 
19 #include "source/i_audio_capture_source.h"
20 #include <iostream>
21 #include <cstring>
22 #include <sstream>
23 #include <v1_0/iaudio_manager.h>
24 #include "ashmem.h"
25 #include "adapter/i_device_manager.h"
26 #include "util/callback_wrapper.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioCategory RemoteAudioCategory;
31 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioCapture RemoteIAudioCapture;
32 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioFormat RemoteAudioFormat;
33 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioSampleAttributes RemoteAudioSampleAttributes;
34 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioDeviceDescriptor RemoteAudioDeviceDescriptor;
35 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioSceneDescriptor RemoteAudioSceneDescriptor;
36 
37 class RemoteFastAudioCaptureSource : public IAudioCaptureSource, public IDeviceManagerCallback {
38 public:
39     RemoteFastAudioCaptureSource(const std::string &deviceNetworkId);
40     ~RemoteFastAudioCaptureSource();
41 
42     int32_t Init(const IAudioSourceAttr &attr) override;
43     void DeInit(void) override;
44     bool IsInited(void) override;
45 
46     int32_t Start(void) override;
47     int32_t Stop(void) override;
48     int32_t Resume(void) override;
49     int32_t Pause(void) override;
50     int32_t Flush(void) override;
51     int32_t Reset(void) override;
52     int32_t CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes) override;
53     int32_t CaptureFrameWithEc(FrameDesc *fdesc, uint64_t &replyBytes, FrameDesc *fdescEc,
54         uint64_t &replyBytesEc) override;
55 
56     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
57 
58     int32_t SetVolume(float left, float right) override;
59     int32_t GetVolume(float &left, float &right) override;
60     int32_t SetMute(bool isMute) override;
61     int32_t GetMute(bool &isMute) override;
62 
63     uint64_t GetTransactionId(void) override;
64     int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
65     float GetMaxAmplitude(void) override;
66 
67     int32_t SetAudioScene(AudioScene audioScene, bool scoExcludeFlag = false) override;
68 
69     int32_t UpdateActiveDevice(DeviceType inputDevice) override;
70     void RegistCallback(uint32_t type, IAudioSourceCallback *callback) override;
71 
72     int32_t UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE], const size_t size) final;
73     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
74 
75     void DumpInfo(std::string &dumpString) override;
76 
77     void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override;
78 
79     void OnAudioParamChange(const std::string &adapterName, const AudioParamKey key, const std::string &condition,
80         const std::string &value) override;
81 
82 private:
83     int32_t GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe,
84         uint32_t &byteSizePerFrame, uint32_t &syncInfoSize) override;
85     int32_t GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
86 
87     static uint32_t PcmFormatToBit(AudioSampleFormat format);
88     static RemoteAudioFormat ConvertToHdiFormat(AudioSampleFormat format);
89     static RemoteAudioCategory GetAudioCategory(AudioScene audioScene);
PrintAttr(void)90     inline std::string PrintAttr(void)
91     {
92         std::stringstream attrStr;
93         attrStr << "adapterName: " << attr_.adapterName << ", openMicSpeaker: " << attr_.openMicSpeaker;
94         attrStr << ", format: " << static_cast<int32_t>(attr_.format) << ", sampleRate: " << attr_.sampleRate;
95         attrStr << ", channel: " << attr_.channel << ", volume: " << attr_.volume << ", filePath: " << attr_.filePath;
96         attrStr << ", deviceNetworkId: " << attr_.deviceNetworkId << ", deviceType: " << attr_.deviceType;
97         return attrStr.str();
98     }
99     void InitAudioSampleAttr(RemoteAudioSampleAttributes &param);
100     void InitDeviceDesc(RemoteAudioDeviceDescriptor &deviceDesc);
101     void InitSceneDesc(RemoteAudioSceneDescriptor &sceneDesc, AudioScene audioScene);
102     int32_t CreateCapture(void);
103 
104     // low latency
105     int32_t PrepareMmapBuffer(const RemoteAudioSampleAttributes &param);
106     int32_t CheckPositionTime(void);
107 
108 private:
109     static constexpr uint32_t DEEP_BUFFER_CAPTURE_PERIOD_SIZE = 3840;
110     static constexpr int32_t HALF_FACTOR = 2;
111     static constexpr int32_t MAX_GET_POSITION_TRY_COUNT = 10;
112     static constexpr int32_t MAX_GET_POSITION_HANDLE_TIME = 10000000; // 10000000us
113     static constexpr int32_t MAX_GET_POSITION_WAIT_TIME = 10000000; // 10ms
114     static constexpr int32_t INVALID_FD = -1;
115     static constexpr uint32_t CAPTURE_INTERLEAVED = 1;
116 
117     const std::string deviceNetworkId_ = "";
118     IAudioSourceAttr attr_ = {};
119     SourceCallbackWrapper callback_ = {};
120     std::atomic<bool> sourceInited_ = false;
121     std::atomic<bool> captureInited_ = false;
122     std::atomic<bool> started_ = false;
123     std::atomic<bool> paused_ = false;
124     float leftVolume_ = 0;
125     float rightVolume_ = 0;
126     uint32_t hdiCaptureId_ = 0;
127     sptr<RemoteIAudioCapture> audioCapture_ = nullptr;
128 #ifdef DEBUG_DIRECT_USE_HDI
129     FILE *dumpFile_ = nullptr;
130     std::string dumpFileName_ = "/data/local/tmp/remote_fast_audio_capture.pcm";
131 #endif
132     std::atomic<bool> muteState_ = false;
133 
134     // low latency
135     int32_t bufferFd_ = INVALID_FD;
136     uint32_t bufferTotalFrameSize_ = 0;
137     uint32_t eachReadFrameSize_ = 0;
138     uint32_t syncInfoSize_ = 0;
139 #ifdef DEBUG_DIRECT_USE_HDI
140     sptr<Ashmem> ashmemSource_ = nullptr;
141     size_t bufferSize_ = 0;
142 #endif
143 };
144 
145 } // namespace AudioStandard
146 } // namespace OHOS
147 
148 #endif // REMOTE_FAST_AUDIO_CAPTURE_SOURCE_H
149