forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVPlayer.cpp
830 lines (768 loc) · 26.5 KB
/
AVPlayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2013 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include <QtAV/AVPlayer.h>
#include <qevent.h>
#include <qpainter.h>
#include <QApplication>
#include <QEvent>
#include <QtCore/QDir>
//#include <QtCore/QMetaObject>
#include <QtAV/AVDemuxer.h>
#include <QtAV/AudioFormat.h>
#include <QtAV/AudioResampler.h>
#include <QtAV/AudioThread.h>
#include <QtAV/Packet.h>
#include <QtAV/AudioDecoder.h>
#include <QtAV/VideoRenderer.h>
#include <QtAV/OutputSet.h>
#include <QtAV/AVClock.h>
#include <QtAV/QtAV_Compat.h>
#include <QtAV/VideoCapture.h>
#include <QtAV/VideoDecoderTypes.h>
#include <QtAV/WidgetRenderer.h>
#include <QtAV/VideoThread.h>
#include <QtAV/AVDemuxThread.h>
#include <QtAV/EventFilter.h>
#include <QtAV/VideoOutputEventFilter.h>
#include <QtAV/VideoCapture.h>
#include <QtAV/AudioOutput.h>
#if QTAV_HAVE(OPENAL)
#include <QtAV/AOOpenAL.h>
#endif //QTAV_HAVE(OPENAL)
#if QTAV_HAVE(PORTAUDIO)
#include <QtAV/AOPortAudio.h>
#endif //QTAV_HAVE(PORTAUDIO)
#include <QtAV/FilterManager.h>
namespace QtAV {
AVPlayer::AVPlayer(QObject *parent) :
QObject(parent)
, loaded(false)
, _renderer(0)
, _audio(0)
, audio_dec(0)
, video_dec(0)
, audio_thread(0)
, video_thread(0)
, event_filter(0)
, video_capture(0)
, mSpeed(1.0)
, ao_enable(true)
{
mpVOSet = new OutputSet(this);
mpAOSet = new OutputSet(this);
qDebug("%s", aboutQtAV_PlainText().toUtf8().constData());
/*
* call stop() before the window(_renderer) closed to stop the waitcondition
* If close the _renderer widget, the the _renderer may destroy before waking up.
*/
connect(qApp, SIGNAL(aboutToQuit()), SLOT(stop()));
clock = new AVClock(AVClock::AudioClock);
//clock->setClockType(AVClock::ExternalClock);
demuxer.setClock(clock);
connect(&demuxer, SIGNAL(started()), clock, SLOT(start()));
demuxer_thread = new AVDemuxThread(this);
demuxer_thread->setDemuxer(&demuxer);
setPlayerEventFilter(new EventFilter(this));
video_capture = new VideoCapture(this);
}
AVPlayer::~AVPlayer()
{
stop();
if (_audio) {
delete _audio;
_audio = 0;
}
if (audio_dec) {
delete audio_dec;
audio_dec = 0;
}
if (video_dec) {
delete video_dec;
video_dec = 0;
}
}
AVClock* AVPlayer::masterClock()
{
return clock;
}
void AVPlayer::addVideoRenderer(VideoRenderer *renderer)
{
if (!renderer) {
qWarning("add a null renderer!");
return;
}
renderer->setStatistics(&mStatistics);
QObject *voo = renderer->widget();
if (voo) {
//TODO: how to delete filter if no parent?
//the filtering object must be in the same thread as this object.
if (renderer->widget())
voo->installEventFilter(new VideoOutputEventFilter(renderer));
}
mpVOSet->addOutput(renderer);
}
void AVPlayer::removeVideoRenderer(VideoRenderer *renderer)
{
mpVOSet->removeOutput(renderer);
}
void AVPlayer::clearVideoRenderers()
{
mpVOSet->clearOutputs();
}
//TODO: check components compatiblity(also when change the filter chain)
void AVPlayer::setRenderer(VideoRenderer *r)
{
qDebug(">>>>>>>>>>%s", __FUNCTION__);
#if V1_2
if (_renderer && r) {
VideoRenderer::OutAspectRatioMode oar = _renderer->outAspectRatioMode();
r->setOutAspectRatioMode(oar);
if (oar == VideoRenderer::CustomAspectRation) {
r->setOutAspectRatio(_renderer->outAspectRatio());
}
}
setAVOutput(_renderer, r, video_thread);
if (_renderer) {
qDebug("resizeRenderer after setRenderer");
_renderer->resizeRenderer(_renderer->rendererSize()); //IMPORTANT: the swscaler will resize
}
#else
VideoRenderer *vo = renderer();
if (vo && r) {
VideoRenderer::OutAspectRatioMode oar = vo->outAspectRatioMode();
//r->resizeRenderer(vo->rendererSize());
r->setOutAspectRatioMode(oar);
if (oar == VideoRenderer::CustomAspectRation) {
r->setOutAspectRatio(vo->outAspectRatio());
}
}
if (r) {
r->resizeRenderer(r->rendererSize()); //IMPORTANT: the swscaler will resize
}
r->setStatistics(&mStatistics);
clearVideoRenderers();
addVideoRenderer(r);
#endif //V1_2
}
VideoRenderer *AVPlayer::renderer()
{
#if V1_2
return _renderer;
#else
//QList assert empty in debug mode
if (mpVOSet->outputs().isEmpty())
return 0;
return static_cast<VideoRenderer*>(mpVOSet->outputs().last());
#endif //V1_2
}
QList<VideoRenderer*> AVPlayer::videoOutputs()
{
QList<VideoRenderer*> vos;
vos.reserve(mpVOSet->outputs().size());
foreach (AVOutput *out, mpVOSet->outputs()) {
vos.append(static_cast<VideoRenderer*>(out));
}
return vos;
}
void AVPlayer::setAudioOutput(AudioOutput* ao)
{
qDebug(">>>>>>>>>>%s", __FUNCTION__);
setAVOutput(_audio, ao, audio_thread);
}
template<class Out>
void AVPlayer::setAVOutput(Out *&pOut, Out *pNew, AVThread *thread)
{
Out *old = pOut;
bool delete_old = false;
if (pOut == pNew) {
qDebug("output not changed: %p", pOut);
if (thread && thread->output() == pNew) {//avthread already set that output
qDebug("avthread already set that output");
return;
}
} else {
pOut = pNew;
delete_old = true;
}
if (!thread) {
qDebug("avthread not ready. can not set output.");
//no avthread, we can delete it safely
//AVOutput must be allocated in heap. Just like QObject's children.
if (delete_old) {
delete old;
old = 0;
}
return;
}
//FIXME: what if isPaused()==false but pause(true) in another thread?
bool need_lock = isPlaying() && !thread->isPaused();
if (need_lock)
thread->lock();
qDebug("set AVThread output");
thread->setOutput(pOut);
if (pOut) {
pOut->setStatistics(&mStatistics);
if (need_lock)
thread->unlock(); //??why here?
}
//now the old avoutput is not used by avthread, we can delete it safely
//AVOutput must be allocated in heap. Just like QObject's children.
if (delete_old) {
delete old;
old = 0;
}
}
AudioOutput* AVPlayer::audio()
{
return _audio;
}
void AVPlayer::enableAudio(bool enable)
{
ao_enable = enable;
}
void AVPlayer::disableAudio(bool disable)
{
ao_enable = !disable;
}
void AVPlayer::setMute(bool mute)
{
if (_audio)
_audio->setMute(mute);
}
bool AVPlayer::isMute() const
{
return !_audio || _audio->isMute();
}
void AVPlayer::setSpeed(qreal speed)
{
if (speed == mSpeed)
return;
mSpeed = speed;
//TODO: check clock type?
if (_audio && _audio->isAvailable()) {
qDebug("set speed %.2f", mSpeed);
_audio->setSpeed(mSpeed);
} else {
masterClock()->setSpeed(mSpeed);
}
emit speedChanged(mSpeed);
}
qreal AVPlayer::speed() const
{
return mSpeed;
}
//setPlayerEventFilter(0) will remove the previous event filter
void AVPlayer::setPlayerEventFilter(QObject *obj)
{
if (event_filter) {
qApp->removeEventFilter(event_filter);
delete event_filter;
}
event_filter = obj; //the default event filter's parent is this, so AVPlayer will try to delete event_filter
if (obj) {
qApp->installEventFilter(event_filter);
}
}
Statistics& AVPlayer::statistics()
{
return mStatistics;
}
const Statistics& AVPlayer::statistics() const
{
return mStatistics;
}
bool AVPlayer::installAudioFilter(Filter *filter)
{
if (!FilterManager::instance().registerAudioFilter(filter, this)) {
return false;
}
if (!audio_thread)
return false; //install later when avthread created
return audio_thread->installFilter(filter);
}
bool AVPlayer::installVideoFilter(Filter *filter)
{
if (!FilterManager::instance().registerVideoFilter(filter, this)) {
return false;
}
if (!video_thread)
return false; //install later when avthread created
return video_thread->installFilter(filter);
}
bool AVPlayer::uninstallFilter(Filter *filter)
{
if (!FilterManager::instance().unregisterFilter(filter)) {
qWarning("unregister filter %p failed", filter);
return false;
}
/*
* TODO: send FilterUninstallTask(this, filter){this.mFilters.remove} to
* active player's AVThread
*
*/
class UninstallFilterTask : public QRunnable {
public:
UninstallFilterTask(AVThread *thread, Filter *filter):
QRunnable()
, mpThread(thread)
, mpFilter(filter)
{
setAutoDelete(true);
}
virtual void run() {
mpThread->uninstallFilter(mpFilter, false);
//QMetaObject::invokeMethod(FilterManager::instance(), "onUninstallInTargetDone", Qt::AutoConnection, Q_ARG(Filter*, filter));
FilterManager::instance().emitOnUninstallInTargetDone(mpFilter);
}
private:
AVThread *mpThread;
Filter *mpFilter;
};
AVThread *avthread = video_thread;
if (!avthread || !avthread->filters().contains(filter)) {
avthread = audio_thread;
}
if (!avthread || !avthread->filters().contains(filter)) {
return false;
}
avthread->scheduleTask(new UninstallFilterTask(avthread, filter));
return true;
}
/*
* loaded state is the state of current setted file.
* For replaying, we can avoid load a seekable file again.
* For playing a new file, load() is required.
*/
void AVPlayer::setFile(const QString &path)
{
this->path = path;
loaded = false; //
//qApp->activeWindow()->setWindowTitle(path); //crash on linux
}
QString AVPlayer::file() const
{
return path;
}
VideoCapture* AVPlayer::videoCapture()
{
return video_capture;
}
bool AVPlayer::captureVideo()
{
if (!video_capture || !video_thread)
return false;
bool pause_old = isPaused();
if (!video_capture->isAsync())
pause(true);
QString cap_name = QFileInfo(path).completeBaseName();
//FIXME: pts is not correct because of multi-thread
double pts = video_thread->currentPts();
video_capture->setCaptureName(cap_name + "_" + QString::number(pts, 'f', 3));
video_capture->request();
if (!video_capture->isAsync())
pause(pause_old);
return true;
}
bool AVPlayer::play(const QString& path)
{
setFile(path);
play();
return true;//isPlaying();
}
bool AVPlayer::isPlaying() const
{
return (demuxer_thread &&demuxer_thread->isRunning())
|| (audio_thread && audio_thread->isRunning())
|| (video_thread && video_thread->isRunning());
}
void AVPlayer::togglePause()
{
pause(!isPaused());
}
void AVPlayer::pause(bool p)
{
//pause thread. check pause state?
demuxer_thread->pause(p);
if (audio_thread)
audio_thread->pause(p);
if (video_thread)
video_thread->pause(p);
clock->pause(p);
emit paused(p);
}
bool AVPlayer::isPaused() const
{
return (demuxer_thread && demuxer_thread->isPaused())
|| (audio_thread && audio_thread->isPaused())
|| (video_thread && video_thread->isPaused());
}
bool AVPlayer::isLoaded() const
{
return loaded;
}
bool AVPlayer::load(const QString &path)
{
setFile(path);
return load();
}
bool AVPlayer::load()
{
loaded = false;
if (path.isEmpty()) {
qDebug("No file to play...");
return loaded;
}
qDebug("loading: %s ...", path.toUtf8().constData());
if (!demuxer.loadFile(path)) {
return loaded;
}
loaded = true;
demuxer.dump();
formatCtx = demuxer.formatContext();
aCodecCtx = demuxer.audioCodecContext();
vCodecCtx = demuxer.videoCodecContext();
setupAudioThread();
setupVideoThread();
return loaded;
}
qreal AVPlayer::duration() const
{
return qreal(demuxer.duration())/qreal(AV_TIME_BASE); //AVFrameContext.duration time base: AV_TIME_BASE
}
//FIXME: why no demuxer will not get an eof if replaying by seek(0)?
void AVPlayer::play()
{
if (isPlaying())
stop();
/*
* avoid load mutiple times when replaying the same seekable file
* TODO: force load unseekable stream? avio.seekable. currently you
* must setFile() agian to reload an unseekable stream
*/
//FIXME: seek(0) for audio without video crashes, why?
//TODO: no eof if replay by seek(0)
if (true || !isLoaded() || !vCodecCtx) { //if (!isLoaded() && !load())
if (!load()) {
mStatistics.reset();
return;
} else {
initStatistics();
}
} else {
qDebug("seek(0)");
demuxer.seek(0); //FIXME: now assume it is seekable. for unseekable, setFile() again
}
Q_ASSERT(clock != 0);
clock->reset();
if (vCodecCtx && video_thread) {
qDebug("Starting video thread...");
video_thread->start();
}
if (aCodecCtx && audio_thread) {
qDebug("Starting audio thread...");
audio_thread->start();
}
demuxer_thread->start();
//blockSignals(false);
emit started();
}
void AVPlayer::stop()
{
if (!isPlaying())
return;
qDebug("AVPlayer::stop");
//blockSignals(true); //TODO: move emit stopped() before it. or connect avthread.finished() to tryEmitStop() {if (!called_by_stop) emit}
struct avthreads_t {
AVThread *thread;
const char *name;
} threads[] = {
{ audio_thread, "audio thread" },
{ video_thread, "video thread" },
{ 0, 0 }
};
for (int i = 0; threads[i].name; ++i) {
AVThread *thread = threads[i].thread;
if (!thread)
continue;
if (thread->isRunning()) {
qDebug("stopping %s...", threads[i].name);
//avoid emit stopped multiple times. AVThread.stopped() connects to AVDemuxThread.stopped().
thread->blockSignals(true);
thread->stop();
if (!thread->wait(1000)) {
qWarning("Timeout waiting for %s stopped. Terminate it.", threads[i].name);
thread->terminate();
}
thread->blockSignals(false);
}
}
//stop demux thread after avthread is better. otherwise demux thread may be terminated when waiting for avthread ?
if (demuxer_thread->isRunning()) {
qDebug("stopping demux thread...");
demuxer_thread->stop();
//wait for finish then we can safely set the vars, e.g. a/v decoders
if (!demuxer_thread->wait(1000)) {
qWarning("Timeout waiting for demux thread stopped. Terminate it.");
demuxer_thread->terminate(); //Terminate() causes the wait condition destroyed without waking up
}
}
qDebug("all threads [a|v|d] stopped...");
emit stopped();
}
//FIXME: If not playing, it will just play but not play one frame.
void AVPlayer::playNextFrame()
{
if (!isPlaying()) {
play();
}
demuxer_thread->pause(false);
clock->pause(false);
if (audio_thread)
audio_thread->nextAndPause();
if (video_thread)
video_thread->nextAndPause();
clock->pause(true);
demuxer_thread->pause(true);
}
void AVPlayer::seek(qreal pos)
{
if (!demuxer_thread->isRunning())
return;
demuxer_thread->seek(pos);
}
void AVPlayer::seekForward()
{
if (!demuxer_thread->isRunning())
return;
demuxer_thread->seekForward();
qDebug("seek %f%%", clock->value()/duration()*100.0);
}
void AVPlayer::seekBackward()
{
if (!demuxer_thread->isRunning())
return;
demuxer_thread->seekBackward();
qDebug("seek %f%%", clock->value()/duration()*100.0);
}
void AVPlayer::updateClock(qint64 msecs)
{
clock->updateExternalClock(msecs);
}
//TODO: av_guess_frame_rate in latest ffmpeg
void AVPlayer::initStatistics()
{
mStatistics.reset();
mStatistics.url = path;
mStatistics.bit_rate = formatCtx->bit_rate;
mStatistics.format = formatCtx->iformat->name;
//AV_TIME_BASE_Q: msvc error C2143
mStatistics.start_time = QTime(0, 0, 0).addMSecs(int((qreal)formatCtx->start_time/(qreal)AV_TIME_BASE*1000.0));
mStatistics.duration = QTime(0, 0, 0).addMSecs(int((qreal)formatCtx->duration/(qreal)AV_TIME_BASE*1000.0));
struct common_statistics_t {
int stream_idx;
AVCodecContext *ctx;
Statistics::Common *st;
const char *name;
} common_statistics[] = {
{ demuxer.audioStream(), aCodecCtx, &mStatistics.audio, "audio"},
{ demuxer.videoStream(), vCodecCtx, &mStatistics.video, "video"},
{ 0, 0, 0, 0}
};
for (int i = 0; common_statistics[i].name; ++i) {
common_statistics_t cs = common_statistics[i];
if (cs.stream_idx < 0)
continue;
AVStream *stream = formatCtx->streams[cs.stream_idx];
qDebug("stream: %p, duration=%lld (%d ms==%f), time_base=%f", stream, stream->duration, int(qreal(stream->duration)*av_q2d(stream->time_base)*1000.0)
, duration(), av_q2d(stream->time_base));
cs.st->available = true;
cs.st->codec = cs.ctx->codec->name;
cs.st->codec_long = cs.ctx->codec->long_name;
cs.st->total_time = QTime(0, 0, 0).addMSecs(int(qreal(stream->duration)*av_q2d(stream->time_base)*1000.0));
cs.st->start_time = QTime(0, 0, 0).addMSecs(int(qreal(stream->start_time)*av_q2d(stream->time_base)*1000.0));
//FIXME: which 1 should we choose? avg_frame_rate may be nan, r_frame_rate may be wrong(guessed value)
qDebug("codec: %s(%s)", qPrintable(cs.st->codec), qPrintable(cs.st->codec_long));
if (stream->avg_frame_rate.num) //avg_frame_rate.num,den may be 0
cs.st->fps_guess = av_q2d(stream->avg_frame_rate);
else
cs.st->fps_guess = av_q2d(stream->r_frame_rate);
cs.st->fps = cs.st->fps_guess;
cs.st->bit_rate = cs.ctx->bit_rate; //formatCtx
cs.st->avg_frame_rate = av_q2d(stream->avg_frame_rate);
cs.st->frames = stream->nb_frames;
//qDebug("time: %f~%f, nb_frames=%lld", cs.st->start_time, cs.st->total_time, stream->nb_frames); //why crash on mac? av_q2d({0,0})?
qDebug("%s fps: r_frame_rate=%f avg_frame_rate=%f", cs.name, av_q2d(stream->r_frame_rate), av_q2d(stream->avg_frame_rate));
}
if (demuxer.audioStream() >= 0) {
mStatistics.audio_only.block_align = aCodecCtx->block_align;
mStatistics.audio_only.channels = aCodecCtx->channels;
char cl[128]; //
av_get_channel_layout_string(cl, sizeof(cl), -1, aCodecCtx->channel_layout); //TODO: ff version
mStatistics.audio_only.channel_layout = cl;
mStatistics.audio_only.sample_fmt = av_get_sample_fmt_name(aCodecCtx->sample_fmt);
mStatistics.audio_only.frame_number = aCodecCtx->frame_number;
mStatistics.audio_only.frame_size = aCodecCtx->frame_size;
mStatistics.audio_only.sample_rate = aCodecCtx->sample_rate;
}
if (demuxer.videoStream() >= 0) {
mStatistics.video.frames = formatCtx->streams[demuxer.videoStream()]->nb_frames;
mStatistics.video_only.coded_height = vCodecCtx->coded_height;
mStatistics.video_only.coded_width = vCodecCtx->coded_width;
mStatistics.video_only.gop_size = vCodecCtx->gop_size;
mStatistics.video_only.pix_fmt = av_get_pix_fmt_name(vCodecCtx->pix_fmt);
mStatistics.video_only.height = vCodecCtx->height;
mStatistics.video_only.width = vCodecCtx->width;
}
}
void AVPlayer::setupAudioThread()
{
if (aCodecCtx) {
qDebug("has audio");
//TODO: setAudioOutput() like vo
if (!_audio && ao_enable) {
qDebug("new audio output");
#if QTAV_HAVE(OPENAL)
_audio = new AOOpenAL();
#elif QTAV_HAVE(PORTAUDIO)
_audio = new AOPortAudio();
#endif
}
if (!_audio) {
masterClock()->setClockType(AVClock::ExternalClock);
//return;
} else {
_audio->audioFormat().setSampleFormat(AudioFormat::SampleFormat_Float);
_audio->audioFormat().setSampleRate(aCodecCtx->sample_rate);
// 5, 6, 7 may not play
if (aCodecCtx->channels > 2)
_audio->audioFormat().setChannelLayoutFFmpeg(AV_CH_LAYOUT_STEREO);
else
_audio->audioFormat().setChannels(aCodecCtx->channels);
if (!_audio->open()) {
//return; //audio not ready
}
}
if (!audio_dec) {
audio_dec = new AudioDecoder();
}
qDebug("setCodecContext");
audio_dec->setCodecContext(aCodecCtx);
if (_audio)
audio_dec->resampler()->setOutAudioFormat(_audio->audioFormat());
audio_dec->resampler()->inAudioFormat().setSampleFormatFFmpeg(aCodecCtx->sample_fmt);
audio_dec->resampler()->inAudioFormat().setSampleRate(aCodecCtx->sample_rate);
audio_dec->resampler()->inAudioFormat().setChannels(aCodecCtx->channels);
audio_dec->resampler()->inAudioFormat().setChannelLayoutFFmpeg(aCodecCtx->channel_layout);
audio_dec->prepare();
if (!audio_thread) {
qDebug("new audio thread");
audio_thread = new AudioThread(this);
audio_thread->setClock(clock);
audio_thread->setDecoder(audio_dec);
audio_thread->setStatistics(&mStatistics);
qDebug("demux thread setAudioThread");
demuxer_thread->setAudioThread(audio_thread);
//reconnect if disconnected
QList<Filter*> filters = FilterManager::instance().audioFilters(this);
//TODO: isEmpty()==false but size() == 0 in debug mode, it's a Qt bug? we can not just foreach without check empty in debug mode
if (filters.size() > 0) {
foreach (Filter *filter, filters) {
audio_thread->installFilter(filter);
}
}
}
setAudioOutput(_audio);
/*if has video, connect video's only to avoid emitting stopped() mulltiple times
*otherwise connect audio's. but if video exists previously and now is null.
*audio thread will not change. so connection should be here
*/
if (!vCodecCtx) {
disconnect(audio_thread, SIGNAL(finished()), this, SIGNAL(stopped()));
connect(audio_thread, SIGNAL(finished()), this, SIGNAL(stopped()), Qt::DirectConnection);
}
int queue_min = 0.61803*qMax<qreal>(24.0, mStatistics.video.fps);
int queue_max = int(1.61803*(qreal)queue_min); //about 1 second
audio_thread->packetQueue()->setThreshold(queue_min);
audio_thread->packetQueue()->setCapacity(queue_max);
} else {
//set 0 before delete because demux thread will use the address
//TODO: use avthread** ?
demuxer_thread->setAudioThread(0);
if (audio_thread) {
qDebug("release audio thread.");
delete audio_thread;
audio_thread = 0; //shared ptr?
}
if (audio_dec) {
delete audio_dec;
audio_dec = 0;
}
//DO NOT delete AVOutput. it is setted by user
}
}
void AVPlayer::setupVideoThread()
{
if (vCodecCtx) {
if (!video_dec) {
video_dec = VideoDecoderFactory::create(VideoDecoderId_FFmpeg);
}
video_dec->setCodecContext(vCodecCtx);
video_dec->prepare();
if (!video_thread) {
video_thread = new VideoThread(this);
video_thread->setClock(clock);
video_thread->setDecoder(video_dec);
video_thread->setStatistics(&mStatistics);
video_thread->setVideoCapture(video_capture);
video_thread->setOutputSet(mpVOSet);
demuxer_thread->setVideoThread(video_thread);
//reconnect if disconnected
connect(video_thread, SIGNAL(finished()), this, SIGNAL(stopped()), Qt::DirectConnection);
QList<Filter*> filters = FilterManager::instance().videoFilters(this);
if (filters.size() > 0) {
foreach (Filter *filter, filters) {
video_thread->installFilter(filter);
}
}
}
#if V1_2
setRenderer(_renderer);
#endif
int queue_min = 0.61803*qMax<qreal>(24.0, mStatistics.video.fps);
int queue_max = int(1.61803*(qreal)queue_min); //about 1 second
video_thread->packetQueue()->setThreshold(queue_min);
video_thread->packetQueue()->setCapacity(queue_max);
} else {
demuxer_thread->setVideoThread(0); //set 0 before delete. ptr is used in demux thread when set 0
if (video_thread) {
qDebug("release video thread.");
delete video_thread;
video_thread = 0;//shared ptr?
}
if (video_dec) { //TODO: should the decoder managed by avthread?
delete video_dec;
video_dec = 0;
}
//DO NOT delete AVOutput. it is setted by user
}
}
void AVPlayer::setupAVThread(AVThread *&thread, AVCodecContext *ctx)
{
Q_UNUSED(thread);
Q_UNUSED(ctx);
}
} //namespace QtAV