2013-07-12 10:33:38 +04:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
#include "demuxmedia.h"
|
|
|
|
|
2013-11-11 13:52:14 +04:00
|
|
|
DemuxMedia::DemuxMedia(QObject *parent, QQueue<MediaPacket *> *videoQueue,
|
|
|
|
void *channel, int stream_id) : QObject(parent)
|
2012-12-27 06:48:17 +04:00
|
|
|
{
|
|
|
|
this->channel = channel;
|
|
|
|
this->stream_id = stream_id;
|
|
|
|
this->vcrFlag = 0;
|
2013-11-11 13:52:14 +04:00
|
|
|
this->elapsedTime = 0;
|
|
|
|
this->la_seekPos = -1;
|
|
|
|
this->isStopped = 0;
|
|
|
|
this->pausedTime = 0;
|
|
|
|
this->videoQueue = videoQueue;
|
2012-12-27 06:48:17 +04:00
|
|
|
|
2013-11-11 13:52:14 +04:00
|
|
|
playVideo = new PlayVideo(NULL, videoQueue, &sendMutex, channel, 101, 24);
|
2012-12-27 06:48:17 +04:00
|
|
|
playVideoThread = new QThread(this);
|
|
|
|
connect(playVideoThread, SIGNAL(started()), playVideo, SLOT(play()));
|
|
|
|
playVideo->moveToThread(playVideoThread);
|
2013-11-11 13:52:14 +04:00
|
|
|
|
|
|
|
playVideoThread->start();
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DemuxMedia::setVcrOp(int op)
|
|
|
|
{
|
|
|
|
vcrMutex.lock();
|
|
|
|
vcrFlag = op;
|
|
|
|
vcrMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DemuxMedia::startDemuxing()
|
|
|
|
{
|
|
|
|
MediaPacket *mediaPkt;
|
|
|
|
int is_video_frame;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
vcrMutex.lock();
|
|
|
|
switch (vcrFlag)
|
|
|
|
{
|
|
|
|
case VCR_PLAY:
|
|
|
|
vcrFlag = 0;
|
|
|
|
vcrMutex.unlock();
|
2013-11-11 13:52:14 +04:00
|
|
|
if (pausedTime)
|
|
|
|
{
|
|
|
|
elapsedTime = av_gettime() - pausedTime;
|
|
|
|
pausedTime = 0;
|
|
|
|
}
|
|
|
|
isStopped = false;
|
2012-12-27 06:48:17 +04:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VCR_PAUSE:
|
|
|
|
vcrMutex.unlock();
|
2013-11-11 13:52:14 +04:00
|
|
|
if (!pausedTime)
|
|
|
|
{
|
|
|
|
/* save amount of video played so far */
|
|
|
|
pausedTime = av_gettime() - elapsedTime;
|
|
|
|
}
|
2012-12-27 06:48:17 +04:00
|
|
|
usleep(1000 * 100);
|
2013-11-11 13:52:14 +04:00
|
|
|
isStopped = false;
|
2012-12-27 06:48:17 +04:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VCR_STOP:
|
|
|
|
vcrMutex.unlock();
|
2013-11-11 13:52:14 +04:00
|
|
|
|
|
|
|
if (isStopped)
|
|
|
|
{
|
|
|
|
usleep(1000 * 100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
elapsedTime = 0;
|
|
|
|
pausedTime = 0;
|
|
|
|
la_seekPos = -1;
|
|
|
|
xrdpvr_seek_media(0, 0);
|
|
|
|
isStopped = true;
|
2012-12-27 06:48:17 +04:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
vcrMutex.unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
delete mediaPkt;
|
|
|
|
usleep(1000 * 100);
|
2013-06-16 00:36:40 +04:00
|
|
|
xrdpvr_seek_media(0, 0);
|
2013-11-11 13:52:14 +04:00
|
|
|
this->elapsedTime = 0;
|
2012-12-27 06:48:17 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_video_frame)
|
2013-11-11 13:52:14 +04:00
|
|
|
{
|
|
|
|
sendMutex.lock();
|
|
|
|
#if 1
|
2012-12-27 06:48:17 +04:00
|
|
|
videoQueue->enqueue(mediaPkt);
|
2013-11-11 13:52:14 +04:00
|
|
|
#else
|
|
|
|
send_video_pkt(channel, stream_id, mediaPkt->av_pkt);
|
|
|
|
delete mediaPkt;
|
|
|
|
#endif
|
|
|
|
sendMutex.unlock();
|
|
|
|
}
|
2012-12-27 06:48:17 +04:00
|
|
|
else
|
2013-11-11 13:52:14 +04:00
|
|
|
{
|
|
|
|
int frame;
|
|
|
|
sendMutex.lock();
|
|
|
|
send_audio_pkt(channel, stream_id, mediaPkt->av_pkt);
|
|
|
|
sendMutex.unlock();
|
|
|
|
xrdpvr_read_ack(channel, &frame);
|
|
|
|
delete mediaPkt;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMediaPos();
|
|
|
|
if (elapsedTime == 0)
|
|
|
|
{
|
|
|
|
elapsedTime = av_gettime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* time elapsed in 1/100th sec units since play started */
|
|
|
|
emit onElapsedtime((av_gettime() - elapsedTime) / 10000);
|
|
|
|
|
2012-12-27 06:48:17 +04:00
|
|
|
|
|
|
|
} /* end while (1) */
|
|
|
|
}
|
|
|
|
|
2013-11-11 13:52:14 +04:00
|
|
|
void DemuxMedia::onMediaSeek(int value)
|
2012-12-27 06:48:17 +04:00
|
|
|
{
|
2013-11-11 13:52:14 +04:00
|
|
|
posMutex.lock();
|
|
|
|
la_seekPos = value;
|
|
|
|
posMutex.unlock();
|
2012-12-27 06:48:17 +04:00
|
|
|
}
|
|
|
|
|
2013-11-11 13:52:14 +04:00
|
|
|
void DemuxMedia::updateMediaPos()
|
2012-12-27 06:48:17 +04:00
|
|
|
{
|
2013-11-11 13:52:14 +04:00
|
|
|
posMutex.lock();
|
|
|
|
if (la_seekPos >= 0)
|
|
|
|
{
|
|
|
|
xrdpvr_seek_media(la_seekPos, 0);
|
|
|
|
elapsedTime = av_gettime() - la_seekPos * 1000000;
|
|
|
|
la_seekPos = -1;
|
|
|
|
}
|
|
|
|
posMutex.unlock();
|
2012-12-27 06:48:17 +04:00
|
|
|
}
|