gmyth-stream/gmemcoder/src/main.c
author morphbr
Tue May 29 21:24:48 2007 +0100 (2007-05-29)
branchtrunk
changeset 718 3fbcd3d9b2d1
parent 678 89bdaa9e72bf
child 748 bf9dac4bdc5d
permissions -rw-r--r--
[svn r724] * GMyth-Streamer version 0.3 released
- Improved Log architecture;
- Creation of a history for the transcoder's actions
- Creation of an id for each transcoder instanciated
- Also wrapps default actions for python's default logger

- Created new functions to make use of this new Log architecture;
- serve_new_id
- serve_get_log
- serve_get_all_log

- _Lot_ of small bug fixes;

- Inserted header for all files;

- Splited files with too many lines (more than 1 class per file)
in more files;
     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,
    55                 GIOCondition condition,
    56                 gpointer data)
    57 {
    58     GString *cmd = g_string_new ("");
    59     g_io_channel_read_line_string (ch, cmd, NULL, NULL);
    60 
    61     if (strcmp (cmd->str, "PLAY\n") == 0) {
    62         g_mencoder_play_stream (G_MENCODER (data));
    63     } else if (strcmp (cmd->str, "PAUSE\n") == 0) {
    64         g_mencoder_pause_stream (G_MENCODER (data));
    65     } else if (strcmp (cmd->str, "STOP\n") == 0) {
    66         g_mencoder_close_stream (G_MENCODER (data));
    67     } else if (strcmp (cmd->str, "QUIT\n") == 0) {
    68         g_mencoder_close_stream (G_MENCODER (data));
    69         g_main_loop_quit (mainloop);
    70     }
    71     g_string_free (cmd, TRUE);
    72     return TRUE;
    73 }
    74 
    75 int 
    76 main (int argc, char** argv)
    77 {
    78     GMencoder *coder = NULL;
    79     GIOChannel *ch;
    80     gchar **vopts;
    81     gchar **aopts;
    82     gchar **files;
    83     gint i;
    84 
    85     GOptionContext *context;
    86     static const GOptionEntry options []  = {
    87         {"input-files",    'i', 0, G_OPTION_ARG_STRING,          &input_file,    "Input File", NULL},
    88         {"video-encode",    0, 0, G_OPTION_ARG_STRING,          &video_encode,  "GstElementName for used to video encode", NULL},
    89         {"video-opts",      0, 0, G_OPTION_ARG_STRING,          &video_opts,    "Properties to set on video element", NULL},
    90         {"video-fps",       0, 0, G_OPTION_ARG_DOUBLE,          &video_fps,     "Video FPS", NULL},
    91         {"video-rate",      0, 0, G_OPTION_ARG_INT,             &video_rate,    "Video rate", NULL},
    92         {"video-width",     0, 0, G_OPTION_ARG_INT,             &video_width,   "Video width", NULL},
    93         {"video-height",    0, 0, G_OPTION_ARG_INT,             &video_height,  "Video height", NULL},
    94         {"audio-encode",    0, 0, G_OPTION_ARG_STRING,          &audio_encode,  "GstElementName for use to audio encode", NULL},
    95         {"audio-opts",      0, 0, G_OPTION_ARG_STRING,          &audio_opts,    "Properties to set on audio element", NULL},
    96         {"audio-rate",      0, 0, G_OPTION_ARG_INT,             &audio_rate,    "Audio rate", NULL},
    97         {"mux-element",     0, 0, G_OPTION_ARG_STRING,          &mux_name,      "GstElementName for use to mux file", NULL},
    98         {"output-uri",    'o', 0, G_OPTION_ARG_STRING,          &output_uri,    "Uri to output", NULL},
    99         { NULL }
   100     };
   101 
   102 	g_type_init ();
   103     g_thread_init (NULL);
   104 	mainloop = g_main_loop_new (NULL, FALSE);
   105 
   106 	g_set_prgname ("gmemcoder");
   107     context = g_option_context_new (NULL);
   108     g_option_context_set_help_enabled (context, TRUE);
   109     g_option_context_add_main_entries (context, options, NULL);
   110     g_option_context_add_group (context, gst_init_get_option_group ());
   111     g_option_context_parse (context, &argc, &argv, NULL);
   112 
   113 	gst_init (&argc, &argv);
   114 
   115     if (output_uri == NULL) {
   116         g_print ("You need specify output-uri.\nTry --help for more information.\n");
   117         return 1; 
   118     }
   119 
   120     if (input_file == NULL) {
   121         g_print ("You need specify input file\nTry --help for more information.\n");
   122     }
   123 
   124     coder = g_mencoder_new ();
   125     ch = g_io_channel_unix_new (0);
   126 
   127     aopts = g_strsplit (audio_opts, ",", 0);
   128     vopts = g_strsplit (video_opts, ",", 0);
   129 
   130     g_mencoder_setup_stream (coder, 
   131             mux_name,
   132             video_encode, vopts, video_fps, video_rate, video_width, video_height,
   133             audio_encode, aopts, audio_rate, 
   134             output_uri);
   135 
   136     files = g_strsplit (input_file, ",", 0);
   137     for (i=0; i < g_strv_length (files); i++) {
   138         if (!g_mencoder_append_uri (coder, files[i])) {
   139             g_debug ("Invalid uri: %s", files[i]);
   140         } 
   141     }
   142                 
   143     g_strfreev (files);
   144     g_strfreev (aopts);
   145     g_strfreev (vopts);
   146 
   147 
   148     g_io_add_watch (ch, G_IO_IN, _io_channel_cb, coder);    
   149 
   150     g_signal_connect (G_OBJECT (coder),
   151                         "eos",
   152                         G_CALLBACK (_mencoder_eos_cb),
   153                         mainloop);
   154 
   155     g_signal_connect (G_OBJECT (coder),
   156                         "error",
   157                         G_CALLBACK (_mencoder_error_cb),
   158                         mainloop);
   159 
   160     g_mencoder_play_stream (coder);
   161 
   162     g_debug ("RUNNING..");
   163 	g_main_loop_run (mainloop);
   164 	g_debug ("DONE");
   165     g_object_unref (coder);
   166 
   167 	return 0;
   168 }