• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 CAPTURER_IN_SERVER_H
17 #define CAPTURER_IN_SERVER_H
18 
19 #include <mutex>
20 #include "i_capturer_stream.h"
21 #include "i_stream_listener.h"
22 #include "oh_audio_buffer.h"
23 #include "audio_ring_cache.h"
24 #include "recorder_dfx_writer.h"
25 #include "capturer_clock_manager.h"
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 class CapturerInServer : public IStatusCallback, public IReadCallback,
30     public std::enable_shared_from_this<CapturerInServer> {
31 public:
32     CapturerInServer(AudioProcessConfig processConfig, std::weak_ptr<IStreamListener> streamListener);
33     virtual ~CapturerInServer();
34     void OnStatusUpdate(IOperation operation) override;
35     int32_t OnReadData(size_t length) override;
36     int32_t OnReadData(int8_t *outputData, size_t requestDataLen) override;
37 
38     int32_t ResolveBuffer(std::shared_ptr<OHAudioBuffer> &buffer);
39     int32_t ResolveBufferBaseAndGetServerSpanSize(std::shared_ptr<OHAudioBufferBase> &buffer,
40         uint32_t &spanSizeInFrame, uint64_t &engineTotalSizeInFrame);
41     int32_t GetSessionId(uint32_t &sessionId);
42     int32_t Start();
43     int32_t Pause();
44     int32_t Flush();
45     int32_t Stop();
46     int32_t Release(bool isSwitchStream = false);
47 
48     int32_t GetAudioTime(uint64_t &framePos, uint64_t &timestamp);
49     int32_t GetLatency(uint64_t &latency);
50 
51     int32_t Init();
52 
53     int32_t ConfigServerBuffer();
54     int32_t InitBufferStatus();
55     int32_t UpdateReadIndex();
56     BufferDesc DequeueBuffer(size_t length);
57     void ReadData(size_t length);
58     int32_t DrainAudioBuffer();
59 #ifdef HAS_FEATURE_INNERCAPTURER
60     // for inner-cap.
61     int32_t UpdatePlaybackCaptureConfig(const AudioPlaybackCaptureConfig &config);
62     int32_t UpdatePlaybackCaptureConfigInLegacy(const AudioPlaybackCaptureConfig &config);
63 #endif
64     void SetNonInterruptMute(const bool muteFlag);
65     RestoreStatus RestoreSession(RestoreInfo restoreInfo);
66     int32_t StopSession();
67 
68     bool TurnOnMicIndicator(CapturerState capturerState);
69     bool TurnOffMicIndicator(CapturerState capturerState);
70 
71 private:
72     bool CheckBGCapture();
73     int32_t InitCacheBuffer(size_t targetSize);
74     bool IsReadDataOverFlow(size_t length, uint64_t currentWriteFrame,
75         std::shared_ptr<IStreamListener> stateListener);
76     int32_t StartInner();
77     int64_t GetLastAudioDuration();
78     void HandleOperationFlushed();
79     void HandleOperationStopped(CapturerStage stage);
80     void UpdateBufferTimeStamp(size_t readLen);
81     inline void CaptureConcurrentCheck(uint32_t streamIndex);
82 
83     std::mutex statusLock_;
84     std::condition_variable statusCv_;
85     std::shared_ptr<ICapturerStream> stream_ = nullptr;
86     uint32_t streamIndex_ = -1;
87     IOperation operation_ = OPERATION_INVALID;
88     std::atomic<IStatus> status_ = I_STATUS_IDLE;
89 
90     bool needCheckBackground_ = false;
91     bool isMicIndicatorOn_ = false;
92 
93     AudioPlaybackCaptureConfig filterConfig_;
94     std::weak_ptr<IStreamListener> streamListener_;
95     AudioProcessConfig processConfig_;
96     size_t totalSizeInFrame_ = 0;
97     size_t spanSizeInFrame_ = 0;
98     size_t byteSizePerFrame_ = 0;
99     size_t spanSizeInBytes_ = 0;
100     bool isBufferConfiged_  = false;
101     std::atomic<bool> isInited_ = false;
102     std::shared_ptr<OHAudioBuffer> audioServerBuffer_ = nullptr;
103     int32_t needStart = 0;
104     int32_t underflowCount = 0;
105     bool resetTime_ = false;
106     uint64_t resetTimestamp_ = 0;
107     uint32_t overFlowLogFlag_ = 0;
108     std::unique_ptr<AudioRingCache> ringCache_ = nullptr;
109     size_t cacheSizeInBytes_ = 0;
110     std::unique_ptr<uint8_t []> dischargeBuffer_ = nullptr;
111     FILE *dumpS2C_ = nullptr; // server to client dump file
112     std::string dumpFileName_ = "";
113     std::atomic<bool> muteFlag_ = false;
114     std::string traceTag_ = "";
115     mutable int64_t volumeDataCount_ = 0;
116     int32_t innerCapId_ = 0;
117 
118     int64_t lastStartTime_{};
119     int64_t lastStopTime_{};
120     std::unique_ptr<RecorderDfxWriter> recorderDfx_;
121 
122     uint64_t curProcessPos_ = 0;
123     uint64_t lastPosInc_ = 0;
124     std::shared_ptr<CapturerClock> capturerClock_ = nullptr;
125 
126     std::atomic<IStatus> lastStatus_ = I_STATUS_IDLE;
127 };
128 } // namespace AudioStandard
129 } // namespace OHOS
130 #endif // CAPTURER_IN_SERVER_H
131