gmyth-stream/gmencoder/src/main.c
author renatofilho
Wed Jan 30 03:36:13 2008 +0000 (2008-01-30)
branchtrunk
changeset 898 4fbbacbbca34
parent 847 3c18c36245e7
child 931 e8e3219edf5f
permissions -rw-r--r--
[svn r904] updated COPYING file
     1 /* 
     2  *  arch-tag: GMencoder main file
     3  *
     4  *  Copyright (C) 2007 INdT - Renato Filho <renato.filho@indt.org>
     5  *
     6  *  This program is free software; you can redistribute it and/or modify
     7  *  it under the terms of the GNU General Public License as published by
     8  *  the Free Software Foundation; either version 2 of the License, or
     9  *  (at your option) any later version.
    10  *
    11  *  This program is distributed in the hope that it will be useful,
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  *  GNU General Public License for more details.
    15  *
    16  *  You should have received a copy of the GNU General Public License
    17  *  along with this program; if not, write to the Free Software
    18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
    19  *
    20  */
    21 
    22 #include <sys/stat.h>
    23 #include <fcntl.h>
    24 #include <unistd.h>
    25 #include <string.h>
    26 
    27 #include <gst/gst.h>
    28 #include <glib.h>
    29 #include <libgnomevfs/gnome-vfs.h>
    30 
    31 #include "gmencoder.h"
    32 
    33 #define FILE_OUT  1
    34 
    35 static GMainLoop *mainloop = NULL;
    36 /*
    37  * Options 
    38  */
    39 static gchar   *input_file = NULL;
    40 static gchar   *video_encode = NULL;
    41 static gchar   *video_opts = NULL;
    42 static gdouble  video_fps = 0.0;
    43 static gint     video_rate = 0;
    44 static gint     video_width = 0;
    45 static gint     video_height = 0;
    46 static gchar   *audio_encode = NULL;
    47 static gchar   *audio_opts = NULL;
    48 static double   audio_rate = 0.0;
    49 static gchar   *mux_name = NULL;
    50 static gchar   *output_uri = NULL;
    51 static gboolean chunked = FALSE;
    52 static gboolean deinterlace = FALSE;
    53 
    54 
    55 
    56 static gboolean
    57 _quit(gpointer data)
    58 {
    59     //g_object_unref(data);
    60     g_main_loop_quit(mainloop);
    61     return FALSE;
    62 }
    63 
    64 static void
    65 _mencoder_eos_cb(GMencoder * mencoder, gpointer data)
    66 {
    67     g_print("PROGRESS: 100\n");
    68     g_idle_add(_quit, mencoder);
    69 }
    70 
    71 
    72 static void
    73 _mencoder_error_cb(GMencoder * mencoder, const gchar * msg, gpointer data)
    74 {
    75     g_print("Error: %s\n", msg);
    76     g_idle_add(_quit, mencoder);
    77 }
    78 
    79 static          gboolean
    80 _io_channel_cb(GIOChannel * ch, GIOCondition condition, gpointer data)
    81 {
    82     GString        *cmd = g_string_new("");
    83     g_io_channel_read_line_string(ch, cmd, NULL, NULL);
    84 
    85     if (strcmp(cmd->str, "PLAY\n") == 0) {
    86         g_mencoder_play_stream(G_MENCODER(data));
    87     } else if (strcmp(cmd->str, "PAUSE\n") == 0) {
    88         g_mencoder_pause_stream(G_MENCODER(data));
    89     } else if (strcmp(cmd->str, "STOP\n") == 0) {
    90         g_mencoder_close_stream(G_MENCODER(data));
    91     } else if (strcmp(cmd->str, "QUIT\n") == 0) {
    92         g_mencoder_close_stream(G_MENCODER(data));
    93         g_main_loop_quit(mainloop);
    94     }
    95     g_string_free(cmd, TRUE);
    96     return TRUE;
    97 }
    98 
    99 int
   100 main(int argc, char **argv)
   101 {
   102     GMencoder      *coder = NULL;
   103     GIOChannel     *ch;
   104     gchar         **vopts;
   105     gchar         **aopts;
   106     gchar         **files;
   107     gint            i;
   108     gboolean      ret;
   109 
   110     GOptionContext *context;
   111     static const GOptionEntry options[] = {
   112         {"input-files", 'i', 0, G_OPTION_ARG_STRING, &input_file,
   113          "Input File", NULL},
   114 
   115         {"video-encode", 0, 0, G_OPTION_ARG_STRING, &video_encode,
   116          "GstElementName for used to video encode", NULL},
   117 
   118         {"video-opts", 0, 0, G_OPTION_ARG_STRING, &video_opts,
   119          "Properties to set on video element", NULL},
   120 
   121         {"video-fps", 0, 0, G_OPTION_ARG_DOUBLE, &video_fps,
   122          "Video FPS", NULL},
   123 
   124         {"video-rate", 0, 0, G_OPTION_ARG_INT, &video_rate,
   125          "Video rate", NULL},
   126 
   127         {"video-width", 0, 0, G_OPTION_ARG_INT, &video_width,
   128          "Video width", NULL},
   129 
   130         {"video-height", 0, 0, G_OPTION_ARG_INT, &video_height,
   131          "Video height", NULL},
   132 
   133         {"audio-encode", 0, 0, G_OPTION_ARG_STRING, &audio_encode,
   134          "GstElementName for use to audio encode", NULL},
   135 
   136         {"audio-opts", 0, 0, G_OPTION_ARG_STRING, &audio_opts,
   137          "Properties to set on audio element", NULL},
   138 
   139         {"audio-rate", 0, 0, G_OPTION_ARG_INT, &audio_rate,
   140          "Audio rate", NULL},
   141 
   142         {"mux-element", 0, 0, G_OPTION_ARG_STRING, &mux_name,
   143          "GstElementName for use to mux file", NULL},
   144 
   145         {"output-uri", 'o', 0, G_OPTION_ARG_STRING, &output_uri,
   146          "Uri to output", NULL},
   147 
   148 	{"chunked", 'c', 0, G_OPTION_ARG_NONE, &chunked, 
   149          "Send package chunked", NULL},
   150 
   151 	{"deinterlace", 'd', 0, G_OPTION_ARG_NONE, &deinterlace, 
   152          "Use to deinterlace videos", NULL},
   153 
   154 
   155         {NULL}
   156     };
   157 
   158     g_type_init();
   159     g_thread_init(NULL);
   160     gnome_vfs_init ();
   161     mainloop = g_main_loop_new(NULL, FALSE);
   162 
   163     g_set_prgname("gmemcoder");
   164     context = g_option_context_new(NULL);
   165     g_option_context_set_help_enabled(context, TRUE);
   166     g_option_context_add_main_entries(context, options, NULL);
   167     g_option_context_add_group(context, gst_init_get_option_group());
   168     g_option_context_parse(context, &argc, &argv, NULL);
   169 
   170     gst_init(&argc, &argv);
   171 
   172     if (output_uri == NULL) {
   173         g_print("You need to specify output-uri.\nTry --help "
   174                 "for more information.\n");
   175         return 1;
   176     }
   177 
   178     if (input_file == NULL) {
   179         g_print("You need to specify input file\nTry --help "
   180                 "for more information.\n");
   181     }
   182 
   183     coder = g_mencoder_new();
   184     ch = g_io_channel_unix_new(0);
   185 
   186     if (audio_opts != NULL)
   187         aopts = g_strsplit(audio_opts, ",", 0);
   188     else
   189         aopts = NULL;
   190 
   191     if (video_opts != NULL)
   192         vopts = g_strsplit(video_opts, ",", 0);
   193     else
   194         vopts = NULL;
   195 
   196     ret = g_mencoder_setup_stream(coder, chunked, deinterlace, mux_name,
   197                                   video_encode, vopts, video_fps,
   198                                   video_rate, video_width, video_height,
   199                                   audio_encode, aopts, audio_rate, output_uri);
   200 
   201     if (ret == TRUE) {
   202         files = g_strsplit(input_file, ",", 0);
   203         for (i = 0; i < g_strv_length(files); i++) {
   204             if (!g_mencoder_append_uri(coder, files[i])) {
   205                 g_debug("Invalid uri: %s", files[i]);
   206             }
   207         }
   208         g_strfreev(files);
   209     }
   210 
   211     g_strfreev(aopts);
   212     g_strfreev(vopts);
   213 
   214     if (ret == TRUE) {
   215         g_io_add_watch(ch, G_IO_IN, _io_channel_cb, coder);
   216         g_signal_connect(G_OBJECT(coder),
   217                          "eos", G_CALLBACK(_mencoder_eos_cb), mainloop);
   218 
   219         g_signal_connect(G_OBJECT(coder),
   220                          "error", G_CALLBACK(_mencoder_error_cb), mainloop);
   221 
   222         g_mencoder_play_stream(coder);
   223         g_main_loop_run(mainloop);
   224     }
   225 
   226     g_object_unref(coder);
   227     return 0;
   228 }