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, uint64_t pts); 54 void DropOneFrame(); 55 bool Start(); 56 bool Init(const AudioTrack &audioTrack); 57 int64_t GetDecoderTimestamp(); 58 59 private: 60 uint32_t playerId_ = -1; 61 62 std::atomic_bool isRunning_ = false; 63 std::shared_ptr<AudioSink> audioSink_ = nullptr; 64 std::shared_ptr<AudioDecoder> audioDecoder_ = nullptr; 65 std::shared_ptr<AudioDecoderReceiver> audioDecoderReceiver_ = nullptr; 66 AudioTrack audioTrack_; 67 CodecId audioCodecId_ = CODEC_NONE; 68 }; 69 70 } // namespace Sharing 71 } // namespace OHOS 72 #endif