• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 SCREEN_CAPTURE_AUDIO_CAPTURER_WRAPPER_H
17 #define SCREEN_CAPTURE_AUDIO_CAPTURER_WRAPPER_H
18 
19 #include <mutex>
20 #include <cstdlib>
21 #include <thread>
22 #include <string>
23 #include <map>
24 #include <memory>
25 #include <atomic>
26 #include <queue>
27 
28 #include "audio_capturer.h"
29 #include "screen_capture.h"
30 #include "securec.h"
31 
32 #define AUDIO_NS_PER_SECOND (static_cast<uint64_t>(1000000000))
33 
34 namespace OHOS {
35 namespace Media {
36 using namespace AudioStandard;
37 
38 const int64_t AUDIO_CAPTURE_READ_FAILED_WAIT_TIME = 20000000; // 20000000 us 20ms
39 
40 class AudioCapturerCallbackImpl : public AudioCapturerCallback {
41 public:
42     void OnInterrupt(const InterruptEvent &interruptEvent) override;
43     void OnStateChange(const CapturerState state) override;
44 };
45 
46 enum AudioCapturerWrapperState : int32_t {
47     CAPTURER_UNKNOWN = -1,
48     CAPTURER_RECORDING,
49     CAPTURER_PAUSED,
50     CAPTURER_STOPPING,
51     CAPTURER_STOPED,
52     CAPTURER_RELEASED,
53 };
54 
55 class AudioCapturerWrapper {
56 public:
AudioCapturerWrapper(AudioCaptureInfo & audioInfo,std::shared_ptr<ScreenCaptureCallBack> & screenCaptureCb,std::string && name,ScreenCaptureContentFilter filter)57     explicit AudioCapturerWrapper(AudioCaptureInfo &audioInfo,
58         std::shared_ptr<ScreenCaptureCallBack> &screenCaptureCb, std::string &&name,
59         ScreenCaptureContentFilter filter)
60         : screenCaptureCb_(screenCaptureCb), audioInfo_(audioInfo), threadName_(std::move(name)),
61         contentFilter_(filter) {}
62     virtual ~AudioCapturerWrapper();
63     int32_t Start(const OHOS::AudioStandard::AppInfo &appInfo);
64     int32_t Stop();
65     int32_t UpdateAudioCapturerConfig(ScreenCaptureContentFilter &filter);
66     int32_t CaptureAudio();
67     int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer);
68     int32_t GetBufferSize(size_t &size);
69     int32_t ReleaseAudioBuffer();
70     void SetIsInVoIPCall(bool isInVoIPCall);
71 #ifdef SUPPORT_CALL
72     void SetIsInTelCall(bool isInTelCall);
73 #endif
74     AudioCapturerWrapperState GetAudioCapturerState();
75     int32_t UseUpAllLeftBufferUntil(int64_t audioTime);
76     int32_t GetCurrentAudioTime(int64_t &currentAudioTime);
77     int32_t DropBufferUntil(int64_t audioTime);
78     int32_t AddBufferFrom(int64_t timeWindow, int64_t bufferSize, int64_t fromTime);
79     void SetIsMute(bool isMute);
80     bool IsRecording();
81     bool IsStop();
82 
83 protected:
84     virtual void OnStartFailed(ScreenCaptureErrorType errorType, int32_t errorCode);
85 
86 private:
87     std::shared_ptr<OHOS::AudioStandard::AudioCapturer> CreateAudioCapturer(
88         const OHOS::AudioStandard::AppInfo &appInfo);
89     void SetInnerStreamUsage(std::vector<OHOS::AudioStandard::StreamUsage> &usages);
90     void PartiallyPrintLog(int32_t lineNumber, std::string str);
91     int32_t RelativeSleep(int64_t nanoTime);
92     int32_t GetCaptureAudioBuffer(std::shared_ptr<AudioBuffer> audioBuffer, size_t bufferLen);
93 
94 protected:
95     std::shared_ptr<ScreenCaptureCallBack> screenCaptureCb_;
96 
97 private:
98     std::mutex mutex_;
99     AudioCaptureInfo audioInfo_;
100     std::string threadName_;
101     std::unique_ptr<std::thread> readAudioLoop_ = nullptr;
102     std::shared_ptr<OHOS::AudioStandard::AudioCapturer> audioCapturer_ = nullptr;
103     std::shared_ptr<OHOS::Media::AudioCapturerCallbackImpl> audioCaptureCallback_ = nullptr;
104     ScreenCaptureContentFilter contentFilter_;
105     OHOS::AudioStandard::AppInfo appInfo_;
106 
107     std::mutex bufferMutex_;
108     std::condition_variable bufferCond_;
109     std::deque<std::shared_ptr<AudioBuffer>> availBuffers_;
110     std::string bundleName_;
111     std::atomic<bool> isInVoIPCall_ = false;
112     std::atomic<bool> isMute_ = false;
113 #ifdef SUPPORT_CALL
114     std::atomic<bool> isInTelCall_ = false;
115 #endif
116     std::atomic<AudioCapturerWrapperState> captureState_ {CAPTURER_UNKNOWN};
117 
118     /* used for hilog output */
119     std::map<int32_t, int32_t> captureAudioLogCountMap_;
120 
121     static constexpr int64_t INNER_AUDIO_READ_TO_HEAR_TIME = 240000000; // 240ms
122     static constexpr uint32_t WRAPPER_PUSH_AUDIO_SAMPLE_INTERVAL_IN_US = 5000; // 5ms
123     static constexpr uint32_t MAX_THREAD_NAME_LENGTH = 15;
124     static constexpr uint32_t MAX_AUDIO_BUFFER_SIZE = 128;
125     static constexpr uint32_t SEC_TO_NANOSECOND = 1000000000; // 10^9ns
126     static constexpr uint32_t OPERATION_TIMEOUT_IN_MS = 5; // 5ms
127     static constexpr int32_t AC_LOG_SKIP_NUM = 1000;
128     static constexpr uint32_t STOP_WAIT_TIMEOUT_IN_MS = 500; // 500ms
129     static constexpr int64_t AUDIO_CAPTURE_READ_FRAME_TIME = 21333333; // 21333333 ns 21ms
130     static constexpr int32_t MAX_AUDIO_BUFFER_LEN = 10 * 1024 * 1024; // 10M
131 };
132 
133 class MicAudioCapturerWrapper : public AudioCapturerWrapper {
134 public:
MicAudioCapturerWrapper(AudioCaptureInfo & audioInfo,std::shared_ptr<ScreenCaptureCallBack> & screenCaptureCb,std::string && name,ScreenCaptureContentFilter filter)135     explicit MicAudioCapturerWrapper(AudioCaptureInfo &audioInfo,
136         std::shared_ptr<ScreenCaptureCallBack> &screenCaptureCb, std::string &&name,
137         ScreenCaptureContentFilter filter)
138         : AudioCapturerWrapper(audioInfo, screenCaptureCb, std::move(name),
139         filter) {}
~MicAudioCapturerWrapper()140     ~MicAudioCapturerWrapper() override {}
141 
142 protected:
143     void OnStartFailed(ScreenCaptureErrorType errorType, int32_t errorCode) override;
144 };
145 } // namespace Media
146 } // namespace OHOS
147 #endif // SCREEN_CAPTURE_AUDIO_CAPTURER_WRAPPER_H
148