• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "player_dfx_writer.h"
26 #include "recorder_dfx_writer.h"
27 #include "audio_schedule_guard.h"
28 #include "audio_stream_monitor.h"
29 #include "audio_stream_checker.h"
30 
31 namespace OHOS {
32 namespace AudioStandard {
33 class ProcessReleaseCallback {
34 public:
35     virtual ~ProcessReleaseCallback() = default;
36 
37     virtual int32_t OnProcessRelease(IAudioProcessStream *process, bool isSwitchStream = false) = 0;
38 };
39 class AudioProcessInServer;
40 class ProcessDeathRecipient : public IRemoteObject::DeathRecipient {
41 public:
42     ProcessDeathRecipient(AudioProcessInServer *processInServer, ProcessReleaseCallback *processHolder);
43     virtual ~ProcessDeathRecipient() = default;
44     // overridde for DeathRecipient
45     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
46 private:
47     ProcessReleaseCallback *processHolder_ = nullptr;
48     AudioProcessInServer *processInServer_ = nullptr;
49     int64_t createTime_ = 0;
50 };
51 
52 class AudioProcessInServer : public AudioProcessStub, public IAudioProcessStream {
53 public:
54     static sptr<AudioProcessInServer> Create(const AudioProcessConfig &processConfig,
55         ProcessReleaseCallback *releaseCallback);
56     virtual ~AudioProcessInServer();
57 
58     // override for AudioProcess
59     int32_t ResolveBufferBaseAndGetServerSpanSize(std::shared_ptr<OHAudioBufferBase> &buffer,
60         uint32_t &spanSizeInFrame) override;
61 
62     int32_t GetSessionId(uint32_t &sessionId) override;
63 
64     int32_t Start() override;
65 
66     int32_t Pause(bool isFlush) override;
67 
68     int32_t Resume() override;
69 
70     int32_t Stop(int32_t stage) override;
71 
72     int32_t RequestHandleInfo() override;
73 
74     int32_t RequestHandleInfoAsync() override;
75 
76     int32_t Release(bool isSwitchStream) override;
77 
78     int32_t SetDefaultOutputDevice(int32_t defaultOutputDevice, bool skipForce = false) override;
79 
80     int32_t SetSilentModeAndMixWithOthers(bool on) override;
81 
82     int32_t SetSourceDuration(int64_t duration) override;
83 
84     int32_t SetUnderrunCount(uint32_t underrunCnt) override;
85 
86     int32_t SaveAdjustStreamVolumeInfo(float volume, uint32_t sessionId, const std::string& adjustTime,
87         uint32_t code) override;
88 
89     int32_t RegisterProcessCb(const sptr<IRemoteObject>& object) override;
90 
91     int32_t RegisterThreadPriority(int32_t tid, const std::string &bundleName,
92         uint32_t method) override;
93 
94     int32_t SetAudioHapticsSyncId(int32_t audioHapticsSyncId) override;
95     int32_t GetAudioHapticsSyncId() override;
96 
97     // override for IAudioProcessStream, used in endpoint
98     std::shared_ptr<OHAudioBufferBase> GetStreamBuffer() override;
99     AudioStreamInfo GetStreamInfo() override;
100     uint32_t GetAudioSessionId() override;
101     AudioStreamType GetAudioStreamType() override;
102     AudioProcessConfig GetAudioProcessConfig() override;
103     void EnableStandby() override;
104 
105     int Dump(int fd, const std::vector<std::u16string> &args) override;
106     void Dump(std::string &dumpString);
107 
108     int32_t ConfigProcessBuffer(uint32_t &totalSizeInframe, uint32_t &spanSizeInframe,
109         AudioStreamInfo &serverStreamInfo, const std::shared_ptr<OHAudioBufferBase> &endpoint = nullptr);
110 
111     int32_t AddProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener);
112     int32_t RemoveProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener);
113 
114     void SetNonInterruptMute(const bool muteFlag);
115     bool GetMuteState() override;
116     uint32_t GetSessionId();
117     int32_t GetStandbyStatus(bool &isStandby, int64_t &enterStandbyTime);
118 
119     // for inner-cap
120     void SetInnerCapState(bool isInnerCapped, int32_t innerCapId) override;
121     bool GetInnerCapState(int32_t innerCapId) override;
122     std::unordered_map<int32_t, bool> GetInnerCapState() override;
123 
124     AppInfo GetAppInfo() override final;
125     BufferDesc &GetConvertedBuffer() override;
126 
127     void WriteDumpFile(void *buffer, size_t bufferSize) override final;
128 
129     std::time_t GetStartMuteTime() override;
130     void SetStartMuteTime(std::time_t time) override;
131 
132     bool GetSilentState() override;
133     void SetSilentState(bool state) override;
134     void AddMuteWriteFrameCnt(int64_t muteFrameCnt) override;
135     void AddMuteFrameSize(int64_t muteFrameCnt) override;
136     void AddNormalFrameSize() override;
137     void AddNoDataFrameSize() override;
138     StreamStatus GetStreamStatus() override;
139     RestoreStatus RestoreSession(RestoreInfo restoreInfo);
140     int32_t StopSession();
141 
142     bool TurnOnMicIndicator(CapturerState capturerState);
143     bool TurnOffMicIndicator(CapturerState capturerState);
144 
145     uint32_t GetSpanSizeInFrame() override;
146     uint32_t GetByteSizePerFrame() override;
147 public:
148     const AudioProcessConfig processConfig_;
149 
150 private:
151     int32_t StartInner();
152     int64_t GetLastAudioDuration();
153     AudioProcessInServer(const AudioProcessConfig &processConfig, ProcessReleaseCallback *releaseCallback);
154     int32_t InitBufferStatus();
155     bool CheckBGCapturer();
156     void WriterRenderStreamStandbySysEvent(uint32_t sessionId, int32_t standby);
157     void ReportDataToResSched(std::unordered_map<std::string, std::string> payload, uint32_t type);
158 
159 private:
160     std::atomic<bool> muteFlag_ = false;
161     std::atomic<bool> silentModeAndMixWithOthers_ = false;
162     std::mutex innerCapStateMutex_;
163     std::unordered_map<int32_t, bool> innerCapStates_;
164     ProcessReleaseCallback *releaseCallback_ = nullptr;
165     sptr<IRemoteObject> object_ = nullptr;
166     sptr<ProcessDeathRecipient> deathRecipient_ = nullptr;
167 
168     bool needCheckBackground_ = false;
169     bool isMicIndicatorOn_ = false;
170 
171     uint32_t sessionId_ = 0;
172     bool isInited_ = false;
173     std::atomic<StreamStatus> *streamStatus_ = nullptr;
174     std::mutex statusLock_;
175 
176     uint32_t clientTid_ = 0;
177     std::string clientBundleName_;
178 
179     uint32_t totalSizeInframe_ = 0;
180     uint32_t spanSizeInframe_ = 0;
181     uint32_t byteSizePerFrame_ = 0;
182     bool isBufferConfiged_ = false;
183     std::shared_ptr<OHAudioBufferBase> processBuffer_ = nullptr;
184     std::mutex listenerListLock_;
185     std::vector<std::shared_ptr<IProcessStatusListener>> listenerList_;
186     BufferDesc convertedBuffer_ = {};
187     std::string dumpFileName_;
188     FILE *dumpFile_ = nullptr;
189     int64_t enterStandbyTime_ = 0;
190     std::time_t startMuteTime_ = 0;
191     bool isInSilentState_ = false;
192 
193     int64_t lastStartTime_{};
194     int64_t lastStopTime_{};
195     int64_t lastWriteFrame_{};
196     int64_t lastWriteMuteFrame_{};
197     std::atomic<uint32_t> underrunCount_ = 0;
198     int64_t sourceDuration_ = -1;
199     std::unique_ptr<PlayerDfxWriter> playerDfx_;
200     std::unique_ptr<RecorderDfxWriter> recorderDfx_;
201 
202     std::array<std::shared_ptr<SharedAudioScheduleGuard>, METHOD_MAX> scheduleGuards_ = {};
203     std::mutex scheduleGuardsMutex_;
204     std::shared_ptr<AudioStreamChecker> audioStreamChecker_ = nullptr;
205 
206     std::atomic<int32_t> audioHapticsSyncId_ = 0;
207 };
208 } // namespace AudioStandard
209 } // namespace OHOS
210 #endif // AUDIO_PROCESS_IN_SERVER_H
211