1 /*
2  * Copyright (c) 2023 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 
25 namespace OHOS {
26 namespace AudioStandard {
27 class CapturerInServer : public IStatusCallback, public IReadCallback,
28     public std::enable_shared_from_this<CapturerInServer> {
29 public:
30     CapturerInServer(AudioProcessConfig processConfig, std::weak_ptr<IStreamListener> streamListener);
31     virtual ~CapturerInServer();
32     void OnStatusUpdate(IOperation operation) override;
33     int32_t OnReadData(size_t length) override;
34 
35     int32_t ResolveBuffer(std::shared_ptr<OHAudioBuffer> &buffer);
36     int32_t GetSessionId(uint32_t &sessionId);
37     int32_t Start();
38     int32_t Pause();
39     int32_t Flush();
40     int32_t Stop();
41     int32_t Release();
42 
43     int32_t GetAudioTime(uint64_t &framePos, uint64_t &timestamp);
44     int32_t GetLatency(uint64_t &latency);
45 
46     int32_t Init();
47 
48     int32_t ConfigServerBuffer();
49     int32_t InitBufferStatus();
50     int32_t UpdateReadIndex();
51     BufferDesc DequeueBuffer(size_t length);
52     void ReadData(size_t length);
53     int32_t DrainAudioBuffer();
54 
55     // for inner-cap.
56     int32_t UpdatePlaybackCaptureConfig(const AudioPlaybackCaptureConfig &config);
57     int32_t UpdatePlaybackCaptureConfigInLegacy(const AudioPlaybackCaptureConfig &config);
58     void SetNonInterruptMute(const bool muteFlag);
59 
60 private:
61     int32_t InitCacheBuffer(size_t targetSize);
62     bool IsReadDataOverFlow(size_t length, uint64_t currentWriteFrame,
63         std::shared_ptr<IStreamListener> stateListener);
64 
65     std::mutex statusLock_;
66     std::condition_variable statusCv_;
67     std::shared_ptr<ICapturerStream> stream_ = nullptr;
68     uint32_t streamIndex_ = -1;
69     IOperation operation_ = OPERATION_INVALID;
70     IStatus status_ = I_STATUS_IDLE;
71 
72     bool needCheckBackground_ = false;
73 
74     AudioPlaybackCaptureConfig filterConfig_;
75     std::weak_ptr<IStreamListener> streamListener_;
76     AudioProcessConfig processConfig_;
77     size_t totalSizeInFrame_ = 0;
78     size_t spanSizeInFrame_ = 0;
79     size_t byteSizePerFrame_ = 0;
80     size_t spanSizeInBytes_ = 0;
81     bool isBufferConfiged_  = false;
82     std::atomic<bool> isInited_ = false;
83     std::shared_ptr<OHAudioBuffer> audioServerBuffer_ = nullptr;
84     int32_t needStart = 0;
85     int32_t underflowCount = 0;
86     bool resetTime_ = false;
87     uint64_t resetTimestamp_ = 0;
88     uint32_t overFlowLogFlag_ = 0;
89     std::unique_ptr<AudioRingCache> ringCache_ = nullptr;
90     size_t cacheSizeInBytes_ = 0;
91     std::unique_ptr<uint8_t []> dischargeBuffer_ = nullptr;
92     FILE *dumpS2C_ = nullptr; // server to client dump file
93     std::string dumpFileName_ = "";
94     std::atomic<bool> muteFlag_ = false;
95 };
96 } // namespace AudioStandard
97 } // namespace OHOS
98 #endif // CAPTURER_IN_SERVER_H
99