File: banshee-transcoder.c

package info (click to toggle)
banshee 2.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 47,160 kB
  • ctags: 25,019
  • sloc: xml: 163,694; cs: 137,409; sh: 11,650; ansic: 4,790; makefile: 2,928; python: 38
file content (404 lines) | stat: -rw-r--r-- 12,246 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// banshee-transcoder.c
//
// Author:
//   Aaron Bockover <abockover@novell.com>
//
// Copyright (C) 2005-2008 Novell, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#include <gst/gst.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>

typedef struct GstTranscoder GstTranscoder;

typedef void (* GstTranscoderProgressCallback) (GstTranscoder *transcoder, gdouble progress);
typedef void (* GstTranscoderFinishedCallback) (GstTranscoder *transcoder);
typedef void (* GstTranscoderErrorCallback) (GstTranscoder *transcoder, const gchar *error, const gchar *debug);

struct GstTranscoder {
    gboolean is_transcoding;
    guint iterate_timeout_id;
    GstElement *pipeline;
    GstElement *sink_bin;
    gchar *output_uri;
    GstTranscoderProgressCallback progress_cb;
    GstTranscoderFinishedCallback finished_cb;
    GstTranscoderErrorCallback error_cb;
};

// private methods

static void
gst_transcoder_raise_error(GstTranscoder *transcoder, const gchar *error, const gchar *debug)
{
    g_return_if_fail(transcoder != NULL);
    g_return_if_fail(transcoder->error_cb != NULL);
    
    transcoder->error_cb(transcoder, error, debug);
}

static gboolean
gst_transcoder_iterate_timeout(GstTranscoder *transcoder)
{
    gint64 position;
    gint64 duration;

    g_return_val_if_fail(transcoder != NULL, FALSE);

    if(!gst_element_query_duration(transcoder->pipeline, GST_FORMAT_TIME, &duration) ||
        !gst_element_query_position(transcoder->sink_bin, GST_FORMAT_TIME, &position)) {
        return TRUE;
    }

    if(transcoder->progress_cb != NULL) {
        transcoder->progress_cb(transcoder, (double)position / (double)duration);
    }
    
    return TRUE;
}

static void
gst_transcoder_start_iterate_timeout(GstTranscoder *transcoder)
{
    g_return_if_fail(transcoder != NULL);

    if(transcoder->iterate_timeout_id != 0) {
        return;
    }
    
    transcoder->iterate_timeout_id = g_timeout_add(200, 
        (GSourceFunc)gst_transcoder_iterate_timeout, transcoder);
}

static void
gst_transcoder_stop_iterate_timeout(GstTranscoder *transcoder)
{
    g_return_if_fail(transcoder != NULL);
    
    if(transcoder->iterate_timeout_id == 0) {
        return;
    }
    
    g_source_remove(transcoder->iterate_timeout_id);
    transcoder->iterate_timeout_id = 0;
}

static gboolean
gst_transcoder_bus_callback(GstBus *bus, GstMessage *message, gpointer data)
{
    GstTranscoder *transcoder = (GstTranscoder *)data;

    g_return_val_if_fail(transcoder != NULL, FALSE);

    switch(GST_MESSAGE_TYPE(message)) {
        case GST_MESSAGE_ERROR: {
            GError *error;
            gchar *debug;
            
            transcoder->is_transcoding = FALSE;
            gst_transcoder_stop_iterate_timeout(transcoder);
            
            if(transcoder->error_cb != NULL) {
                gst_message_parse_error(message, &error, &debug);
                gst_transcoder_raise_error(transcoder, error->message, debug);
                g_error_free(error);
                g_free(debug);
            }
            
            break;
        }        
        case GST_MESSAGE_EOS:
            gst_element_set_state(GST_ELEMENT(transcoder->pipeline), GST_STATE_NULL);
            g_object_unref(G_OBJECT(transcoder->pipeline));
            transcoder->pipeline = NULL;
            
            transcoder->is_transcoding = FALSE;
            gst_transcoder_stop_iterate_timeout(transcoder);

            /*
             FIXME: Replace with regular stat
             GnomeVFSFileInfo fileinfo;
            if(gnome_vfs_get_file_info(transcoder->output_uri, &fileinfo, 
                GNOME_VFS_FILE_INFO_DEFAULT) == GNOME_VFS_OK) {
                if(fileinfo.size < 100) {
                    gst_transcoder_raise_error(transcoder, 
                        _("No decoder could be found for source format."), NULL);
                    g_remove(transcoder->output_uri);
                    break;
                }
            } else {
                gst_transcoder_raise_error(transcoder, _("Could not stat encoded file"), NULL);
                break;
            }*/
            
            if(transcoder->finished_cb != NULL) {
                transcoder->finished_cb(transcoder);
            }
            break;
        default:
            break;
    }
    
    return TRUE;
}

