1 /* 2 * Copyright (c) 2024 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 OHOS_AUDIO_RENDER_EXT_IMPL_H 17 #define OHOS_AUDIO_RENDER_EXT_IMPL_H 18 19 #include <mutex> 20 #include <string> 21 #include <cmath> 22 23 #include "ashmem.h" 24 #include "audio_render_interface_impl_base.h" 25 #include "daudio_utils.h" 26 27 #include <v1_0/audio_types.h> 28 #include <v1_0/iaudio_render.h> 29 #include <v2_1/id_audio_manager.h> 30 31 namespace OHOS { 32 namespace HDI { 33 namespace DistributedAudio { 34 namespace Audio { 35 namespace V1_0 { 36 using OHOS::HDI::DistributedAudio::Audioext::V2_1::AudioData; 37 using OHOS::HDI::DistributedAudio::Audioext::V2_1::AudioParameter; 38 using OHOS::HDI::DistributedAudio::Audioext::V2_1::CurrentTime; 39 using OHOS::HDI::DistributedAudio::Audioext::V2_1::DAudioEvent; 40 using OHOS::HDI::DistributedAudio::Audioext::V2_1::IDAudioCallback; 41 class AudioRenderExtImpl : public AudioRenderInterfaceImplBase { 42 public: 43 AudioRenderExtImpl(); 44 ~AudioRenderExtImpl() override; 45 46 int32_t GetLatency(uint32_t &ms) override; 47 int32_t RenderFrame(const std::vector<int8_t> &frame, uint64_t &replyBytes) override; 48 int32_t GetRenderPosition(uint64_t &frames, AudioTimeStamp &time) override; 49 int32_t SetRenderSpeed(float speed) override; 50 int32_t GetRenderSpeed(float &speed) override; 51 int32_t SetChannelMode(AudioChannelMode mode) override; 52 int32_t GetChannelMode(AudioChannelMode &mode) override; 53 int32_t RegCallback(const sptr<IAudioCallback> &audioCallback, int8_t cookie) override; 54 int32_t DrainBuffer(AudioDrainNotifyType &type) override; 55 int32_t IsSupportsDrain(bool &support) override; 56 int32_t CheckSceneCapability(const AudioSceneDescriptor &scene, bool &supported) override; 57 int32_t SelectScene(const AudioSceneDescriptor &scene) override; 58 int32_t SetMute(bool mute) override; 59 int32_t GetMute(bool &mute) override; 60 int32_t SetVolume(float volume) override; 61 int32_t GetVolume(float &volume) override; 62 int32_t GetGainThreshold(float &min, float &max) override; 63 int32_t SetGain(float gain) override; 64 int32_t GetGain(float &gain) override; 65 int32_t GetFrameSize(uint64_t &size) override; 66 int32_t GetFrameCount(uint64_t &count) override; 67 int32_t SetSampleAttributes(const AudioSampleAttributes &attrs) override; 68 int32_t GetSampleAttributes(AudioSampleAttributes &attrs) override; 69 int32_t GetCurrentChannelId(uint32_t &channelId) override; 70 int32_t SetExtraParams(const std::string &keyValueList) override; 71 int32_t GetExtraParams(std::string &keyValueList) override; 72 int32_t ReqMmapBuffer(int32_t reqSize, AudioMmapBufferDescriptor &desc) override; 73 int32_t GetMmapPosition(uint64_t &frames, AudioTimeStamp &time) override; 74 int32_t AddAudioEffect(uint64_t effectid) override; 75 int32_t RemoveAudioEffect(uint64_t effectid) override; 76 int32_t GetFrameBufferSize(uint64_t &bufferSize) override; 77 int32_t Start() override; 78 int32_t Stop() override; 79 int32_t Pause() override; 80 int32_t Resume() override; 81 int32_t Flush() override; 82 int32_t TurnStandbyMode() override; 83 int32_t AudioDevDump(int32_t range, int32_t fd) override; 84 int32_t IsSupportsPauseAndResume(bool &supportPause, bool &supportResume) override; 85 const AudioDeviceDescriptor &GetRenderDesc() override; 86 void SetVolumeInner(const uint32_t vol) override; 87 void SetVolumeRangeInner(const uint32_t volMax, const uint32_t volMin) override; 88 uint32_t GetVolumeInner() override; 89 uint32_t GetMaxVolumeInner() override; 90 uint32_t GetMinVolumeInner() override; 91 void SetAttrs(const std::string &adpName, const AudioDeviceDescriptor &desc, const AudioSampleAttributes &attrs, 92 const sptr<IDAudioCallback> &callback, const int32_t dhId) override; 93 void SetDumpFlagInner() override; 94 AudioRenderStatus GetRenderStatus() override; 95 void SetRenderStatus(AudioRenderStatus status) override; 96 sptr<IAudioCallback> GetAudioCallback() override; 97 98 private: 99 float GetFadeRate(uint32_t currentIndex, const uint32_t durationIndex); 100 int32_t FadeInProcess(const uint32_t durationFrame, int8_t* frameData, const size_t frameLength); 101 int32_t InitAshmem(int32_t ashmemLength); 102 void UnInitAshmem(); 103 104 private: 105 std::string adapterName_; 106 int32_t dhId_ = 0; 107 uint32_t renderId_ = 0; 108 AudioDeviceDescriptor devDesc_ = {}; 109 AudioSampleAttributes devAttrs_ = {}; 110 111 uint32_t timeInterval_ = AUDIO_MMAP_NOIRQ_INTERVAL; 112 uint32_t minTimeInterval_ = 30; 113 uint32_t maxTimeInterval_ = 80; 114 float renderSpeed_ = 0; 115 uint32_t currentFrame_ = 0; 116 std::mutex renderMtx_; 117 AudioChannelMode channelMode_ = AUDIO_CHANNEL_NORMAL; 118 AudioRenderStatus renderStatus_ = RENDER_STATUS_CLOSE; 119 sptr<IDAudioCallback> audioExtCallback_ = nullptr; 120 sptr<IAudioCallback> renderCallback_ = nullptr; 121 bool firstOpenFlag_ = true; 122 OHOS::sptr<OHOS::Ashmem> ashmem_ = nullptr; 123 int32_t ashmemLength_ = 0; 124 int32_t lengthPerTrans_ = 0; 125 int32_t fd_ = 0; 126 127 std::mutex volMtx_; 128 uint32_t vol_ = 0; 129 uint32_t volMax_ = 0; 130 uint32_t volMin_ = 0; 131 }; 132 #ifdef __cplusplus 133 extern "C" { 134 #endif 135 __attribute__((visibility("default"))) AudioRenderInterfaceImplBase *GetRenderImplExt(); 136 #ifdef __cplusplus 137 } 138 #endif 139 } // namespace V1_0 140 } // namespace Audio 141 } // namespace DistributedAudio 142 } // namespace HDI 143 } // namespace OHOS 144 #endif // OHOS_AUDIO_RENDER_EXT_IMPL_H 145