Skip to content

Commit

Permalink
remove eof packet. check AVDemuxer.atEnd() instead
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Jan 15, 2015
1 parent bc954e0 commit 274ef45
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 37 deletions.
29 changes: 13 additions & 16 deletions src/AVDemuxThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,18 @@ void AVDemuxThread::run()
vqueue->setBlocking(true);
}
while (!end) {
if (end || demuxer->atEnd()) {
end = true;
//connect to stop is ok too
//avthread can stop. do not clear queue, make sure all data are played
if (audio_thread) {
audio_thread->setDemuxEnded(true);
}
if (video_thread) {
video_thread->setDemuxEnded(true);
}
break;
}
processNextSeekTask();
if (tryPause()) {
continue; //the queue is empty and will block
Expand All @@ -405,27 +417,12 @@ void AVDemuxThread::run()
}
QMutexLocker locker(&buffer_mutex);
Q_UNUSED(locker);
if (end) {
break;
}
if (!demuxer->readFrame()) {
continue;
}
index = demuxer->stream();
pkt = demuxer->packet(); //TODO: how to avoid additional copy?
//connect to stop is ok too
if (pkt.isEnd()) {
qDebug("read end packet %d A:%d V:%d", index, audio_stream, video_stream);
end = true;
//avthread can stop. do not clear queue, make sure all data are played
if (audio_thread) {
audio_thread->setDemuxEnded(true);
}
if (video_thread) {
video_thread->setDemuxEnded(true);
}
break;
}

/*1 is empty but another is enough, then do not block to
ensure the empty one can put packets immediatly.
But usually it will not happen, why?
Expand Down
3 changes: 0 additions & 3 deletions src/AVDemuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,9 @@ bool AVDemuxer::readFrame()
if (!eof) {
eof = true;
started_ = false;
m_pkt = Packet(); //flush
m_pkt.markEnd();
setMediaStatus(EndOfMedia);
qDebug("End of file. %s %d", __FUNCTION__, __LINE__);
emit finished();
return ret == AVERROR_EOF;
}
return false; //frames after eof are eof frames
}
Expand Down
9 changes: 1 addition & 8 deletions src/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

namespace QtAV {

const qreal Packet::kEndPts = -0.618;

class PacketPrivate : public QSharedData
{
public:
Expand Down Expand Up @@ -64,6 +62,7 @@ bool Packet::fromAVPacket(Packet* pkt, const AVPacket *avpkt, double time_base)
if (pkt->isCorrupt)
qDebug("currupt packet. pts: %f", pkt->pts);

// from av_read_frame: pkt->pts can be AV_NOPTS_VALUE if the video format has B-frames, so it is better to rely on pkt->dts if you do not decompress the payload.
// old code set pts as dts is valid
if (avpkt->pts != AV_NOPTS_VALUE)
pkt->pts = avpkt->pts * time_base;
Expand Down Expand Up @@ -173,12 +172,6 @@ Packet::~Packet()
{
}

void Packet::markEnd()
{
qDebug("mark as end packet");
pts = kEndPts;
}

const AVPacket *Packet::asAVPacket() const
{
if (d.constData()) {
Expand Down
4 changes: 2 additions & 2 deletions src/QtAV/AVDemuxer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ class Q_AV_EXPORT AVDemuxer : public QObject
* Read a packet from 1 of the streams. use packet() to get the result packet. packet() returns last valid packet.
* So do not use packet() if readFrame() failed.
* Call readFrame() and seek() in the same thread.
* \return false if error occurs, interrupted by user or time out(getInterruptTimeout())
* \return false if eof, error occurs, interrupted by user or time out(getInterruptTimeout())
*/
bool readFrame();
/*!
* \brief packet
* If readFrame() return true, it's current readed packet, otherwise it's last packet().
* return the packet read by demuxer. packet is invalid if readFrame() returns false.
*/
Packet packet() const;
/*!
Expand Down
8 changes: 0 additions & 8 deletions src/QtAV/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class Q_AV_EXPORT Packet
Packet& operator =(const Packet& other);

inline bool isValid() const;
inline bool isEnd() const;
void markEnd();
/*!
* \brief asAVPacket
* If Packet is constructed from AVPacket, then data and properties are the same as that AVPacket.
Expand All @@ -64,7 +62,6 @@ class Q_AV_EXPORT Packet
qint64 position; // position in source file byte stream

private:
static const qreal kEndPts;
// TODO: implicity shared. can not use QSharedData
// we must define default/copy ctor, dtor and operator= so that we can provide only forward declaration of PacketPrivate
mutable QSharedDataPointer<PacketPrivate> d;
Expand All @@ -75,11 +72,6 @@ bool Packet::isValid() const
return !isCorrupt && !data.isNull() && pts >= 0 && duration >= 0; //!data.isEmpty()?
}

bool Packet::isEnd() const
{
return pts == kEndPts;
}

} //namespace QtAV

#endif // QAV_PACKET_H

0 comments on commit 274ef45

Please sign in to comment.