renatofilho@586: #include <sys/stat.h>
renatofilho@586: #include <fcntl.h>
renatofilho@586: #include <unistd.h>
renatofilho@586: #include <string.h>
renatofilho@586: 
renatofilho@586: #include <gst/gst.h>
renatofilho@586: #include <glib.h> 
renatofilho@586: 
renatofilho@588: #include "gmencoder.h"
renatofilho@586: 
renatofilho@588: #define FILE_OUT  1
renatofilho@586: 
renatofilho@586: static GMainLoop *mainloop = NULL;
renatofilho@588: /* Options */
renatofilho@588: static gchar*   input_file      = NULL;
renatofilho@588: static gchar*   video_encode    = NULL;
renatofilho@588: static gchar*   video_opts      = NULL;
renatofilho@588: static gdouble  video_fps       = 0.0;
renatofilho@588: static gint     video_rate      = 0;
renatofilho@588: static gint     video_width     = 0;
renatofilho@588: static gint     video_height    = 0;
renatofilho@588: static gchar*   audio_encode    = NULL;
renatofilho@588: static gchar*   audio_opts      = NULL;
renatofilho@588: static double   audio_rate      = 0.0;
renatofilho@600: static gchar*   mux_name        = NULL;
renatofilho@600: static gchar*   output_uri      = NULL;
renatofilho@586: 
renatofilho@586: 
renatofilho@678: 
renatofilho@678: static gboolean
renatofilho@678: _quit (gpointer data)
renatofilho@678: {
renatofilho@678: 	g_object_unref (data);
renatofilho@678: 	g_main_loop_quit (mainloop);
renatofilho@678: 	return FALSE;
renatofilho@678: }
renatofilho@678: 
renatofilho@586: static void
renatofilho@586: _mencoder_eos_cb (GMencoder *mencoder, gpointer data)
renatofilho@678: {		
renatofilho@586:     g_print ("EOS\n");
renatofilho@678: 	g_idle_add (_quit, mencoder);
renatofilho@586: }
renatofilho@586: 
renatofilho@678: 
renatofilho@586: static void
renatofilho@586: _mencoder_error_cb (GMencoder *mencoder, const gchar* msg, gpointer data)
renatofilho@586: {
renatofilho@586:     g_print ("Error: %s\n", msg);
renatofilho@678: 	g_idle_add (_quit, mencoder);
renatofilho@586: }
renatofilho@586: 
renatofilho@586: static gboolean
renatofilho@586: _io_channel_cb (GIOChannel *ch,
renatofilho@586:                 GIOCondition condition,
renatofilho@586:                 gpointer data)
renatofilho@586: {
renatofilho@586:     GString *cmd = g_string_new ("");
renatofilho@586:     g_io_channel_read_line_string (ch, cmd, NULL, NULL);
renatofilho@586: 
renatofilho@586:     if (strcmp (cmd->str, "PLAY\n") == 0) {
renatofilho@586:         g_mencoder_play_stream (G_MENCODER (data));
renatofilho@586:     } else if (strcmp (cmd->str, "PAUSE\n") == 0) {
renatofilho@586:         g_mencoder_pause_stream (G_MENCODER (data));
renatofilho@586:     } else if (strcmp (cmd->str, "STOP\n") == 0) {
renatofilho@586:         g_mencoder_close_stream (G_MENCODER (data));
renatofilho@586:     } else if (strcmp (cmd->str, "QUIT\n") == 0) {
renatofilho@586:         g_mencoder_close_stream (G_MENCODER (data));
renatofilho@586:         g_main_loop_quit (mainloop);
renatofilho@586:     }
renatofilho@586:     g_string_free (cmd, TRUE);
renatofilho@586:     return TRUE;
renatofilho@586: }
renatofilho@586: 
renatofilho@586: int 
renatofilho@586: main (int argc, char** argv)
renatofilho@586: {
renatofilho@600:     GMencoder *coder = NULL;
renatofilho@586:     GIOChannel *ch;
renatofilho@588:     gchar **vopts;
renatofilho@588:     gchar **aopts;
renatofilho@600:     gchar **files;
renatofilho@600:     gint i;
renatofilho@588: 
renatofilho@588:     GOptionContext *context;
renatofilho@588:     static const GOptionEntry options []  = {
renatofilho@600:         {"input-files",    'i', 0, G_OPTION_ARG_STRING,          &input_file,    "Input File", NULL},
renatofilho@588:         {"video-encode",    0, 0, G_OPTION_ARG_STRING,          &video_encode,  "GstElementName for used to video encode", NULL},
renatofilho@588:         {"video-opts",      0, 0, G_OPTION_ARG_STRING,          &video_opts,    "Properties to set on video element", NULL},
renatofilho@588:         {"video-fps",       0, 0, G_OPTION_ARG_DOUBLE,          &video_fps,     "Video FPS", NULL},
renatofilho@588:         {"video-rate",      0, 0, G_OPTION_ARG_INT,             &video_rate,    "Video rate", NULL},
renatofilho@588:         {"video-width",     0, 0, G_OPTION_ARG_INT,             &video_width,   "Video width", NULL},
renatofilho@588:         {"video-height",    0, 0, G_OPTION_ARG_INT,             &video_height,  "Video height", NULL},
renatofilho@588:         {"audio-encode",    0, 0, G_OPTION_ARG_STRING,          &audio_encode,  "GstElementName for use to audio encode", NULL},
renatofilho@588:         {"audio-opts",      0, 0, G_OPTION_ARG_STRING,          &audio_opts,    "Properties to set on audio element", NULL},
renatofilho@588:         {"audio-rate",      0, 0, G_OPTION_ARG_INT,             &audio_rate,    "Audio rate", NULL},
renatofilho@600:         {"mux-element",     0, 0, G_OPTION_ARG_STRING,          &mux_name,      "GstElementName for use to mux file", NULL},
renatofilho@600:         {"output-uri",    'o', 0, G_OPTION_ARG_STRING,          &output_uri,    "Uri to output", NULL},
renatofilho@588:         { NULL }
renatofilho@588:     };
renatofilho@586: 
renatofilho@586: 	g_type_init ();
renatofilho@588:     g_thread_init (NULL);
renatofilho@600: 	mainloop = g_main_loop_new (NULL, FALSE);
renatofilho@588: 
renatofilho@600: 	g_set_prgname ("gmemcoder");
renatofilho@588:     context = g_option_context_new (NULL);
renatofilho@588:     g_option_context_set_help_enabled (context, TRUE);
renatofilho@588:     g_option_context_add_main_entries (context, options, NULL);
renatofilho@588:     g_option_context_add_group (context, gst_init_get_option_group ());
renatofilho@588:     g_option_context_parse (context, &argc, &argv, NULL);
renatofilho@588: 
renatofilho@586: 	gst_init (&argc, &argv);
renatofilho@586: 
renatofilho@600:     if (output_uri == NULL) {
renatofilho@600:         g_print ("You need specify output-uri.\nTry --help for more information.\n");
renatofilho@588:         return 1; 
renatofilho@588:     }
renatofilho@588: 
renatofilho@588:     if (input_file == NULL) {
renatofilho@588:         g_print ("You need specify input file\nTry --help for more information.\n");
renatofilho@588:     }
renatofilho@586: 
renatofilho@586:     coder = g_mencoder_new ();
renatofilho@586:     ch = g_io_channel_unix_new (0);
renatofilho@586: 
renatofilho@588:     aopts = g_strsplit (audio_opts, ",", 0);
renatofilho@588:     vopts = g_strsplit (video_opts, ",", 0);
renatofilho@588: 
renatofilho@588:     g_mencoder_setup_stream (coder, 
renatofilho@600:             mux_name,
renatofilho@588:             video_encode, vopts, video_fps, video_rate, video_width, video_height,
renatofilho@588:             audio_encode, aopts, audio_rate, 
renatofilho@600:             output_uri);
renatofilho@588: 
renatofilho@600:     files = g_strsplit (input_file, ",", 0);
renatofilho@600:     for (i=0; i < g_strv_length (files); i++) {
renatofilho@600:         if (!g_mencoder_append_uri (coder, files[i])) {
renatofilho@600:             g_debug ("Invalid uri: %s", files[i]);
renatofilho@600:         } 
renatofilho@600:     }
renatofilho@600:                 
renatofilho@600:     g_strfreev (files);
renatofilho@588:     g_strfreev (aopts);
renatofilho@588:     g_strfreev (vopts);
renatofilho@586: 
renatofilho@600: 
renatofilho@586:     g_io_add_watch (ch, G_IO_IN, _io_channel_cb, coder);    
renatofilho@586: 
renatofilho@586:     g_signal_connect (G_OBJECT (coder),
renatofilho@586:                         "eos",
renatofilho@586:                         G_CALLBACK (_mencoder_eos_cb),
renatofilho@586:                         mainloop);
renatofilho@586: 
renatofilho@586:     g_signal_connect (G_OBJECT (coder),
renatofilho@586:                         "error",
renatofilho@586:                         G_CALLBACK (_mencoder_error_cb),
renatofilho@586:                         mainloop);
renatofilho@586: 
renatofilho@600:     g_mencoder_play_stream (coder);
renatofilho@600: 
renatofilho@600:     g_debug ("RUNNING..");
renatofilho@586: 	g_main_loop_run (mainloop);
renatofilho@634: 	g_debug ("DONE");
renatofilho@600:     g_object_unref (coder);
renatofilho@600: 
renatofilho@586: 	return 0;
renatofilho@586: }