• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 AUDIO_CAPTURE_AS_IMPL_H
17 #define AUDIO_CAPTURE_AS_IMPL_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 #include "audio_capture.h"
25 #include "audio_capturer.h"
26 #include "nocopyable.h"
27 
28 namespace OHOS {
29 namespace Media {
30 struct AudioCacheCtrl {
31     std::mutex captureMutex_;
32     std::condition_variable captureCond_;
33     std::condition_variable pauseCond_;
34     std::queue<std::shared_ptr<AudioBuffer>> captureQueue_;
35     uint64_t lastTimeStamp_ = 0;
36     uint64_t pausedTime_ = 1; // the timestamp when audio pause called
37     uint64_t resumeTime_ = 0; // the timestamp when audio resume called
38     uint32_t pausedCount_ = 0; // the paused count times
39     uint64_t persistTime_ = 0;
40     uint64_t totalPauseTime_ = 0;
41 };
42 
43 class AudioCaptureAsImpl : public AudioCapture, public NoCopyable {
44 public:
45     AudioCaptureAsImpl();
46     virtual ~AudioCaptureAsImpl();
47 
48     int32_t SetCaptureParameter(uint32_t bitrate, uint32_t channels, uint32_t sampleRate,
49         const AppInfo &appInfo) override;
50     bool IsSupportedCaptureParameter(uint32_t bitrate, uint32_t channels, uint32_t sampleRate) override;
51     int32_t GetCaptureParameter(uint32_t &bitrate, uint32_t &channels, uint32_t &sampleRate) override;
52     int32_t GetSegmentInfo(uint64_t &start) override;
53     int32_t StartAudioCapture() override;
54     int32_t StopAudioCapture() override;
55     int32_t PauseAudioCapture() override;
56     int32_t ResumeAudioCapture() override;
57     std::shared_ptr<AudioBuffer> GetBuffer() override;
58     int32_t WakeUpAudioThreads() override;
59 
60     bool CheckAndGetCaptureParameter(uint32_t bitrate, uint32_t channels, uint32_t sampleRate,
61         AudioStandard::AudioCapturerParams &params);
62 
63 private:
64     std::unique_ptr<OHOS::AudioStandard::AudioCapturer> audioCapturer_ = nullptr;
65     size_t bufferSize_ = 0; // minimum size of each buffer acquired from AudioServer
66     uint64_t bufferDurationNs_ = 0; // each buffer
67 
68     // audio cache
69     enum AudioRecorderState : int32_t {
70         RECORDER_INITIALIZED = 0,
71         RECORDER_RUNNING,
72         RECORDER_PAUSED,
73         RECORDER_RESUME,
74         RECORDER_STOP,
75     };
76 
77     static constexpr int MAX_QUEUE_SIZE = 100;
78 
79     int32_t AudioCaptureLoop();
80     void GetAudioCaptureBuffer();
81     void EmptyCaptureQueue();
82     std::shared_ptr<AudioBuffer> GetSegmentData();
83     std::unique_ptr<AudioCacheCtrl> audioCacheCtrl_;
84     std::unique_ptr<std::thread> captureLoop_;
85     std::mutex pauseMutex_;
86     std::atomic<int32_t> curState_ = RECORDER_INITIALIZED;
87     std::atomic<bool> captureLoopErr_ { false };
88     uint64_t lastInputTime_ = 0;
89 };
90 } // namespace Media
91 } // namespace OHOS
92 
93 #endif // AUDIO_CAPTURE_AS_IMPL_H
94