2012-12-09 04:36:41 +04:00
|
|
|
#include "decoderthread.h"
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* o need to maintain aspect ratio while resizing
|
|
|
|
* o clicking in the middle of the slider bar shuld move the slider to the middle
|
|
|
|
* o need to be able to rewind the move when it is done playing
|
|
|
|
* o need to be able to load another move and play it w/o restarting player
|
|
|
|
* o pause button needs to work
|
|
|
|
* o need images for btns
|
|
|
|
*/
|
|
|
|
|
2012-12-09 04:36:41 +04:00
|
|
|
DecoderThread::DecoderThread()
|
|
|
|
{
|
|
|
|
channel = NULL;
|
|
|
|
geometry.setX(0);
|
|
|
|
geometry.setY(0);
|
|
|
|
geometry.setWidth(0);
|
|
|
|
geometry.setHeight(0);
|
|
|
|
stream_id = 101;
|
|
|
|
elapsedTime = 0;
|
2012-12-27 06:48:17 +04:00
|
|
|
la_seekPos = -1;
|
|
|
|
videoTimer = NULL;
|
|
|
|
audioTimer = NULL;
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderThread::run()
|
|
|
|
{
|
|
|
|
/* need a media file */
|
|
|
|
if (filename.length() == 0)
|
|
|
|
{
|
|
|
|
emit on_decoderErrorMsg("No media file",
|
|
|
|
"Please select a media file to play");
|
|
|
|
return;
|
|
|
|
}
|
2012-12-27 06:48:17 +04:00
|
|
|
}
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::startMediaPlay()
|
|
|
|
{
|
|
|
|
MediaPacket *mediaPkt;
|
|
|
|
int is_video_frame;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* setup video timer; each time this timer fires, it sends */
|
|
|
|
/* one video pkt to the client then resets the callback duration */
|
|
|
|
videoTimer = new QTimer;
|
|
|
|
connect(videoTimer, SIGNAL(timeout()), this, SLOT(videoTimerCallback()));
|
|
|
|
//videoTimer->start(1500);
|
|
|
|
|
|
|
|
/* setup audio timer; does the same as above, but with audio pkts */
|
|
|
|
audioTimer = new QTimer;
|
|
|
|
connect(audioTimer, SIGNAL(timeout()), this, SLOT(audioTimerCallback()));
|
|
|
|
//audioTimer->start(500);
|
|
|
|
|
|
|
|
/* setup pktTimer; each time this timer fires, it reads AVPackets */
|
|
|
|
/* and puts them into audio/video Queues */
|
|
|
|
pktTimer = new QTimer;
|
|
|
|
connect(pktTimer, SIGNAL(timeout()), this, SLOT(pktTimerCallback()));
|
|
|
|
|
|
|
|
while (1)
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
/* fill the audio/video queues with initial data; thereafter */
|
|
|
|
/* data will be filled by pktTimerCallback() */
|
|
|
|
if ((audioQueue.count() >= 3000) || (videoQueue.count() >= 3000))
|
|
|
|
{
|
|
|
|
//pktTimer->start(50);
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
//videoTimer->start(1500);
|
|
|
|
//audioTimer->start(500);
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
playVideo = new PlayVideo(NULL, &videoQueue, channel, 101);
|
|
|
|
playVideoThread = new QThread(this);
|
|
|
|
connect(playVideoThread, SIGNAL(started()), playVideo, SLOT(play()));
|
|
|
|
playVideo->moveToThread(playVideoThread);
|
|
|
|
playVideoThread->start();
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
playAudio = new PlayAudio(NULL, &audioQueue, channel, 101);
|
|
|
|
playAudioThread = new QThread(this);
|
|
|
|
connect(playAudioThread, SIGNAL(started()), playAudio, SLOT(play()));
|
|
|
|
playAudio->moveToThread(playAudioThread);
|
|
|
|
playAudioThread->start();
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
mediaPkt = new MediaPacket;
|
|
|
|
rv = xrdpvr_get_frame(&mediaPkt->av_pkt,
|
|
|
|
&is_video_frame,
|
|
|
|
&mediaPkt->delay_in_us);
|
|
|
|
if (rv < 0)
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
/* looks like we reached end of file */
|
|
|
|
break;
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
if (is_video_frame)
|
|
|
|
videoQueue.enqueue(mediaPkt);
|
|
|
|
else
|
|
|
|
audioQueue.enqueue(mediaPkt);
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
} /* end while (1) */
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderThread::on_mediaSeek(int value)
|
|
|
|
{
|
|
|
|
mutex.lock();
|
|
|
|
la_seekPos = value;
|
|
|
|
mutex.unlock();
|
2012-12-27 06:48:17 +04:00
|
|
|
|
|
|
|
qDebug() << "media seek value=" << value;
|
|
|
|
|
|
|
|
/* pktTimer stops at end of media; need to restart it */
|
|
|
|
if (!pktTimer->isActive())
|
|
|
|
{
|
|
|
|
updateSlider();
|
|
|
|
pktTimer->start(100);
|
|
|
|
}
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderThread::setFilename(QString filename)
|
|
|
|
{
|
|
|
|
this->filename = filename;
|
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::stopPlayer()
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
pktTimer->stop();
|
|
|
|
audioQueue.clear();
|
|
|
|
videoQueue.clear();
|
|
|
|
}
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::pausePlayer()
|
|
|
|
{
|
|
|
|
pktTimer->stop();
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::resumePlayer()
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
pktTimer->start(100);
|
|
|
|
}
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::close()
|
|
|
|
{
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::audioTimerCallback()
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
MediaPacket *pkt;
|
|
|
|
int delayInMs;
|
|
|
|
|
|
|
|
if (audioQueue.isEmpty())
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
qDebug() << "audioTimerCallback: got empty";
|
|
|
|
audioTimer->setInterval(100);
|
|
|
|
return;
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
pkt = audioQueue.dequeue();
|
|
|
|
delayInMs = (int) ((float) pkt->delay_in_us / 1000.0);
|
|
|
|
send_audio_pkt(channel, 101, pkt->av_pkt);
|
|
|
|
delete pkt;
|
|
|
|
|
|
|
|
//qDebug() << "audioTimerCallback: delay :" << delayInMs;
|
|
|
|
|
|
|
|
audioTimer->setInterval(delayInMs);
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::videoTimerCallback()
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
MediaPacket *pkt;
|
|
|
|
int delayInMs;
|
|
|
|
|
|
|
|
if (videoQueue.isEmpty())
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
qDebug() << "videoTimerCallback: GOT EMPTY";
|
|
|
|
videoTimer->setInterval(100);
|
|
|
|
return;
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
pkt = videoQueue.dequeue();
|
|
|
|
delayInMs = (int) ((float) pkt->delay_in_us / 1000.0);
|
|
|
|
send_video_pkt(channel, 101, pkt->av_pkt);
|
|
|
|
delete pkt;
|
|
|
|
updateSlider();
|
|
|
|
//qDebug() << "videoTimerCallback: delay :" << delayInMs;
|
|
|
|
videoTimer->setInterval(delayInMs);
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::pktTimerCallback()
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
MediaPacket *mediaPkt;
|
|
|
|
int is_video_frame;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
while (1)
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
qDebug() << "pktTimerCallback: audioCount=" << audioQueue.count() << "videoCount=" << videoQueue.count();
|
|
|
|
#if 1
|
|
|
|
if ((audioQueue.count() >= 20) || (videoQueue.count() >= 20))
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
if (videoQueue.count() >= 60)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
mediaPkt = new MediaPacket;
|
|
|
|
rv = xrdpvr_get_frame(&mediaPkt->av_pkt,
|
|
|
|
&is_video_frame,
|
|
|
|
&mediaPkt->delay_in_us);
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
/* looks like we reached end of file */
|
|
|
|
qDebug() << "###### looks like we reached EOF";
|
|
|
|
pktTimer->stop();
|
|
|
|
// LK_TODO set some flag so audio/video timer also stop when q is empty
|
|
|
|
return;
|
|
|
|
}
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
if (is_video_frame)
|
|
|
|
videoQueue.enqueue(mediaPkt);
|
|
|
|
else
|
|
|
|
audioQueue.enqueue(mediaPkt);
|
|
|
|
}
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
void DecoderThread::updateSlider()
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
if (elapsedTime == 0)
|
|
|
|
elapsedTime = av_gettime();
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
/* time elapsed in 1/100th sec units since play started */
|
|
|
|
emit on_elapsedtime((av_gettime() - elapsedTime) / 10000);
|
2012-12-09 04:36:41 +04:00
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
mutex.lock();
|
|
|
|
if (la_seekPos >= 0)
|
2012-12-09 04:36:41 +04:00
|
|
|
{
|
2012-12-27 06:48:17 +04:00
|
|
|
qDebug() << "seeking to" << la_seekPos;
|
|
|
|
//audioTimer->stop();
|
|
|
|
//videoTimer->stop();
|
|
|
|
xrdpvr_seek_media(la_seekPos, 0);
|
|
|
|
elapsedTime = av_gettime() - la_seekPos * 1000000;
|
|
|
|
//audioTimer->start(10);
|
|
|
|
//videoTimer->start(10);
|
|
|
|
la_seekPos = -1;
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|
2012-12-27 06:48:17 +04:00
|
|
|
mutex.unlock();
|
2012-12-09 04:36:41 +04:00
|
|
|
}
|