gmyth-stream/gmencoder/src/main.c
author renatofilho
Tue Jan 29 18:40:18 2008 +0000 (2008-01-29)
branchtrunk
changeset 895 4bdd0f922b68
parent 789 f9cd59844f78
child 898 4fbbacbbca34
permissions -rw-r--r--
[svn r901] fixed var names
     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 #include <libgnomevfs/gnome-vfs.h>
     9 
    10 #include "gmencoder.h"
    11 
    12 #define FILE_OUT  1
    13 
    14 static GMainLoop *mainloop = NULL;
    15 /*
    16  * Options 
    17  */
    18 static gchar   *input_file = NULL;
    19 static gchar   *video_encode = NULL;
    20 static gchar   *video_opts = NULL;
    21 static gdouble  video_fps = 0.0;
    22 static gint     video_rate = 0;
    23 static gint     video_width = 0;
    24 static gint     video_height = 0;
    25 static gchar   *audio_encode = NULL;
    26 static gchar   *audio_opts = NULL;
    27 static double   audio_rate = 0.0;
    28 static gchar   *mux_name = NULL;
    29 static gchar   *output_uri = NULL;
    30 static gboolean chunked = FALSE;
    31 static gboolean deinterlace = FALSE;
    32 
    33 
    34 
    35 static gboolean
    36 _quit(gpointer data)
    37 {
    38     //g_object_unref(data);
    39     g_main_loop_quit(mainloop);
    40     return FALSE;
    41 }
    42 
    43 static void
    44 _mencoder_eos_cb(GMencoder * mencoder, gpointer data)
    45 {
    46     g_print("PROGRESS: 100\n");
    47     g_idle_add(_quit, mencoder);
    48 }
    49 
    50 
    51 static void
    52 _mencoder_error_cb(GMencoder * mencoder, const gchar * msg, gpointer data)
    53 {
    54     g_print("Error: %s\n", msg);
    55     g_idle_add(_quit, mencoder);
    56 }
    57 
    58 static          gboolean
    59 _io_channel_cb(GIOChannel * ch, GIOCondition condition, gpointer data)
    60 {
    61     GString        *cmd = g_string_new("");
    62     g_io_channel_read_line_string(ch, cmd, NULL, NULL);
    63 
    64     if (strcmp(cmd->str, "PLAY\n") == 0) {
    65         g_mencoder_play_stream(G_MENCODER(data));
    66     } else if (strcmp(cmd->str, "PAUSE\n") == 0) {
    67         g_mencoder_pause_stream(G_MENCODER(data));
    68     } else if (strcmp(cmd->str, "STOP\n") == 0) {
    69         g_mencoder_close_stream(G_MENCODER(data));
    70     } else if (strcmp(cmd->str, "QUIT\n") == 0) {
    71         g_mencoder_close_stream(G_MENCODER(data));
    72         g_main_loop_quit(mainloop);
    73     }
    74     g_string_free(cmd, TRUE);
    75     return TRUE;
    76 }
    77 
    78 int
    79 main(int argc, char **argv)
    80 {
    81     GMencoder      *coder = NULL;
    82     GIOChannel     *ch;
    83     gchar         **vopts;
    84     gchar         **aopts;
    85     gchar         **files;
    86     gint            i;
    87     gboolean      ret;
    88 
    89     GOptionContext *context;
    90     static const GOptionEntry options[] = {
    91         {"input-files", 'i', 0, G_OPTION_ARG_STRING, &input_file,
    92          "Input File", NULL},
    93 
    94         {"video-encode", 0, 0, G_OPTION_ARG_STRING, &video_encode,
    95          "GstElementName for used to video encode", NULL},
    96 
    97         {"video-opts", 0, 0, G_OPTION_ARG_STRING, &video_opts,
    98          "Properties to set on video element", NULL},
    99 
   100         {"video-fps", 0, 0, G_OPTION_ARG_DOUBLE, &video_fps,
   101          "Video FPS", NULL},
   102 
   103         {"video-rate", 0, 0, G_OPTION_ARG_INT, &video_rate,
   104          "Video rate", NULL},
   105 
   106         {"video-width", 0, 0, G_OPTION_ARG_INT, &video_width,
   107          "Video width", NULL},
   108 
   109         {"video-height", 0, 0, G_OPTION_ARG_INT, &video_height,
   110          "Video height", NULL},
   111 
   112         {"audio-encode", 0, 0, G_OPTION_ARG_STRING, &audio_encode,
   113          "GstElementName for use to audio encode", NULL},
   114 
   115         {"audio-opts", 0, 0, G_OPTION_ARG_STRING, &audio_opts,
   116          "Properties to set on audio element", NULL},
   117 
   118         {"audio-rate", 0, 0, G_OPTION_ARG_INT, &audio_rate,
   119          "Audio rate", NULL},
   120 
   121         {"mux-element", 0, 0, G_OPTION_ARG_STRING, &mux_name,
   122          "GstElementName for use to mux file", NULL},
   123 
   124         {"output-uri", 'o', 0, G_OPTION_ARG_STRING, &output_uri,
   125          "Uri to output", NULL},
   126 
   127 	{"chunked", 'c', 0, G_OPTION_ARG_NONE, &chunked, 
   128          "Send package chunked", NULL},
   129 
   130 	{"deinterlace", 'd', 0, G_OPTION_ARG_NONE, &deinterlace, 
   131          "Use to deinterlace videos", NULL},
   132 
   133 
   134         {NULL}
   135     };
   136 
   137     g_type_init();
   138     g_thread_init(NULL);
   139     gnome_vfs_init ();
   140     mainloop = g_main_loop_new(NULL, FALSE);
   141 
   142     g_set_prgname("gmemcoder");
   143     context = g_option_context_new(NULL);
   144     g_option_context_set_help_enabled(context, TRUE);
   145     g_option_context_add_main_entries(context, options, NULL);
   146     g_option_context_add_group(context, gst_init_get_option_group());
   147     g_option_context_parse(context, &argc, &argv, NULL);
   148 
   149     gst_init(&argc, &argv);
   150 
   151     if (output_uri == NULL) {
   152         g_print("You need to specify output-uri.\nTry --help "
   153                 "for more information.\n");
   154         return 1;
   155     }
   156 
   157     if (input_file == NULL) {
   158         g_print("You need to specify input file\nTry --help "
   159                 "for more information.\n");
   160     }
   161 
   162     coder = g_mencoder_new();
   163     ch = g_io_channel_unix_new(0);
   164 
   165     if (audio_opts != NULL)
   166         aopts = g_strsplit(audio_opts, ",", 0);
   167     else
   168         aopts = NULL;
   169 
   170     if (video_opts != NULL)
   171         vopts = g_strsplit(video_opts, ",", 0);
   172     else
   173         vopts = NULL;
   174 
   175     ret = g_mencoder_setup_stream(coder, chunked, deinterlace, mux_name,
   176                                   video_encode, vopts, video_fps,
   177                                   video_rate, video_width, video_height,
   178                                   audio_encode, aopts, audio_rate, output_uri);
   179 
   180     if (ret == TRUE) {
   181         files = g_strsplit(input_file, ",", 0);
   182         for (i = 0; i < g_strv_length(files); i++) {
   183             if (!g_mencoder_append_uri(coder, files[i])) {
   184                 g_debug("Invalid uri: %s", files[i]);
   185             }
   186         }
   187         g_strfreev(files);
   188     }
   189 
   190     g_strfreev(aopts);
   191     g_strfreev(vopts);
   192 
   193     if (ret == TRUE) {
   194         g_io_add_watch(ch, G_IO_IN, _io_channel_cb, coder);
   195         g_signal_connect(G_OBJECT(coder),
   196                          "eos", G_CALLBACK(_mencoder_eos_cb), mainloop);
   197 
   198         g_signal_connect(G_OBJECT(coder),
   199                          "error", G_CALLBACK(_mencoder_error_cb), mainloop);
   200 
   201         g_mencoder_play_stream(coder);
   202         g_main_loop_run(mainloop);
   203     }
   204 
   205     g_object_unref(coder);
   206     return 0;
   207 }