gmyth-stream/gmencoder/src/main.c
branchtrunk
changeset 847 3c18c36245e7
parent 789 f9cd59844f78
child 898 4fbbacbbca34
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth-stream/gmencoder/src/main.c	Tue Sep 18 13:45:36 2007 +0100
     1.3 @@ -0,0 +1,207 @@
     1.4 +#include <sys/stat.h>
     1.5 +#include <fcntl.h>
     1.6 +#include <unistd.h>
     1.7 +#include <string.h>
     1.8 +
     1.9 +#include <gst/gst.h>
    1.10 +#include <glib.h>
    1.11 +#include <libgnomevfs/gnome-vfs.h>
    1.12 +
    1.13 +#include "gmencoder.h"
    1.14 +
    1.15 +#define FILE_OUT  1
    1.16 +
    1.17 +static GMainLoop *mainloop = NULL;
    1.18 +/*
    1.19 + * Options 
    1.20 + */
    1.21 +static gchar   *input_file = NULL;
    1.22 +static gchar   *video_encode = NULL;
    1.23 +static gchar   *video_opts = NULL;
    1.24 +static gdouble  video_fps = 0.0;
    1.25 +static gint     video_rate = 0;
    1.26 +static gint     video_width = 0;
    1.27 +static gint     video_height = 0;
    1.28 +static gchar   *audio_encode = NULL;
    1.29 +static gchar   *audio_opts = NULL;
    1.30 +static double   audio_rate = 0.0;
    1.31 +static gchar   *mux_name = NULL;
    1.32 +static gchar   *output_uri = NULL;
    1.33 +static gboolean chunked = FALSE;
    1.34 +static gboolean deinterlace = FALSE;
    1.35 +
    1.36 +
    1.37 +
    1.38 +static gboolean
    1.39 +_quit(gpointer data)
    1.40 +{
    1.41 +    //g_object_unref(data);
    1.42 +    g_main_loop_quit(mainloop);
    1.43 +    return FALSE;
    1.44 +}
    1.45 +
    1.46 +static void
    1.47 +_mencoder_eos_cb(GMencoder * mencoder, gpointer data)
    1.48 +{
    1.49 +    g_print("PROGRESS: 100\n");
    1.50 +    g_idle_add(_quit, mencoder);
    1.51 +}
    1.52 +
    1.53 +
    1.54 +static void
    1.55 +_mencoder_error_cb(GMencoder * mencoder, const gchar * msg, gpointer data)
    1.56 +{
    1.57 +    g_print("Error: %s\n", msg);
    1.58 +    g_idle_add(_quit, mencoder);
    1.59 +}
    1.60 +
    1.61 +static          gboolean
    1.62 +_io_channel_cb(GIOChannel * ch, GIOCondition condition, gpointer data)
    1.63 +{
    1.64 +    GString        *cmd = g_string_new("");
    1.65 +    g_io_channel_read_line_string(ch, cmd, NULL, NULL);
    1.66 +
    1.67 +    if (strcmp(cmd->str, "PLAY\n") == 0) {
    1.68 +        g_mencoder_play_stream(G_MENCODER(data));
    1.69 +    } else if (strcmp(cmd->str, "PAUSE\n") == 0) {
    1.70 +        g_mencoder_pause_stream(G_MENCODER(data));
    1.71 +    } else if (strcmp(cmd->str, "STOP\n") == 0) {
    1.72 +        g_mencoder_close_stream(G_MENCODER(data));
    1.73 +    } else if (strcmp(cmd->str, "QUIT\n") == 0) {
    1.74 +        g_mencoder_close_stream(G_MENCODER(data));
    1.75 +        g_main_loop_quit(mainloop);
    1.76 +    }
    1.77 +    g_string_free(cmd, TRUE);
    1.78 +    return TRUE;
    1.79 +}
    1.80 +
    1.81 +int
    1.82 +main(int argc, char **argv)
    1.83 +{
    1.84 +    GMencoder      *coder = NULL;
    1.85 +    GIOChannel     *ch;
    1.86 +    gchar         **vopts;
    1.87 +    gchar         **aopts;
    1.88 +    gchar         **files;
    1.89 +    gint            i;
    1.90 +    gboolean      ret;
    1.91 +
    1.92 +    GOptionContext *context;
    1.93 +    static const GOptionEntry options[] = {
    1.94 +        {"input-files", 'i', 0, G_OPTION_ARG_STRING, &input_file,
    1.95 +         "Input File", NULL},
    1.96 +
    1.97 +        {"video-encode", 0, 0, G_OPTION_ARG_STRING, &video_encode,
    1.98 +         "GstElementName for used to video encode", NULL},
    1.99 +
   1.100 +        {"video-opts", 0, 0, G_OPTION_ARG_STRING, &video_opts,
   1.101 +         "Properties to set on video element", NULL},
   1.102 +
   1.103 +        {"video-fps", 0, 0, G_OPTION_ARG_DOUBLE, &video_fps,
   1.104 +         "Video FPS", NULL},
   1.105 +
   1.106 +        {"video-rate", 0, 0, G_OPTION_ARG_INT, &video_rate,
   1.107 +         "Video rate", NULL},
   1.108 +
   1.109 +        {"video-width", 0, 0, G_OPTION_ARG_INT, &video_width,
   1.110 +         "Video width", NULL},
   1.111 +
   1.112 +        {"video-height", 0, 0, G_OPTION_ARG_INT, &video_height,
   1.113 +         "Video height", NULL},
   1.114 +
   1.115 +        {"audio-encode", 0, 0, G_OPTION_ARG_STRING, &audio_encode,
   1.116 +         "GstElementName for use to audio encode", NULL},
   1.117 +
   1.118 +        {"audio-opts", 0, 0, G_OPTION_ARG_STRING, &audio_opts,
   1.119 +         "Properties to set on audio element", NULL},
   1.120 +
   1.121 +        {"audio-rate", 0, 0, G_OPTION_ARG_INT, &audio_rate,
   1.122 +         "Audio rate", NULL},
   1.123 +
   1.124 +        {"mux-element", 0, 0, G_OPTION_ARG_STRING, &mux_name,
   1.125 +         "GstElementName for use to mux file", NULL},
   1.126 +
   1.127 +        {"output-uri", 'o', 0, G_OPTION_ARG_STRING, &output_uri,
   1.128 +         "Uri to output", NULL},
   1.129 +
   1.130 +	{"chunked", 'c', 0, G_OPTION_ARG_NONE, &chunked, 
   1.131 +         "Send package chunked", NULL},
   1.132 +
   1.133 +	{"deinterlace", 'd', 0, G_OPTION_ARG_NONE, &deinterlace, 
   1.134 +         "Use to deinterlace videos", NULL},
   1.135 +
   1.136 +
   1.137 +        {NULL}
   1.138 +    };
   1.139 +
   1.140 +    g_type_init();
   1.141 +    g_thread_init(NULL);
   1.142 +    gnome_vfs_init ();
   1.143 +    mainloop = g_main_loop_new(NULL, FALSE);
   1.144 +
   1.145 +    g_set_prgname("gmemcoder");
   1.146 +    context = g_option_context_new(NULL);
   1.147 +    g_option_context_set_help_enabled(context, TRUE);
   1.148 +    g_option_context_add_main_entries(context, options, NULL);
   1.149 +    g_option_context_add_group(context, gst_init_get_option_group());
   1.150 +    g_option_context_parse(context, &argc, &argv, NULL);
   1.151 +
   1.152 +    gst_init(&argc, &argv);
   1.153 +
   1.154 +    if (output_uri == NULL) {
   1.155 +        g_print("You need to specify output-uri.\nTry --help "
   1.156 +                "for more information.\n");
   1.157 +        return 1;
   1.158 +    }
   1.159 +
   1.160 +    if (input_file == NULL) {
   1.161 +        g_print("You need to specify input file\nTry --help "
   1.162 +                "for more information.\n");
   1.163 +    }
   1.164 +
   1.165 +    coder = g_mencoder_new();
   1.166 +    ch = g_io_channel_unix_new(0);
   1.167 +
   1.168 +    if (audio_opts != NULL)
   1.169 +        aopts = g_strsplit(audio_opts, ",", 0);
   1.170 +    else
   1.171 +        aopts = NULL;
   1.172 +
   1.173 +    if (video_opts != NULL)
   1.174 +        vopts = g_strsplit(video_opts, ",", 0);
   1.175 +    else
   1.176 +        vopts = NULL;
   1.177 +
   1.178 +    ret = g_mencoder_setup_stream(coder, chunked, deinterlace, mux_name,
   1.179 +                                  video_encode, vopts, video_fps,
   1.180 +                                  video_rate, video_width, video_height,
   1.181 +                                  audio_encode, aopts, audio_rate, output_uri);
   1.182 +
   1.183 +    if (ret == TRUE) {
   1.184 +        files = g_strsplit(input_file, ",", 0);
   1.185 +        for (i = 0; i < g_strv_length(files); i++) {
   1.186 +            if (!g_mencoder_append_uri(coder, files[i])) {
   1.187 +                g_debug("Invalid uri: %s", files[i]);
   1.188 +            }
   1.189 +        }
   1.190 +        g_strfreev(files);
   1.191 +    }
   1.192 +
   1.193 +    g_strfreev(aopts);
   1.194 +    g_strfreev(vopts);
   1.195 +
   1.196 +    if (ret == TRUE) {
   1.197 +        g_io_add_watch(ch, G_IO_IN, _io_channel_cb, coder);
   1.198 +        g_signal_connect(G_OBJECT(coder),
   1.199 +                         "eos", G_CALLBACK(_mencoder_eos_cb), mainloop);
   1.200 +
   1.201 +        g_signal_connect(G_OBJECT(coder),
   1.202 +                         "error", G_CALLBACK(_mencoder_error_cb), mainloop);
   1.203 +
   1.204 +        g_mencoder_play_stream(coder);
   1.205 +        g_main_loop_run(mainloop);
   1.206 +    }
   1.207 +
   1.208 +    g_object_unref(coder);
   1.209 +    return 0;
   1.210 +}