gmyth-stream/gmemcoder/src/main.c
author renatofilho
Thu Jun 14 18:22:32 2007 +0100 (2007-06-14)
branchtrunk
changeset 753 8ee634511c1e
parent 748 bf9dac4bdc5d
child 754 cb885ee44618
permissions -rw-r--r--
[svn r759] fixed indent using GNU Style
     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 /* Options */
    15 static gchar *input_file = NULL;
    16 static gchar *video_encode = NULL;
    17 static gchar *video_opts = NULL;
    18 static gdouble video_fps = 0.0;
    19 static gint video_rate = 0;
    20 static gint video_width = 0;
    21 static gint video_height = 0;
    22 static gchar *audio_encode = NULL;
    23 static gchar *audio_opts = NULL;
    24 static double audio_rate = 0.0;
    25 static gchar *mux_name = NULL;
    26 static gchar *output_uri = NULL;
    27 
    28 
    29 
    30 static gboolean
    31 _quit(gpointer data)
    32 {
    33   g_object_unref(data);
    34   g_main_loop_quit(mainloop);
    35   return FALSE;
    36 }
    37 
    38 static void
    39 _mencoder_eos_cb(GMencoder * mencoder, gpointer data)
    40 {
    41   g_print("EOS\n");
    42   g_idle_add(_quit, mencoder);
    43 }
    44 
    45 
    46 static void
    47 _mencoder_error_cb(GMencoder * mencoder, const gchar * msg, gpointer data)
    48 {
    49   g_print("Error: %s\n", msg);
    50   g_idle_add(_quit, mencoder);
    51 }
    52 
    53 static gboolean
    54 _io_channel_cb(GIOChannel * ch, GIOCondition condition, gpointer data)
    55 {
    56   GString *cmd = g_string_new("");
    57   g_io_channel_read_line_string(ch, cmd, NULL, NULL);
    58 
    59   if (strcmp(cmd->str, "PLAY\n") == 0)
    60 	{
    61 	  g_mencoder_play_stream(G_MENCODER(data));
    62 	}
    63   else if (strcmp(cmd->str, "PAUSE\n") == 0)
    64 	{
    65 	  g_mencoder_pause_stream(G_MENCODER(data));
    66 	}
    67   else if (strcmp(cmd->str, "STOP\n") == 0)
    68 	{
    69 	  g_mencoder_close_stream(G_MENCODER(data));
    70 	}
    71   else if (strcmp(cmd->str, "QUIT\n") == 0)
    72 	{
    73 	  g_mencoder_close_stream(G_MENCODER(data));
    74 	  g_main_loop_quit(mainloop);
    75 	}
    76   g_string_free(cmd, TRUE);
    77   return TRUE;
    78 }
    79 
    80 int
    81 main(int argc, char **argv)
    82 {
    83   GMencoder *coder = NULL;
    84   GIOChannel *ch;
    85   gchar **vopts;
    86   gchar **aopts;
    87   gchar **files;
    88   gint i;
    89 
    90   GOptionContext *context;
    91   static const GOptionEntry options[] = {
    92 	{"input-files", 'i', 0, G_OPTION_ARG_STRING, &input_file,
    93 	 "Input File", NULL},
    94 
    95 	{"video-encode", 0, 0, G_OPTION_ARG_STRING, &video_encode,
    96 	 "GstElementName for used to video encode", NULL},
    97 
    98 	{"video-opts", 0, 0, G_OPTION_ARG_STRING, &video_opts,
    99 	 "Properties to set on video element", NULL},
   100 
   101 	{"video-fps", 0, 0, G_OPTION_ARG_DOUBLE, &video_fps,
   102 	 "Video FPS", NULL},
   103 
   104 	{"video-rate", 0, 0, G_OPTION_ARG_INT, &video_rate,
   105 	 "Video rate", NULL},
   106 
   107 	{"video-width", 0, 0, G_OPTION_ARG_INT, &video_width,
   108 	 "Video width", NULL},
   109 
   110 	{"video-height", 0, 0, G_OPTION_ARG_INT, &video_height,
   111 	 "Video height", NULL},
   112 
   113 	{"audio-encode", 0, 0, G_OPTION_ARG_STRING, &audio_encode,
   114 	 "GstElementName for use to audio encode", NULL},
   115 
   116 	{"audio-opts", 0, 0, G_OPTION_ARG_STRING, &audio_opts,
   117 	 "Properties to set on audio element", NULL},
   118 
   119 	{"audio-rate", 0, 0, G_OPTION_ARG_INT, &audio_rate,
   120 	 "Audio rate", NULL},
   121 
   122 	{"mux-element", 0, 0, G_OPTION_ARG_STRING, &mux_name,
   123 	 "GstElementName for use to mux file", NULL},
   124 
   125 	{"output-uri", 'o', 0, G_OPTION_ARG_STRING, &output_uri,
   126 	 "Uri to output", 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 	{
   146 	  g_print("You need to specify output-uri.\nTry --help "
   147 			  "for more information.\n");
   148 	  return 1;
   149 	}
   150 
   151   if (input_file == NULL)
   152 	{
   153 	  g_print("You need to specify input file\nTry --help "
   154 			  "for more information.\n");
   155 	}
   156 
   157   coder = g_mencoder_new();
   158   ch = g_io_channel_unix_new(0);
   159 
   160   aopts = g_strsplit(audio_opts, ",", 0);
   161   vopts = g_strsplit(video_opts, ",", 0);
   162 
   163   g_mencoder_setup_stream(coder, mux_name,
   164 						  video_encode, vopts, video_fps,
   165 						  video_rate, video_width, video_height,
   166 						  audio_encode, aopts, audio_rate, output_uri);
   167 
   168   files = g_strsplit(input_file, ",", 0);
   169   for (i = 0; i < g_strv_length(files); i++)
   170 	{
   171 	  if (!g_mencoder_append_uri(coder, files[i]))
   172 		{
   173 		  g_debug("Invalid uri: %s", files[i]);
   174 		}
   175 	}
   176 
   177   g_strfreev(files);
   178   g_strfreev(aopts);
   179   g_strfreev(vopts);
   180 
   181 
   182   g_io_add_watch(ch, G_IO_IN, _io_channel_cb, coder);
   183 
   184   g_signal_connect(G_OBJECT(coder),
   185 				   "eos", G_CALLBACK(_mencoder_eos_cb), mainloop);
   186 
   187   g_signal_connect(G_OBJECT(coder),
   188 				   "error", G_CALLBACK(_mencoder_error_cb), mainloop);
   189 
   190   g_mencoder_play_stream(coder);
   191 
   192   g_debug("RUNNING..");
   193   g_main_loop_run(mainloop);
   194   g_debug("DONE");
   195   g_object_unref(coder);
   196 
   197   return 0;
   198 }