gmyth-stream/gmemcoder/src/main.c
author renatofilho
Tue Jul 03 22:45:24 2007 +0100 (2007-07-03)
branchtrunk
changeset 772 47de5c5976bf
parent 760 36b8564d10e6
child 777 4127375c2a03
permissions -rw-r--r--
[svn r778] more properties type supported
     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 /*
    15  * Options 
    16  */
    17 static gchar   *input_file = NULL;
    18 static gchar   *video_encode = NULL;
    19 static gchar   *video_opts = NULL;
    20 static gdouble  video_fps = 0.0;
    21 static gint     video_rate = 0;
    22 static gint     video_width = 0;
    23 static gint     video_height = 0;
    24 static gchar   *audio_encode = NULL;
    25 static gchar   *audio_opts = NULL;
    26 static double   audio_rate = 0.0;
    27 static gchar   *mux_name = NULL;
    28 static gchar   *output_uri = NULL;
    29 static gboolean chunked = FALSE;
    30 
    31 
    32 
    33 static          gboolean
    34 _quit(gpointer data)
    35 {
    36     g_object_unref(data);
    37     g_main_loop_quit(mainloop);
    38     return FALSE;
    39 }
    40 
    41 static void
    42 _mencoder_eos_cb(GMencoder * mencoder, gpointer data)
    43 {
    44     g_print("PROGRESS: 100\n");
    45     g_idle_add(_quit, mencoder);
    46 }
    47 
    48 
    49 static void
    50 _mencoder_error_cb(GMencoder * mencoder, const gchar * msg, gpointer data)
    51 {
    52     g_print("Error: %s\n", msg);
    53     g_idle_add(_quit, mencoder);
    54 }
    55 
    56 static          gboolean
    57 _io_channel_cb(GIOChannel * ch, GIOCondition condition, 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     gboolean      ret;
    86 
    87     GOptionContext *context;
    88     static const GOptionEntry options[] = {
    89         {"input-files", 'i', 0, G_OPTION_ARG_STRING, &input_file,
    90          "Input File", NULL},
    91 
    92         {"video-encode", 0, 0, G_OPTION_ARG_STRING, &video_encode,
    93          "GstElementName for used to video encode", NULL},
    94 
    95         {"video-opts", 0, 0, G_OPTION_ARG_STRING, &video_opts,
    96          "Properties to set on video element", NULL},
    97 
    98         {"video-fps", 0, 0, G_OPTION_ARG_DOUBLE, &video_fps,
    99          "Video FPS", NULL},
   100 
   101         {"video-rate", 0, 0, G_OPTION_ARG_INT, &video_rate,
   102          "Video rate", NULL},
   103 
   104         {"video-width", 0, 0, G_OPTION_ARG_INT, &video_width,
   105          "Video width", NULL},
   106 
   107         {"video-height", 0, 0, G_OPTION_ARG_INT, &video_height,
   108          "Video height", NULL},
   109 
   110         {"audio-encode", 0, 0, G_OPTION_ARG_STRING, &audio_encode,
   111          "GstElementName for use to audio encode", NULL},
   112 
   113         {"audio-opts", 0, 0, G_OPTION_ARG_STRING, &audio_opts,
   114          "Properties to set on audio element", NULL},
   115 
   116         {"audio-rate", 0, 0, G_OPTION_ARG_INT, &audio_rate,
   117          "Audio rate", NULL},
   118 
   119         {"mux-element", 0, 0, G_OPTION_ARG_STRING, &mux_name,
   120          "GstElementName for use to mux file", NULL},
   121 
   122         {"output-uri", 'o', 0, G_OPTION_ARG_STRING, &output_uri,
   123          "Uri to output", NULL},
   124 
   125 	{"chunked", 'c', 0, G_OPTION_ARG_NONE, &chunked, 
   126          "Send package chunked", NULL},
   127 
   128         {NULL}
   129     };
   130 
   131     g_type_init();
   132     g_thread_init(NULL);
   133     mainloop = g_main_loop_new(NULL, FALSE);
   134 
   135     g_set_prgname("gmemcoder");
   136     context = g_option_context_new(NULL);
   137     g_option_context_set_help_enabled(context, TRUE);
   138     g_option_context_add_main_entries(context, options, NULL);
   139     g_option_context_add_group(context, gst_init_get_option_group());
   140     g_option_context_parse(context, &argc, &argv, NULL);
   141 
   142     gst_init(&argc, &argv);
   143 
   144     if (output_uri == NULL) {
   145         g_print("You need to specify output-uri.\nTry --help "
   146                 "for more information.\n");
   147         return 1;
   148     }
   149 
   150     if (input_file == NULL) {
   151         g_print("You need to specify input file\nTry --help "
   152                 "for more information.\n");
   153     }
   154 
   155     coder = g_mencoder_new();
   156     ch = g_io_channel_unix_new(0);
   157 
   158     if (audio_opts != NULL)
   159         aopts = g_strsplit(audio_opts, ",", 0);
   160     else
   161         aopts = NULL;
   162 
   163     if (video_opts != NULL)
   164         vopts = g_strsplit(video_opts, ",", 0);
   165     else
   166         vopts = NULL;
   167 
   168     
   169     ret = g_mencoder_setup_stream(coder, chunked, mux_name,
   170                                   video_encode, vopts, video_fps,
   171                                   video_rate, video_width, video_height,
   172                                   audio_encode, aopts, audio_rate, output_uri);
   173 
   174     if (ret == TRUE) {
   175         files = g_strsplit(input_file, ",", 0);
   176         for (i = 0; i < g_strv_length(files); i++) {
   177             if (!g_mencoder_append_uri(coder, files[i])) {
   178                 g_debug("Invalid uri: %s", files[i]);
   179             }
   180         }
   181         g_strfreev(files);
   182     }
   183 
   184     g_strfreev(aopts);
   185     g_strfreev(vopts);
   186 
   187     if (ret == TRUE) {
   188         g_io_add_watch(ch, G_IO_IN, _io_channel_cb, coder);
   189         g_signal_connect(G_OBJECT(coder),
   190                          "eos", G_CALLBACK(_mencoder_eos_cb), mainloop);
   191 
   192         g_signal_connect(G_OBJECT(coder),
   193                          "error", G_CALLBACK(_mencoder_error_cb), mainloop);
   194 
   195         g_mencoder_play_stream(coder);
   196         g_main_loop_run(mainloop);
   197     }
   198 
   199     g_object_unref(coder);
   200     return 0;
   201 }