Skip to content

Commit

Permalink
添加章节跳转的槽函数,已初步测试通过。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangwenx190 committed Aug 22, 2017
1 parent fc15d1f commit 090ebb2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
62 changes: 61 additions & 1 deletion src/AVPlayer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
/******************************************************************************
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2017 Wang Bin <[email protected]>
Expand Down Expand Up @@ -45,6 45,9 @@
#include "QtAV/private/AVCompat.h"
#include "utils/internal.h"
#include "utils/Logger.h"
extern "C" {
#include <libavutil/mathematics.h>
}

#define EOF_ISSUE_SOLVED 0
namespace QtAV {
Expand Down Expand Up @@ -680,6 683,7 @@ void AVPlayer::loadInternal()
d->video_tracks = d->getTracksInfo(&d->demuxer, AVDemuxer::VideoStream);
Q_EMIT internalVideoTracksChanged(d->video_tracks);
Q_EMIT durationChanged(duration());
Q_EMIT chaptersChanged(chapters());
// setup parameters from loaded media
d->media_start_pts = d->demuxer.startTime();
// TODO: what about other proctols? some vob duration() == 0
Expand Down Expand Up @@ -715,6 719,7 @@ void AVPlayer::unload()
d->vdec = 0;
}
d->demuxer.unload();
Q_EMIT chaptersChanged(0);
Q_EMIT durationChanged(0LL); // for ui, slider is invalid. use stopped instead, and remove this signal here?
// ??
d->audio_tracks = d->getTracksInfo(&d->demuxer, AVDemuxer::AudioStream);
Expand Down Expand Up @@ -1385,6 1390,42 @@ void AVPlayer::tryClearVideoRenderers()
}
}

void AVPlayer::seekChapter(int incr)
{
if (!chapters())
return;

qint64 pos = masterClock()->value() * AV_TIME_BASE;
int i = 0;

AVFormatContext *ic = d->demuxer.formatContext();

AVRational av_time_base_q;
av_time_base_q.num = 1;
av_time_base_q.den = AV_TIME_BASE;

/* find the current chapter */
for (i = 0; i < chapters(); i) {
AVChapter *ch = ic->chapters[i];
if (av_compare_ts(pos, av_time_base_q, ch->start, ch->time_base) < 0) {
--i;
break;
}
}

i = incr;
//i = FFMAX(i, 0);
if (i <= 0)
i = 0;
if (i >= chapters())
return;

//av_log(NULL, AV_LOG_VERBOSE, "Seeking to chapter %d.\n", i);
qDebug() << QString::fromLatin1("Seeking to chapter : ") << QString::number(i);
setPosition(av_rescale_q(ic->chapters[i]->start, ic->chapters[i]->time_base,
av_time_base_q) / 1000);
}

void AVPlayer::stop()
{
// check d->timer_id, <0 return?
Expand Down Expand Up @@ -1532,6 1573,20 @@ void AVPlayer::seekBackward()
seek(position() - kSeekMS);
}

void AVPlayer::seekNextChapter()
{
if (chapters() <= 1)
return;
seekChapter(1);
}

void AVPlayer::seekPreviousChapter()
{
if (chapters() <= 1)
return;
seekChapter(-1);
}

void AVPlayer::setSeekType(SeekType type)
{
d->seek_type = type;
Expand Down Expand Up @@ -1635,6 1690,11 @@ int AVPlayer::saturation() const
return d->saturation;
}

unsigned int AVPlayer::chapters() const
{
return d->demuxer.formatContext()->nb_chapters;
}

void AVPlayer::setSaturation(int val)
{
if (d->saturation == val)
Expand Down
8 changes: 7 additions & 1 deletion src/QtAV/AVPlayer.h
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
/******************************************************************************
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2017 Wang Bin <[email protected]>
Expand Down Expand Up @@ -82,6 82,7 @@ class Q_AV_EXPORT AVPlayer : public QObject
Q_PROPERTY(State state READ state WRITE setState NOTIFY stateChanged)
Q_PROPERTY(QtAV::MediaStatus mediaStatus READ mediaStatus NOTIFY mediaStatusChanged)
Q_PROPERTY(QtAV::MediaEndAction mediaEndAction READ mediaEndAction WRITE setMediaEndAction NOTIFY mediaEndActionChanged)
Q_PROPERTY(qint64 chapters READ chapters NOTIFY chaptersChanged)
Q_ENUMS(State)
public:
/*!
Expand Down Expand Up @@ -374,6 375,7 @@ class Q_AV_EXPORT AVPlayer : public QObject
int contrast() const;
int hue() const; //not implemented
int saturation() const;
unsigned int chapters() const;
/*!
* \sa AVDemuxer::setOptions()
* example:
Expand Down Expand Up @@ -489,6 491,8 @@ public Q_SLOTS:
void seek(qint64 pos); //ms. same as setPosition(pos)
void seekForward();
void seekBackward();
void seekNextChapter();
void seekPreviousChapter();
void setSeekType(SeekType type);
SeekType seekType() const;

Expand Down Expand Up @@ -579,6 583,7 @@ public Q_SLOTS:
void contrastChanged(int val);
void hueChanged(int val);
void saturationChanged(int val);
void chaptersChanged(unsigned int val);
void subtitleStreamChanged(int value);
/*!
* \brief internalAudioTracksChanged
Expand Down Expand Up @@ -611,6 616,7 @@ private Q_SLOTS:
void updateMediaStatus(QtAV::MediaStatus status);
void onSeekFinished(qint64 value);
void tryClearVideoRenderers();
void seekChapter(int incr);
protected:
// TODO: set position check timer interval
virtual void timerEvent(QTimerEvent *);
Expand Down

0 comments on commit 090ebb2

Please sign in to comment.