static GstElement *
gst_transcoder_build_encoder(const gchar *encoder_pipeline)
{
    GstElement *encoder = NULL;
    gchar *pipeline;
    GError *error = NULL;
    
    pipeline = g_strdup_printf("%s", encoder_pipeline); 
    encoder = gst_parse_bin_from_description(pipeline, TRUE, &error);
    g_free(pipeline);
    
    if(error != NULL) {
        return NULL;
    }
    
    return encoder;
}    

static void
gst_transcoder_pad_added(GstElement *decodebin, GstPad *pad, 
    gpointer data)
{
    GstCaps *caps;
    GstStructure *str;
    GstPad *audiopad;
    GstTranscoder *transcoder = (GstTranscoder *)data;

    g_return_if_fail(transcoder != NULL);

    audiopad = gst_element_get_static_pad(transcoder->sink_bin, "sink");
    
    if(GST_PAD_IS_LINKED(audiopad)) {
        g_object_unref(audiopad);
        return;
    }

    caps = gst_pad_query_caps(pad, NULL);
    str = gst_caps_get_structure(caps, 0);
    
    if(!g_strrstr(gst_structure_get_name(str), "audio")) {
        gst_caps_unref(caps);
        gst_object_unref(audiopad);
        return;
    }
   
    gst_caps_unref(caps);
    gst_pad_link(pad, audiopad);
}

static gboolean
gst_transcoder_create_pipeline(GstTranscoder *transcoder, 
    const char *input_uri, const char *output_uri, 
    const gchar *encoder_pipeline)
{
    GstElement *source_elem;
    GstElement *decoder_elem;
    GstElement *encoder_elem;
    GstElement *sink_elem;
    GstElement *conv_elem;
    GstElement *resample_elem;
    GstPad *encoder_pad;

    if(transcoder == NULL) {
        return FALSE;
    }
    
    transcoder->pipeline = gst_pipeline_new("pipeline");

    source_elem = gst_element_make_from_uri(GST_URI_SRC, input_uri, "source", NULL);
    if(source_elem == NULL) {
        gst_transcoder_raise_error(transcoder, _("Could not create source element"), NULL);
        return FALSE;
    }

    decoder_elem = gst_element_factory_make("decodebin", "decodebin");
    if(decoder_elem == NULL) {
        gst_transcoder_raise_error(transcoder, _("Could not create decodebin plugin"), NULL);
        return FALSE;
    }
    
    sink_elem = gst_element_make_from_uri(GST_URI_SINK, output_uri, "sink", NULL);
    if(sink_elem == NULL) {
        gst_transcoder_raise_error(transcoder, _("Could not create sink element"), NULL);
        return FALSE;
    }
    
    transcoder->sink_bin = gst_bin_new("sinkbin");
    if(transcoder->sink_bin == NULL) {
        gst_transcoder_raise_error(transcoder, _("Could not create sinkbin plugin"), NULL);
        return FALSE;
    }
    
    conv_elem = gst_element_factory_make("audioconvert", "audioconvert");
    if(conv_elem == NULL) {
        gst_transcoder_raise_error(transcoder, _("Could not create audioconvert plugin"), NULL);
        return FALSE;
    }
    
    resample_elem = gst_element_factory_make("audioresample", "audioresample");
    if(resample_elem == NULL) {
        gst_transcoder_raise_error(transcoder, _("Could not create audioresample plugin"), NULL);
        return FALSE;
    }

    encoder_elem = gst_transcoder_build_encoder(encoder_pipeline);
    if(encoder_elem == NULL) {
         gst_transcoder_raise_error(transcoder, _("Could not create encoding pipeline"), encoder_pipeline);
         return FALSE;
    }

    encoder_pad = gst_element_get_static_pad(conv_elem, "sink");
    if(encoder_pad == NULL) {
        gst_transcoder_raise_error(transcoder, _("Could not get sink pad from encoder"), NULL);
        return FALSE;
    }
    
    gst_bin_add_many(GST_BIN(transcoder->sink_bin), conv_elem, resample_elem, encoder_elem, sink_elem, NULL);
    gst_element_link_many(conv_elem, resample_elem, encoder_elem, sink_elem, NULL);
    
    gst_element_add_pad(transcoder->sink_bin, gst_ghost_pad_new("sink", encoder_pad));
    gst_object_unref(encoder_pad);
    
    gst_bin_add_many(GST_BIN(transcoder->pipeline), source_elem, decoder_elem, 
        transcoder->sink_bin, NULL);
        
    gst_element_link(source_elem, decoder_elem);

    g_signal_connect(decoder_elem, "pad-added", 
        G_CALLBACK(gst_transcoder_pad_added), transcoder);

    gst_bus_add_watch(gst_pipeline_get_bus(GST_PIPELINE(transcoder->pipeline)), 
        gst_transcoder_bus_callback, transcoder);
    
    return TRUE;
}

