• 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_AUDIO_CAPTURE_SOURCE_H
17 #define REMOTE_AUDIO_CAPTURE_SOURCE_H
18 
19 #include "source/i_audio_capture_source.h"
20 #include <iostream>
21 #include <cstring>
22 #include <v1_0/iaudio_manager.h>
23 #include "adapter/i_device_manager.h"
24 #include "util/callback_wrapper.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioCategory RemoteAudioCategory;
29 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioCapture RemoteIAudioCapture;
30 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioFormat RemoteAudioFormat;
31 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioSampleAttributes RemoteAudioSampleAttributes;
32 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioDeviceDescriptor RemoteAudioDeviceDescriptor;
33 
34 class RemoteAudioCaptureSource : public IAudioCaptureSource, public IDeviceManagerCallback {
35 public:
36     RemoteAudioCaptureSource(const std::string &deviceNetworkId);
37     ~RemoteAudioCaptureSource();
38 
39     int32_t Init(const IAudioSourceAttr &attr) override;
40     void DeInit(void) override;
41     bool IsInited(void) override;
42 
43     int32_t Start(void) override;
44     int32_t Stop(void) override;
45     int32_t Resume(void) override;
46     int32_t Pause(void) override;
47     int32_t Flush(void) override;
48     int32_t Reset(void) override;
49     int32_t CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes) override;
50     int32_t CaptureFrameWithEc(FrameDesc *fdesc, uint64_t &replyBytes, FrameDesc *fdescEc,
51         uint64_t &replyBytesEc) override;
52 
53     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
54 
55     int32_t SetVolume(float left, float right) override;
56     int32_t GetVolume(float &left, float &right) override;
57     int32_t SetMute(bool isMute) override;
58     int32_t GetMute(bool &isMute) override;
59 
60     uint64_t GetTransactionId(void) override;
61     int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
62     float GetMaxAmplitude(void) override;
63 
64     int32_t SetAudioScene(AudioScene audioScene, bool scoExcludeFlag = false) override;
65 
66     int32_t UpdateActiveDevice(DeviceType inputDevice) override;
67     void RegistCallback(uint32_t type, IAudioSourceCallback *callback) override;
68 
69     int32_t UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE], const size_t size) final;
70     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
71 
72     void DumpInfo(std::string &dumpString) override;
73 
74     void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override;
75 
76     void OnAudioParamChange(const std::string &adapterName, const AudioParamKey key, const std::string &condition,
77         const std::string &value) override;
78 
79 private:
80     static RemoteAudioFormat ConvertToHdiFormat(AudioSampleFormat format);
81     static RemoteAudioCategory GetAudioCategory(AudioScene audioScene);
82     void InitAudioSampleAttr(RemoteAudioSampleAttributes &param);
83     void InitDeviceDesc(RemoteAudioDeviceDescriptor &deviceDesc);
84     int32_t CreateCapture(void);
85     void DestroyCapture(void);
86     void CheckUpdateState(char *frame, size_t replyBytes);
87 
88 private:
89     static constexpr uint32_t DEEP_BUFFER_CAPTURE_PERIOD_SIZE = 4096;
90     static constexpr uint16_t GET_MAX_AMPLITUDE_FRAMES_THRESHOLD = 10;
91     static constexpr int32_t HALF_FACTOR = 2;
92     static constexpr uint32_t AUDIO_BUFFER_SIZE = 16 * 1024;
93     static constexpr const char *DUMP_REMOTE_CAPTURE_SOURCE_FILENAME = "dump_remote_audiosource.pcm";
94 
95     const std::string deviceNetworkId_ = "";
96     IAudioSourceAttr attr_ = {};
97     SourceCallbackWrapper callback_ = {};
98     std::atomic<bool> sourceInited_ = false;
99     std::atomic<bool> captureInited_ = false;
100     std::atomic<bool> started_ = false;
101     std::atomic<bool> paused_ = false;
102     uint32_t hdiCaptureId_ = 0;
103     std::mutex createCaptureMutex_;
104     sptr<RemoteIAudioCapture> audioCapture_ = nullptr;
105     // for get amplitude
106     float maxAmplitude_ = 0;
107     int64_t lastGetMaxAmplitudeTime_ = 0;
108     int64_t last10FrameStartTime_ = 0;
109     bool startUpdate_ = false;
110     int captureFrameNum_ = 0;
111     FILE *dumpFile_ = nullptr;
112     std::string dumpFileName_ = "";
113     bool muteState_ = false;
114 };
115 
116 } // namespace AudioStandard
117 } // namespace OHOS
118 
119 #endif // REMOTE_AUDIO_CAPTURE_SOURCE_H
120