Browse code

avplay: Move the stream setup in the main thread

And refactor the code in preparation of the following
patches.

Signed-off-by: Luca Barbato <lu_zero@gentoo.org>

Luca Barbato authored on 2016/01/02 20:19:27
Showing 1 changed files
... ...
@@ -1204,7 +1204,7 @@ retry:
1204 1204
     }
1205 1205
 }
1206 1206
 
1207
-static void stream_close(VideoState *is)
1207
+static void player_close(VideoState *is)
1208 1208
 {
1209 1209
     VideoPicture *vp;
1210 1210
     int i;
... ...
@@ -1235,7 +1235,7 @@ static void stream_close(VideoState *is)
1235 1235
 static void do_exit(void)
1236 1236
 {
1237 1237
     if (cur_stream) {
1238
-        stream_close(cur_stream);
1238
+        player_close(cur_stream);
1239 1239
         cur_stream = NULL;
1240 1240
     }
1241 1241
     uninit_opts();
... ...
@@ -2256,16 +2256,28 @@ static int decode_interrupt_cb(void *ctx)
2256 2256
     return global_video_state && global_video_state->abort_request;
2257 2257
 }
2258 2258
 
2259
-/* this thread gets the stream from the disk or the network */
2260
-static int decode_thread(void *arg)
2259
+static void stream_close(VideoState *is)
2260
+{
2261
+    /* disable interrupting */
2262
+    global_video_state = NULL;
2263
+
2264
+    /* close each stream */
2265
+    if (is->audio_stream >= 0)
2266
+        stream_component_close(is, is->audio_stream);
2267
+    if (is->video_stream >= 0)
2268
+        stream_component_close(is, is->video_stream);
2269
+    if (is->subtitle_stream >= 0)
2270
+        stream_component_close(is, is->subtitle_stream);
2271
+    if (is->ic) {
2272
+        avformat_close_input(&is->ic);
2273
+    }
2274
+}
2275
+
2276
+static int stream_setup(VideoState *is)
2261 2277
 {
2262
-    VideoState *is = arg;
2263 2278
     AVFormatContext *ic = NULL;
2264 2279
     int err, i, ret;
2265 2280
     int st_index[AVMEDIA_TYPE_NB];
2266
-    AVPacket pkt1, *pkt = &pkt1;
2267
-    int eof = 0;
2268
-    int pkt_in_play_range = 0;
2269 2281
     AVDictionaryEntry *t;
2270 2282
     AVDictionary **opts;
2271 2283
     int orig_nb_streams;
... ...
@@ -2385,6 +2397,23 @@ static int decode_thread(void *arg)
2385 2385
         goto fail;
2386 2386
     }
2387 2387
 
2388
+    return 0;
2389
+
2390
+fail:
2391
+    stream_close(is);
2392
+
2393
+    return ret;
2394
+}
2395
+
2396
+/* this thread gets the stream from the disk or the network */
2397
+static int decode_thread(void *arg)
2398
+{
2399
+    VideoState *is        = arg;
2400
+    AVPacket pkt1, *pkt   = &pkt1;
2401
+    AVFormatContext *ic   = is->ic;
2402
+    int pkt_in_play_range = 0;
2403
+    int ret, eof          = 0;
2404
+
2388 2405
     for (;;) {
2389 2406
         if (is->abort_request)
2390 2407
             break;
... ...
@@ -2499,20 +2528,9 @@ static int decode_thread(void *arg)
2499 2499
     }
2500 2500
 
2501 2501
     ret = 0;
2502
- fail:
2503
-    /* disable interrupting */
2504
-    global_video_state = NULL;
2505 2502
 
2506
-    /* close each stream */
2507
-    if (is->audio_stream >= 0)
2508
-        stream_component_close(is, is->audio_stream);
2509
-    if (is->video_stream >= 0)
2510
-        stream_component_close(is, is->video_stream);
2511
-    if (is->subtitle_stream >= 0)
2512
-        stream_component_close(is, is->subtitle_stream);
2513
-    if (is->ic) {
2514
-        avformat_close_input(&is->ic);
2515
-    }
2503
+fail:
2504
+    stream_close(is);
2516 2505
 
2517 2506
     if (ret != 0) {
2518 2507
         SDL_Event event;
... ...
@@ -2536,6 +2554,11 @@ static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2536 2536
     is->ytop    = 0;
2537 2537
     is->xleft   = 0;
2538 2538
 
2539
+    if (stream_setup(is) < 0) {
2540
+        av_free(is);
2541
+        return NULL;
2542
+    }
2543
+
2539 2544
     /* start video display */
2540 2545
     is->pictq_mutex = SDL_CreateMutex();
2541 2546
     is->pictq_cond  = SDL_CreateCond();