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 AUDIO_PROCESS_IN_SERVER_H
17 #define AUDIO_PROCESS_IN_SERVER_H
18 
19 #include <mutex>
20 #include <sstream>
21 
22 #include "audio_process_stub.h"
23 #include "i_audio_process_stream.h"
24 #include "i_process_status_listener.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 class ProcessReleaseCallback {
29 public:
30     virtual ~ProcessReleaseCallback() = default;
31 
32     virtual int32_t OnProcessRelease(IAudioProcessStream *process, bool isSwitchStream = false) = 0;
33 };
34 class AudioProcessInServer;
35 class ProcessDeathRecipient : public IRemoteObject::DeathRecipient {
36 public:
37     ProcessDeathRecipient(AudioProcessInServer *processInServer, ProcessReleaseCallback *processHolder);
38     virtual ~ProcessDeathRecipient() = default;
39     // overridde for DeathRecipient
40     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
41 private:
42     ProcessReleaseCallback *processHolder_ = nullptr;
43     AudioProcessInServer *processInServer_ = nullptr;
44 };
45 
46 class AudioProcessInServer : public AudioProcessStub, public IAudioProcessStream {
47 public:
48     static sptr<AudioProcessInServer> Create(const AudioProcessConfig &processConfig,
49         ProcessReleaseCallback *releaseCallback);
50     virtual ~AudioProcessInServer();
51 
52     // override for AudioProcess
53     int32_t ResolveBuffer(std::shared_ptr<OHAudioBuffer> &buffer) override;
54 
55     int32_t GetSessionId(uint32_t &sessionId) override;
56 
57     int32_t Start() override;
58 
59     int32_t Pause(bool isFlush) override;
60 
61     int32_t Resume() override;
62 
63     int32_t Stop() override;
64 
65     int32_t RequestHandleInfo(bool isAsync) override;
66 
67     int32_t Release(bool isSwitchStream = false) override;
68 
69     int32_t RegisterProcessCb(sptr<IRemoteObject> object) override;
70 
71     // override for IAudioProcessStream, used in endpoint
72     std::shared_ptr<OHAudioBuffer> GetStreamBuffer() override;
73     AudioStreamInfo GetStreamInfo() override;
74     uint32_t GetAudioSessionId() override;
75     AudioStreamType GetAudioStreamType() override;
76     AudioProcessConfig GetAudioProcessConfig() override;
77 
78     int Dump(int fd, const std::vector<std::u16string> &args) override;
79     void Dump(std::string &dumpString);
80 
81     int32_t ConfigProcessBuffer(uint32_t &totalSizeInframe, uint32_t &spanSizeInframe,
82         DeviceStreamInfo &serverStreamInfo, const std::shared_ptr<OHAudioBuffer> &endpoint = nullptr);
83 
84     int32_t AddProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener);
85     int32_t RemoveProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener);
86 
87     void SetNonInterruptMute(const bool muteFlag);
88     bool GetMuteState() override;
89     uint32_t GetSessionId();
90 
91     // for inner-cap
92     void SetInnerCapState(bool isInnerCapped) override;
93     bool GetInnerCapState() override;
94 
95     AppInfo GetAppInfo() override final;
96     BufferDesc &GetConvertedBuffer() override;
97     int32_t RegisterThreadPriority(uint32_t tid, const std::string &bundleName) override;
98 
99     void WriteDumpFile(void *buffer, size_t bufferSize) override final;
100 
101     int32_t SetSilentModeAndMixWithOthers(bool on) override;
102 
103 public:
104     const AudioProcessConfig processConfig_;
105 
106 private:
107     AudioProcessInServer(const AudioProcessConfig &processConfig, ProcessReleaseCallback *releaseCallback);
108     int32_t InitBufferStatus();
109     void WriterRenderStreamStandbySysEvent(uint32_t sessionId, int32_t standby);
110 
111 private:
112     std::atomic<bool> muteFlag_ = false;
113     std::atomic<bool> silentModeAndMixWithOthers_ = false;
114     bool isInnerCapped_ = false;
115     ProcessReleaseCallback *releaseCallback_ = nullptr;
116 
117     bool needCheckBackground_ = false;
118 
119     uint32_t sessionId_ = 0;
120     bool isInited_ = false;
121     std::atomic<StreamStatus> *streamStatus_ = nullptr;
122     std::mutex statusLock_;
123 
124     uint32_t clientTid_ = 0;
125     std::string clientBundleName_;
126     bool clientThreadPriorityRequested_ = false;
127 
128     uint32_t totalSizeInframe_ = 0;
129     uint32_t spanSizeInframe_ = 0;
130     uint32_t byteSizePerFrame_ = 0;
131     bool isBufferConfiged_ = false;
132     std::shared_ptr<OHAudioBuffer> processBuffer_ = nullptr;
133     std::mutex listenerListLock_;
134     std::vector<std::shared_ptr<IProcessStatusListener>> listenerList_;
135     BufferDesc convertedBuffer_ = {};
136     std::string dumpFileName_;
137     FILE *dumpFile_ = nullptr;
138 };
139 } // namespace AudioStandard
140 } // namespace OHOS
141 #endif // AUDIO_PROCESS_IN_SERVER_H
142