提交 ee5afeff 编写于 作者: Z Zhang Rui

ffplay: introduce minisdl_thread

上级 4c3787dc
......@@ -4,7 +4,8 @@ include $(CLEAR_VARS)
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(MY_APP_FFMPEG_INCLUDE_PATH)
# LOCAL_LDLIBS += -ldl -llog
LOCAL_SRC_FILES := pkt_queue.c
LOCAL_SRC_FILES := minisdl/minisdl_thread.c
LOCAL_SRC_FILES += pkt_queue.c
LOCAL_SRC_FILES += ffplay.c
LOCAL_SHARED_LIBRARIES := ffmpeg
......
/*****************************************************************************
* demux_thread.c
*****************************************************************************
*
* copyright (c) 2001 Fabrice Bellard
* copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
*
* This file is part of ijkPlayer.
*
* ijkPlayer is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* ijkPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with ijkPlayer; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "internal.h"
/*****************************************************************************
* internal.h
*****************************************************************************
*
* copyright (c) 2001 Fabrice Bellard
* copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
*
* This file is part of ijkPlayer.
*
* ijkPlayer is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* ijkPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with ijkPlayer; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PLAYER_H
#define PLAYER_H
#include <inttypes.h>
enum {
AV_SYNC_AUDIO_MASTER, /* default choice */
AV_SYNC_VIDEO_MASTER,
AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
};
#endif
/*****************************************************************************
* minisdl_thread.c
*****************************************************************************
*
* copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
*
* This file is part of ijkPlayer.
*
* ijkPlayer is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* ijkPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with ijkPlayer; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h>
#include <assert.h>
#include "minisdl_thread.h"
SDL_mutex *SDL_CreateMutex(void)
{
SDL_mutex *mutex;
mutex = (SDL_mutex *) malloc(sizeof(SDL_mutex));
if (!mutex)
return NULL;
if (pthread_mutex_init(&mutex->id, NULL) != 0) {
free(mutex);
}
return mutex;
}
void SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex) {
pthread_mutex_destroy(&mutex->id);
free(mutex);
}
}
int SDL_LockMutex(SDL_mutex *mutex)
{
assert(mutex);
if (!mutex)
return -1;
return pthread_mutex_lock(&mutex->id);
}
int SDL_UnlockMutex(SDL_mutex *mutex)
{
assert(mutex);
if (!mutex)
return -1;
return pthread_mutex_unlock(&mutex->id);
}
SDL_cond *SDL_CreateCond(void)
{
SDL_cond *cond;
cond = (SDL_cond *) malloc(sizeof(SDL_cond));
if (!cond)
return NULL;
if (pthread_cond_init(&cond->id, NULL) != 0) {
free(cond);
}
return cond;
}
void SDL_DestroyCond(SDL_cond *cond)
{
if (cond) {
pthread_cond_destroy(&cond->id);
free(cond);
}
}
int SDL_CondSignal(SDL_cond *cond)
{
assert(cond);
if (!cond)
return -1;
return pthread_cond_signal(&cond->id);
}
int SDL_CondBroadcast(SDL_cond *cond)
{
assert(cond);
if (!cond)
return -1;
return pthread_cond_broadcast(&cond->id);
}
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, uint32_t ms)
{
int retval;
struct timeval delta;
struct timespec abstime;
assert(cond);
assert(mutex);
if (!cond || !mutex) {
return -1;
}
gettimeofday(&delta, NULL);
abstime.tv_sec = delta.tv_sec + (ms / 1000);
abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
if (abstime.tv_nsec > 1000000000) {
abstime.tv_sec += 1;
abstime.tv_nsec -= 1000000000;
}
while (1) {
retval = pthread_cond_timedwait(&cond->id, &mutex->id, &abstime);
if (retval == 0)
return 0;
else if (retval == EINTR)
continue;
else if (retval == ETIMEDOUT)
return SDL_MUTEX_TIMEDOUT;
else
break;
}
return -1;
}
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
assert(cond);
assert(mutex);
if (!cond || !mutex)
return -1;
return pthread_cond_wait(&cond->id, &mutex->id);
}
/*****************************************************************************
* minisdl_thread.h
*****************************************************************************
*
* copyright (c) 2001 Fabrice Bellard
* copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
*
* This file is part of ijkPlayer.
*
* ijkPlayer is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* ijkPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with ijkPlayer; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef MINISDL_THREAD_H
#define MINISDL_THREAD_H
#include <stdint.h>
#include <pthread.h>
#define SDL_MUTEX_TIMEDOUT 1
#define SDL_MUTEX_MAXWAIT (~(uint32_t)0)
typedef struct SDL_mutex {
pthread_mutex_t id;
} SDL_mutex;
SDL_mutex *SDL_CreateMutex(void);
void SDL_DestroyMutex(SDL_mutex * mutex);
int SDL_LockMutex(SDL_mutex * mutex);
int SDL_UnlockMutex(SDL_mutex * mutex);
//int SDL_TryLockMutex(SDL_mutex * mutex);
typedef struct SDL_cond {
pthread_cond_t id;
} SDL_cond;
SDL_cond *SDL_CreateCond(void);
void SDL_DestroyCond(SDL_cond * cond);
int SDL_CondSignal(SDL_cond * cond);
int SDL_CondBroadcast(SDL_cond * cond);
int SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, uint32_t ms);
int SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
#endif
......@@ -24,16 +24,16 @@
#include "pkt_queue.h"
AVPacket g_flush_pkt;
static AVPacket g_flush_pkt;
int packet_queue_put(PacketQueue *q, AVPacket *pkt);
int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
{
MyAVPacketList *pkt1;
if (q->abort_request)
return -1;
return -1;
pkt1 = av_malloc(sizeof(MyAVPacketList));
if (!pkt1)
......@@ -52,7 +52,7 @@ int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
q->nb_packets++;
q->size += pkt1->pkt.size + sizeof(*pkt1);
/* XXX: should duplicate packet data in DV case */
pthread_cond_signal(&q->cond);
SDL_CondSignal(q->cond);
return 0;
}
......@@ -64,9 +64,9 @@ int packet_queue_put(PacketQueue *q, AVPacket *pkt)
if (pkt != &g_flush_pkt && av_dup_packet(pkt) < 0)
return -1;
pthread_mutex_lock(&q->mutex);
SDL_LockMutex(q->mutex);
ret = packet_queue_put_private(q, pkt);
pthread_mutex_unlock(&q->mutex);
SDL_UnlockMutex(q->mutex);
if (pkt != &g_flush_pkt && ret < 0)
av_free_packet(pkt);
......@@ -78,8 +78,8 @@ int packet_queue_put(PacketQueue *q, AVPacket *pkt)
void packet_queue_init(PacketQueue *q)
{
memset(q, 0, sizeof(PacketQueue));
pthread_mutex_init(&q->mutex, NULL);
pthread_cond_init(&q->cond, NULL);
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
q->abort_request = 1;
}
......@@ -87,7 +87,7 @@ void packet_queue_flush(PacketQueue *q)
{
MyAVPacketList *pkt, *pkt1;
pthread_mutex_lock(&q->mutex);
SDL_LockMutex(q->mutex);
for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
pkt1 = pkt->next;
av_free_packet(&pkt->pkt);
......@@ -97,33 +97,33 @@ void packet_queue_flush(PacketQueue *q)
q->first_pkt = NULL;
q->nb_packets = 0;
q->size = 0;
pthread_mutex_unlock(&q->mutex);
SDL_UnlockMutex(q->mutex);
}
void packet_queue_destroy(PacketQueue *q)
{
packet_queue_flush(q);
pthread_mutex_destroy(&q->mutex);
pthread_cond_destroy(&q->cond);
SDL_DestroyMutex(q->mutex);
SDL_DestroyCond(q->cond);
}
void packet_queue_abort(PacketQueue *q)
{
pthread_mutex_lock(&q->mutex);
SDL_LockMutex(q->mutex);
q->abort_request = 1;
pthread_cond_signal(&q->cond);
SDL_CondSignal(q->cond);
pthread_mutex_unlock(&q->mutex);
SDL_UnlockMutex(q->mutex);
}
void packet_queue_start(PacketQueue *q)
{
pthread_mutex_lock(&q->mutex);
SDL_LockMutex(q->mutex);
q->abort_request = 0;
packet_queue_put_private(q, &g_flush_pkt);
pthread_mutex_unlock(&q->mutex);
SDL_UnlockMutex(q->mutex);
}
/* return < 0 if aborted, 0 if no packet and > 0 if packet. */
......@@ -132,7 +132,7 @@ int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
MyAVPacketList *pkt1;
int ret;
pthread_mutex_lock(&q->mutex);
SDL_LockMutex(q->mutex);
for (;;) {
if (q->abort_request) {
......@@ -157,9 +157,9 @@ int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
ret = 0;
break;
} else {
pthread_cond_wait(&q->cond, &q->mutex);
SDL_CondWait(q->cond, q->mutex);
}
}
pthread_mutex_unlock(&q->mutex);
SDL_UnlockMutex(q->mutex);
return ret;
}
......@@ -25,7 +25,7 @@
#ifndef PKT_QUEUE_H
#define PKT_QUEUE_H
#include <pthread.h>
#include <minisdl/minisdl_thread.h>
#include <libavformat/avformat.h>
typedef struct MyAVPacketList {
......@@ -40,8 +40,8 @@ typedef struct PacketQueue {
int size;
int abort_request;
int serial;
pthread_mutex_t mutex;
pthread_cond_t cond;
SDL_mutex *mutex;
SDL_cond *cond;
} PacketQueue;
void packet_queue_init(PacketQueue *q);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册