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