summaryrefslogtreecommitdiff
path: root/src/video/VideoDemuxerThread.cpp
blob: 370e23761aaa8506a88b0a1df238a1bf7284f4c0 (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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "VideoDemuxerThread.h"

#include "../base/TimeSource.h"

using namespace std;

namespace avg {

VideoDemuxerThread::VideoDemuxerThread(CQueue& cmdQ, AVFormatContext* pFormatContext,
        const map<int, VideoMsgQueuePtr>& packetQs)
    : WorkerThread<VideoDemuxerThread>("VideoDemuxer", cmdQ),
      m_PacketQs(packetQs),
      m_bEOF(false),
      m_pFormatContext(pFormatContext),
      m_pDemuxer()
{
    map<int, VideoMsgQueuePtr>::iterator it;
    for (it = m_PacketQs.begin(); it != m_PacketQs.end(); it++) {
        int streamIndex = it->first;
        m_PacketQEOFMap[streamIndex] = false;
    }
}

VideoDemuxerThread::~VideoDemuxerThread()
{
}

bool VideoDemuxerThread::init()
{
    map<int, VideoMsgQueuePtr>::iterator it;
    vector<int> streamIndexes;
    for (it = m_PacketQs.begin(); it != m_PacketQs.end(); it++) {
        streamIndexes.push_back(it->first);
    }
    m_pDemuxer = FFMpegDemuxerPtr(new FFMpegDemuxer(m_pFormatContext, streamIndexes));
    return true;
}

bool VideoDemuxerThread::work() 
{
    if (m_bEOF) {
        waitForCommand();
    } else {
        map<int, VideoMsgQueuePtr>::iterator it;
        int shortestQ = -1;
        int shortestLength = INT_MAX;
        for (it = m_PacketQs.begin(); it != m_PacketQs.end(); it++) {
            if (it->second->size() < shortestLength && 
                    it->second->size() < it->second->getMaxSize() &&
                    !m_PacketQEOFMap[it->first])
            {
                shortestLength = it->second->size();
                shortestQ = it->first;
            }
        }
        
        if (shortestQ < 0) {
            // All queues are at their max capacity. Take a nap and try again later.
            // Note that we can't wait on the queue. If decoding is paused, the queues can
            // remain full indefinitely and commands from the application (seek() and 
            // close() must still be processed.
            msleep(10);
            return true;
        }

        AVPacket * pPacket = m_pDemuxer->getPacket(shortestQ);
        VideoMsgPtr pMsg(new VideoMsg);
        if (pPacket == 0) {
            onStreamEOF(shortestQ);
            pMsg->setEOF();
        } else {
            pMsg->setPacket(pPacket);
        }
        m_PacketQs[shortestQ]->push(pMsg);
        msleep(0);
    }
    return true;
}

void VideoDemuxerThread::seek(int seqNum, float destTime)
{
    map<int, VideoMsgQueuePtr>::iterator it;
    m_pDemuxer->seek(destTime);
    for (it = m_PacketQs.begin(); it != m_PacketQs.end(); it++) {
        VideoMsgQueuePtr pPacketQ = it->second;
        clearQueue(pPacketQ);

        // send SEEK_DONE
        VideoMsgPtr pMsg(new VideoMsg);
        pMsg->setSeekDone(seqNum, destTime);
        pPacketQ->push(pMsg);
        m_PacketQEOFMap[it->first] = false;
    }
    m_bEOF = false;
}
       
void VideoDemuxerThread::close()
{
    map<int, VideoMsgQueuePtr>::iterator it;
    for (it = m_PacketQs.begin(); it != m_PacketQs.end(); it++) {
        VideoMsgQueuePtr pPacketQ = it->second;
        clearQueue(pPacketQ);

        VideoMsgPtr pMsg(new VideoMsg);
        pMsg->setClosed();
        pPacketQ->push(pMsg);
        m_PacketQEOFMap[it->first] = false;
    }
    stop();
}
        
void VideoDemuxerThread::onStreamEOF(int streamIndex)
{
    m_PacketQEOFMap[streamIndex] = true;
    m_bEOF = true;
    map<int, bool>::iterator it;
    for (it = m_PacketQEOFMap.begin(); it != m_PacketQEOFMap.end(); it++) {
        if (!it->second) {
            m_bEOF = false;
            break;
        }
    }
}
        
void VideoDemuxerThread::clearQueue(VideoMsgQueuePtr pPacketQ)
{
    VideoMsgPtr pMsg;
    do {
        pMsg = pPacketQ->pop(false);
        if (pMsg) {
            pMsg->freePacket();
        }
    } while (pMsg);
}

}