Browse code

avplay: Statically allocate the player state

And move the resource deallocation in stream_open failure path.

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

Luca Barbato authored on 2016/01/02 20:19:31
Showing 1 changed files
... ...
@@ -270,7 +270,8 @@ static int autorotate = 1;
270 270
 
271 271
 /* current context */
272 272
 static int is_full_screen;
273
-static PlayerState *player;
273
+static PlayerState player_state;
274
+static PlayerState *player = &player_state;
274 275
 static int64_t audio_callback_time;
275 276
 
276 277
 static AVPacket flush_pkt;
... ...
@@ -1229,7 +1230,6 @@ static void player_close(PlayerState *is)
1229 1229
     if (is->img_convert_ctx)
1230 1230
         sws_freeContext(is->img_convert_ctx);
1231 1231
 #endif
1232
-    av_free(is);
1233 1232
 }
1234 1233
 
1235 1234
 static void do_exit(void)
... ...
@@ -2399,8 +2399,6 @@ static int stream_setup(PlayerState *is)
2399 2399
     return 0;
2400 2400
 
2401 2401
 fail:
2402
-    stream_close(is);
2403
-
2404 2402
     return ret;
2405 2403
 }
2406 2404
 
... ...
@@ -2541,21 +2539,18 @@ fail:
2541 2541
     return 0;
2542 2542
 }
2543 2543
 
2544
-static PlayerState *stream_open(const char *filename, AVInputFormat *iformat)
2544
+static int stream_open(PlayerState *is,
2545
+                       const char *filename, AVInputFormat *iformat)
2545 2546
 {
2546
-    PlayerState *is;
2547
+    int ret;
2547 2548
 
2548
-    is = av_mallocz(sizeof(PlayerState));
2549
-    if (!is)
2550
-        return NULL;
2551 2549
     av_strlcpy(is->filename, filename, sizeof(is->filename));
2552 2550
     is->iformat = iformat;
2553 2551
     is->ytop    = 0;
2554 2552
     is->xleft   = 0;
2555 2553
 
2556
-    if (stream_setup(is) < 0) {
2557
-        av_free(is);
2558
-        return NULL;
2554
+    if ((ret = stream_setup(is)) < 0) {
2555
+        return ret;
2559 2556
     }
2560 2557
 
2561 2558
     /* start video display */
... ...
@@ -2567,12 +2562,12 @@ static PlayerState *stream_open(const char *filename, AVInputFormat *iformat)
2567 2567
 
2568 2568
     is->av_sync_type = av_sync_type;
2569 2569
     is->refresh_tid  = SDL_CreateThread(refresh_thread, is);
2570
+    if (!is->refresh_tid)
2571
+        return -1;
2570 2572
     is->parse_tid    = SDL_CreateThread(decode_thread, is);
2571
-    if (!is->parse_tid) {
2572
-        av_free(is);
2573
-        return NULL;
2574
-    }
2575
-    return is;
2573
+    if (!is->parse_tid)
2574
+        return -1;
2575
+    return 0;
2576 2576
 }
2577 2577
 
2578 2578
 static void stream_cycle_channel(PlayerState *is, int codec_type)
... ...
@@ -3057,7 +3052,11 @@ int main(int argc, char **argv)
3057 3057
     av_init_packet(&flush_pkt);
3058 3058
     flush_pkt.data = (uint8_t *)&flush_pkt;
3059 3059
 
3060
-    player = stream_open(input_filename, file_iformat);
3060
+    if (stream_open(player, input_filename, file_iformat) < 0) {
3061
+        fprintf(stderr, "Could not setup the player\n");
3062
+        stream_close(player);
3063
+        exit(1);
3064
+    }
3061 3065
 
3062 3066
     event_loop();
3063 3067