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