提交 6bcf00f4 编写于 作者: B bbcallen

ios: notify playback state change

上级 2bf698dc
......@@ -36,6 +36,7 @@
#define FFP_MSG_BUFFERING_BYTES_UPDATE 503 /* arg1 = cached data in bytes, arg2 = high water mark */
#define FFP_MSG_BUFFERING_TIME_UPDATE 504 /* arg1 = cached duration in milliseconds, arg2 = high water mark */
#define FFP_MSG_SEEK_COMPLETE 600
#define FFP_MSG_PLAYBACK_STATE_CHANGED 700
#define FFP_REQ_START 20001
#define FFP_REQ_PAUSE 20002
......
......@@ -71,6 +71,12 @@ void ijkmp_global_uninit()
ffp_global_uninit();
}
void ijkmp_change_state_l(IjkMediaPlayer *mp, int new_state)
{
mp->mp_state = new_state;
ffp_notify_msg1(mp->ffplayer, FFP_MSG_PLAYBACK_STATE_CHANGED);
}
IjkMediaPlayer *ijkmp_create(int (*msg_loop)(void*))
{
IjkMediaPlayer *mp = (IjkMediaPlayer *) av_mallocz(sizeof(IjkMediaPlayer));
......@@ -163,7 +169,7 @@ void ijkmp_reset_l(IjkMediaPlayer *mp)
ffp_reset_internal(mp->ffplayer);
av_freep(&mp->data_source);
mp->mp_state = MP_STATE_IDLE;
ijkmp_change_state_l(mp, MP_STATE_IDLE);
}
void ijkmp_reset(IjkMediaPlayer *mp)
......@@ -226,7 +232,7 @@ static int ijkmp_set_data_source_l(IjkMediaPlayer *mp, const char *url)
if (!mp->data_source)
return EIJK_OUT_OF_MEMORY;
mp->mp_state = MP_STATE_INITIALIZED;
ijkmp_change_state_l(mp, MP_STATE_INITIALIZED);
return 0;
}
......@@ -259,7 +265,7 @@ static int ijkmp_prepare_async_l(IjkMediaPlayer *mp)
assert(mp->data_source);
mp->mp_state = MP_STATE_ASYNC_PREPARING;
ijkmp_change_state_l(mp, MP_STATE_ASYNC_PREPARING);
msg_queue_start(&mp->ffplayer->msg_queue);
ijkmp_inc_ref(mp);
......@@ -268,7 +274,7 @@ static int ijkmp_prepare_async_l(IjkMediaPlayer *mp)
int retval = ffp_prepare_async_l(mp->ffplayer, mp->data_source);
if (retval < 0) {
mp->mp_state = MP_STATE_ERROR;
ijkmp_change_state_l(mp, MP_STATE_ERROR);
return retval;
}
......@@ -388,7 +394,7 @@ static int ijkmp_stop_l(IjkMediaPlayer *mp)
return retval;
}
mp->mp_state = MP_STATE_STOPPED;
ijkmp_change_state_l(mp, MP_STATE_STOPPED);
return 0;
}
......@@ -457,6 +463,11 @@ int ijkmp_seek_to(IjkMediaPlayer *mp, long msec)
return retval;
}
int ijkmp_get_state(IjkMediaPlayer *mp)
{
return mp->mp_state;
}
static long ijkmp_get_current_position_l(IjkMediaPlayer *mp)
{
return ffp_get_current_position_l(mp->ffplayer);
......@@ -512,7 +523,7 @@ int ijkmp_get_msg(IjkMediaPlayer *mp, AVMessage *msg, int block)
MPTRACE("ijkmp_get_msg: FFP_MSG_PREPARED\n");
pthread_mutex_lock(&mp->mutex);
if (mp->mp_state == MP_STATE_ASYNC_PREPARING) {
mp->mp_state = MP_STATE_PREPARED;
ijkmp_change_state_l(mp, MP_STATE_PREPARED);
} else {
// FIXME: 1: onError() ?
ALOGE("FFP_MSG_PREPARED: expecting mp_state==MP_STATE_ASYNC_PREPARING\n");
......@@ -525,7 +536,7 @@ int ijkmp_get_msg(IjkMediaPlayer *mp, AVMessage *msg, int block)
pthread_mutex_lock(&mp->mutex);
mp->restart_from_beginning = 1;
mp->mp_state = MP_STATE_COMPLETED;
ijkmp_change_state_l(mp, MP_STATE_COMPLETED);
pthread_mutex_unlock(&mp->mutex);
break;
......@@ -549,19 +560,19 @@ int ijkmp_get_msg(IjkMediaPlayer *mp, AVMessage *msg, int block)
ALOGD("ijkmp_get_msg: FFP_REQ_START: restart from beginning\n");
retval = ffp_start_from_l(mp->ffplayer, 0);
if (retval == 0)
mp->mp_state = MP_STATE_STARTED;
ijkmp_change_state_l(mp, MP_STATE_STARTED);
} else {
ALOGD("ijkmp_get_msg: FFP_REQ_START: restart from seek pos\n");
retval = ffp_start_l(mp->ffplayer);
if (retval == 0)
mp->mp_state = MP_STATE_STARTED;
ijkmp_change_state_l(mp, MP_STATE_STARTED);
}
mp->restart_from_beginning = 0;
} else {
ALOGD("ijkmp_get_msg: FFP_REQ_START: start on fly\n");
retval = ffp_start_l(mp->ffplayer);
if (retval == 0)
mp->mp_state = MP_STATE_STARTED;
ijkmp_change_state_l(mp, MP_STATE_STARTED);
}
}
pthread_mutex_unlock(&mp->mutex);
......@@ -574,7 +585,7 @@ int ijkmp_get_msg(IjkMediaPlayer *mp, AVMessage *msg, int block)
if (0 == ikjmp_chkst_pause_l(mp->mp_state)) {
int retval = ffp_pause_l(mp->ffplayer);
if (retval == 0)
mp->mp_state = MP_STATE_PAUSED;
ijkmp_change_state_l(mp, MP_STATE_STARTED);
}
pthread_mutex_unlock(&mp->mutex);
break;
......
......@@ -167,6 +167,7 @@ int ijkmp_start(IjkMediaPlayer *mp);
int ijkmp_pause(IjkMediaPlayer *mp);
int ijkmp_stop(IjkMediaPlayer *mp);
int ijkmp_seek_to(IjkMediaPlayer *mp, long msec);
int ijkmp_get_state(IjkMediaPlayer *mp);
bool ijkmp_is_playing(IjkMediaPlayer *mp);
long ijkmp_get_current_position(IjkMediaPlayer *mp);
long ijkmp_get_duration(IjkMediaPlayer *mp);
......
......@@ -41,7 +41,7 @@
@synthesize playableDuration;
@synthesize isPreparedToPlay;
@synthesize playbackState = _playbackState;
@synthesize playbackState;
@synthesize loadState = _loadState;
@synthesize naturalSize = _naturalSize;
......@@ -124,6 +124,40 @@
return ijkmp_is_playing(_mediaPlayer);
}
- (MPMoviePlaybackState)playbackState
{
if (!_mediaPlayer)
return NO;
MPMoviePlaybackState mp_state = MPMoviePlaybackStateStopped;
int state = ijkmp_get_state(_mediaPlayer);
switch (state) {
case MP_STATE_STOPPED:
case MP_STATE_COMPLETED:
case MP_STATE_ERROR:
case MP_STATE_END:
mp_state = MPMoviePlaybackStateStopped;
break;
case MP_STATE_IDLE:
case MP_STATE_INITIALIZED:
case MP_STATE_ASYNC_PREPARING:
case MP_STATE_PREPARED:
case MP_STATE_PAUSED:
mp_state = MPMoviePlaybackStatePaused;
break;
case MP_STATE_STARTED:
mp_state = MPMoviePlaybackStatePlaying;
break;
}
// MPMoviePlaybackStatePlaying,
// MPMoviePlaybackStatePaused,
// MPMoviePlaybackStateStopped,
// MPMoviePlaybackStateInterrupted,
// MPMoviePlaybackStateSeekingForward,
// MPMoviePlaybackStateSeekingBackward
return ijkmp_is_playing(_mediaPlayer);
}
- (void)setCurrentPlaybackTime:(NSTimeInterval)aCurrentPlaybackTime
{
if (!_mediaPlayer)
......@@ -168,8 +202,6 @@
case FFP_MSG_ERROR: {
NSLog(@"FFP_MSG_ERROR: %d", avmsg->arg1);
_playbackState = MPMoviePlaybackStateStopped;
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerPlaybackDidFinishNotification object:self];
......@@ -191,8 +223,6 @@
break;
case FFP_MSG_COMPLETED: {
_playbackState = MPMoviePlaybackStateStopped;
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerPlaybackDidFinishNotification object:self];
......@@ -246,6 +276,11 @@
case FFP_MSG_BUFFERING_TIME_UPDATE:
// NSLog(@"FFP_MSG_BUFFERING_TIME_UPDATE: %d", avmsg->arg1);
break;
case FFP_MSG_PLAYBACK_STATE_CHANGED:
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerPlaybackStateDidChangeNotification
object:self];
break;
case FFP_MSG_SEEK_COMPLETE:
// NSLog(@"FFP_MSG_SEEK_COMPLETE:");
break;
......
......@@ -51,13 +51,15 @@
self.scalingMode = MPMovieScalingModeAspectFit;
self.shouldAutoplay = YES;
self.useApplicationAudioSession = NO;
[self IJK_installMovieNotificationObservers];
}
return self;
}
- (void)dealloc
{
[self removeMovieNotificationObservers];
[self IJK_removeMovieNotificationObservers];
}
- (BOOL)isPlaying
......@@ -73,30 +75,30 @@
#pragma mark Movie Notification Handlers
/* Register observers for the various movie object notifications. */
-(void)installMovieNotificationObservers
-(void)IJK_installMovieNotificationObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dispatchMPMediaPlaybackIsPreparedToPlayDidChangeNotification:)
selector:@selector(IJK_dispatchMPMediaPlaybackIsPreparedToPlayDidChangeNotification:)
name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dispatchMPMoviePlayerLoadStateDidChangeNotification:)
selector:@selector(IJK_dispatchMPMoviePlayerLoadStateDidChangeNotification:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dispatchMPMoviePlayerPlaybackDidFinishNotification:)
selector:@selector(IJK_dispatchMPMoviePlayerPlaybackDidFinishNotification:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dispatchMPMoviePlayerPlaybackStateDidChangeNotification:)
selector:@selector(IJK_dispatchMPMoviePlayerPlaybackStateDidChangeNotification:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self];
}
- (void)removeMovieNotificationObservers
- (void)IJK_removeMovieNotificationObservers
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:self];
......@@ -105,22 +107,22 @@
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self];
}
- (void)dispatchMPMediaPlaybackIsPreparedToPlayDidChangeNotification:(NSNotification*)notification
- (void)IJK_dispatchMPMediaPlaybackIsPreparedToPlayDidChangeNotification:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMediaPlaybackIsPreparedToPlayDidChangeNotification object:notification.object userInfo:notification.userInfo];
}
- (void)dispatchMPMoviePlayerLoadStateDidChangeNotification:(NSNotification*)notification
- (void)IJK_dispatchMPMoviePlayerLoadStateDidChangeNotification:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMoviePlayerLoadStateDidChangeNotification object:notification.object userInfo:notification.userInfo];
}
- (void)dispatchMPMoviePlayerPlaybackDidFinishNotification:(NSNotification*)notification
- (void)IJK_dispatchMPMoviePlayerPlaybackDidFinishNotification:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMoviePlayerPlaybackDidFinishNotification object:notification.object userInfo:notification.userInfo];
}
- (void)dispatchMPMoviePlayerPlaybackStateDidChangeNotification:(NSNotification*)notification
- (void)IJK_dispatchMPMoviePlayerPlaybackStateDidChangeNotification:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMoviePlayerPlaybackStateDidChangeNotification object:notification.object userInfo:notification.userInfo];
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册