summaryrefslogtreecommitdiffstats
path: root/vrplayer/demuxmedia.cpp
blob: 7a13619e6dc769ba1010faaaead8c733ae7b784e (plain)
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
#include "demuxmedia.h"

DemuxMedia::DemuxMedia(QObject *parent, QQueue<MediaPacket *> *audioQueue,
                       QQueue<MediaPacket *> *videoQueue, void *channel, int stream_id) :
    QObject(parent)
{
    this->audioQueue = audioQueue;
    this->videoQueue = videoQueue;
    this->channel = channel;
    this->stream_id = stream_id;
    this->threadsStarted = false;
    this->vcrFlag = 0;

    playAudio = new PlayAudio(NULL, audioQueue, &sendMutex, channel, 101);
    playAudioThread = new QThread(this);
    connect(playAudioThread, SIGNAL(started()), playAudio, SLOT(play()));
    playAudio->moveToThread(playAudioThread);

    playVideo = new PlayVideo(NULL, videoQueue, &sendMutex, channel, 101);
    playVideoThread = new QThread(this);
    connect(playVideoThread, SIGNAL(started()), playVideo, SLOT(play()));
    playVideo->moveToThread(playVideoThread);
}

void DemuxMedia::setVcrOp(int op)
{
    vcrMutex.lock();
    vcrFlag = op;
    vcrMutex.unlock();

    if (playVideo)
        playVideo->setVcrOp(op);

    if (playAudio)
        playAudio->setVcrOp(op);
}

void DemuxMedia::startDemuxing()
{
    MediaPacket *mediaPkt;
    int          is_video_frame;
    int          rv;

    if ((audioQueue == NULL) || (videoQueue == NULL))
        return;

    while (1)
    {
        vcrMutex.lock();
        switch (vcrFlag)
        {
        case VCR_PLAY:
            vcrFlag = 0;
            vcrMutex.unlock();
            continue;
            break;

        case VCR_PAUSE:
            vcrMutex.unlock();
            usleep(1000 * 100);
            continue;
            break;

        case VCR_STOP:
            vcrMutex.unlock();
            usleep(1000 * 100);
            continue;
            break;

        default:
            vcrMutex.unlock();
            break;
        }

        if ((audioQueue->count() >= 20) || (videoQueue->count() >= 20))
        {
            if (!threadsStarted)
                startAudioVideoThreads();

            usleep(1000 * 20);
        }

        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);
            continue;
        }

        if (is_video_frame)
            videoQueue->enqueue(mediaPkt);
        else
            audioQueue->enqueue(mediaPkt);

    } /* end while (1) */
}

PlayVideo * DemuxMedia::getPlayVideoInstance()
{
    return this->playVideo;
}

void DemuxMedia::startAudioVideoThreads()
{
    if (threadsStarted)
        return;

    playVideoThread->start();
    playAudioThread->start();
    threadsStarted = true;
}