Skip to content

Commit

Permalink
add AVPlayer.state property
Browse files Browse the repository at this point in the history
state api is much easier than play(), stop() and pause(bool)
  • Loading branch information
wang-bin committed Dec 4, 2015
1 parent 990968f commit fe7974a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/AVPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,21 @@ void AVPlayer::togglePause()

void AVPlayer::pause(bool p)
{
if (!isPlaying())
return;
if (isPaused() == p)
return;
//pause thread. check pause state?
d->read_thread->pause(p);
if (d->athread)
d->athread->pause(p);
if (d->vthread)
d->vthread->pause(p);
d->clock->pause(p);
emit paused(p);

d->state = p ? PausedState : PlayingState;
Q_EMIT stateChanged(d->state);
Q_EMIT paused(p);
}

bool AVPlayer::isPaused() const
Expand Down Expand Up @@ -1109,6 +1116,35 @@ int AVPlayer::subtitleStreamCount() const
return d->demuxer.subtitleStreams().size();
}

AVPlayer::State AVPlayer::state() const
{
return d->state;
}

void AVPlayer::setState(State value)
{
if (d->state == value)
return;
if (value == StoppedState) {
stop();
return;
}
if (value == PausedState) {
pause(true);
return;
}
// value == PlayingState
if (d->state == StoppedState) {
play();
return;
}
if (d->state == PausedState) {
pause(false);
return;
}

}

//FIXME: why no d->demuxer will not get an eof if replaying by seek(0)?
void AVPlayer::play()
{
Expand Down Expand Up @@ -1248,7 +1284,9 @@ void AVPlayer::playInternal()
if (d->last_position > 0)
setPosition(d->last_position); //just use d->demuxer.startTime()/duration()?

emit started(); //we called stop(), so must emit started()
d->state = PlayingState;
Q_EMIT stateChanged(d->state);
Q_EMIT started(); //we called stop(), so must emit started()
}

void AVPlayer::stopFromDemuxerThread()
Expand All @@ -1263,7 +1301,9 @@ void AVPlayer::stopFromDemuxerThread()
d->media_end = kInvalidPosition;
d->repeat_current = d->repeat_max = 0;
qDebug("avplayer emit stopped()");
emit stopped();
d->state = StoppedState;
Q_EMIT stateChanged(d->state);
Q_EMIT stopped();
} else {
qDebug("stopPosition() == mediaStopPosition() or !seekable. repeate: %d/%d", currentRepeat(), repeat());
d->repeat_current++;
Expand Down Expand Up @@ -1470,6 +1510,8 @@ void AVPlayer::stepForward()
void AVPlayer::stepBackward()
{
d->clock->pause(true);
d->state = PausedState;
Q_EMIT stateChanged(d->state);
Q_EMIT paused(true);
d->read_thread->stepBackward();
}
Expand Down
1 change: 1 addition & 0 deletions src/AVPlayerPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ AVPlayer::Private::Private()
, force_fps(0)
, notify_interval(-500)
, status(NoMedia)
, state(AVPlayer::StoppedState)
{
demuxer.setInterruptTimeout(interrupt_timeout);
/*
Expand Down
1 change: 1 addition & 0 deletions src/AVPlayerPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class AVPlayer::Private
// <0: auto compute internally, |notify_interval| is the real interval
int notify_interval;
MediaStatus status; // status changes can be from demuxer or demux thread
AVPlayer::State state;
QMutex load_mutex;
};

Expand Down
18 changes: 18 additions & 0 deletions src/QtAV/AVPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ class Q_AV_EXPORT AVPlayer : public QObject
Q_PROPERTY(int brightness READ brightness WRITE setBrightness NOTIFY brightnessChanged)
Q_PROPERTY(int contrast READ contrast WRITE setContrast NOTIFY contrastChanged)
Q_PROPERTY(int saturation READ saturation WRITE setSaturation NOTIFY saturationChanged)
Q_PROPERTY(State state READ state WRITE setState NOTIFY stateChanged)
Q_ENUMS(State)
public:
enum State {
StoppedState,
PlayingState, /// Start to play if it was stopped, or resume if it was paused
PausedState
};

/// Supported input protocols. A static string list
static const QStringList& supportedProtocols();

Expand Down Expand Up @@ -238,6 +246,15 @@ class Q_AV_EXPORT AVPlayer : public QObject
bool play(const QString& path);
bool isPlaying() const;
bool isPaused() const;
/*!
* \brief state
* Player's playback state. Default is StoppedState.
* setState() is a replacement of play(), stop(), pause(bool)
* \return
*/
State state() const;
void setState(State value);

// TODO: use id as parameter and return ptr?
void addVideoRenderer(VideoRenderer *renderer);
void removeVideoRenderer(VideoRenderer *renderer);
Expand Down Expand Up @@ -474,6 +491,7 @@ public slots:
void paused(bool p);
void started();
void stopped();
void stateChanged(QtAV::AVPlayer::State state);
void speedChanged(qreal speed);
void repeatChanged(int r);
void currentRepeatChanged(int r);
Expand Down

0 comments on commit fe7974a

Please sign in to comment.