gmyth-stream/gmemcoder/src/main.c
author melunko
Tue Apr 24 19:45:27 2007 +0100 (2007-04-24)
branchtrunk
changeset 592 0f77fcb97269
parent 586 bd0ad44171e7
child 600 7da2a5e32fa6
permissions -rw-r--r--
[svn r598] changed ScheduleInfo attribute from record_id to schedule_id
     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*   output_element  = NULL;
    26 static gchar*   output_opts     = NULL;
    27 
    28 static void
    29 _mencoder_ready_cb (GMencoder *mencoder, gpointer data)
    30 {
    31     g_mencoder_play_stream (mencoder);
    32 }
    33 
    34 static void
    35 _mencoder_eos_cb (GMencoder *mencoder, gpointer data)
    36 {
    37     g_print ("EOS\n");
    38     g_main_loop_quit ((GMainLoop *) data);
    39 }
    40 
    41 static void
    42 _mencoder_error_cb (GMencoder *mencoder, const gchar* msg, gpointer data)
    43 {
    44     g_print ("Error: %s\n", msg);
    45     g_main_loop_quit ((GMainLoop *) data);
    46 }
    47 
    48 static gboolean
    49 _io_channel_cb (GIOChannel *ch,
    50                 GIOCondition condition,
    51                 gpointer data)
    52 {
    53     GString *cmd = g_string_new ("");
    54     g_io_channel_read_line_string (ch, cmd, NULL, NULL);
    55 
    56     if (strcmp (cmd->str, "PLAY\n") == 0) {
    57         g_mencoder_play_stream (G_MENCODER (data));
    58     } else if (strcmp (cmd->str, "PAUSE\n") == 0) {
    59         g_mencoder_pause_stream (G_MENCODER (data));
    60     } else if (strcmp (cmd->str, "STOP\n") == 0) {
    61         g_mencoder_close_stream (G_MENCODER (data));
    62     } else if (strcmp (cmd->str, "QUIT\n") == 0) {
    63         g_mencoder_close_stream (G_MENCODER (data));
    64         g_main_loop_quit (mainloop);
    65     }
    66     g_string_free (cmd, TRUE);
    67     return TRUE;
    68 }
    69 
    70 int 
    71 main (int argc, char** argv)
    72 {
    73     GMencoder *coder;
    74     GIOChannel *ch;
    75     gchar **oopts;
    76     gchar **vopts;
    77     gchar **aopts;
    78 
    79     GOptionContext *context;
    80     static const GOptionEntry options []  = {
    81         {"input-file",    'i', 0, G_OPTION_ARG_STRING,          &input_file,    "Input File", NULL},
    82         {"video-encode",    0, 0, G_OPTION_ARG_STRING,          &video_encode,  "GstElementName for used to video encode", NULL},
    83         {"video-opts",      0, 0, G_OPTION_ARG_STRING,          &video_opts,    "Properties to set on video element", NULL},
    84         {"video-fps",       0, 0, G_OPTION_ARG_DOUBLE,          &video_fps,     "Video FPS", NULL},
    85         {"video-rate",      0, 0, G_OPTION_ARG_INT,             &video_rate,    "Video rate", NULL},
    86         {"video-width",     0, 0, G_OPTION_ARG_INT,             &video_width,   "Video width", NULL},
    87         {"video-height",    0, 0, G_OPTION_ARG_INT,             &video_height,  "Video height", NULL},
    88         {"audio-encode",    0, 0, G_OPTION_ARG_STRING,          &audio_encode,  "GstElementName for use to audio encode", NULL},
    89         {"audio-opts",      0, 0, G_OPTION_ARG_STRING,          &audio_opts,    "Properties to set on audio element", NULL},
    90         {"audio-rate",      0, 0, G_OPTION_ARG_INT,             &audio_rate,    "Audio rate", NULL},
    91         {"output-element",  0, 0, G_OPTION_ARG_STRING,          &output_element,"GstElementName for use to output", NULL},
    92         {"output-opts",     0, 0, G_OPTION_ARG_STRING,          &output_opts,   "Properties to set on output element", NULL},
    93         { NULL }
    94     };
    95 
    96 	g_type_init ();
    97     g_thread_init (NULL);
    98 
    99 	//g_set_prgname ("gmemcoder");
   100     context = g_option_context_new (NULL);
   101     g_option_context_set_help_enabled (context, TRUE);
   102     g_option_context_add_main_entries (context, options, NULL);
   103     g_option_context_add_group (context, gst_init_get_option_group ());
   104     g_option_context_parse (context, &argc, &argv, NULL);
   105 
   106 	gst_init (&argc, &argv);
   107 
   108     if (output_element == NULL) {
   109         g_print ("You need specify output-element name.\nTry --help for more information.\n");
   110         return 1; 
   111     }
   112 
   113     if (input_file == NULL) {
   114         g_print ("You need specify input file\nTry --help for more information.\n");
   115     }
   116 
   117     coder = g_mencoder_new ();
   118     ch = g_io_channel_unix_new (0);
   119 
   120     aopts = g_strsplit (audio_opts, ",", 0);
   121     vopts = g_strsplit (video_opts, ",", 0);
   122     oopts = g_strsplit (output_opts, ",", 0);
   123 
   124     g_mencoder_setup_stream (coder, 
   125             input_file, 
   126             video_encode, vopts, video_fps, video_rate, video_width, video_height,
   127             audio_encode, aopts, audio_rate, 
   128             output_element, oopts);
   129 
   130     g_strfreev (aopts);
   131     g_strfreev (vopts);
   132     g_strfreev (oopts);
   133 
   134 	mainloop = g_main_loop_new (NULL, FALSE);
   135     g_io_add_watch (ch, G_IO_IN, _io_channel_cb, coder);    
   136 
   137 
   138     g_signal_connect (G_OBJECT (coder),
   139                         "ready",
   140                         G_CALLBACK (_mencoder_ready_cb),
   141                         NULL);
   142 
   143     g_signal_connect (G_OBJECT (coder),
   144                         "eos",
   145                         G_CALLBACK (_mencoder_eos_cb),
   146                         mainloop);
   147 
   148     g_signal_connect (G_OBJECT (coder),
   149                         "error",
   150                         G_CALLBACK (_mencoder_error_cb),
   151                         mainloop);
   152 
   153 	g_main_loop_run (mainloop);
   154 
   155 	return 0;
   156 }