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