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