gmyth-stream/gmemcoder/src/main.c
author rosfran
Thu May 17 15:43:02 2007 +0100 (2007-05-17)
branchtrunk
changeset 683 4a784466828d
parent 634 43f66895cc86
child 691 726550e48c21
permissions -rw-r--r--
[svn r689] Added new test case to TV Chain; put back the testing scripts configuration file.
     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 
    30 static gboolean
    31 _quit (gpointer data)
    32 {
    33 	g_debug ("Quit");
    34 	g_object_unref (data);
    35 	g_main_loop_quit (mainloop);
    36 	return FALSE;
    37 }
    38 
    39 static void
    40 _mencoder_eos_cb (GMencoder *mencoder, gpointer data)
    41 {		
    42     g_print ("EOS\n");
    43 	g_idle_add (_quit, mencoder);
    44 }
    45 
    46 
    47 static void
    48 _mencoder_error_cb (GMencoder *mencoder, const gchar* msg, gpointer data)
    49 {
    50     g_print ("Error: %s\n", msg);
    51 	g_idle_add (_quit, mencoder);
    52 }
    53 
    54 static gboolean
    55 _io_channel_cb (GIOChannel *ch,
    56                 GIOCondition condition,
    57                 gpointer data)
    58 {
    59     GString *cmd = g_string_new ("");
    60     g_io_channel_read_line_string (ch, cmd, NULL, NULL);
    61 
    62     if (strcmp (cmd->str, "PLAY\n") == 0) {
    63         g_mencoder_play_stream (G_MENCODER (data));
    64     } else if (strcmp (cmd->str, "PAUSE\n") == 0) {
    65         g_mencoder_pause_stream (G_MENCODER (data));
    66     } else if (strcmp (cmd->str, "STOP\n") == 0) {
    67         g_mencoder_close_stream (G_MENCODER (data));
    68     } else if (strcmp (cmd->str, "QUIT\n") == 0) {
    69         g_mencoder_close_stream (G_MENCODER (data));
    70         g_main_loop_quit (mainloop);
    71     }
    72     g_string_free (cmd, TRUE);
    73     return TRUE;
    74 }
    75 
    76 int 
    77 main (int argc, char** argv)
    78 {
    79     GMencoder *coder = NULL;
    80     GIOChannel *ch;
    81     gchar **vopts;
    82     gchar **aopts;
    83     gchar **files;
    84     gint i;
    85 
    86     GOptionContext *context;
    87     static const GOptionEntry options []  = {
    88         {"input-files",    'i', 0, G_OPTION_ARG_STRING,          &input_file,    "Input File", NULL},
    89         {"video-encode",    0, 0, G_OPTION_ARG_STRING,          &video_encode,  "GstElementName for used to video encode", NULL},
    90         {"video-opts",      0, 0, G_OPTION_ARG_STRING,          &video_opts,    "Properties to set on video element", NULL},
    91         {"video-fps",       0, 0, G_OPTION_ARG_DOUBLE,          &video_fps,     "Video FPS", NULL},
    92         {"video-rate",      0, 0, G_OPTION_ARG_INT,             &video_rate,    "Video rate", NULL},
    93         {"video-width",     0, 0, G_OPTION_ARG_INT,             &video_width,   "Video width", NULL},
    94         {"video-height",    0, 0, G_OPTION_ARG_INT,             &video_height,  "Video height", NULL},
    95         {"audio-encode",    0, 0, G_OPTION_ARG_STRING,          &audio_encode,  "GstElementName for use to audio encode", NULL},
    96         {"audio-opts",      0, 0, G_OPTION_ARG_STRING,          &audio_opts,    "Properties to set on audio element", NULL},
    97         {"audio-rate",      0, 0, G_OPTION_ARG_INT,             &audio_rate,    "Audio rate", NULL},
    98         {"mux-element",     0, 0, G_OPTION_ARG_STRING,          &mux_name,      "GstElementName for use to mux file", NULL},
    99         {"output-uri",    'o', 0, G_OPTION_ARG_STRING,          &output_uri,    "Uri to output", NULL},
   100         { NULL }
   101     };
   102 
   103 	g_type_init ();
   104     g_thread_init (NULL);
   105 	mainloop = g_main_loop_new (NULL, FALSE);
   106 
   107 	g_set_prgname ("gmemcoder");
   108     context = g_option_context_new (NULL);
   109     g_option_context_set_help_enabled (context, TRUE);
   110     g_option_context_add_main_entries (context, options, NULL);
   111     g_option_context_add_group (context, gst_init_get_option_group ());
   112     g_option_context_parse (context, &argc, &argv, NULL);
   113 
   114 	gst_init (&argc, &argv);
   115 
   116     if (output_uri == NULL) {
   117         g_print ("You need specify output-uri.\nTry --help for more information.\n");
   118         return 1; 
   119     }
   120 
   121     if (input_file == NULL) {
   122         g_print ("You need specify input file\nTry --help for more information.\n");
   123     }
   124 
   125     coder = g_mencoder_new ();
   126     ch = g_io_channel_unix_new (0);
   127 
   128     aopts = g_strsplit (audio_opts, ",", 0);
   129     vopts = g_strsplit (video_opts, ",", 0);
   130 
   131     g_mencoder_setup_stream (coder, 
   132             mux_name,
   133             video_encode, vopts, video_fps, video_rate, video_width, video_height,
   134             audio_encode, aopts, audio_rate, 
   135             output_uri);
   136 
   137     files = g_strsplit (input_file, ",", 0);
   138     for (i=0; i < g_strv_length (files); i++) {
   139         if (!g_mencoder_append_uri (coder, files[i])) {
   140             g_debug ("Invalid uri: %s", files[i]);
   141         } 
   142     }
   143                 
   144     g_strfreev (files);
   145     g_strfreev (aopts);
   146     g_strfreev (vopts);
   147 
   148 
   149     g_io_add_watch (ch, G_IO_IN, _io_channel_cb, coder);    
   150 
   151     g_signal_connect (G_OBJECT (coder),
   152                         "eos",
   153                         G_CALLBACK (_mencoder_eos_cb),
   154                         mainloop);
   155 
   156     g_signal_connect (G_OBJECT (coder),
   157                         "error",
   158                         G_CALLBACK (_mencoder_error_cb),
   159                         mainloop);
   160 
   161     g_mencoder_play_stream (coder);
   162 
   163     g_debug ("RUNNING..");
   164 	g_main_loop_run (mainloop);
   165 	g_debug ("DONE");
   166     g_object_unref (coder);
   167 
   168 	return 0;
   169 }