1 /* 2 * Copyright (c) 2025 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 REMOTE_AUDIO_RENDER_SINK_H 17 #define REMOTE_AUDIO_RENDER_SINK_H 18 19 #include "sink/i_audio_render_sink.h" 20 #include <iostream> 21 #include <cstring> 22 #include <unordered_map> 23 #include <v1_0/iaudio_manager.h> 24 #include <thread> 25 #include <shared_mutex> 26 #include "adapter/i_device_manager.h" 27 #include "util/callback_wrapper.h" 28 29 namespace OHOS { 30 namespace AudioStandard { 31 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioCategory RemoteAudioCategory; 32 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioRender RemoteIAudioRender; 33 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioFormat RemoteAudioFormat; 34 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioSampleAttributes RemoteAudioSampleAttributes; 35 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioDeviceDescriptor RemoteAudioDeviceDescriptor; 36 37 class RemoteAudioRenderSink : public IAudioRenderSink, public IDeviceManagerCallback { 38 public: 39 struct RenderWrapper { 40 uint32_t hdiRenderId_ = 0; 41 sptr<RemoteIAudioRender> audioRender_ = nullptr; 42 FILE *dumpFile_ = nullptr; 43 std::string dumpFileName_ = ""; 44 }; 45 46 public: 47 explicit RemoteAudioRenderSink(const std::string &deviceNetworkId); 48 ~RemoteAudioRenderSink(); 49 50 int32_t Init(const IAudioSinkAttr &attr) override; 51 void DeInit(void) override; 52 bool IsInited(void) override; 53 54 int32_t Start(void) override; 55 int32_t Stop(void) override; 56 int32_t Resume(void) override; 57 int32_t Pause(void) override; 58 int32_t Flush(void) override; 59 int32_t Reset(void) override; 60 int32_t RenderFrame(char &data, uint64_t len, uint64_t &writeLen) override; 61 int64_t GetVolumeDataCount() override; 62 63 int32_t SuspendRenderSink(void) override; 64 int32_t RestoreRenderSink(void) override; 65 66 void SetAudioParameter(const AudioParamKey key, const std::string &condition, const std::string &value) override; 67 std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override; 68 69 int32_t SetVolume(float left, float right) override; 70 int32_t GetVolume(float &left, float &right) override; 71 72 int32_t GetLatency(uint32_t &latency) override; 73 int32_t GetTransactionId(uint64_t &transactionId) override; 74 int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override; 75 float GetMaxAmplitude(void) override; 76 void SetAudioMonoState(bool audioMono) override; 77 void SetAudioBalanceValue(float audioBalance) override; 78 79 int32_t SetAudioScene(AudioScene audioScene, bool scoExcludeFlag = false) override; 80 int32_t GetAudioScene(void) override; 81 82 int32_t UpdateActiveDevice(std::vector<DeviceType> &outputDevices) override; 83 void RegistCallback(uint32_t type, IAudioSinkCallback *callback) override; 84 void ResetActiveDeviceForDisconnect(DeviceType device) override; 85 86 int32_t SetPaPower(int32_t flag) override; 87 int32_t SetPriPaPower(void) override; 88 89 int32_t UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS], const size_t size) final; 90 int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final; 91 92 int32_t SplitRenderFrame(char &data, uint64_t len, uint64_t &writeLen, const char *streamType) override; 93 94 void DumpInfo(std::string &dumpString) override; 95 96 void OnAudioParamChange(const std::string &adapterName, const AudioParamKey key, const std::string &condition, 97 const std::string &value) override; 98 99 void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override; 100 101 private: 102 static RemoteAudioFormat ConvertToHdiFormat(AudioSampleFormat format); 103 static RemoteAudioCategory GetAudioCategory(AudioScene audioScene); 104 void InitSplitStream(const char *splitStreamStr, std::vector<RemoteAudioCategory> &splitStreamVector); 105 void InitAudioSampleAttr(RemoteAudioSampleAttributes ¶m, RemoteAudioCategory type); 106 void InitDeviceDesc(RemoteAudioDeviceDescriptor &deviceDesc); 107 int32_t CreateRender(RemoteAudioCategory type); 108 int32_t DoSetOutputRoute(void); 109 void CheckUpdateState(char *data, uint64_t len); 110 int32_t RenderFrame(char &data, uint64_t len, uint64_t &writeLen, RemoteAudioCategory type); 111 112 void JoinStartThread(); 113 114 private: 115 static constexpr uint32_t AUDIO_CHANNELCOUNT = 2; 116 static constexpr uint32_t AUDIO_SAMPLE_RATE_48K = 48000; 117 static constexpr uint32_t DEEP_BUFFER_RENDER_PERIOD_SIZE = 4096; 118 static constexpr float DEFAULT_VOLUME_LEVEL = 1.0f; 119 static constexpr uint16_t GET_MAX_AMPLITUDE_FRAMES_THRESHOLD = 10; 120 static constexpr int32_t HALF_FACTOR = 2; 121 static constexpr const char *MEDIA_STREAM_TYPE = "1"; 122 static constexpr const char *COMMUNICATION_STREAM_TYPE = "2"; 123 static constexpr const char *NAVIGATION_STREAM_TYPE = "13"; 124 static constexpr const char *DUMP_REMOTE_RENDER_SINK_FILENAME = "dump_remote_audiosink"; 125 static const std::unordered_map<std::string, RemoteAudioCategory> SPLIT_STREAM_MAP; 126 127 const std::string deviceNetworkId_ = ""; 128 IAudioSinkAttr attr_ = {}; 129 SinkCallbackWrapper callback_ = {}; 130 std::atomic<bool> sinkInited_ = false; 131 std::atomic<bool> renderInited_ = false; 132 std::atomic<bool> isThreadRunning_ = false; 133 std::atomic<bool> started_ = false; 134 std::atomic<bool> paused_ = false; 135 136 std::shared_ptr<std::thread> startThread_ = nullptr; 137 138 float leftVolume_ = DEFAULT_VOLUME_LEVEL; 139 float rightVolume_ = DEFAULT_VOLUME_LEVEL; 140 std::mutex sinkMutex_; 141 std::shared_mutex renderWrapperMutex_; 142 std::mutex createRenderMutex_; 143 std::mutex threadMutex_; 144 std::unordered_map<RemoteAudioCategory, struct RenderWrapper> audioRenderWrapperMap_; 145 // for get amplitude 146 float maxAmplitude_ = 0; 147 int64_t lastGetMaxAmplitudeTime_ = 0; 148 int64_t last10FrameStartTime_ = 0; 149 bool startUpdate_ = false; 150 int renderFrameNum_ = 0; 151 // for dfx log 152 std::string logUtilsTag_ = "RemoteSink"; 153 mutable int64_t volumeDataCount_ = 0; 154 }; 155 156 } // namespace AudioStandard 157 } // namespace OHOS 158 159 #endif // REMOTE_AUDIO_RENDER_SINK_H 160