forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVDemuxThread.cpp
364 lines (336 loc) · 11.2 KB
/
AVDemuxThread.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
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2014 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/AVDemuxThread.h>
#include <QtAV/AVDemuxer.h>
#include <QtAV/AVDecoder.h>
#include <QtAV/Packet.h>
#include <QtAV/AVThread.h>
#include <QtCore/QTimer>
#include <QtCore/QEventLoop>
#define CORRECT_END 1
namespace QtAV {
class QueueEmptyCall : public PacketQueue::StateChangeCallback
{
public:
QueueEmptyCall(AVDemuxThread* thread):
mDemuxThread(thread)
{}
virtual void call() {
if (!mDemuxThread)
return;
if (mDemuxThread->isEnd())
return;
AVThread *thread = mDemuxThread->videoThread();
//qDebug("try wake up video queue");
if (thread)
thread->packetQueue()->blockFull(false);
//qDebug("try wake up audio queue");
thread = mDemuxThread->audioThread();
if (thread)
thread->packetQueue()->blockFull(false);
}
private:
AVDemuxThread *mDemuxThread;
};
AVDemuxThread::AVDemuxThread(QObject *parent) :
QThread(parent),paused(false),seeking(false),end(true)
,demuxer(0)
,audio_thread(0),video_thread(0)
{
}
AVDemuxThread::AVDemuxThread(AVDemuxer *dmx, QObject *parent) :
QThread(parent),paused(false),seeking(false),end(true)
,audio_thread(0),video_thread(0)
, running_threads(0)
{
setDemuxer(dmx);
}
void AVDemuxThread::setDemuxer(AVDemuxer *dmx)
{
demuxer = dmx;
}
void AVDemuxThread::setAVThread(AVThread*& pOld, AVThread *pNew)
{
if (pOld == pNew)
return;
if (pOld) {
if (pOld->isRunning())
pOld->stop();
if (running_threads > 0)
--running_threads; //need it?
qDebug("AVDemuxThread::setAVThread running_threads=%d", running_threads);
#if CORRECT_END
disconnect(pOld, SIGNAL(terminated()), this, SLOT(notifyEnd()));
disconnect(pOld, SIGNAL(finished()), this, SLOT(notifyEnd()));
#endif //CORRECT_END
}
pOld = pNew;
if (!pNew)
return;
#if CORRECT_END
connect(pOld, SIGNAL(terminated()), this, SLOT(notifyEnd()));
connect(pOld, SIGNAL(finished()), this, SLOT(notifyEnd()));
#endif //CORRECT_END
pOld->packetQueue()->setEmptyCallback(new QueueEmptyCall(this));
}
void AVDemuxThread::setAudioThread(AVThread *thread)
{
setAVThread(audio_thread, thread);
}
void AVDemuxThread::setVideoThread(AVThread *thread)
{
setAVThread(video_thread, thread);
}
AVThread* AVDemuxThread::videoThread()
{
return video_thread;
}
AVThread* AVDemuxThread::audioThread()
{
return audio_thread;
}
void AVDemuxThread::seek(qint64 pos)
{
qDebug("demux thread start to seek...");
seeking = true;
end = false;
if (audio_thread) {
audio_thread->setDemuxEnded(false);
audio_thread->packetQueue()->clear();
}
if (video_thread) {
video_thread->setDemuxEnded(false);
video_thread->packetQueue()->clear();
}
demuxer->seek(pos);
// TODO: why queue may not empty?
if (audio_thread) {
audio_thread->packetQueue()->clear();
audio_thread->packetQueue()->put(Packet());
}
if (video_thread) {
video_thread->packetQueue()->clear();
video_thread->packetQueue()->put(Packet());
}
//if (subtitle_thread) {
// subtitle_thread->packetQueue()->clear();
// subtitle_thread->packetQueue()->put(Packet());
//}
seeking = false;
seek_cond.wakeAll();
if (isPaused()) {
pause(false);
AVThread *avthread = video_thread;
if (!avthread)
avthread = audio_thread;
avthread->pause(false);
QEventLoop loop;
QTimer::singleShot(40, &loop, SLOT(quit()));
loop.exec();
pause(true);
avthread->pause(true);
}
}
bool AVDemuxThread::isPaused() const
{
return paused;
}
bool AVDemuxThread::isEnd() const
{
return end;
}
//No more data to put. So stop blocking the queue to take the reset elements
void AVDemuxThread::stop()
{
qDebug("void AVDemuxThread::stop()");
end = true;
//this will not affect the pause state if we pause the output
//TODO: why remove blockFull(false) can not play another file?
if (audio_thread) {
audio_thread->setDemuxEnded(true);
audio_thread->packetQueue()->clear();
audio_thread->packetQueue()->blockFull(false); //??
}
if (video_thread) {
video_thread->setDemuxEnded(true);
video_thread->packetQueue()->clear();
video_thread->packetQueue()->blockFull(false); //?
}
pause(false);
seek_cond.wakeAll();
}
void AVDemuxThread::pause(bool p)
{
qDebug("demux thread pause %d", p);
if (paused == p)
return;
paused = p;
if (!paused)
cond.wakeAll();
}
void AVDemuxThread::notifyEnd()
{
pause(false);
seek_cond.wakeAll();
cond.wakeAll();
// not direct connect, in receiver's thread. change running_threads is ok
--running_threads;
if (running_threads > 0) {
qDebug("%d avthread not finished....", running_threads);
return;
}
qDebug("all avthread finished. notify demux thread<<<<<<");
end = true;
if (audio_thread) {
audio_thread->setDemuxEnded(true);
audio_thread->packetQueue()->clear();
audio_thread->packetQueue()->blockFull(false); //??
}
if (video_thread) {
video_thread->setDemuxEnded(true);
video_thread->packetQueue()->clear();
video_thread->packetQueue()->blockFull(false); //?
}
}
void AVDemuxThread::run()
{
end = false;
if (audio_thread && !audio_thread->isRunning())
audio_thread->start(QThread::HighPriority);
if (video_thread && !video_thread->isRunning())
video_thread->start();
running_threads = 0;
if (audio_thread)
++running_threads;
if (video_thread)
++running_threads;
qDebug("demux thread start running...%d avthreads", running_threads);
audio_stream = demuxer->audioStream();
video_stream = demuxer->videoStream();
int index = 0;
Packet pkt;
pause(false);
qDebug("get av queue a/v thread = %p %p", audio_thread, video_thread);
PacketQueue *aqueue = audio_thread ? audio_thread->packetQueue() : 0;
PacketQueue *vqueue = video_thread ? video_thread->packetQueue() : 0;
if (aqueue) {
aqueue->clear();
aqueue->setBlocking(true);
}
if (vqueue) {
vqueue->clear();
vqueue->setBlocking(true);
}
while (!end) {
if (tryPause())
continue; //the queue is empty and will block
QMutexLocker locker(&buffer_mutex);
Q_UNUSED(locker);
if (end) {
#if CORRECT_END
if ((audio_thread && audio_thread->isRunning())
|| (video_thread && video_thread->isRunning())) {
qDebug("demux read end but avthread still running. waiting for finish...");
cond.wait(&buffer_mutex);
}
if (end)
#endif //CORRECT_END
break;
}
if (seeking) {
qDebug("Demuxer is seeking... wait for seek end");
if (!seek_cond.wait(&buffer_mutex, 200)) { //will return the same state(i.e. lock)
qWarning("seek timed out");
}
}
if (!demuxer->readFrame()) {
continue;
}
index = demuxer->stream();
pkt = *demuxer->packet(); //TODO: how to avoid additional copy?
//connect to stop is ok too
if (pkt.isEnd()) {
qDebug("read end packet %d A:%d V:%d", index, audio_stream, video_stream);
end = true;
bool all_end = true;
//avthread can stop. do not clear queue, make sure all data are played
if (audio_thread) {
audio_thread->setDemuxEnded(true);
all_end &= !audio_thread->isRunning();
}
if (video_thread) {
video_thread->setDemuxEnded(true);
all_end &= !video_thread->isRunning();
}
if (aqueue)
aqueue->put(Packet());
if (vqueue)
vqueue->put(Packet());
if (!all_end) {
cond.wait(&buffer_mutex);
}
break;
}
/*1 is empty but another is enough, then do not block to
ensure the empty one can put packets immediatly.
But usually it will not happen, why?
*/
/* demux thread will be blocked only when 1 queue is full and still put
* if vqueue is full and aqueue becomes empty, then demux thread
* will be blocked. so we should wake up another queue when empty(or threshold?).
* TODO: the video stream and audio stream may be group by group. provide it
* stream data: aaaaaaavvvvvvvaaaaaaaavvvvvvvvvaaaaaa, it happens
* stream data: aavavvavvavavavavavavavavvvaavavavava, it's ok
*/
//TODO: use cache queue, take from cache queue if not empty?
if (index == audio_stream) {
/* if vqueue if not blocked and full, and aqueue is empty, then put to
* vqueue will block demuex thread
*/
if (aqueue) {
if (vqueue)
aqueue->blockFull(vqueue->isEnough());
aqueue->put(pkt); //affect video_thread
}
} else if (index == video_stream) {
if (vqueue) {
if (aqueue)
vqueue->blockFull(aqueue->isEnough());
vqueue->put(pkt); //affect audio_thread
}
} else { //subtitle
continue;
}
}
//flush. seeking will be omitted when stopped
if (aqueue)
aqueue->put(Packet());
if (vqueue)
vqueue->put(Packet());
running_threads = 0;
qDebug("Demux thread stops running....");
}
bool AVDemuxThread::tryPause()
{
if (!paused)
return false;
QMutexLocker lock(&buffer_mutex);
Q_UNUSED(lock);
cond.wait(&buffer_mutex);
return true;
}
} //namespace QtAV