Skip to content

Commit

Permalink
videotoolbox: turn vtbcontext into singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
xinzhengzhang committed Nov 8, 2016
1 parent a51cfe6 commit 4636b17
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import <CoreVideo/CVHostTime.h>
#import <Foundation/Foundation.h>
#import "IJKDeviceModel.h"
#include <stdatomic.h>

#define IJK_VTB_FCC_AVCC SDL_FOURCC('C', 'c', 'v', 'a')

Expand Down Expand Up @@ -95,6 +96,10 @@
SDL_SpeedSampler sampler;
};

static volatile int _Atomic vtb_active = 0;
static SDL_mutex *vtb_cond_mutex = NULL;
static SDL_cond *vtb_cond = NULL;

static void vtbformat_destroy(VTBFormatDesc *fmt_desc);
static int vtbformat_init(VTBFormatDesc *fmt_desc, AVCodecParameters *codecpar);

Expand Down Expand Up @@ -934,6 +939,11 @@ void videotoolbox_free(VideoToolBoxContext* context)
vtbformat_destroy(&context->fmt_desc);

avcodec_parameters_free(&context->codecpar);
assert(vtb_cond && vtb_cond_mutex);
SDL_LockMutex(vtb_cond_mutex);
atomic_store(&vtb_active, 0);
SDL_CondSignal(vtb_cond);
SDL_UnlockMutex(vtb_cond_mutex);
}

int videotoolbox_decode_frame(VideoToolBoxContext* context)
Expand Down Expand Up @@ -1120,8 +1130,31 @@ static int vtbformat_init(VTBFormatDesc *fmt_desc, AVCodecParameters *codecpar)

VideoToolBoxContext* videotoolbox_create(FFPlayer* ffp, AVCodecContext* avctx)
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
vtb_active = 0;
atomic_store(&vtb_active, 0);
vtb_cond = SDL_CreateCond();
vtb_cond_mutex = SDL_CreateMutex();
});

int ret = 0;

SDL_LockMutex(vtb_cond_mutex);
if (atomic_load(&vtb_active)) {
SDL_CondWaitTimeout(vtb_cond, vtb_cond_mutex, 1000 * 10);
ret = atomic_load(&vtb_active);
}
if (!ret) {
atomic_store(&vtb_active, 1);
}
SDL_UnlockMutex(vtb_cond_mutex);

if (ret) {
ALOGW("%s - videotoolbox can not exists twice at the same time", __FUNCTION__);
return NULL;
}

VideoToolBoxContext *context_vtb = (VideoToolBoxContext *)mallocz(sizeof(VideoToolBoxContext));

context_vtb->sample_info_mutex = SDL_CreateMutex();
Expand Down

0 comments on commit 4636b17

Please sign in to comment.