gmyth-stream/gmemcoder/src/main.c
author renatofilho
Fri May 11 14:41:11 2007 +0100 (2007-05-11)
branchtrunk
changeset 644 b937c837e929
parent 600 7da2a5e32fa6
child 678 89bdaa9e72bf
permissions -rw-r--r--
[svn r650] created test program
     1 #include <sys/stat.h>
     2 #include <fcntl.h>
     3 #include <unistd.h>
     4 #include <string.h>
     5 
     6 #include <gst/gst.h>
     7 #include <glib.h> 
     8 
     9 #include "gmencoder.h"
    10 
    11 #define FILE_OUT  1
    12 
    13 static GMainLoop *mainloop = NULL;
    14 /* Options */
    15 static gchar*   input_file      = NULL;
    16 static gchar*   video_encode    = NULL;
    17 static gchar*   video_opts      = NULL;
    18 static gdouble  video_fps       = 0.0;
    19 static gint     video_rate      = 0;
    20 static gint     video_width     = 0;
    21 static gint     video_height    = 0;
    22 static gchar*   audio_encode    = NULL;
    23 static gchar*   audio_opts      = NULL;
    24 static double   audio_rate      = 0.0;
    25 static gchar*   mux_name        = NULL;
    26 static gchar*   output_uri      = NULL;
    27 
    28 
    29 static void
    30 _mencoder_eos_cb (GMencoder *mencoder, gpointer data)
    31 {
    32     g_print ("EOS\n");
    33     g_main_loop_quit ((GMainLoop *) data);
    34 }
    35 
    36 static void
    37 _mencoder_error_cb (GMencoder *mencoder, const gchar* msg, gpointer data)
    38 {
    39     g_print ("Error: %s\n", msg);
    40 	g_mencoder_close_stream (mencoder);
    41     g_main_loop_quit ((GMainLoop *) data);
    42 }
    43 
    44 static gboolean
    45 _io_channel_cb (GIOChannel *ch,
    46                 GIOCondition condition,
    47                 gpointer data)
    48 {
    49     GString *cmd = g_string_new ("");
    50     g_io_channel_read_line_string (ch, cmd, NULL, NULL);
    51 
    52     if (strcmp (cmd->str, "PLAY\n") == 0) {
    53         g_mencoder_play_stream (G_MENCODER (data));
    54     } else if (strcmp (cmd->str, "PAUSE\n") == 0) {
    55         g_mencoder_pause_stream (G_MENCODER (data));
    56     } else if (strcmp (cmd->str, "STOP\n") == 0) {
    57         g_mencoder_close_stream (G_MENCODER (data));
    58     } else if (strcmp (cmd->str, "QUIT\n") == 0) {
    59         g_mencoder_close_stream (G_MENCODER (data));
    60         g_main_loop_quit (mainloop);
    61     }
    62     g_string_free (cmd, TRUE);
    63     return TRUE;
    64 }
    65 
    66 int 
    67 main (int argc, char** argv)
    68 {
    69     GMencoder *coder = NULL;
    70     GIOChannel *ch;
    71     gchar **vopts;
    72     gchar **aopts;
    73     gchar **files;
    74     gint i;
    75 
    76     GOptionContext *context;
    77     static const GOptionEntry options []  = {
    78         {"input-files",    'i', 0, G_OPTION_ARG_STRING,          &input_file,    "Input File", NULL},
    79         {"video-encode",    0, 0, G_OPTION_ARG_STRING,          &video_encode,  "GstElementName for used to video encode", NULL},
    80         {"video-opts",      0, 0, G_OPTION_ARG_STRING,          &video_opts,    "Properties to set on video element", NULL},
    81         {"video-fps",       0, 0, G_OPTION_ARG_DOUBLE,          &video_fps,     "Video FPS", NULL},
    82         {"video-rate",      0, 0, G_OPTION_ARG_INT,             &video_rate,    "Video rate", NULL},
    83         {"video-width",     0, 0, G_OPTION_ARG_INT,             &video_width,   "Video width", NULL},
    84         {"video-height",    0, 0, G_OPTION_ARG_INT,             &video_height,  "Video height", NULL},
    85         {"audio-encode",    0, 0, G_OPTION_ARG_STRING,          &audio_encode,  "GstElementName for use to audio encode", NULL},
    86         {"audio-opts",      0, 0, G_OPTION_ARG_STRING,          &audio_opts,    "Properties to set on audio element", NULL},
    87         {"audio-rate",      0, 0, G_OPTION_ARG_INT,             &audio_rate,    "Audio rate", NULL},
    88         {"mux-element",     0, 0, G_OPTION_ARG_STRING,          &mux_name,      "GstElementName for use to mux file", NULL},
    89         {"output-uri",    'o', 0, G_OPTION_ARG_STRING,          &output_uri,    "Uri to output", NULL},
    90         { NULL }
    91     };
    92 
    93 	g_type_init ();
    94     g_thread_init (NULL);
    95 	mainloop = g_main_loop_new (NULL, FALSE);
    96 
    97 	g_set_prgname ("gmemcoder");
    98     context = g_option_context_new (NULL);
    99     g_option_context_set_help_enabled (context, TRUE);
   100     g_option_context_add_main_entries (context, options, NULL);
   101     g_option_context_add_group (context, gst_init_get_option_group ());
   102     g_option_context_parse (context, &argc, &argv, NULL);
   103 
   104 	gst_init (&argc, &argv);
   105 
   106     if (output_uri == NULL) {
   107         g_print ("You need specify output-uri.\nTry --help for more information.\n");
   108         return 1; 
   109     }
   110 
   111     if (input_file == NULL) {
   112         g_print ("You need specify input file\nTry --help for more information.\n");
   113     }
   114 
   115     coder = g_mencoder_new ();
   116     ch = g_io_channel_unix_new (0);
   117 
   118     aopts = g_strsplit (audio_opts, ",", 0);
   119     vopts = g_strsplit (video_opts, ",", 0);
   120 
   121     g_mencoder_setup_stream (coder, 
   122             mux_name,
   123             video_encode, vopts, video_fps, video_rate, video_width, video_height,
   124             audio_encode, aopts, audio_rate, 
   125             output_uri);
   126 
   127     files = g_strsplit (input_file, ",", 0);
   128     for (i=0; i < g_strv_length (files); i++) {
   129         if (!g_mencoder_append_uri (coder, files[i])) {
   130             g_debug ("Invalid uri: %s", files[i]);
   131         } 
   132     }
   133                 
   134     g_strfreev (files);
   135     g_strfreev (aopts);
   136     g_strfreev (vopts);
   137 
   138 
   139     g_io_add_watch (ch, G_IO_IN, _io_channel_cb, coder);    
   140 
   141     g_signal_connect (G_OBJECT (coder),
   142                         "eos",
   143                         G_CALLBACK (_mencoder_eos_cb),
   144                         mainloop);
   145 
   146     g_signal_connect (G_OBJECT (coder),
   147                         "error",
   148                         G_CALLBACK (_mencoder_error_cb),
   149                         mainloop);
   150 
   151     g_mencoder_play_stream (coder);
   152 
   153     g_debug ("RUNNING..");
   154 	g_main_loop_run (mainloop);
   155 	g_debug ("DONE");
   156     g_object_unref (coder);
   157 
   158 	return 0;
   159 }