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