• 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 WAKEUP_AUDIO_CAPTURE_SOURCE_H
17 #define WAKEUP_AUDIO_CAPTURE_SOURCE_H
18 
19 #include "source/i_audio_capture_source.h"
20 #include "source/audio_capture_source.h"
21 #include <iostream>
22 #include <cstring>
23 #include <mutex>
24 #include "v5_0/iaudio_manager.h"
25 #include "audio_hdi_log.h"
26 #include "audio_errors.h"
27 #include "util/callback_wrapper.h"
28 
29 namespace OHOS {
30 namespace AudioStandard {
31 class WakeupBuffer {
32 public:
33     explicit WakeupBuffer(IAudioCaptureSource *source);
34     ~WakeupBuffer() = default;
35 
36     int32_t Poll(char *frame, uint64_t requestBytes, uint64_t &replyBytes, uint64_t &curWritePos);
37 
38 private:
MemcpyAndCheck(void * dest,size_t destMax,const void * src,size_t count)39     static inline void MemcpyAndCheck(void *dest, size_t destMax, const void *src, size_t count)
40     {
41         CHECK_AND_RETURN_LOG(memcpy_s(dest, destMax, src, count) == EOK, "copy fail");
42     }
43     void Offer(const char *frame, const uint64_t bufferBytes);
44 
45 private:
46     static constexpr size_t MAX_BUFFER_SIZE = 32000; // 2 seconds
47 
48     size_t size_ = 0;
49     std::unique_ptr<char[]> buffer_ = nullptr;
50     IAudioCaptureSource *source_ = nullptr;
51     std::mutex mutex_;
52     uint64_t head_ = 0;
53     uint64_t headNum_ = 0;
54 };
55 
56 class WakeupAudioCaptureSource : public IAudioCaptureSource {
57 public:
58     WakeupAudioCaptureSource(const uint32_t captureId);
59     ~WakeupAudioCaptureSource() = default;
60 
61     int32_t Init(const IAudioSourceAttr &attr) override;
62     void DeInit(void) override;
63     bool IsInited(void) override;
64 
65     int32_t Start(void) override;
66     int32_t Stop(void) override;
67     int32_t Resume(void) override;
68     int32_t Pause(void) override;
69     int32_t Flush(void) override;
70     int32_t Reset(void) override;
71     int32_t CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes) override;
72     int32_t CaptureFrameWithEc(FrameDesc *fdesc, uint64_t &replyBytes, FrameDesc *fdescEc,
73         uint64_t &replyBytesEc) override;
74 
75     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
76 
77     int32_t SetVolume(float left, float right) override;
78     int32_t GetVolume(float &left, float &right) override;
79     int32_t SetMute(bool isMute) override;
80     int32_t GetMute(bool &isMute) override;
81 
82     uint64_t GetTransactionId(void) override;
83     int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
84     float GetMaxAmplitude(void) override;
85 
86     int32_t SetAudioScene(AudioScene audioScene, bool scoExcludeFlag = false) override;
87 
88     int32_t UpdateActiveDevice(DeviceType inputDevice) override;
89     void RegistCallback(uint32_t type, IAudioSourceCallback *callback) override;
90     void RegistCallback(uint32_t type, std::shared_ptr<IAudioSourceCallback> callback) override;
91 
92     int32_t UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE], const size_t size) final;
93     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
94 
95     void DumpInfo(std::string &dumpString) override;
96 
97     void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override;
98 
99 private:
100     AudioCaptureSource audioCaptureSource_;
101     static inline std::unique_ptr<WakeupBuffer> wakeupBuffer_ = nullptr;
102     static inline std::mutex wakeupMutex_;
103     static inline int sourceInitCount_ = 0;
104     static inline int startCount_ = 0;
105 
106     std::atomic<bool> sourceInited_ = false;
107     std::atomic<bool> started_ = false;
108     uint64_t curWritePos_ = 0;
109 };
110 
111 } // namespace AudioStandard
112 } // namespace OHOS
113 
114 #endif // WAKEUP_AUDIO_CAPTURE_SOURCE_H
115