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#ifndef AUDIO_CAPTURER_SERVER_H
16#define AUDIO_CAPTURER_SERVER_H
17
18#include <mutex>
19#include <pthread.h>
20#include <thread>
21#include "audio_capturer_impl.h"
22#include "ipc_skeleton.h"
23#include "serializer.h"
24#include "surface.h"
25
26using OHOS::Audio::AudioCapturerImpl;
27using OHOS::Surface;
28namespace OHOS {
29namespace Audio {
30enum AudioCapturerFuncId {
31    AUD_CAP_FUNC_CONNECT,
32    AUD_CAP_FUNC_DISCONNECT,
33    AUD_CAP_FUNC_GET_FRAME_COUNT,
34    AUD_CAP_FUNC_GET_STATUS,
35    AUD_CAP_FUNC_GET_TIME,
36    AUD_CAP_FUNC_SET_INFO,
37    AUD_CAP_FUNC_GET_INFO,
38    AUD_CAP_FUNC_START,
39    AUD_CAP_FUNC_STOP,
40    AUD_CAP_FUNC_RELEASE,
41    AUD_CAP_FUNC_GET_MIN_FRAME_COUNT,
42    AUD_CAP_FUNC_SET_SURFACE,
43    AUD_CAP_FUNC_BUTT,
44};
45
46static constexpr int32_t DEFAULT_IPC_SIZE = 100;
47#define AUDIO_CAPTURER_SERVICE_NAME "AudioCapServer"
48
49/* Since IPC is serialized, there is no concurrency problem */
50class AudioCapturerServer {
51public:
52    AudioCapturerServer() = default;
53    ~AudioCapturerServer() = default;
54
55    int32_t AudioCapturerServerInit();
56    static AudioCapturerServer *GetInstance();
57    AudioCapturerImpl *GetAudioCapturer(pid_t pid);
58    void AcceptServer(pid_t pid, IpcIo *reply);
59    void DropServer(pid_t pid, IpcIo *reply);
60    void Dispatch(int32_t funcId, pid_t pid, IpcIo *req, IpcIo *reply);
61    static void *ReadAudioDataProcess(void *serverStr);
62
63private:
64    void GetMinFrameCount(IpcIo *req, IpcIo *reply);
65    int32_t SetSurfaceProcess(Surface *surface);
66    void SetInfo(AudioCapturerImpl *capturer, IpcIo *req, IpcIo *reply);
67    void GetInfo(AudioCapturerImpl *capturer, IpcIo *reply);
68    void Start(AudioCapturerImpl *capturer, IpcIo *reply);
69    void Stop(AudioCapturerImpl *capturer, IpcIo *reply);
70    void GetMiniFrameCount(IpcIo *req, IpcIo *reply);
71    void GetFrameCount(AudioCapturerImpl *capturer, IpcIo *reply);
72    void GetStatus(AudioCapturerImpl *capturer, IpcIo *reply);
73    void SetSurface(IpcIo *req, IpcIo *reply);
74    SurfaceBuffer *GetCacheBuffer(void);
75    void CancelBuffer(SurfaceBuffer *buffer);
76    void FreeCacheBuffer(void);
77
78    pid_t clientPid_ = -1;
79    AudioCapturerImpl *capturer_ = nullptr;
80    Surface *surface_ = nullptr;
81    // std::thread dataThreadId_;
82    pthread_t dataThreadId_ = 0;
83    std::mutex lock_;
84    bool threadExit_ = false;
85    SurfaceBuffer *bufCache_ = nullptr;
86};
87
88void AudioCapturerServiceReg();
89}  // namespace Audio
90}  // namespace OHOS
91#endif // AUDIO_CAPTURER_SERVER_H
92