// public methods

GstTranscoder *
gst_transcoder_new ()
{
    return g_new0 (GstTranscoder, 1);
}

void
gst_transcoder_free(GstTranscoder *transcoder)
{
    g_return_if_fail(transcoder != NULL);
    gst_transcoder_stop_iterate_timeout(transcoder);
    
    if(GST_IS_ELEMENT(transcoder->pipeline)) {
        gst_element_set_state(GST_ELEMENT(transcoder->pipeline), GST_STATE_NULL);
        gst_object_unref(GST_OBJECT(transcoder->pipeline));
    }

    if(transcoder->output_uri != NULL) {
        g_free(transcoder->output_uri);
        transcoder->output_uri = NULL;
    }
    
    g_free(transcoder);
    transcoder = NULL;
}

void 
gst_transcoder_transcode(GstTranscoder *transcoder, const gchar *input_uri, 
    const gchar *output_uri, const gchar *encoder_pipeline)
{
    g_return_if_fail(transcoder != NULL);
    
    if(transcoder->is_transcoding) {
        return;
    }
    
    if(!gst_transcoder_create_pipeline(transcoder, input_uri, output_uri, encoder_pipeline)) {
        gst_transcoder_raise_error(transcoder, _("Could not construct pipeline"), NULL); 
        return;
    }
    
    if(transcoder->output_uri != NULL) {
        g_free(transcoder->output_uri);
    }
    
    transcoder->output_uri = g_strdup(output_uri);
    transcoder->is_transcoding = TRUE;
    
    gst_element_set_state(GST_ELEMENT(transcoder->pipeline), GST_STATE_PLAYING);
    gst_transcoder_start_iterate_timeout(transcoder);
}

void 
gst_transcoder_cancel(GstTranscoder *transcoder)
{
    g_return_if_fail(transcoder != NULL);
    gst_transcoder_stop_iterate_timeout(transcoder);
    
    transcoder->is_transcoding = FALSE;
    
    if(GST_IS_ELEMENT(transcoder->pipeline)) {
        gst_element_set_state(GST_ELEMENT(transcoder->pipeline), GST_STATE_NULL);
        gst_object_unref(GST_OBJECT(transcoder->pipeline));
    }
    
    g_remove(transcoder->output_uri);
}

void
gst_transcoder_set_progress_callback(GstTranscoder *transcoder, 
    GstTranscoderProgressCallback cb)
{
    g_return_if_fail(transcoder != NULL);
    transcoder->progress_cb = cb;
}

void
gst_transcoder_set_finished_callback(GstTranscoder *transcoder, 
    GstTranscoderFinishedCallback cb)
{
    g_return_if_fail(transcoder != NULL);
    transcoder->finished_cb = cb;
}

void
gst_transcoder_set_error_callback(GstTranscoder *transcoder, 
    GstTranscoderErrorCallback cb)
{
    g_return_if_fail(transcoder != NULL);
    transcoder->error_cb = cb;
}

gboolean
gst_transcoder_get_is_transcoding(GstTranscoder *transcoder)
{
    g_return_val_if_fail(transcoder != NULL, FALSE);
    return transcoder->is_transcoding;
}