summaryrefslogtreecommitdiffstats
path: root/vrplayer/playvideo.cpp
blob: 24ef492dfe56e28ae1300c608f4c6c0d1b7e5ae7 (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
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

#include <unistd.h>

#include "playvideo.h"
#include <QDebug>

PlayVideo::PlayVideo(QObject *parent,
                     QQueue<MediaPacket *> *videoQueue,
                     QMutex *sendMutex,
                     void *channel,
                     int stream_id) :
    QObject(parent)
{
    this->videoQueue = videoQueue;
    this->channel = channel;
    this->sendMutex = sendMutex;
    this->stream_id = stream_id;
    elapsedTime = 0;
    pausedTime = 0;
    la_seekPos = -1;
    vcrFlag = 0;
    isStopped = false;
}

void PlayVideo::play()
{
    MediaPacket *pkt;
    int usl;

    while (1)
    {
        vcrMutex.lock();
        switch (vcrFlag)
        {
            case VCR_PLAY:
                vcrFlag = 0;
                vcrMutex.unlock();
                if (pausedTime)
                {
                    elapsedTime = av_gettime() - pausedTime;
                    pausedTime = 0;
                }
                isStopped = false;
                continue;
                break;

            case VCR_PAUSE:
                vcrMutex.unlock();
                if (!pausedTime)
                {
                    /* save amount of video played so far */
                    pausedTime = av_gettime() - elapsedTime;
                }
                usleep(1000 * 100);
                isStopped = false;
                continue;
                break;

            case VCR_STOP:
                vcrMutex.unlock();
                if (isStopped)
                {
                    usleep(1000 * 100);
                    continue;
                }
                clearVideoQ();
                elapsedTime = 0;
                pausedTime = 0;
                la_seekPos = -1;
                xrdpvr_seek_media(0, 0);
                isStopped = true;
                continue;
                break;

            default:
                vcrMutex.unlock();
                goto label1;
                break;
        }

label1:

        if (videoQueue->isEmpty())
        {
            continue;
        }
        pkt = videoQueue->dequeue();
        sendMutex->lock();
        send_video_pkt(channel, stream_id, pkt->av_pkt);
        sendMutex->unlock();
        usl = pkt->delay_in_us;
        if (usl < 0)
        {
            usl = 0;
        }
        if (usl > 100 * 1000)
        {
            usl = 100 * 1000;
        }
        usleep(usl);
        delete pkt;
        updateMediaPos();
        if (elapsedTime == 0)
            elapsedTime = av_gettime();

        /* time elapsed in 1/100th sec units since play started */
        emit onElapsedtime((av_gettime() - elapsedTime) / 10000);
    }
}

void PlayVideo::onMediaRestarted()
{
    elapsedTime = av_gettime();
}

void PlayVideo::onMediaSeek(int value)
{
    posMutex.lock();
    la_seekPos = value;
    posMutex.unlock();
}

void PlayVideo::updateMediaPos()
{
#if 0
    if (elapsedTime == 0)
        elapsedTime = av_gettime();

    /* time elapsed in 1/100th sec units since play started */
    emit onElapsedtime((av_gettime() - elapsedTime) / 10000);
#endif

    posMutex.lock();
    if (la_seekPos >= 0)
    {
        //qDebug() << "seeking to" << la_seekPos;
        xrdpvr_seek_media(la_seekPos, 0);
        elapsedTime = av_gettime() - la_seekPos * 1000000;
        la_seekPos = -1;
    }
    posMutex.unlock();
}

void PlayVideo::setVcrOp(int op)
{
    vcrMutex.lock();
    this->vcrFlag = op;
    vcrMutex.unlock();
}

void PlayVideo::clearVideoQ()
{
    MediaPacket *pkt;

    while (!videoQueue->isEmpty())
    {
        pkt = videoQueue->dequeue();
        av_free_packet((AVPacket *) pkt->av_pkt);
        delete pkt;
    }
}

#if 0
void DecoderThread::updateSlider()
{
    if (elapsedTime == 0)
        elapsedTime = av_gettime();

    /* time elapsed in 1/100th sec units since play started */
    emit onElapsedtime((av_gettime() - elapsedTime) / 10000);

    mutex.lock();
    if (la_seekPos >= 0)
    {
        //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;
    }
    mutex.unlock();
}
#endif