Open and close the file in the main thread in alffplay

This avoid problems with the file being closed while a video refresh is still
scheduled.
This commit is contained in:
Chris Robinson 2014-06-08 03:26:34 -07:00
parent be66692f94
commit 8ca1f4f371

View File

@ -1117,7 +1117,7 @@ static int decode_interrupt_cb(void *ctx)
int decode_thread(void *arg)
{
MovieState *movState = (MovieState *)arg;
AVFormatContext *fmtCtx;
AVFormatContext *fmtCtx = movState->pFormatCtx;
AVPacket *packet = (AVPacket[1]){};
int video_index = -1;
int audio_index = -1;
@ -1125,30 +1125,6 @@ int decode_thread(void *arg)
movState->videoStream = -1;
movState->audioStream = -1;
fmtCtx = avformat_alloc_context();
fmtCtx->interrupt_callback = (AVIOInterruptCB){.callback=decode_interrupt_cb, .opaque=movState};
if(avio_open2(&fmtCtx->pb, movState->filename, AVIO_FLAG_READ, &fmtCtx->interrupt_callback, NULL))
{
fprintf(stderr, "Failed to open %s\n", movState->filename);
goto fail;
}
/* Open movie file */
if(avformat_open_input(&fmtCtx, movState->filename, NULL, NULL) != 0)
{
fprintf(stderr, "Failed to open %s\n", movState->filename);
goto fail;
}
movState->pFormatCtx = fmtCtx;
/* Retrieve stream information */
if(avformat_find_stream_info(fmtCtx, NULL) < 0)
{
fprintf(stderr, "%s: failed to find stream info\n", movState->filename);
goto fail;
}
/* Dump information about file onto standard error */
av_dump_format(fmtCtx, 0, movState->filename, 0);
@ -1259,7 +1235,6 @@ fail:
if(movState->audioStream >= 0)
althrd_join(movState->audio.thread, NULL);
avformat_close_input(&movState->pFormatCtx);
SDL_PushEvent(&(SDL_Event){ .user={.type=FF_QUIT_EVENT, .data1=movState} });
return 0;
@ -1389,8 +1364,33 @@ int main(int argc, char *argv[])
movState->av_sync_type = DEFAULT_AV_SYNC_TYPE;
movState->pFormatCtx = avformat_alloc_context();
movState->pFormatCtx->interrupt_callback = (AVIOInterruptCB){.callback=decode_interrupt_cb, .opaque=movState};
if(avio_open2(&movState->pFormatCtx->pb, movState->filename, AVIO_FLAG_READ,
&movState->pFormatCtx->interrupt_callback, NULL))
{
fprintf(stderr, "Failed to open %s\n", movState->filename);
return 1;
}
/* Open movie file */
if(avformat_open_input(&movState->pFormatCtx, movState->filename, NULL, NULL) != 0)
{
fprintf(stderr, "Failed to open %s\n", movState->filename);
return 1;
}
/* Retrieve stream information */
if(avformat_find_stream_info(movState->pFormatCtx, NULL) < 0)
{
fprintf(stderr, "%s: failed to find stream info\n", movState->filename);
return 1;
}
schedule_refresh(movState, 40);
if(althrd_create(&movState->parse_thread, decode_thread, movState) != althrd_success)
{
fprintf(stderr, "Failed to create parse thread!\n");
@ -1453,6 +1453,8 @@ int main(int argc, char *argv[])
case FF_QUIT_EVENT:
althrd_join(movState->parse_thread, NULL);
avformat_close_input(&movState->pFormatCtx);
almtx_destroy(&movState->audio.src_mutex);
almtx_destroy(&movState->video.pictq_mutex);
alcnd_destroy(&movState->video.pictq_cond);