1 /* 2 * Copyright (c) 2022-2022 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 HISTREAMER_AU_SERVER_SINK_PLUGIN_H 17 #define HISTREAMER_AU_SERVER_SINK_PLUGIN_H 18 19 #include <atomic> 20 #include <memory> 21 22 #include "audio_info.h" 23 #include "audio_renderer.h" 24 #include "foundation/osal/thread/mutex.h" 25 #include "plugin/common/plugin_audio_tags.h" 26 #include "plugins/ffmpeg_adapter/utils/ffmpeg_utils.h" 27 #include "plugin/interface/audio_sink_plugin.h" 28 #include "timestamp.h" 29 30 namespace OHOS { 31 namespace Media { 32 namespace Plugin { 33 namespace AuSrSinkPlugin { 34 class AudioServerSinkPlugin : public Plugin::AudioSinkPlugin { 35 public: 36 explicit AudioServerSinkPlugin(std::string name); 37 38 ~AudioServerSinkPlugin() override; 39 40 Status Init() override; 41 42 Status Deinit() override; 43 44 Status Prepare() override; 45 46 Status Reset() override; 47 48 Status Start() override; 49 50 Status Stop() override; 51 52 Status GetParameter(Tag tag, ValueType& para) override; 53 54 Status SetParameter(Tag tag, const ValueType& para) override; 55 GetAllocator()56 std::shared_ptr<Allocator> GetAllocator() override 57 { 58 return nullptr; 59 } 60 SetCallback(Callback * cb)61 Status SetCallback(Callback* cb) override 62 { 63 callback_ = cb; 64 return Status::OK; 65 } 66 GetMute(bool & mute)67 Status GetMute(bool& mute) override 68 { 69 return Status::OK; 70 } 71 SetMute(bool mute)72 Status SetMute(bool mute) override 73 { 74 return Status::OK; 75 } 76 77 Status GetVolume(float& volume) override; 78 79 Status SetVolume(float volume) override; 80 GetSpeed(float & speed)81 Status GetSpeed(float& speed) override 82 { 83 return Status::OK; 84 } 85 SetSpeed(float speed)86 Status SetSpeed(float speed) override 87 { 88 return Status::OK; 89 } 90 91 Status Pause() override; 92 93 Status Resume() override; 94 95 Status GetLatency(uint64_t& hstTime) override; 96 GetFrameSize(size_t & size)97 Status GetFrameSize(size_t& size) override 98 { 99 return Status::OK; 100 } 101 GetFrameCount(uint32_t & count)102 Status GetFrameCount(uint32_t& count) override 103 { 104 return Status::OK; 105 } 106 107 Status Write(const std::shared_ptr<Buffer>& input) override; 108 109 Status Flush() override; 110 111 Status Drain() override; 112 private: 113 class AudioRendererCallbackImpl : public OHOS::AudioStandard::AudioRendererCallback { 114 public: 115 AudioRendererCallbackImpl(Callback* cb, bool& isPaused); 116 void OnInterrupt(const OHOS::AudioStandard::InterruptEvent& interruptEvent) override; 117 void OnStateChange(const OHOS::AudioStandard::RendererState state, 118 const AudioStandard::StateChangeCmdType __attribute__((unused)) cmdType) override; 119 private: 120 Callback* callback_ {}; 121 bool isPaused_ {false}; 122 }; 123 void ReleaseRender(); 124 bool StopRender(); 125 bool AssignSampleRateIfSupported(uint32_t sampleRate); 126 bool AssignChannelNumIfSupported(uint32_t channelNum); 127 bool AssignSampleFmtIfSupported(AudioSampleFormat sampleFormat); 128 void SetInterruptMode(AudioStandard::InterruptMode interruptMode); 129 130 OSAL::Mutex renderMutex_ {}; 131 Callback* callback_ {}; 132 AudioRenderInfo audioRenderInfo_ {}; 133 AudioStandard::AudioRendererOptions rendererOptions_ {}; 134 AudioStandard::InterruptMode audioInterruptMode_ {AudioStandard::InterruptMode::SHARE_MODE}; 135 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_ {nullptr}; 136 std::shared_ptr<OHOS::AudioStandard::AudioRendererCallback> audioRendererCallback_ {nullptr}; 137 AudioStandard::AudioRendererParams rendererParams_ {}; 138 139 bool fmtSupported_ {false}; 140 bool isForcePaused_ {false}; 141 AVSampleFormat reSrcFfFmt_ {AV_SAMPLE_FMT_NONE}; 142 const AudioStandard::AudioSampleFormat reStdDestFmt_ {AudioStandard::AudioSampleFormat::SAMPLE_S16LE}; 143 const AVSampleFormat reFfDestFmt_ {AV_SAMPLE_FMT_S16}; 144 AudioChannelLayout channelLayout_ {}; 145 uint32_t channels_ {}; 146 uint32_t samplesPerFrame_ {}; 147 uint32_t bitsPerSample_ {0}; 148 uint32_t sampleRate_ {}; 149 int64_t bitRate_ {0}; 150 int32_t appPid_ {0}; 151 int32_t appUid_ {0}; 152 bool needReformat_ {false}; 153 Plugin::Seekable seekable_ {Plugin::Seekable::INVALID}; 154 std::shared_ptr<Ffmpeg::Resample> resample_ {nullptr}; 155 }; 156 } // AuSrSinkPlugin 157 } // Plugin 158 } // Media 159 } // OHOS 160 #endif // HISTREAMER_AU_SERVER_SINK_PLUGIN_H 161