typedef struct sort_queue {
AVFrame pic; //解码后一帧
int serial;
int64_t sort; //如果有pts则为pts,否则为dts
volatile struct sort_queue * nextframe; //下一帧
} sort_queue; //解码队列,排在前面的先出,根据pts排序
volatile sort_queue *m_sort_queue; //记录对头
volatile int32_t m_queue_depth; //队列深度
static void SortQueuePush(Ijk_VideoToolBox_Opaque * ctx, sort_queue * newFrame) //插入一帧
{
pthread_mutex_lock(&ctx->m_queue_mutex);
volatile sort_queue*queueWalker = ctx->m_sort_queue;
if(!queueWalker || (newFrame->sort< queueWalker->sort)) { //这种情况下将newFrame直接插入首端
newFrame->nextframe= queueWalker;
ctx->m_sort_queue= newFrame;
}else{ //这种情况下根据sort即pts插入到合适位置
bool frameInserted =false;
volatile sort_queue*nextFrame =NULL;
while(!frameInserted) {
nextFrame = queueWalker->nextframe;
if(!nextFrame || (newFrame->sort< nextFrame->sort)) {
newFrame->nextframe= nextFrame;
queueWalker->nextframe= newFrame;
frameInserted =true;
}
queueWalker = nextFrame;
}
}
ctx->m_queue_depth++;
pthread_mutex_unlock(&ctx->m_queue_mutex);
}
static void SortQueuePop(Ijk_VideoToolBox_Opaque* context) //删除一帧
{
if(!context->m_sort_queue|| context->m_queue_depth==0) {
return;
}
pthread_mutex_lock(&context->m_queue_mutex);
volatile sort_queue*top_frame = context->m_sort_queue;
context->m_sort_queue = context->m_sort_queue->nextframe;
context->m_queue_depth--;
pthread_mutex_unlock(&context->m_queue_mutex);
CVBufferRelease(top_frame->pic.opaque);
free((void*)top_frame);
}
网友评论