1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_AUDIO_SINK_DECODER_H 17 #define OHOS_SHARING_AUDIO_SINK_DECODER_H 18 19 #include "codec/include/codec_factory.h" 20 #include "common/const_def.h" 21 #include "common/identifier.h" 22 #include "common/media_log.h" 23 #include "mediaplayer/include/audio_sink.h" 24 #include "utils/data_buffer.h" 25 26 27 namespace OHOS { 28 namespace Sharing { 29 30 class AudioDecoderReceiver : public FrameDestination { 31 public: AudioDecoderReceiver(std::shared_ptr<AudioSink> & audioSink)32 AudioDecoderReceiver(std::shared_ptr<AudioSink> &audioSink) : sink_(audioSink){}; 33 OnFrame(const Frame::Ptr & frame)34 void OnFrame(const Frame::Ptr &frame) override 35 { 36 MEDIA_LOGD("trace."); 37 auto sink = sink_.lock(); 38 if (sink != nullptr && frame) { 39 sink->Write((uint8_t *)frame->Data(), frame->Size()); 40 } 41 } 42 43 private: 44 std::weak_ptr<AudioSink> sink_; 45 }; 46 47 class AudioPlayer : public IdentifierInfo { 48 public: 49 virtual ~AudioPlayer(); 50 51 void Stop(); 52 void Release(); 53 void SetVolume(float volume); 54 void ProcessAudioData(DataBuffer::Ptr data); 55 bool Start(); 56 bool Init(CodecId audioCodecId = CODEC_G711A); 57 bool SetAudioFormat(int32_t channel = DEFAULT_CHANNEL, int32_t sampleRate = DEFAULT_SAMPLERATE); 58 59 private: 60 uint32_t playerId_ = -1; 61 int32_t channel_ = DEFAULT_CHANNEL; 62 int32_t sampleRate_ = DEFAULT_SAMPLERATE; 63 64 std::atomic_bool isRunning_ = false; 65 std::shared_ptr<AudioSink> audioSink_ = nullptr; 66 std::shared_ptr<AudioDecoder> audioDecoder_ = nullptr; 67 std::shared_ptr<AudioDecoderReceiver> audioDecoderReceiver_ = nullptr; 68 69 CodecId audioCodecId_ = CODEC_NONE; 70 }; 71 72 } // namespace Sharing 73 } // namespace OHOS 74 #endif