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

#include <unistd.h>
#include <sys/time.h>

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

PlayVideo::PlayVideo(QObject *parent,
                     QQueue<MediaPacket *> *videoQueue,
                     QMutex *sendMutex,
                     void *channel,
                     int stream_id, int fps) :
    QObject(parent)
{
    this->videoQueue = videoQueue;
    this->channel = channel;
    this->sendMutex = sendMutex;
    this->stream_id = stream_id;
    this->fps = fps;
}

/**
 ******************************************************************************/
static int
get_mstime(void)
{
    struct timeval tp;

    gettimeofday(&tp, 0);
    return (tp.tv_sec * 1000) + (tp.tv_usec / 1000);
}

void PlayVideo::play()
{
    MediaPacket *pkt;
    int now_time;
    int sleep_time;
    int last_display_time;

    last_display_time = 0;
    while (1)
    {
        sendMutex->lock();
        if (videoQueue->isEmpty())
        {
            sendMutex->unlock();
            usleep(10 * 1000);
            continue;
        }
        pkt = videoQueue->dequeue();
        send_video_pkt(channel, stream_id, pkt->av_pkt);
        sendMutex->unlock();
        now_time = get_mstime();
        sleep_time = now_time - last_display_time;
        if (sleep_time > (1000 / fps))
        {
            sleep_time = (1000 / fps);
        }
        if (sleep_time > 0)
        {
            usleep(sleep_time * 1000);
        }
        last_display_time = now_time;
        delete pkt;
    }
}