aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2014-06-08 03:26:34 -0700
committerChris Robinson <[email protected]>2014-06-08 03:26:34 -0700
commit8ca1f4f37151a202b60a45e5303816c5bba2e3ed (patch)
tree8b70cf82e90af87cfacb6f33dd0b692516c30837 /examples
parentbe66692f940728fdd557f34d023fc39ca7757442 (diff)
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.
Diffstat (limited to 'examples')
-rw-r--r--examples/alffplay.c54
1 files changed, 28 insertions, 26 deletions
diff --git a/examples/alffplay.c b/examples/alffplay.c
index c02d758f..762582c4 100644
--- a/examples/alffplay.c
+++ b/examples/alffplay.c
@@ -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);