1 /*
2  * Copyright (c) 2021-2022 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 FRAMEWORKS_AUDIO_CAPTURER_CLIENT_H
17 #define FRAMEWORKS_AUDIO_CAPTURER_CLIENT_H
18 
19 #include <mutex>
20 #include "audio_capturer.h"
21 #include "iproxy_client.h"
22 #include "media_info.h"
23 #include "surface.h"
24 
25 using OHOS::Surface;
26 using std::mutex;
27 using OHOS::SurfaceBuffer;
28 using OHOS::IBufferConsumerListener;
29 using OHOS::Audio::Timestamp;
30 using OHOS::Audio::AudioCapturerInfo;
31 
32 namespace OHOS {
33 namespace Audio {
34 class AudioCapturer::AudioCapturerClient : public IBufferConsumerListener {
35 public:
36     AudioCapturerClient();
37     ~AudioCapturerClient();
38     static bool GetMinFrameCount(int32_t sampleRate, int32_t channelCount, AudioCodecFormat audioFormat,
39                                 size_t &frameCount);
40     uint64_t GetFrameCount();
41     int32_t SetCapturerInfo(const AudioCapturerInfo info);
42     int32_t GetCapturerInfo(AudioCapturerInfo &info);
43     bool Start();
44     int32_t Read(uint8_t *buffer, size_t userSize, bool isBlockingRead);
45     State GetStatus();
46     bool GetAudioTime(Timestamp &timestamp, Timestamp::Timebase base);
47     bool Stop();
48     bool Release();
49     void OnBufferAvailable() override;
50     static AudioCapturerClient *GetInstance();
51 
52 private:
53     bool InitAudioCapturerClient();
54     IClientProxy *GetIClientProxy();
55     int32_t InitSurface(void);
56     int32_t DeleteSurface(void);
57     void ReleaseAllBuffer();
58 
59     SvcIdentity sid_;
60     IClientProxy *proxy_ = nullptr;
61     std::shared_ptr<Surface> surface_;
62     std::mutex lock_;
63     Timestamp curTimestamp_;
64     bool timeStampValid_ = false;
65 };
66 }  // namespace Audio
67 }  // namespace OHOS
68 
69 #endif // FRAMEWORKS_AUDIO_CAPTURER_CLIENT_H
70