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 namespace OHOS { 27 namespace Sharing { 28 29 class AudioDecoderReceiver : public FrameDestination { 30 public: AudioDecoderReceiver(std::shared_ptr<AudioSink> & audioSink)31 AudioDecoderReceiver(std::shared_ptr<AudioSink> &audioSink) : sink_(audioSink) {}; 32 OnFrame(const Frame::Ptr & frame)33 void OnFrame(const Frame::Ptr &frame) override 34 { 35 MEDIA_LOGD("trace."); 36 auto sink = sink_.lock(); 37 if (sink != nullptr && frame) { 38 sink->Write((uint8_t *)frame->Data(), frame->Size()); 39 } 40 } 41 42 private: 43 std::weak_ptr<AudioSink> sink_; 44 }; 45 46 class AudioPlayer : public IdentifierInfo { 47 public: 48 virtual ~AudioPlayer(); 49 50 void Stop(); 51 void Release(); 52 void SetVolume(float volume); 53 void ProcessAudioData(DataBuffer::Ptr data); 54 bool Start(); 55 bool Init(const AudioTrack &audioTrack); 56 57 private: 58 uint32_t playerId_ = -1; 59 60 std::atomic_bool isRunning_ = false; 61 std::shared_ptr<AudioSink> audioSink_ = nullptr; 62 std::shared_ptr<AudioDecoder> audioDecoder_ = nullptr; 63 std::shared_ptr<AudioDecoderReceiver> audioDecoderReceiver_ = nullptr; 64 AudioTrack audioTrack_; 65 CodecId audioCodecId_ = CODEC_NONE; 66 }; 67 68 } // namespace Sharing 69 } // namespace OHOS 70 #endif