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 void EnableStandby() override; 78 79 int Dump(int fd, const std::vector<std::u16string> &args) override; 80 void Dump(std::string &dumpString); 81 82 int32_t ConfigProcessBuffer(uint32_t &totalSizeInframe, uint32_t &spanSizeInframe, 83 DeviceStreamInfo &serverStreamInfo, const std::shared_ptr<OHAudioBuffer> &endpoint = nullptr); 84 85 int32_t AddProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener); 86 int32_t RemoveProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener); 87 88 void SetNonInterruptMute(const bool muteFlag); 89 bool GetMuteState() override; 90 uint32_t GetSessionId(); 91 int32_t GetStandbyStatus(bool &isStandby, int64_t &enterStandbyTime); 92 93 // for inner-cap 94 void SetInnerCapState(bool isInnerCapped) override; 95 bool GetInnerCapState() override; 96 97 AppInfo GetAppInfo() override final; 98 BufferDesc &GetConvertedBuffer() override; 99 int32_t RegisterThreadPriority(uint32_t tid, const std::string &bundleName) override; 100 101 void WriteDumpFile(void *buffer, size_t bufferSize) override final; 102 103 int32_t SetSilentModeAndMixWithOthers(bool on) override; 104 105 public: 106 const AudioProcessConfig processConfig_; 107 108 private: 109 AudioProcessInServer(const AudioProcessConfig &processConfig, ProcessReleaseCallback *releaseCallback); 110 int32_t InitBufferStatus(); 111 void WriterRenderStreamStandbySysEvent(uint32_t sessionId, int32_t standby); 112 113 private: 114 std::atomic<bool> muteFlag_ = false; 115 std::atomic<bool> silentModeAndMixWithOthers_ = false; 116 bool isInnerCapped_ = false; 117 ProcessReleaseCallback *releaseCallback_ = nullptr; 118 119 bool needCheckBackground_ = false; 120 121 uint32_t sessionId_ = 0; 122 bool isInited_ = false; 123 std::atomic<StreamStatus> *streamStatus_ = nullptr; 124 std::mutex statusLock_; 125 126 uint32_t clientTid_ = 0; 127 std::string clientBundleName_; 128 bool clientThreadPriorityRequested_ = false; 129 130 uint32_t totalSizeInframe_ = 0; 131 uint32_t spanSizeInframe_ = 0; 132 uint32_t byteSizePerFrame_ = 0; 133 bool isBufferConfiged_ = false; 134 std::shared_ptr<OHAudioBuffer> processBuffer_ = nullptr; 135 std::mutex listenerListLock_; 136 std::vector<std::shared_ptr<IProcessStatusListener>> listenerList_; 137 BufferDesc convertedBuffer_ = {}; 138 std::string dumpFileName_; 139 FILE *dumpFile_ = nullptr; 140 int64_t enterStandbyTime_ = 0; 141 }; 142 } // namespace AudioStandard 143 } // namespace OHOS 144 #endif // AUDIO_PROCESS_IN_SERVER_H 145