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 #include "Player.h"
20 #include "av_codec_sample_log.h"
21 #include "dfx/error/av_codec_sample_error.h"
22 #include "lppCallback.h"
23 #include "video_encoder.h"
24 #include <bits/alltypes.h>
25 #include <netinet/tcp.h>
26 #include <queue>
27
28 #undef LOG_TAG
29 #define LOG_TAG "player"
30
31 namespace {
32 constexpr int BALANCE_VALUE = 5;
33 using namespace std::chrono_literals;
34 static const int MS_TO_S = 1000;
35 constexpr int64_t WAIT_TIME_US_THRESHOLD_WARNING = -1 * 40 * 1000; // warning threshold 40ms
36 constexpr int64_t WAIT_TIME_US_THRESHOLD = 1 * 1000 * 1000; // max sleep time 1s
37 constexpr int64_t SINK_TIME_US_THRESHOLD = 100000; // max sink time 100ms
38 constexpr int32_t BYTES_PER_SAMPLE_2 = 2; // 2 bytes per sample
39 constexpr double VSYNC_TIME = 1000 / 60; // frame time
40 constexpr double LIP_SYNC_BALANCE_VALUE = 2; // the balance value of sync sound and picture
41 constexpr int32_t MAX_BUFFER_SIZE = 2 * 1024 * 1024;
42 constexpr int64_t WAIT_OUT_TIME = 50000; // 等待时间MS
43 constexpr int32_t VIDEO_FRAME_COUNT = 5; // 视频聚包帧数
44 const int BUFFER_INFO_TYPE = 11; // 假设11表示某种缓冲区类型
45 enum AVSinkState {
46 IDLE,
47 INIT,
48 PREPARED,
49 PLAYING,
50 PAUSE,
51 STOP,
52 };
53 } // namespace
54
~Player()55 Player::~Player() { Player::StartRelease(); }
56
Intercept()57 bool Player::Intercept()
58 {
59 if (demuxer_ == nullptr || lppVideoStreamer_ == nullptr || lppAudioStreamer_ == nullptr) {
60 return true;
61 }
62 return false;
63 }
64
CreateLppSet()65 int32_t Player::CreateLppSet()
66 {
67 AVCODEC_SAMPLE_LOGW("CreateLppSet Set IN");
68
69 if (!sampleInfo_.isInit) {
70 return 0;
71 }
72 std::unique_lock<std::mutex> lock(mutex_);
73 if (demuxer_ == nullptr) {
74 demuxer_ = std::make_unique<Demuxer>();
75 }
76 int32_t ret = demuxer_->Create(sampleInfo_);
77
78 AVCODEC_SAMPLE_LOGW("CreateLppSet Set OUT");
79 return AVCODEC_SAMPLE_ERR_OK;
80 }
81
SyncAudio()82 int32_t Player::SyncAudio()
83 {
84 int retSync = lppVideoStreamer_->SetSyncAudioStreamer(lppAudioStreamer_->lppAudioStreamer_);
85 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetSyncAudioStreamer retSync:::%{public}d", retSync);
86 isReleased_ = false;
87 isStarted_ = true;
88 if (lppVideoContext_) {
89 AVCODEC_SAMPLE_LOGE("Create lppVideo thread Start");
90 if (lppVideoDataNeededThread_ == nullptr) {
91 lppVideoDataNeededThread_ = std::make_unique<std::thread>(&Player::LppVideoDataNeededThread, this);
92 }
93 AVCODEC_SAMPLE_LOGE("Create lppVideo thread END");
94 }
95 if (lppContext_) {
96 AVCODEC_SAMPLE_LOGE("Create audioThread Start");
97 if (LppDataNeededThread_ == nullptr) {
98 LppDataNeededThread_ = std::make_unique<std::thread>(&Player::LppDataNeededThread1, this);
99 }
100 AVCODEC_SAMPLE_LOGE("Create audioThread END");
101 }
102 return retSync;
103 }
104
CreateLppAudioStreamer()105 int32_t Player::CreateLppAudioStreamer()
106 {
107 AVCODEC_SAMPLE_LOGW("Create IN");
108 if (lppAudioStreamer_ == nullptr) {
109 AVCODEC_SAMPLE_LOGE("lppAudioStreamer_ is Release, should init");
110 return -1;
111 }
112 lppAudioStreamer_->Create(sampleInfo_.audioCodecMime);
113 lppContext_ = new LppUserData;
114 lppAudioStreamer_->SetCallback(lppContext_);
115 AVCODEC_SAMPLE_LOGW("Create OUT");
116 return AVCODEC_SAMPLE_ERR_OK;
117 }
118
CreateLppAudioStreamerNull()119 int32_t Player::CreateLppAudioStreamerNull()
120 {
121 AVCODEC_SAMPLE_LOGW("Create IN");
122 lppAudioStreamer_->Create("");
123 lppContext_ = new LppUserData;
124 AVCODEC_SAMPLE_LOGW("Create OUT");
125 return -1;
126 }
127
AudioStreamerSetParameter()128 int32_t Player::AudioStreamerSetParameter()
129 {
130 int32_t ret = lppAudioStreamer_->SetParameter(sampleInfo_);
131 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetParameter ret:::%{public}d", ret);
132 return ret;
133 }
AudioStreamerGetParameter()134 int32_t Player::AudioStreamerGetParameter()
135 {
136 int32_t ret = lppAudioStreamer_->GetParameter(sampleInfo_);
137 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ getParameter ret:::%{public}d", ret);
138 return ret;
139 }
140
AudioStreamerConfigure()141 int32_t Player::AudioStreamerConfigure()
142 {
143 int32_t ret = lppAudioStreamer_->Configure(sampleInfo_);
144 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Configure ret:::%{public}d", ret);
145 return ret;
146 }
147
AudioStreamerPrepare()148 int32_t Player::AudioStreamerPrepare()
149 {
150 int32_t ret = lppAudioStreamer_->Prepare();
151 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Prepare ret:::%{public}d", ret);
152 return ret;
153 }
154
AudioSetErrorCallback()155 int32_t Player::AudioSetErrorCallback()
156 {
157 int32_t ret = lppAudioStreamer_->SetErrorCallback(lppContext_);
158 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetErrorCallback ret:::%{public}d", ret);
159 return ret;
160 }
AudioStreamerSetCallbackCreateNull()161 int32_t Player::AudioStreamerSetCallbackCreateNull()
162 {
163 int32_t ret = lppAudioStreamer_->SetCallbackCreateNull(lppContext_);
164 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetCallbackCreateNull ret:::%{public}d", ret);
165 return ret;
166 }
AudioStreamerSetCallbackCreate()167 int32_t Player::AudioStreamerSetCallbackCreate()
168 {
169 int32_t ret = lppAudioStreamer_->SetCallbackCreate(lppContext_);
170 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetCallbackCreate ret:::%{public}d", ret);
171 return ret;
172 }
AudioStreamerSetDataNeededListener()173 int32_t Player::AudioStreamerSetDataNeededListener()
174 {
175 int32_t ret = lppAudioStreamer_->SetDataNeededListener(lppContext_);
176 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetDataNeededListener ret:::%{public}d", ret);
177 return ret;
178 }
AudioStreamerSetPositionUpdateListener()179 int32_t Player::AudioStreamerSetPositionUpdateListener()
180 {
181 int32_t ret = lppAudioStreamer_->SetPositionUpdateListener(lppContext_);
182 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetPositionUpdateListener ret:::%{public}d", ret);
183 return ret;
184 }
AudioStreamerSetCallback()185 int32_t Player::AudioStreamerSetCallback()
186 {
187 int32_t ret = lppAudioStreamer_->SetCallback(lppContext_);
188 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetCallback ret:::%{public}d", ret);
189 return ret;
190 }
AudioSetInterruptListener()191 int32_t Player::AudioSetInterruptListener()
192 {
193 int32_t ret = lppAudioStreamer_->SetInterruptListener(lppContext_);
194 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetInterruptListener ret:::%{public}d", ret);
195 return ret;
196 }
AudioSetEosCallback()197 int32_t Player::AudioSetEosCallback()
198 {
199 int32_t ret = lppAudioStreamer_->SetEosCallback(lppContext_);
200 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetEosCallback ret:::%{public}d", ret);
201 return ret;
202 }
AudioSetOnDeviceChanged()203 int32_t Player::AudioSetOnDeviceChanged()
204 {
205 int32_t ret = lppAudioStreamer_->SetOnDeviceChanged(lppContext_);
206 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetOnDeviceChanged ret:::%{public}d", ret);
207 return ret;
208 }
AudioreturnFrames()209 int32_t Player::AudioreturnFrames()
210 {
211 int32_t ret = lppAudioStreamer_->returnFrames(lppContext_);
212 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ returnFrames ret:::%{public}d", ret);
213 return ret;
214 }
AudioCallbackDestroy()215 int32_t Player::AudioCallbackDestroy()
216 {
217 int32_t ret = lppAudioStreamer_->CallbackDestroy();
218 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ CallbackDestroy ret:::%{public}d", ret);
219 return ret;
220 }
AudioDestroy()221 int32_t Player::AudioDestroy()
222 {
223 int32_t ret = lppAudioStreamer_->Destroy();
224 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Destroy ret:::%{public}d", ret);
225 return ret;
226 }
227
CreateLppVideoStreamer()228 int32_t Player::CreateLppVideoStreamer()
229 {
230 AVCODEC_SAMPLE_LOGW("Create IN");
231 if (lppVideoStreamer_ == nullptr) {
232 AVCODEC_SAMPLE_LOGE("lppVideoStreamer_ is Release, should init");
233 return -1;
234 }
235 AVCODEC_SAMPLE_LOGI("QZM FORMAT : %{public}s", sampleInfo_.videoCodecMime.c_str());
236 sampleInfo_.window = NativeXComponentSample::PluginManager::GetInstance()->pluginWindow_;
237 // video/avc
238 lppVideoStreamer_->Create(sampleInfo_.videoCodecMime.c_str());
239 lppVideoContext_ = new LppUserData;
240 int64_t durationNum = 100012;
241 lppVideoContext_->num = durationNum;
242 int32_t ret = lppVideoStreamer_->SetCallback(lppVideoContext_);
243 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetCallback ret:::%{public}d", ret);
244 ret = lppVideoStreamer_->SetErrorCallback(lppVideoContext_);
245 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetErrorCallback ret:::%{public}d", ret);
246 ret = lppVideoStreamer_->SetFirstFrameDecodedCallback(lppVideoContext_);
247 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetFirstFrameDecoded ret:::%{public}d", ret);
248 AVCODEC_SAMPLE_LOGW("Create OUT");
249 return AVCODEC_SAMPLE_ERR_OK;
250 }
251
CreateLppVideoStreamerNull()252 int32_t Player::CreateLppVideoStreamerNull()
253 {
254 AVCODEC_SAMPLE_LOGW("Create IN");
255 AVCODEC_SAMPLE_LOGI("QZM FORMAT : %{public}s", sampleInfo_.videoCodecMime.c_str());
256 sampleInfo_.window = NativeXComponentSample::PluginManager::GetInstance()->pluginWindow_;
257 // video/avc
258 lppVideoStreamer_->Create("");
259 lppVideoContext_ = new LppUserData;
260 int64_t durationNum = 100012;
261 lppVideoContext_->num = durationNum;
262 AVCODEC_SAMPLE_LOGW("Create OUT");
263 return -1;
264 }
265
VideoStreamerSetCallbackCreateNull()266 int32_t Player::VideoStreamerSetCallbackCreateNull()
267 {
268 int32_t ret = lppVideoStreamer_->SetCallbackCreateNull(lppVideoContext_);
269 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetCallbackCreateNull ret:::%{public}d", ret);
270 return ret;
271 }
VideoStreamerSetDataNeededListener()272 int32_t Player::VideoStreamerSetDataNeededListener()
273 {
274 int32_t ret = lppVideoStreamer_->SetDataNeededListener(lppVideoContext_);
275 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetDataNeededListener ret:::%{public}d", ret);
276 return ret;
277 }
VideoStreamerSetStreamChangedListener()278 int32_t Player::VideoStreamerSetStreamChangedListener()
279 {
280 int32_t ret = lppVideoStreamer_->SetStreamChangedListener(lppVideoContext_);
281 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetStreamChangedListener ret:::%{public}d", ret);
282 return ret;
283 }
VideoStreamerSetCallback()284 int32_t Player::VideoStreamerSetCallback()
285 {
286 int32_t ret = lppVideoStreamer_->SetCallback(lppVideoContext_);
287 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetCallback ret:::%{public}d", ret);
288 return ret;
289 }
VideoStreamerSetErrorCallback()290 int32_t Player::VideoStreamerSetErrorCallback()
291 {
292 int32_t ret = lppVideoStreamer_->SetErrorCallback(lppVideoContext_);
293 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetErrorCallback ret:::%{public}d", ret);
294 return ret;
295 }
VideoStreamerSetFirstFrameDecodedCallback()296 int32_t Player::VideoStreamerSetFirstFrameDecodedCallback()
297 {
298 int32_t ret = lppVideoStreamer_->SetFirstFrameDecodedCallback(lppVideoContext_);
299 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetFirstFrameDecodedCallback ret:::%{public}d", ret);
300 return ret;
301 }
VideoStreamerSetRenderStartCallback()302 int32_t Player::VideoStreamerSetRenderStartCallback()
303 {
304 int32_t ret = lppVideoStreamer_->SetRenderStartCallback(lppVideoContext_);
305 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetRenderStartCallback ret:::%{public}d", ret);
306 return ret;
307 }
VideoStreamerSetEosCallback()308 int32_t Player::VideoStreamerSetEosCallback()
309 {
310 int32_t ret = lppVideoStreamer_->SetEosCallback(lppVideoContext_);
311 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetEosCallback ret:::%{public}d", ret);
312 return ret;
313 }
VideoStreamerCallbackDestroy()314 int32_t Player::VideoStreamerCallbackDestroy()
315 {
316 int32_t ret = lppVideoStreamer_->CallbackDestroy();
317 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ CallbackDestroy ret:::%{public}d", ret);
318 return ret;
319 }
VideoStreamerDestroy()320 int32_t Player::VideoStreamerDestroy()
321 {
322 int32_t ret = lppVideoStreamer_->Destroy();
323 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Destroy ret:::%{public}d", ret);
324 return ret;
325 }
VideoStreamerreturnFrames()326 int32_t Player::VideoStreamerreturnFrames()
327 {
328 int32_t ret = lppVideoStreamer_->returnFrames(lppVideoContext_);
329 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ returnFrames ret:::%{public}d", ret);
330 return ret;
331 }
VideoStreamerSetTargetStartFrame()332 int32_t Player::VideoStreamerSetTargetStartFrame()
333 {
334 AVCODEC_SAMPLE_LOGI("============== Seek ACC IN ===============");
335 int64_t targetPts = LIP_SYNC_BALANCE_VALUE * 1000;
336 AVCODEC_SAMPLE_LOGI("SetTargetStartFrame %{public}ld", targetPts);
337 int ret = 0;
338 // 给视频设置回调和TargetPts
339 if (lppVideoStreamer_ != nullptr) {
340 ret = lppVideoStreamer_->SetTargetStartFrame(targetPts, LppCallback::OnTargetArrived, WAIT_OUT_TIME,
341 lppVideoContext_);
342 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetTargetStartFrame ret:::%{public}d", ret);
343 }
344 AVCODEC_SAMPLE_LOGI("============== Seek ACC OUT ===============");
345 return ret;
346 }
VideoStreamerConfigure()347 int32_t Player::VideoStreamerConfigure()
348 {
349 int32_t ret = lppVideoStreamer_->Configure(sampleInfo_);
350 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Configure ret:::%{public}d", ret);
351 return ret;
352 }
VideoStreamerSetParameter()353 int32_t Player::VideoStreamerSetParameter()
354 {
355 int32_t ret = lppVideoStreamer_->SetParameter(sampleInfo_);
356 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetParameter ret:::%{public}d", ret);
357 return ret;
358 }
VideoStreamerGetParameter()359 int32_t Player::VideoStreamerGetParameter()
360 {
361 int32_t ret = lppVideoStreamer_->GetParameter(sampleInfo_);
362 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ GetParameter ret:::%{public}d", ret);
363 return ret;
364 }
VideoStreamerSetSurface()365 int32_t Player::VideoStreamerSetSurface()
366 {
367 int32_t ret = lppVideoStreamer_->SetVideoSurface(sampleInfo_);
368 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetVideoSurface ret:::%{public}d", ret);
369 return ret;
370 }
371
VideoStreamerPrepare()372 int32_t Player::VideoStreamerPrepare()
373 {
374 int32_t ret = lppVideoStreamer_->Prepare();
375 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Prepare ret:::%{public}d", ret);
376 return ret;
377 }
378
VideoSetErrorCallback()379 int32_t Player::VideoSetErrorCallback()
380 {
381 int32_t ret = lppVideoStreamer_->SetErrorCallback(lppVideoContext_);
382 return ret;
383 }
Init(SampleInfo & sampleInfo)384 int32_t Player::Init(SampleInfo &sampleInfo)
385 {
386 Stop();
387 Reset();
388 Release();
389 std::unique_lock<std::mutex> lock(mutex_);
390 CHECK_AND_RETURN_RET_LOG(!isStarted_, AVCODEC_SAMPLE_ERR_ERROR, "Already started.");
391 sampleInfo_ = sampleInfo;
392 sampleInfo_.isInit = true;
393 CHECK_AND_RETURN_RET_LOG(demuxer_ == nullptr && videoDecoder_ == nullptr && audioDecoder_ == nullptr,
394 AVCODEC_SAMPLE_ERR_ERROR, "Already started.");
395
396 videoDecoder_ = std::make_unique<VideoDecoder>();
397 audioDecoder_ = std::make_unique<AudioDecoder>();
398 demuxer_ = std::make_unique<Demuxer>();
399 AVCODEC_SAMPLE_LOGI("Create lppAudioStreamer_ Init SUCC");
400 lppAudioStreamer_ = std::make_shared<LppAudioStreamer>();
401 lppVideoStreamer_ = std::make_shared<LppVideoStreamer>();
402
403 isReleased_ = false;
404 AVCODEC_SAMPLE_LOGI("Init Succeed");
405 return AVCODEC_SAMPLE_ERR_OK;
406 }
407
Configure()408 int32_t Player::Configure()
409 {
410 CreatePrepare();
411 Start1();
412 return 0;
413 }
414
CreatePrepare()415 int32_t Player::CreatePrepare()
416 {
417 AVCODEC_SAMPLE_LOGW("Prepare IN");
418 if (!sampleInfo_.isInit) {
419 return 0;
420 }
421 std::unique_lock<std::mutex> lock(mutex_);
422 if (demuxer_ == nullptr) {
423 demuxer_ = std::make_unique<Demuxer>();
424 }
425 int32_t ret = demuxer_->Create(sampleInfo_);
426
427 CreateLppAudioStreamer();
428 AudioStreamerConfigure();
429
430 CreateLppVideoStreamer();
431 VideoStreamerConfigure();
432 VideoStreamerSetSurface();
433
434 int retSync = lppVideoStreamer_->SetSyncAudioStreamer(lppAudioStreamer_->lppAudioStreamer_);
435 VideoStreamerPrepare();
436 AudioStreamerPrepare();
437
438 isReleased_ = false;
439
440 isStarted_ = true;
441 if (lppVideoContext_) {
442 AVCODEC_SAMPLE_LOGE("Create lppVideo thread Start");
443 if (lppVideoDataNeededThread_ == nullptr) {
444 lppVideoDataNeededThread_ = std::make_unique<std::thread>(&Player::LppVideoDataNeededThread, this);
445 }
446 AVCODEC_SAMPLE_LOGE("Create lppVideo thread END");
447 }
448 if (lppContext_) {
449 AVCODEC_SAMPLE_LOGE("Create audioThread Start");
450 if (LppDataNeededThread_ == nullptr) {
451 LppDataNeededThread_ = std::make_unique<std::thread>(&Player::LppDataNeededThread1, this);
452 }
453 AVCODEC_SAMPLE_LOGE("Create audioThread END");
454 }
455 AVCODEC_SAMPLE_LOGW("Prepare OUT");
456 return 0;
457 }
458
Start()459 int32_t Player::Start()
460 {
461 std::unique_lock<std::mutex> lock(mutex_);
462 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
463 int32_t ret;
464 CHECK_AND_RETURN_RET_LOG(!isStarted_, AVCODEC_SAMPLE_ERR_ERROR, "Already started.");
465 AVCODEC_SAMPLE_LOGW("aStart IN");
466 isStarted_ = true;
467 if (lppContext_) {
468 AVCODEC_SAMPLE_LOGE("Create thread Start");
469 ret = lppAudioStreamer_->Start();
470 LppDataNeededThread_ = std::make_unique<std::thread>(&Player::LppDataNeededThread1, this);
471 AVCODEC_SAMPLE_LOGE("Create thread END");
472 }
473
474 if (lppVideoContext_) {
475 AVCODEC_SAMPLE_LOGE("Create lppVideo thread Start");
476 ret = lppVideoStreamer_->StartDecode(true);
477 AVCODEC_SAMPLE_LOGE("Create lppVideo thread Start1");
478 ret = lppVideoStreamer_->StartRender();
479 AVCODEC_SAMPLE_LOGE("Create lppVideo thread Start2");
480 isStarted_ = true;
481 AVCODEC_SAMPLE_LOGE("Create lppVideo thread Start3");
482 lppVideoDataNeededThread_ = std::make_unique<std::thread>(&Player::LppVideoDataNeededThread, this);
483 AVCODEC_SAMPLE_LOGE("Create lppVideo thread END");
484 }
485
486 AVCODEC_SAMPLE_LOGI("Succeed");
487 doneCond_.notify_all();
488 return AVCODEC_SAMPLE_ERR_OK;
489 }
490
StartDecoder()491 int32_t Player::StartDecoder()
492 {
493 isStarted_ = true;
494 int32_t ret;
495 if (lppVideoContext_) {
496 AVCODEC_SAMPLE_LOGE("Create lppVideo thread Start");
497 ret = lppVideoStreamer_->StartDecode(true);
498 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ StartDecode ret:::%{public}d", ret);
499 AVCODEC_SAMPLE_LOGE("Create lppVideo thread END");
500 }
501 return ret;
502 }
503
RenderFirstFrame()504 int32_t Player::RenderFirstFrame()
505 {
506 int32_t retVideo;
507 if (lppVideoContext_ && lppVideoStreamer_) {
508 AVCODEC_SAMPLE_LOGE("RenderFirstFrame Start");
509 retVideo = lppVideoStreamer_->RenderFirstFrame();
510 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ RenderFirstFrame retVideo:::%{public}d", retVideo);
511 AVCODEC_SAMPLE_LOGE("RenderFirstFrame END");
512 }
513 return retVideo;
514 }
515
StartRender()516 int32_t Player::StartRender()
517 {
518 isStarted_ = true;
519 int32_t ret;
520 if (lppVideoContext_ && lppVideoStreamer_) {
521 AVCODEC_SAMPLE_LOGE("StartRender Start");
522 ret = lppVideoStreamer_->StartRender();
523 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ StartRender ret:::%{public}d", ret);
524 AVCODEC_SAMPLE_LOGE("StartRender END");
525 }
526 return ret;
527 }
528
StartAudio()529 int32_t Player::StartAudio()
530 {
531 isStarted_ = true;
532 int32_t ret;
533 if (lppContext_) {
534 AVCODEC_SAMPLE_LOGE("Create audioThread Start");
535 ret = lppAudioStreamer_->Start();
536 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Start ret:::%{public}d", ret);
537 AVCODEC_SAMPLE_LOGE("Create audioThread END");
538 }
539 return ret;
540 }
541
Start1()542 int32_t Player::Start1()
543 {
544 StartDecoder();
545 usleep(WAIT_OUT_TIME);
546 RenderFirstFrame();
547 StartRender();
548 StartAudio();
549 return AVCODEC_SAMPLE_ERR_OK;
550 }
551
Stop()552 int32_t Player::Stop()
553 {
554 std::unique_lock<std::mutex> lock(mutex_);
555 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
556 AVCODEC_SAMPLE_LOGW("stop in");
557 isStarted_ = false;
558 int32_t ret;
559 int32_t retVideo;
560 if (lppAudioStreamer_ != nullptr) {
561 ret = lppAudioStreamer_->Stop();
562 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Stop ret:::%{public}d", ret);
563 }
564 if (lppVideoStreamer_ != nullptr) {
565 retVideo = lppVideoStreamer_->Stop();
566 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Stop retVideo:::%{public}d", retVideo);
567 }
568 if (retVideo != AVCODEC_SAMPLE_ERR_OK) {
569 ret = retVideo;
570 }
571 AVCODEC_SAMPLE_LOGW("stop out");
572 lock.unlock();
573 return ret;
574 }
575
AudioStop()576 int32_t Player::AudioStop()
577 {
578 std::unique_lock<std::mutex> lock(mutex_);
579 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
580 AVCODEC_SAMPLE_LOGW("AudioStop in");
581 isStarted_ = false;
582 int32_t ret;
583 if (lppAudioStreamer_ != nullptr) {
584 ret = lppAudioStreamer_->Stop();
585 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Stop ret:::%{public}d", ret);
586 }
587 AVCODEC_SAMPLE_LOGW("AudioStop out");
588 lock.unlock();
589 return ret;
590 }
591
VideoStop()592 int32_t Player::VideoStop()
593 {
594 std::unique_lock<std::mutex> lock(mutex_);
595 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
596 AVCODEC_SAMPLE_LOGW("VideoStop in");
597 isStarted_ = false;
598 int32_t ret;
599 if (lppVideoStreamer_ != nullptr) {
600 ret = lppVideoStreamer_->Stop();
601 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Stop ret:::%{public}d", ret);
602 }
603 AVCODEC_SAMPLE_LOGW("VideoStop out");
604 lock.unlock();
605 return ret;
606 }
607
Reset()608 int32_t Player::Reset()
609 {
610 std::unique_lock<std::mutex> lock(mutex_);
611 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
612 isStarted_ = false;
613 int32_t ret;
614 int32_t retVideo;
615 ReleaseThread();
616 AVCODEC_SAMPLE_LOGW("reset in1");
617 if (demuxer_ != nullptr) {
618 demuxer_->Release();
619 demuxer_.reset();
620 }
621 AVCODEC_SAMPLE_LOGW("reset in");
622 if (lppAudioStreamer_ != nullptr) {
623 ret = lppAudioStreamer_->Reset();
624 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Reset ret:::%{public}d", ret);
625 }
626 if (lppVideoStreamer_ != nullptr) {
627 retVideo = lppVideoStreamer_->Reset();
628 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Reset retVideo:::%{public}d", retVideo);
629 }
630 if (retVideo != AVCODEC_SAMPLE_ERR_OK) {
631 ret = retVideo;
632 }
633 AVCODEC_SAMPLE_LOGW("reset out");
634 lock.unlock();
635 return ret;
636 }
637
AudioReset()638 int32_t Player::AudioReset()
639 {
640 std::unique_lock<std::mutex> lock(mutex_);
641 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
642 isStarted_ = false;
643 int32_t ret;
644 ReleaseThread();
645 AVCODEC_SAMPLE_LOGW("reset in1");
646 if (demuxer_ != nullptr) {
647 demuxer_->Release();
648 demuxer_.reset();
649 }
650 AVCODEC_SAMPLE_LOGW("reset in");
651 if (lppAudioStreamer_ != nullptr) {
652 ret = lppAudioStreamer_->Reset();
653 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Reset ret:::%{public}d", ret);
654 }
655 AVCODEC_SAMPLE_LOGW("reset out");
656 lock.unlock();
657 return ret;
658 }
VideoReset()659 int32_t Player::VideoReset()
660 {
661 std::unique_lock<std::mutex> lock(mutex_);
662 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
663 isStarted_ = false;
664 int32_t ret;
665 ReleaseThread();
666 AVCODEC_SAMPLE_LOGW("reset in1");
667 if (demuxer_ != nullptr) {
668 demuxer_->Release();
669 demuxer_.reset();
670 }
671 AVCODEC_SAMPLE_LOGW("reset in");
672 if (lppVideoStreamer_ != nullptr) {
673 ret = lppVideoStreamer_->Reset();
674 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Reset ret:::%{public}d", ret);
675 }
676 AVCODEC_SAMPLE_LOGW("reset out");
677 lock.unlock();
678 return ret;
679 }
680
Pause()681 int32_t Player::Pause()
682 {
683 std::unique_lock<std::mutex> lock(mutex_);
684 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
685 AVCODEC_SAMPLE_LOGW("Pause in");
686 int32_t ret;
687 int32_t retVideo;
688 if (lppAudioStreamer_ != nullptr) {
689 ret = lppAudioStreamer_->Pause();
690 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Pause ret:::%{public}d", ret);
691 }
692 if (lppVideoStreamer_ != nullptr) {
693 retVideo = lppVideoStreamer_->Pause();
694 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Pause retVideo:::%{public}d", retVideo);
695 }
696 if (retVideo != AVCODEC_SAMPLE_ERR_OK) {
697 ret = retVideo;
698 }
699 AVCODEC_SAMPLE_LOGW("Pause out");
700 lock.unlock();
701 return ret;
702 }
703
AudioPause()704 int32_t Player::AudioPause()
705 {
706 std::unique_lock<std::mutex> lock(mutex_);
707 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
708 AVCODEC_SAMPLE_LOGW("Pause in");
709 int32_t ret;
710 if (lppAudioStreamer_ != nullptr) {
711 ret = lppAudioStreamer_->Pause();
712 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Pause ret:::%{public}d", ret);
713 }
714 AVCODEC_SAMPLE_LOGW("Pause out");
715 lock.unlock();
716 return ret;
717 }
718
VideoPause()719 int32_t Player::VideoPause()
720 {
721 std::unique_lock<std::mutex> lock(mutex_);
722 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
723 AVCODEC_SAMPLE_LOGW("Pause in");
724 int32_t ret;
725 if (lppVideoStreamer_ != nullptr) {
726 ret = lppVideoStreamer_->Pause();
727 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Pause ret:::%{public}d", ret);
728 }
729 AVCODEC_SAMPLE_LOGW("Pause out");
730 lock.unlock();
731 return ret;
732 }
733
Resume()734 int32_t Player::Resume()
735 {
736 std::unique_lock<std::mutex> lock(mutex_);
737 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
738 AVCODEC_SAMPLE_LOGW("Resume in");
739 int32_t ret;
740 int32_t retVideo;
741 if (lppAudioStreamer_ != nullptr) {
742 ret = lppAudioStreamer_->Resume();
743 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Resume ret:::%{public}d", ret);
744 }
745 if (lppVideoStreamer_ != nullptr) {
746 retVideo = lppVideoStreamer_->Resume();
747 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Resume retVideo:::%{public}d", retVideo);
748 }
749 if (retVideo != AVCODEC_SAMPLE_ERR_OK) {
750 ret = retVideo;
751 }
752 AVCODEC_SAMPLE_LOGW("Resume out");
753 lock.unlock();
754 return ret;
755 }
AudioResume()756 int32_t Player::AudioResume()
757 {
758 std::unique_lock<std::mutex> lock(mutex_);
759 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
760 AVCODEC_SAMPLE_LOGW("Resume in");
761 int32_t ret;
762 if (lppAudioStreamer_ != nullptr) {
763 ret = lppAudioStreamer_->Resume();
764 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Resume ret:::%{public}d", ret);
765 }
766 AVCODEC_SAMPLE_LOGW("Resume out");
767 lock.unlock();
768 return ret;
769 }
VideoResume()770 int32_t Player::VideoResume()
771 {
772 std::unique_lock<std::mutex> lock(mutex_);
773 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
774 AVCODEC_SAMPLE_LOGW("Resume in");
775 int32_t ret;
776 if (lppVideoStreamer_ != nullptr) {
777 ret = lppVideoStreamer_->Resume();
778 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Resume ret:::%{public}d", ret);
779 }
780 AVCODEC_SAMPLE_LOGW("Resume out");
781 lock.unlock();
782 return ret;
783 }
784
Start2()785 int32_t Player::Start2()
786 {
787 std::unique_lock<std::mutex> lock(mutex_);
788 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
789 AVCODEC_SAMPLE_LOGW("Resume in");
790 if (lppAudioStreamer_ != nullptr) {
791 lppAudioStreamer_->Start();
792 }
793 if (lppVideoStreamer_ != nullptr) {
794 lppVideoStreamer_->Resume();
795 }
796 AVCODEC_SAMPLE_LOGW("Resume out");
797 lock.unlock();
798 return 0;
799 }
800
Flush()801 int32_t Player::Flush()
802 {
803 std::unique_lock<std::mutex> lock(mutex_);
804 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
805 AVCODEC_SAMPLE_LOGW("Flush in");
806 int32_t ret;
807 if (lppAudioStreamer_ != nullptr) {
808 ret = lppAudioStreamer_->Flush();
809 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Flush ret:::%{public}d", ret);
810 }
811 AVCODEC_SAMPLE_LOGW("Flush out1");
812 if (lppVideoStreamer_ != nullptr) {
813 ret = lppVideoStreamer_->Flush();
814 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Flush ret:::%{public}d", ret);
815 }
816 AVCODEC_SAMPLE_LOGW("Flush out2");
817 lock.unlock();
818 return ret;
819 }
AudioFlush()820 int32_t Player::AudioFlush()
821 {
822 std::unique_lock<std::mutex> lock(mutex_);
823 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
824 AVCODEC_SAMPLE_LOGW("Flush in");
825 int32_t ret;
826 if (lppAudioStreamer_ != nullptr) {
827 ret = lppAudioStreamer_->Flush();
828 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ Flush ret:::%{public}d", ret);
829 }
830 AVCODEC_SAMPLE_LOGW("Flush out1");
831 lock.unlock();
832 return ret;
833 }
VideoFlush()834 int32_t Player::VideoFlush()
835 {
836 std::unique_lock<std::mutex> lock(mutex_);
837 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
838 AVCODEC_SAMPLE_LOGW("Flush in");
839 int32_t ret;
840 if (lppVideoStreamer_ != nullptr) {
841 ret = lppVideoStreamer_->Flush();
842 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ Flush ret:::%{public}d", ret);
843 }
844 AVCODEC_SAMPLE_LOGW("Flush out2");
845 lock.unlock();
846 return ret;
847 }
848
SetSpeed(double speed)849 int32_t Player::SetSpeed(double speed)
850 {
851 std::unique_lock<std::mutex> lock(mutex_);
852 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
853 AVCODEC_SAMPLE_LOGW("SetSpeed in");
854 int32_t ret;
855 int32_t retVideo;
856 if (lppAudioStreamer_ != nullptr) {
857 ret = lppAudioStreamer_->SetPlayBackSpeed(speed);
858 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetPlayBackSpeed ret:::%{public}d", ret);
859 }
860 if (lppVideoStreamer_ != nullptr) {
861 retVideo = lppVideoStreamer_->SetPlaybackSpeed(speed);
862 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetPlayBackSpeed retVideo:::%{public}d", retVideo);
863 }
864 if (retVideo != AVCODEC_SAMPLE_ERR_OK) {
865 ret = retVideo;
866 }
867 AVCODEC_SAMPLE_LOGW("SetSpeed out");
868 lock.unlock();
869 return ret;
870 }
SetAudioSpeed(double speed)871 int32_t Player::SetAudioSpeed(double speed)
872 {
873 std::unique_lock<std::mutex> lock(mutex_);
874 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
875 AVCODEC_SAMPLE_LOGW("SetSpeed in");
876 int32_t ret;
877 if (lppAudioStreamer_ != nullptr) {
878 ret = lppAudioStreamer_->SetPlayBackSpeed(speed);
879 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ SetPlayBackSpeed ret:::%{public}d", ret);
880 }
881 AVCODEC_SAMPLE_LOGW("SetSpeed out");
882 lock.unlock();
883 return ret;
884 }
SetVideoSpeed(double speed)885 int32_t Player::SetVideoSpeed(double speed)
886 {
887 std::unique_lock<std::mutex> lock(mutex_);
888 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
889 AVCODEC_SAMPLE_LOGW("SetSpeed in");
890 int32_t ret;
891 if (lppVideoStreamer_ != nullptr) {
892 ret = lppVideoStreamer_->SetPlaybackSpeed(speed);
893 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetPlayBackSpeed ret:::%{public}d", ret);
894 }
895 AVCODEC_SAMPLE_LOGW("SetSpeed out");
896 lock.unlock();
897 return ret;
898 }
899
SetVolume(double volume)900 int32_t Player::SetVolume(double volume)
901 {
902 std::unique_lock<std::mutex> lock(mutex_);
903 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
904 AVCODEC_SAMPLE_LOGW("SetVolume in");
905 int32_t ret;
906 if (lppAudioStreamer_ != nullptr) {
907 ret = lppAudioStreamer_->SetVolume(volume);
908 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetVolume ret:::%{public}d", ret);
909 }
910 AVCODEC_SAMPLE_LOGW("SetVolume out");
911 lock.unlock();
912 return ret;
913 }
914
915 // pause/flush/seek/resume
Seek(int64_t seekTime,int32_t mode,bool acc)916 int32_t Player::Seek(int64_t seekTime, int32_t mode, bool acc)
917 {
918 auto start = std::chrono::high_resolution_clock::now();
919 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
920 int64_t durationTime = 1000;
921 if (sampleInfo_.duration < seekTime * durationTime) {
922 AVCODEC_SAMPLE_LOGI("Seek over duration %{public}ld", sampleInfo_.duration);
923 return 0;
924 }
925 Pause();
926 std::unique_lock<std::mutex> lock(mutex_);
927 AVCODEC_SAMPLE_LOGW("Seek in");
928 OH_AVSeekMode enumNum = static_cast<OH_AVSeekMode>(mode);
929 AVCODEC_SAMPLE_LOGI("OH_AVSeekMode %{public}d", mode);
930 demuxer_->Seek(seekTime, enumNum);
931
932 // Seek后唤醒
933 {
934 std::unique_lock<std::mutex> eosFlagMutexLock(lppContext_->eosFlagMutex);
935 lppContext_->eosFlag_ = false;
936 AVCODEC_SAMPLE_LOGI("AUDIO eosFlag_ %{public}d", lppContext_->eosFlag_);
937 lppContext_->eosCond_.notify_all();
938 eosFlagMutexLock.unlock();
939 }
940 {
941 std::unique_lock<std::mutex> eosFlagMutexLock(lppVideoContext_->eosFlagMutex);
942 lppVideoContext_->eosFlag_ = false;
943 AVCODEC_SAMPLE_LOGI("VIDEO eosFlag_ %{public}d", lppContext_->eosFlag_);
944 lppVideoContext_->eosCond_.notify_all();
945 eosFlagMutexLock.unlock();
946 }
947
948 AVCODEC_SAMPLE_LOGW("Seek out");
949 lock.unlock();
950 AVCODEC_SAMPLE_LOGW("Seek out1");
951 Flush();
952 AVCODEC_SAMPLE_LOGW("Seek out2");
953 Start2();
954 AVCODEC_SAMPLE_LOGW("Seek out3");
955 auto end = std::chrono::high_resolution_clock::now();
956 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
957 AVCODEC_SAMPLE_LOGI("seek duration %lld", duration);
958 return 0;
959 }
960
ReadToAudioTargetPts(int64_t targetPts)961 int64_t Player::ReadToAudioTargetPts(int64_t targetPts)
962 {
963
964 OH_AVBuffer *avbuffer = OH_AVBuffer_Create(MAX_BUFFER_SIZE);
965 CodecBufferInfo bufferInfo(BUFFER_INFO_TYPE, avbuffer);
966 while (true) {
967 demuxer_->ReadSample(demuxer_->GetAudioTrackId(), reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer),
968 bufferInfo.attr);
969 if (targetPts <= bufferInfo.attr.pts) {
970 break;
971 }
972 if (bufferInfo.attr.flags & AVCODEC_BUFFER_FLAGS_EOS) {
973 break;
974 }
975 }
976 return bufferInfo.attr.pts;
977 }
978
Seek(int64_t seekTime,int32_t mode)979 int32_t Player::Seek(int64_t seekTime, int32_t mode)
980 {
981 CHECK_AND_RETURN_RET_LOG(!Intercept(), AVCODEC_SAMPLE_ERR_ERROR, "Intercept nullptr.");
982 int64_t durationTime = 1000;
983 if (sampleInfo_.duration < seekTime * durationTime) {
984 AVCODEC_SAMPLE_LOGI("Seek over duration %{public}ld", sampleInfo_.duration);
985 return 0;
986 }
987 lppSeekThread_ = std::make_unique<std::thread>(&Player::SeekInner, this, seekTime, mode);
988 lppSeekThread_->join();
989 return 0;
990 }
991
SeekInner(int64_t seekTime,int32_t mode)992 void Player::SeekInner(int64_t seekTime, int32_t mode)
993 {
994 AVCODEC_SAMPLE_LOGI("============== Seek ACC IN ===============");
995 int64_t targetPts = seekTime * 1000;
996 AVCODEC_SAMPLE_LOGI("SetTargetStartFrame %{public}ld", targetPts);
997 // 暂停
998 Pause();
999 // Flush
1000 Flush();
1001 // seek
1002 demuxer_->Seek(seekTime, SEEK_MODE_PREVIOUS_SYNC);
1003 // 音频读取到对应帧
1004 ReadToAudioTargetPts(targetPts);
1005 // 给视频设置回调和TargetPts
1006 if (lppVideoStreamer_ != nullptr) {
1007 int ret = lppVideoStreamer_->SetTargetStartFrame(targetPts, LppCallback::OnTargetArrived, WAIT_OUT_TIME,
1008 lppVideoContext_);
1009 AVCODEC_SAMPLE_LOGW("lppVideoStreamer_ SetTargetStartFrame ret:::%{public}d", ret);
1010 lppVideoStreamer_->Resume();
1011 }
1012 // 等待返回
1013 std::unique_lock<std::mutex> lock(lppVideoContext_->seekMutex_);
1014
1015 lppVideoContext_->seekReturn_ = false;
1016 lock.unlock();
1017 // 返回成功,启动音频
1018 if (lppAudioStreamer_ != nullptr) {
1019 lppAudioStreamer_->Start();
1020 }
1021 AVCODEC_SAMPLE_LOGI("============== Seek ACC OUT ===============");
1022 }
1023
StartRelease()1024 void Player::StartRelease()
1025 {
1026 CHECK_AND_RETURN_LOG(!Intercept(), "Intercept nullptr.");
1027 AVCODEC_SAMPLE_LOGI("StartRelease");
1028 std::unique_lock<std::mutex> lock(doneMutex);
1029 if (audioRenderer_) {
1030 OH_AudioRenderer_Stop(audioRenderer_);
1031 }
1032 if (!isReleased_) {
1033 isReleased_ = true;
1034 Release();
1035 }
1036 lock.unlock();
1037 }
1038
GetDurationTime()1039 int64_t Player::GetDurationTime()
1040 {
1041 AVCODEC_SAMPLE_LOGI("GetDurationTime %{public}ld", sampleInfo_.duration);
1042 return sampleInfo_.duration;
1043 }
1044
GetProgressTime()1045 int64_t Player::GetProgressTime()
1046 {
1047 if (lppContext_ == nullptr) {
1048 return 0;
1049 }
1050 AVCODEC_SAMPLE_LOGI("GetProgressTime position %{public}ld", lppContext_->position);
1051 int64_t tmp = sampleInfo_.duration / 1000000 < (lppContext_->position / 1000)
1052 ? sampleInfo_.duration
1053 : ((lppContext_->position / 1000) * 1000000);
1054 return tmp;
1055 }
1056
ReleaseThread()1057 void Player::ReleaseThread()
1058 {
1059 if (lppContext_) {
1060 std::unique_lock<std::mutex> eosFlagMutexLock(lppContext_->eosFlagMutex);
1061 lppContext_->eosFlag_ = false;
1062 lppContext_->eosCond_.notify_all();
1063 eosFlagMutexLock.unlock();
1064 std::unique_lock<std::mutex> lock(lppContext_->inputMutex);
1065 lppContext_->returnFrame = true;
1066 lppContext_->inputCond.notify_all();
1067 lock.unlock();
1068 }
1069
1070 if (lppVideoContext_) {
1071 std::unique_lock<std::mutex> eosFlagMutexLock(lppVideoContext_->eosFlagMutex);
1072 lppVideoContext_->eosFlag_ = false;
1073 lppVideoContext_->eosCond_.notify_all();
1074 eosFlagMutexLock.unlock();
1075 std::unique_lock<std::mutex> lockVideo(lppVideoContext_->inputMutex);
1076 lppVideoContext_->returnFrame = true;
1077 lppVideoContext_->inputCond.notify_all();
1078 lockVideo.unlock();
1079 }
1080 if (LppDataNeededThread_ && LppDataNeededThread_->joinable()) {
1081 LppDataNeededThread_->join();
1082 LppDataNeededThread_.reset();
1083 }
1084 if (lppVideoDataNeededThread_ && lppVideoDataNeededThread_->joinable()) {
1085 lppVideoDataNeededThread_->join();
1086 lppVideoDataNeededThread_.reset();
1087 }
1088 }
1089
Release()1090 void Player::Release()
1091 {
1092 std::lock_guard<std::mutex> lock(mutex_);
1093 isStarted_ = false;
1094 isAudioDone = false;
1095 isVideoDone = false;
1096 // 清空队列
1097 while (audioDecContext_ && !audioDecContext_->renderQueue.empty()) {
1098 audioDecContext_->renderQueue.pop();
1099 }
1100 if (audioRenderer_ != nullptr) {
1101 OH_AudioRenderer_Release(audioRenderer_);
1102 audioRenderer_ = nullptr;
1103 }
1104 #ifdef DEBUG_DECODE
1105 if (audioOutputFile_.is_open()) {
1106 audioOutputFile_.close();
1107 }
1108 #endif
1109 ReleaseThread();
1110
1111 if (demuxer_ != nullptr) {
1112 demuxer_->Release();
1113 demuxer_.reset();
1114 }
1115 if (videoDecoder_ != nullptr) {
1116 videoDecoder_->Release();
1117 videoDecoder_.reset();
1118 }
1119 if (audioDecoder_ != nullptr) {
1120 audioDecoder_->Release();
1121 audioDecoder_.reset();
1122 }
1123 if (lppAudioStreamer_ != nullptr) {
1124 lppAudioStreamer_->Release();
1125 lppAudioStreamer_.reset();
1126 }
1127 if (lppVideoStreamer_ != nullptr) {
1128 lppVideoStreamer_->Release();
1129 lppVideoStreamer_.reset();
1130 }
1131 ReleaseDone();
1132 if (builder_ != nullptr) {
1133 OH_AudioStreamBuilder_Destroy(builder_);
1134 builder_ = nullptr;
1135 }
1136 doneCond_.notify_all();
1137 AVCODEC_SAMPLE_LOGI("Succeed");
1138 }
1139
ReleaseDone()1140 void Player::ReleaseDone()
1141 {
1142 if (videoDecContext_ != nullptr) {
1143 delete videoDecContext_;
1144 videoDecContext_ = nullptr;
1145 }
1146 if (audioDecContext_ != nullptr) {
1147 delete audioDecContext_;
1148 audioDecContext_ = nullptr;
1149 }
1150 if (lppContext_ != nullptr) {
1151 delete lppContext_;
1152 lppContext_ = nullptr;
1153 }
1154 if (lppVideoContext_ != nullptr) {
1155 delete lppVideoContext_;
1156 lppVideoContext_ = nullptr;
1157 }
1158 }
1159
LppDataNeededThread1()1160 void Player::LppDataNeededThread1()
1161 {
1162 int32_t totalSize = 4467270;
1163 AVCODEC_SAMPLE_LOGI("====== AVBUFFER1234 CREATE ======");
1164 OH_AVBuffer *avbuffer = OH_AVBuffer_Create(totalSize);
1165 while (true) {
1166 CHECK_AND_BREAK_LOG(isStarted_, "Decoder input thread out");
1167 // 到达EOS帧后不再送数据,直到Seek
1168 std::unique_lock<std::mutex> eosLock(lppContext_->eosMutex);
1169 lppContext_->eosCond_.wait_for(eosLock, 150000s, [this]() { return !lppContext_->eosFlag_; });
1170 eosLock.unlock();
1171 // 等待数据回调到达
1172 std::unique_lock<std::mutex> lock(lppContext_->inputMutex);
1173 lppContext_->inputCond.wait_for(lock, 150000s, [this]() { return lppContext_->returnFrame; });
1174 lppContext_->returnFrame = false;
1175 CHECK_AND_BREAK_LOG(isStarted_, "VD Decoder output thread out");
1176 lppContext_->count = 1;
1177 // 聚包数量
1178 int count = 10;
1179 while (count > 0) {
1180 CHECK_AND_BREAK_LOG(!lppContext_->eosFlag_, "AUDIO is EOS");
1181 count--;
1182 CodecBufferInfo bufferInfo(BUFFER_INFO_TYPE, avbuffer);
1183 demuxer_->ReadSample(demuxer_->GetAudioTrackId(), reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer),
1184 bufferInfo.attr);
1185 AVCODEC_SAMPLE_LOGI("size %{public}d", bufferInfo.attr.size);
1186 AVCODEC_SAMPLE_LOGI("pts %{public}ld", bufferInfo.attr.pts);
1187 this->progress = bufferInfo.attr.pts;
1188 int32_t remain = OH_AVSamplesBuffer_GetRemainedCapacity(lppContext_->framePacket_);
1189 AVCODEC_SAMPLE_LOGI("remain %{public}d", remain);
1190 AVCODEC_SAMPLE_LOGI("count %{public}d", count);
1191 AVCODEC_SAMPLE_LOGI("AUDIO pts %{public}ld", bufferInfo.attr.pts);
1192 // EOS帧置位
1193 if ((bufferInfo.attr.flags & AVCODEC_BUFFER_FLAGS_EOS)) {
1194 AVCODEC_SAMPLE_LOGI("Catch EOS, audio thread out");
1195 OH_AVBuffer_SetBufferAttr(reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer), &bufferInfo.attr);
1196 std::unique_lock<std::mutex> eosFlagMutexLock(lppContext_->eosFlagMutex);
1197 lppContext_->eosFlag_ = true;
1198 AVCODEC_SAMPLE_LOGI("AUDIO eosFlag_ %{public}d", lppContext_->eosFlag_);
1199 eosFlagMutexLock.unlock();
1200 }
1201 OH_AVSamplesBuffer_AppendOneBuffer(lppContext_->framePacket_,
1202 reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer));
1203 }
1204 usleep(BALANCE_VALUE);
1205 usleep(BALANCE_VALUE);
1206 int ret = lppAudioStreamer_->returnFrames(lppContext_);
1207 AVCODEC_SAMPLE_LOGW("lppAudioStreamer_ returnFrames ret:::%{public}d", ret);
1208 // 一次回调处理完再下一次
1209 lock.unlock();
1210 }
1211 AVCODEC_SAMPLE_LOGI("====== AVBUFFER1234 DESTROY ======");
1212 OH_AVBuffer_Destroy(avbuffer);
1213 }
1214
LppVideoDataNeededThread()1215 void Player::LppVideoDataNeededThread()
1216 {
1217 bool eosFlag = 0;
1218 int32_t totalSize = 4467270;
1219 AVCODEC_SAMPLE_LOGI("====== AVBUFFER1234 CREATE1 ======");
1220 OH_AVBuffer *avbuffer = OH_AVBuffer_Create(totalSize);
1221 while (true) {
1222 CHECK_AND_BREAK_LOG(isStarted_, "Decoder input thread out");
1223 std::unique_lock<std::mutex> eosLock(lppVideoContext_->eosMutex);
1224 lppVideoContext_->eosCond_.wait_for(eosLock, 150000s, [this]() { return !lppVideoContext_->eosFlag_; });
1225 std::unique_lock<std::mutex> lock(lppVideoContext_->inputMutex);
1226 lppVideoContext_->inputCond.wait_for(lock, 150000s, [this]() { return lppVideoContext_->returnFrame; });
1227 CHECK_AND_BREAK_LOG(isStarted_, "VD Decoder output thread out");
1228 lppVideoContext_->returnFrame = false;
1229 CHECK_AND_BREAK_LOG(isStarted_, "Work done, thread out");
1230 lppVideoContext_->count = VIDEO_FRAME_COUNT;
1231 AVCODEC_SAMPLE_LOGI("LppVideoDataNeededThread count %{public}d", lppVideoContext_->count);
1232 while (lppVideoContext_->count > 0) {
1233 CHECK_AND_BREAK_LOG(!lppVideoContext_->eosFlag_, "VIDEO is EOS");
1234 CodecBufferInfo bufferInfo(BUFFER_INFO_TYPE, avbuffer);
1235 demuxer_->ReadSample(demuxer_->GetVideoTrackId(), reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer),
1236 bufferInfo.attr);
1237 AVCODEC_SAMPLE_LOGI("LppVideoDataNeededThread ptsacc %{public}ld", bufferInfo.attr.pts);
1238 int32_t remain = OH_AVSamplesBuffer_GetRemainedCapacity(lppVideoContext_->framePacket_);
1239 if ((bufferInfo.attr.flags & AVCODEC_BUFFER_FLAGS_EOS)) {
1240 AVCODEC_SAMPLE_LOGI("Catch EOS, video thread out");
1241 OH_AVBuffer_SetBufferAttr(reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer), &bufferInfo.attr);
1242 std::unique_lock<std::mutex> eosFlagMutexLock(lppVideoContext_->eosFlagMutex);
1243 lppVideoContext_->eosFlag_ = true;
1244 AVCODEC_SAMPLE_LOGI("VIDEO eosFlag_ %{public}d", lppContext_->eosFlag_);
1245 eosFlagMutexLock.unlock();
1246 }
1247 OH_AVSamplesBuffer_AppendOneBuffer(lppVideoContext_->framePacket_,
1248 reinterpret_cast<OH_AVBuffer *>(bufferInfo.buffer));
1249 lppVideoContext_->count--;
1250 AVCODEC_SAMPLE_LOGI("LppVideoDataNeededThread count %{public}d", lppVideoContext_->count);
1251 }
1252 AVCODEC_SAMPLE_LOGI("LppVideoDataNeededThread returnFrames");
1253 AVCODEC_SAMPLE_LOGI("LppVideoDataNeededThread count %{public}d", lppVideoContext_->count);
1254 if (lppVideoContext_->count == VIDEO_FRAME_COUNT) {
1255 continue;
1256 }
1257 int ret = lppVideoStreamer_->returnFrames(lppVideoContext_);
1258 AVCODEC_SAMPLE_LOGI("LppVideoDataNeededThread returnFrames end");
1259 lock.unlock();
1260 }
1261 AVCODEC_SAMPLE_LOGI("====== AVBUFFER1234 DESTROY1 ======");
1262 OH_AVBuffer_Destroy(avbuffer);
1263 }
1264