1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. 3 */ 4 /* 5 * Copyright (C) 2023 Huawei Device Co., Ltd. 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef VIDEO_CODEC_PLAYER_H 20 #define VIDEO_CODEC_PLAYER_H 21 22 #include <bits/alltypes.h> 23 #include <mutex> 24 #include <memory> 25 #include <atomic> 26 #include <thread> 27 #include <unistd.h> 28 #include <ohaudio/native_audiorenderer.h> 29 #include <ohaudio/native_audiostreambuilder.h> 30 #include <fstream> 31 #include "lpp_audio_streamer.h" 32 #include "lpp_video_streamer.h" 33 #include "video_decoder.h" 34 #include "audio_decoder.h" 35 #include "multimedia/player_framework/native_avbuffer.h" 36 #include "demuxer.h" 37 #include "sample_info.h" 38 #include "plugin_manager.h" 39 #include "video_encoder.h" 40 41 class Player { 42 public: Player()43 Player(){}; 44 ~Player(); 45 GetInstance()46 static Player& GetInstance() 47 { 48 static Player player; 49 return player; 50 } 51 52 int32_t Init(SampleInfo &sampleInfo); 53 int32_t CreateLppSet(); 54 int32_t SyncAudio(); 55 int32_t CreateLppAudioStreamer(); 56 int32_t CreateLppAudioStreamerNull(); 57 int32_t AudioStreamerSetCallbackCreateNull(); 58 int32_t AudioStreamerSetCallbackCreate(); 59 int32_t AudioStreamerSetDataNeededListener(); 60 int32_t AudioStreamerSetPositionUpdateListener(); 61 int32_t AudioStreamerSetCallback(); 62 int32_t AudioSetEosCallback(); 63 int32_t AudioSetOnDeviceChanged(); 64 int32_t AudioreturnFrames(); 65 int32_t AudioCallbackDestroy(); 66 int32_t AudioDestroy(); 67 int32_t AudioSetInterruptListener(); 68 int32_t AudioStreamerConfigure(); 69 int32_t AudioStreamerSetParameter(); 70 int32_t AudioStreamerGetParameter(); 71 int32_t AudioStreamerPrepare(); 72 int32_t CreateLppVideoStreamer(); 73 int32_t CreateLppVideoStreamerNull(); 74 int32_t VideoStreamerSetCallbackCreateNull(); 75 int32_t VideoStreamerSetDataNeededListener(); 76 int32_t VideoStreamerSetStreamChangedListener(); 77 int32_t VideoStreamerSetCallback(); 78 int32_t VideoStreamerSetErrorCallback(); 79 int32_t VideoStreamerSetFirstFrameDecodedCallback(); 80 int32_t VideoStreamerSetRenderStartCallback(); 81 int32_t VideoStreamerSetEosCallback(); 82 int32_t VideoStreamerCallbackDestroy(); 83 int32_t VideoStreamerDestroy(); 84 int32_t VideoStreamerreturnFrames(); 85 int32_t VideoStreamerSetTargetStartFrame(); 86 int32_t VideoStreamerConfigure(); 87 int32_t VideoStreamerSetParameter(); 88 int32_t VideoStreamerGetParameter(); 89 int32_t VideoStreamerSetSurface(); 90 int32_t VideoStreamerPrepare(); 91 int32_t VideoSetErrorCallback(); 92 int32_t AudioSetErrorCallback(); 93 int32_t Start(); 94 int32_t Start1(); 95 int32_t Start2(); 96 int32_t AudioStop(); 97 int32_t VideoStop(); 98 int32_t Stop(); 99 int32_t Reset(); 100 int32_t AudioReset(); 101 int32_t VideoReset(); 102 int32_t Pause(); 103 int32_t AudioPause(); 104 int32_t VideoPause(); 105 int32_t Resume(); 106 int32_t AudioResume(); 107 int32_t VideoResume(); 108 int32_t SetSpeed(double speed); 109 int32_t SetAudioSpeed(double speed); 110 int32_t SetVideoSpeed(double speed); 111 int32_t SetVolume(double volume); 112 int32_t Configure(); 113 int32_t Flush(); 114 int32_t AudioFlush(); 115 int32_t VideoFlush(); 116 int32_t Seek(int64_t seekTime, int32_t mode, bool acc); 117 int32_t Seek(int64_t seekTime, int32_t mode); 118 int32_t CreatePrepare(); 119 int32_t StartDecoder(); 120 int32_t RenderFirstFrame(); 121 int32_t StartRender(); 122 int32_t StartAudio(); 123 void Release(); 124 void ReleaseDone(); 125 void StartRelease(); 126 int64_t GetDurationTime(); 127 int64_t GetProgressTime(); 128 129 private: 130 void VideoDecInputThread(); 131 void VideoDecOutputThread(); 132 void AudioDecInputThread(); 133 void AudioDecOutputThread(); 134 void LppDataNeededThread(); 135 void LppDataNeededThread1(); 136 void LppDataNeededThread2(); 137 void LppDataProducerThread(); 138 void LppVideoDataNeededThread(); 139 void SeekInner(int64_t seekTime, int32_t mode); 140 void ReleaseThread(); 141 int32_t CreateAudioDecoder(); 142 int32_t CreateVideoDecoder(); 143 int64_t GetCurrentTime(); 144 bool Intercept(); 145 146 int64_t ReadToAudioTargetPts(int64_t pts); 147 148 std::unique_ptr<VideoDecoder> videoDecoder_ = nullptr; 149 std::shared_ptr<AudioDecoder> audioDecoder_ = nullptr; 150 std::shared_ptr<LppAudioStreamer> lppAudioStreamer_ = nullptr; 151 std::shared_ptr<LppVideoStreamer> lppVideoStreamer_ = nullptr; 152 std::unique_ptr<Demuxer> demuxer_ = nullptr; 153 std::unique_ptr<VideoEncoder> videoEncoder_ = nullptr; 154 155 std::mutex mutex_; 156 std::atomic<bool> isStarted_ { false }; 157 std::atomic<bool> isReleased_ { false }; 158 std::atomic<bool> isAudioDone { false }; 159 std::atomic<bool> isVideoDone { false }; 160 std::unique_ptr<std::thread> videoDecInputThread_ = nullptr; 161 std::unique_ptr<std::thread> videoDecOutputThread_ = nullptr; 162 std::unique_ptr<std::thread> audioDecInputThread_ = nullptr; 163 std::unique_ptr<std::thread> audioDecOutputThread_ = nullptr; 164 std::condition_variable doneCond_; 165 std::mutex doneMutex; 166 SampleInfo sampleInfo_; 167 CodecUserData *videoDecContext_ = nullptr; 168 CodecUserData *audioDecContext_ = nullptr; 169 OH_AudioStreamBuilder* builder_ = nullptr; 170 OH_AudioRenderer* audioRenderer_ = nullptr; 171 172 LppUserData *lppContext_ = nullptr; 173 // 等回调消费数据 174 std::unique_ptr<std::thread> LppDataNeededThread_ = nullptr; 175 // 生产数据,Max 176 std::unique_ptr<std::thread> LppDataProducerThread_ = nullptr; 177 void GetOneBuffer(); 178 179 LppUserData *lppVideoContext_ = nullptr; 180 std::unique_ptr<std::thread> lppVideoDataNeededThread_ = nullptr; 181 std::unique_ptr<std::thread> lppSeekThread_ = nullptr; 182 183 int64_t progress = 0; 184 int64_t nowTimeStamp = 0; 185 int64_t audioTimeStamp = 0; 186 int64_t writtenSampleCnt = 0; 187 int64_t audioBufferPts = 0; 188 #ifdef DEBUG_DECODE 189 std::ofstream audioOutputFile_; // for debug 190 #endif 191 static constexpr int64_t MICROSECOND_TO_S = 1000000; 192 static constexpr int64_t NANO_TO_S = 1000000000; 193 }; 194 195 #endif // VIDEO_CODEC_PLAYER_H