ijkmedia/ijkplayer/ijkplayer_internal.h 这个头文件中包含一个结构体
struct IjkMediaPlayer {
volatile int ref_count; //应用当前对象的数目
pthread_mutex_t mutex; //线程锁,对IjkMediaPlayer的各种对象操作都要加锁
FFPlayer *ffplayer; //编解码相关真正的内核
int (*msg_loop)(void*);
SDL_Thread *msg_thread;
SDL_Thread _msg_thread;
int mp_state;
char *data_source;
void *weak_thiz;
int restart;
int restart_from_beginning;
int seek_req;
long seek_msec;
};
我们一个一个字段去分析
void ijkmp_inc_ref(IjkMediaPlayer *mp)
{
assert(mp);
__sync_fetch_and_add(&mp->ref_count, 1); 多线程操作计数器加1
}
void ijkmp_dec_ref(IjkMediaPlayer *mp)
{
if (!mp)
return;
int ref_count = __sync_sub_and_fetch(&mp->ref_count, 1); 多线程操作计数器减1
if (ref_count == 0) {
MPTRACE("ijkmp_dec_ref(): ref=0\n");
ijkmp_shutdown(mp);
ijkmp_destroy_p(&mp);
}
}
可以看到ref_count就是IjkMediaPlayer的引用基数。
通过原子性的操作函数 __sync_fetch_and_add和__sync_sub_and_fetch函数进行操作。
我们在ijkmp_dec_ref中看到,当引用记数变为0后,调用了销毁操作。
我们看下销毁的操作
void ijkmp_shutdown(IjkMediaPlayer *mp)
{
return ijkmp_shutdown_l(mp);
}
void ijkmp_shutdown_l(IjkMediaPlayer *mp)
{
assert(mp);
MPTRACE("ijkmp_shutdown_l()\n");
if (mp->ffplayer) {
ffp_stop_l(mp->ffplayer);
ffp_wait_stop_l(mp->ffplayer);
}
MPTRACE("ijkmp_shutdown_l()=void\n");
}
/*
*这个函数暂停、取消队列和seek请求
*/
int ffp_stop_l(FFPlayer *ffp)
{
assert(ffp);
VideoState *is = ffp->is;
if (is) {
is->abort_request = 1;
toggle_pause(ffp, 1);
}
msg_queue_abort(&ffp->msg_queue);
if (ffp->enable_accurate_seek && is && is->accurate_seek_mutex
&& is->audio_accurate_seek_cond && is->video_accurate_seek_cond) {
SDL_LockMutex(is->accurate_seek_mutex);
is->audio_accurate_seek_req = 0;
is->video_accurate_seek_req = 0;
SDL_CondSignal(is->audio_accurate_seek_cond);
SDL_CondSignal(is->video_accurate_seek_cond);
SDL_UnlockMutex(is->accurate_seek_mutex);
}
return 0;
}
int ffp_wait_stop_l(FFPlayer *ffp)
{
assert(ffp);
if (ffp->is) {
ffp_stop_l(ffp);
stream_close(ffp);
ffp->is = NULL;
}
return 0;
}
取消视频和音频解码、 关闭输入流、 销毁队列、 销毁锁、释放分配的一些对象
static void stream_close(FFPlayer *ffp)
{
VideoState *is = ffp->is;
/* XXX: use a special url_shutdown call to abort parse cleanly */
is->abort_request = 1;
packet_queue_abort(&is->videoq);
packet_queue_abort(&is->audioq);
av_log(NULL, AV_LOG_DEBUG, "wait for read_tid\n");
SDL_WaitThread(is->read_tid, NULL);
/* close each stream */
if (is->audio_stream >= 0)
stream_component_close(ffp, is->audio_stream);
if (is->video_stream >= 0)
stream_component_close(ffp, is->video_stream);
if (is->subtitle_stream >= 0)
stream_component_close(ffp, is->subtitle_stream);
avformat_close_input(&is->ic);
av_log(NULL, AV_LOG_DEBUG, "wait for video_refresh_tid\n");
SDL_WaitThread(is->video_refresh_tid, NULL);
packet_queue_destroy(&is->videoq);
packet_queue_destroy(&is->audioq);
packet_queue_destroy(&is->subtitleq);
/* free all pictures */
frame_queue_destory(&is->pictq);
frame_queue_destory(&is->sampq);
frame_queue_destory(&is->subpq);
SDL_DestroyCond(is->audio_accurate_seek_cond);
SDL_DestroyCond(is->video_accurate_seek_cond);
SDL_DestroyCond(is->continue_read_thread);
SDL_DestroyMutex(is->accurate_seek_mutex);
SDL_DestroyMutex(is->play_mutex);
#if !CONFIG_AVFILTER
sws_freeContext(is->img_convert_ctx);
#endif
#ifdef FFP_MERGE
sws_freeContext(is->sub_convert_ctx);
#endif
#if defined(__ANDROID__)
if (ffp->soundtouch_enable && is->handle != NULL) {
ijk_soundtouch_destroy(is->handle);
}
#endif
if (ffp->get_img_info) {
if (ffp->get_img_info->frame_img_convert_ctx) {
sws_freeContext(ffp->get_img_info->frame_img_convert_ctx);
}
if (ffp->get_img_info->frame_img_codec_ctx) {
avcodec_free_context(&ffp->get_img_info->frame_img_codec_ctx);
}
av_freep(&ffp->get_img_info->img_path);
av_freep(&ffp->get_img_info);
}
av_free(is->filename);
av_free(is);
ffp->is = NULL;
}
上面的代码我们可以看到就是删除队列请求、释放掉我们分配的内存等。就是做播放器的扫尾工作
通过名字可以猜到和循环取数据有关
IjkMediaPlayer *ijkmp_create(int (*msg_loop)(void*))
{
IjkMediaPlayer *mp = (IjkMediaPlayer *) mallocz(sizeof(IjkMediaPlayer));
if (!mp)
goto fail;
mp->ffplayer = ffp_create();
if (!mp->ffplayer)
goto fail;
mp->msg_loop = msg_loop;
ijkmp_inc_ref(mp);
pthread_mutex_init(&mp->mutex, NULL);
return mp;
fail:
ijkmp_destroy_p(&mp);
return NULL;
}
IjkMediaPlayer *ijkmp_android_create(int(*msg_loop)(void*))
{
IjkMediaPlayer *mp = ijkmp_create(msg_loop);
if (!mp)
goto fail;
mp->ffplayer->vout = SDL_VoutAndroid_CreateForAndroidSurface();
if (!mp->ffplayer->vout)
goto fail;
mp->ffplayer->pipeline = ffpipeline_create_from_android(mp->ffplayer);
if (!mp->ffplayer->pipeline)
goto fail;
ffpipeline_set_vout(mp->ffplayer->pipeline, mp->ffplayer->vout);
return mp;
fail:
ijkmp_dec_ref_p(&mp);
return NULL;
}
static void
IjkMediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
{
MPTRACE("%s\n", __func__);
IjkMediaPlayer *mp = ijkmp_android_create(message_loop);
JNI_CHECK_GOTO(mp, env, "java/lang/OutOfMemoryError", "mpjni: native_setup: ijkmp_create() failed", LABEL_RETURN);
jni_set_media_player(env, thiz, mp);
ijkmp_set_weak_thiz(mp, (*env)->NewGlobalRef(env, weak_this));
ijkmp_set_inject_opaque(mp, ijkmp_get_weak_thiz(mp));
ijkmp_set_ijkio_inject_opaque(mp, ijkmp_get_weak_thiz(mp));
ijkmp_android_set_mediacodec_select_callback(mp, mediacodec_select_callback, ijkmp_get_weak_thiz(mp));
LABEL_RETURN:
ijkmp_dec_ref_p(&mp);
}
static int message_loop(void *arg)
{
MPTRACE("%s\n", __func__);
JNIEnv *env = NULL;
if (JNI_OK != SDL_JNI_SetupThreadEnv(&env)) {
ALOGE("%s: SetupThreadEnv failed\n", __func__);
return -1;
}
IjkMediaPlayer *mp = (IjkMediaPlayer*) arg;
JNI_CHECK_GOTO(mp, env, NULL, "mpjni: native_message_loop: null mp", LABEL_RETURN);
message_loop_n(env, mp);
LABEL_RETURN:
ijkmp_dec_ref_p(&mp);
MPTRACE("message_loop exit");
return 0;
}
static void message_loop_n(JNIEnv *env, IjkMediaPlayer *mp)
{
jobject weak_thiz = (jobject) ijkmp_get_weak_thiz(mp);
JNI_CHECK_GOTO(weak_thiz, env, NULL, "mpjni: message_loop_n: null weak_thiz", LABEL_RETURN);
while (1) {
AVMessage msg;
int retval = ijkmp_get_msg(mp, &msg, 1);
if (retval < 0)
break;
// block-get should never return 0
assert(retval > 0);
switch (msg.what) {
case FFP_MSG_FLUSH:
MPTRACE("FFP_MSG_FLUSH:\n");
post_event(env, weak_thiz, MEDIA_NOP, 0, 0);
break;
case FFP_MSG_ERROR:
MPTRACE("FFP_MSG_ERROR: %d\n", msg.arg1);
post_event(env, weak_thiz, MEDIA_ERROR, MEDIA_ERROR_IJK_PLAYER, msg.arg1);
break;
case FFP_MSG_PREPARED:
MPTRACE("FFP_MSG_PREPARED:\n");
post_event(env, weak_thiz, MEDIA_PREPARED, 0, 0);
break;
case FFP_MSG_COMPLETED:
MPTRACE("FFP_MSG_COMPLETED:\n");
post_event(env, weak_thiz, MEDIA_PLAYBACK_COMPLETE, 0, 0);
break;
case FFP_MSG_VIDEO_SIZE_CHANGED:
MPTRACE("FFP_MSG_VIDEO_SIZE_CHANGED: %d, %d\n", msg.arg1, msg.arg2);
post_event(env, weak_thiz, MEDIA_SET_VIDEO_SIZE, msg.arg1, msg.arg2);
break;
case FFP_MSG_SAR_CHANGED:
MPTRACE("FFP_MSG_SAR_CHANGED: %d, %d\n", msg.arg1, msg.arg2);
post_event(env, weak_thiz, MEDIA_SET_VIDEO_SAR, msg.arg1, msg.arg2);
break;
case FFP_MSG_VIDEO_RENDERING_START:
MPTRACE("FFP_MSG_VIDEO_RENDERING_START:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_VIDEO_RENDERING_START, 0);
break;
case FFP_MSG_AUDIO_RENDERING_START:
MPTRACE("FFP_MSG_AUDIO_RENDERING_START:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_AUDIO_RENDERING_START, 0);
break;
case FFP_MSG_VIDEO_ROTATION_CHANGED:
MPTRACE("FFP_MSG_VIDEO_ROTATION_CHANGED: %d\n", msg.arg1);
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_VIDEO_ROTATION_CHANGED, msg.arg1);
break;
case FFP_MSG_AUDIO_DECODED_START:
MPTRACE("FFP_MSG_AUDIO_DECODED_START:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_AUDIO_DECODED_START, 0);
break;
case FFP_MSG_VIDEO_DECODED_START:
MPTRACE("FFP_MSG_VIDEO_DECODED_START:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_VIDEO_DECODED_START, 0);
break;
case FFP_MSG_OPEN_INPUT:
MPTRACE("FFP_MSG_OPEN_INPUT:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_OPEN_INPUT, 0);
break;
case FFP_MSG_FIND_STREAM_INFO:
MPTRACE("FFP_MSG_FIND_STREAM_INFO:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_FIND_STREAM_INFO, 0);
break;
case FFP_MSG_COMPONENT_OPEN:
MPTRACE("FFP_MSG_COMPONENT_OPEN:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_COMPONENT_OPEN, 0);
break;
case FFP_MSG_BUFFERING_START:
MPTRACE("FFP_MSG_BUFFERING_START:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_BUFFERING_START, msg.arg1);
break;
case FFP_MSG_BUFFERING_END:
MPTRACE("FFP_MSG_BUFFERING_END:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_BUFFERING_END, msg.arg1);
break;
case FFP_MSG_BUFFERING_UPDATE:
// MPTRACE("FFP_MSG_BUFFERING_UPDATE: %d, %d", msg.arg1, msg.arg2);
post_event(env, weak_thiz, MEDIA_BUFFERING_UPDATE, msg.arg1, msg.arg2);
break;
case FFP_MSG_BUFFERING_BYTES_UPDATE:
break;
case FFP_MSG_BUFFERING_TIME_UPDATE:
break;
case FFP_MSG_SEEK_COMPLETE:
MPTRACE("FFP_MSG_SEEK_COMPLETE:\n");
post_event(env, weak_thiz, MEDIA_SEEK_COMPLETE, 0, 0);
break;
case FFP_MSG_ACCURATE_SEEK_COMPLETE:
MPTRACE("FFP_MSG_ACCURATE_SEEK_COMPLETE:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_MEDIA_ACCURATE_SEEK_COMPLETE, msg.arg1);
break;
case FFP_MSG_PLAYBACK_STATE_CHANGED:
break;
case FFP_MSG_TIMED_TEXT:
if (msg.obj) {
jstring text = (*env)->NewStringUTF(env, (char *)msg.obj);
post_event2(env, weak_thiz, MEDIA_TIMED_TEXT, 0, 0, text);
J4A_DeleteLocalRef__p(env, &text);
}
else {
post_event2(env, weak_thiz, MEDIA_TIMED_TEXT, 0, 0, NULL);
}
break;
case FFP_MSG_GET_IMG_STATE:
if (msg.obj) {
jstring file_name = (*env)->NewStringUTF(env, (char *)msg.obj);
post_event2(env, weak_thiz, MEDIA_GET_IMG_STATE, msg.arg1, msg.arg2, file_name);
J4A_DeleteLocalRef__p(env, &file_name);
}
else {
post_event2(env, weak_thiz, MEDIA_GET_IMG_STATE, msg.arg1, msg.arg2, NULL);
}
break;
case FFP_MSG_VIDEO_SEEK_RENDERING_START:
MPTRACE("FFP_MSG_VIDEO_SEEK_RENDERING_START:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_VIDEO_SEEK_RENDERING_START, msg.arg1);
break;
case FFP_MSG_AUDIO_SEEK_RENDERING_START:
MPTRACE("FFP_MSG_AUDIO_SEEK_RENDERING_START:\n");
post_event(env, weak_thiz, MEDIA_INFO, MEDIA_INFO_AUDIO_SEEK_RENDERING_START, msg.arg1);
break;
default:
ALOGE("unknown FFP_MSG_xxx(%d)\n", msg.what);
break;
}
msg_free_res(&msg);
}
LABEL_RETURN:
;
}
inline static void post_event(JNIEnv *env, jobject weak_this, int what, int arg1, int arg2)
{
// MPTRACE("post_event(%p, %p, %d, %d, %d)", (void*)env, (void*) weak_this, what, arg1, arg2);
J4AC_IjkMediaPlayer__postEventFromNative(env, weak_this, what, arg1, arg2, NULL);
// MPTRACE("post_event()=void");
}
@CalledByNative
private static void postEventFromNative(Object weakThiz, int what,
int arg1, int arg2, Object obj) {
if (weakThiz == null)
return;
@SuppressWarnings("rawtypes")
IjkMediaPlayer mp = (IjkMediaPlayer) ((WeakReference) weakThiz).get();
if (mp == null) {
return;
}
if (what == MEDIA_INFO && arg1 == MEDIA_INFO_STARTED_AS_NEXT) {
// this acquires the wakelock if needed, and sets the client side
// state
mp.start();
}
if (mp.mEventHandler != null) {
Message m = mp.mEventHandler.obtainMessage(what, arg1, arg2, obj);
mp.mEventHandler.sendMessage(m);
}
}
可以看到message_loop_n就是个死循环,在不停的取消息,然后通过jni层post_event发送到java层进行处理。
在这里死循环缺不卡cpu是怎么实现的呢?
int ijkmp_get_msg(IjkMediaPlayer *mp, AVMessage *msg, int block)
{
assert(mp);
while (1) {
int continue_wait_next_msg = 0;
int retval = msg_queue_get(&mp->ffplayer->msg_queue, msg, block);
if (retval <= 0)
return retval;
........
}
inline static int msg_queue_get(MessageQueue *q, AVMessage *msg, int block)
{
AVMessage *msg1;
int ret;
SDL_LockMutex(q->mutex);
for (;;) {
if (q->abort_request) {
ret = -1;
break;
}
msg1 = q->first_msg;
if (msg1) {
q->first_msg = msg1->next;
if (!q->first_msg)
q->last_msg = NULL;
q->nb_messages--;
*msg = *msg1;
msg1->obj = NULL;
#ifdef FFP_MERGE
av_free(msg1);
#else
msg1->next = q->recycle_msg;
q->recycle_msg = msg1;
#endif
ret = 1;
break;
} else if (!block) {
ret = 0;
break;
} else {
SDL_CondWait(q->cond, q->mutex);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
我们通过上面的代码就会写阻塞性的代码,
SDL_LockMutex->SDL_CondWait->SDL_UnlockMutex
static int ijkmp_prepare_async_l(IjkMediaPlayer *mp)
{
......
// released in msg_loop
ijkmp_inc_ref(mp);
mp->msg_thread = SDL_CreateThreadEx(&mp->_msg_thread, ijkmp_msg_loop, mp, "ff_msg_loop");
......
}
SDL_Thread *SDL_CreateThreadEx(SDL_Thread *thread, int (*fn)(void *), void *data, const char *name)
{
thread->func = fn;
thread->data = data;
strlcpy(thread->name, name, sizeof(thread->name) - 1);
int retval = pthread_create(&thread->id, NULL, SDL_RunThread, thread);
if (retval)
return NULL;
return thread;
}
通过上面代码我们学会了如何创建线程,如何设置线程执行的方法,以及方法所需要的数据
我们知道线程执行的方法是ijkmp_msg_loop,也即是前面我们提到的函数msg_loop,用于适时获取播放器状态。
/*-
* ijkmp_set_data_source() -> MP_STATE_INITIALIZED
*
* ijkmp_reset -> self
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_IDLE 0
/*-
* ijkmp_prepare_async() -> MP_STATE_ASYNC_PREPARING
*
* ijkmp_reset -> MP_STATE_IDLE
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_INITIALIZED 1
/*-
* ... -> MP_STATE_PREPARED
* ... -> MP_STATE_ERROR
*
* ijkmp_reset -> MP_STATE_IDLE
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_ASYNC_PREPARING 2
/*-
* ijkmp_seek_to() -> self
* ijkmp_start() -> MP_STATE_STARTED
*
* ijkmp_reset -> MP_STATE_IDLE
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_PREPARED 3
/*-
* ijkmp_seek_to() -> self
* ijkmp_start() -> self
* ijkmp_pause() -> MP_STATE_PAUSED
* ijkmp_stop() -> MP_STATE_STOPPED
* ... -> MP_STATE_COMPLETED
* ... -> MP_STATE_ERROR
*
* ijkmp_reset -> MP_STATE_IDLE
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_STARTED 4
/*-
* ijkmp_seek_to() -> self
* ijkmp_start() -> MP_STATE_STARTED
* ijkmp_pause() -> self
* ijkmp_stop() -> MP_STATE_STOPPED
*
* ijkmp_reset -> MP_STATE_IDLE
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_PAUSED 5
/*-
* ijkmp_seek_to() -> self
* ijkmp_start() -> MP_STATE_STARTED (from beginning)
* ijkmp_pause() -> self
* ijkmp_stop() -> MP_STATE_STOPPED
*
* ijkmp_reset -> MP_STATE_IDLE
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_COMPLETED 6
/*-
* ijkmp_stop() -> self
* ijkmp_prepare_async() -> MP_STATE_ASYNC_PREPARING
*
* ijkmp_reset -> MP_STATE_IDLE
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_STOPPED 7
/*-
* ijkmp_reset -> MP_STATE_IDLE
* ijkmp_release -> MP_STATE_END
*/
#define MP_STATE_ERROR 8
/*-
* ijkmp_release -> self
*/
#define MP_STATE_END 9
总共9种状态,什么操作会走到什么状态。这个很重要。
static int ijkmp_set_data_source_l(IjkMediaPlayer *mp, const char *url)
{
assert(mp);
assert(url);
......
freep((void**)&mp->data_source);
mp->data_source = strdup(url);
if (!mp->data_source)
return EIJK_OUT_OF_MEMORY;
ijkmp_change_state_l(mp, MP_STATE_INITIALIZED);
return 0;
}
可以看到也就是我们外部设置的源地址
对应java层的tv.danmaku.ijk.media.player.IjkMediaPlayer对象
int ijkmp_get_msg(IjkMediaPlayer *mp, AVMessage *msg, int block)
case FFP_MSG_COMPLETED:
MPTRACE("ijkmp_get_msg: FFP_MSG_COMPLETED\n");
pthread_mutex_lock(&mp->mutex);
mp->restart = 1;
mp->restart_from_beginning = 1;
ijkmp_change_state_l(mp, MP_STATE_COMPLETED);
pthread_mutex_unlock(&mp->mutex);
break;
case FFP_REQ_START:
MPTRACE("ijkmp_get_msg: FFP_REQ_START\n");
continue_wait_next_msg = 1;
pthread_mutex_lock(&mp->mutex);
if (0 == ikjmp_chkst_start_l(mp->mp_state)) {
// FIXME: 8 check seekable
if (mp->restart) {
if (mp->restart_from_beginning) {
av_log(mp->ffplayer, AV_LOG_DEBUG, "ijkmp_get_msg: FFP_REQ_START: restart from beginning\n");
retval = ffp_start_from_l(mp->ffplayer, 0);
if (retval == 0)
ijkmp_change_state_l(mp, MP_STATE_STARTED);
} else {
av_log(mp->ffplayer, AV_LOG_DEBUG, "ijkmp_get_msg: FFP_REQ_START: restart from seek pos\n");
retval = ffp_start_l(mp->ffplayer);
if (retval == 0)
ijkmp_change_state_l(mp, MP_STATE_STARTED);
}
mp->restart = 0;
mp->restart_from_beginning = 0;
} else {
av_log(mp->ffplayer, AV_LOG_DEBUG, "ijkmp_get_msg: FFP_REQ_START: start on fly\n");
retval = ffp_start_l(mp->ffplayer);
if (retval == 0)
ijkmp_change_state_l(mp, MP_STATE_STARTED);
}
}
pthread_mutex_unlock(&mp->mutex);
break;
}
我们可以看到在播放完成时设置restart=1,restart_from_beginning=1.再次收到播放消息的时候,就会从头开始播放
顾名思义,seek请求以及seek的毫米级别的位置