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