~ubuntu-branches/ubuntu/raring/brasero/raring-proposed

« back to all changes in this revision

Viewing changes to .pc/99_fix-mp3-projects.patch/libbrasero-utils/brasero-metadata.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-01-24 23:07:10 UTC
  • Revision ID: package-import@ubuntu.com-20130124230710-ik50o2gfst5s6qa1
Tags: 3.6.1-0ubuntu2
* 99_fix-mp3-projects.patch:
  - Bugzilla patch to fix "not suitable for audio or video media"
    error when trying to create a mp3 audio project (LP: #1072151)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
 * Libbrasero-misc
 
4
 * Copyright (C) Philippe Rouquier 2005-2009 <bonfire-app@wanadoo.fr>
 
5
 *
 
6
 * Libbrasero-misc 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
 * The Libbrasero-misc authors hereby grant permission for non-GPL compatible
 
12
 * GStreamer plugins to be used and distributed together with GStreamer
 
13
 * and Libbrasero-misc. This permission is above and beyond the permissions granted
 
14
 * by the GPL license by which Libbrasero-burn is covered. If you modify this code
 
15
 * you may extend this exception to your version of the code, but you are not
 
16
 * obligated to do so. If you do not wish to do so, delete this exception
 
17
 * statement from your version.
 
18
 * 
 
19
 * Libbrasero-misc is distributed in the hope that it will be useful,
 
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 * GNU Library General Public License for more details.
 
23
 * 
 
24
 * You should have received a copy of the GNU General Public License
 
25
 * along with this program; if not, write to:
 
26
 *      The Free Software Foundation, Inc.,
 
27
 *      51 Franklin Street, Fifth Floor
 
28
 *      Boston, MA  02110-1301, USA.
 
29
 */
 
30
 
 
31
#ifdef HAVE_CONFIG_H
 
32
#  include <config.h>
 
33
#endif
 
34
 
 
35
#include <string.h>
 
36
 
 
37
#include <glib.h>
 
38
#include <glib/gi18n-lib.h>
 
39
#include <glib-object.h>
 
40
 
 
41
#include <gdk-pixbuf/gdk-pixbuf.h>
 
42
 
 
43
#include <gst/gst.h>
 
44
#include <gst/base/gstbasesink.h>
 
45
#include <gst/pbutils/install-plugins.h>
 
46
#include <gst/pbutils/missing-plugins.h>
 
47
#include <gst/tag/tag.h>
 
48
 
 
49
#include "brasero-misc.h"
 
50
#include "brasero-metadata.h"
 
51
 
 
52
#define BRASERO_METADATA_SILENCE_INTERVAL               100000000LL
 
53
#define BRASERO_METADATA_INITIAL_STATE                  GST_STATE_PAUSED
 
54
 
 
55
struct BraseroMetadataPrivate {
 
56
        GstElement *pipeline;
 
57
        GstElement *source;
 
58
        GstElement *decode;
 
59
        GstElement *convert;
 
60
        GstElement *level;
 
61
        GstElement *sink;
 
62
 
 
63
        GstElement *pipeline_mp3;
 
64
 
 
65
        GstElement *audio;
 
66
        GstElement *video;
 
67
 
 
68
        GstElement *snapshot;
 
69
 
 
70
        GError *error;
 
71
        guint watch;
 
72
        guint watch_mp3;
 
73
 
 
74
        BraseroMetadataSilence *silence;
 
75
 
 
76
        BraseroMetadataFlag flags;
 
77
        BraseroMetadataInfo *info;
 
78
 
 
79
        /* This is for automatic missing plugin install */
 
80
        GSList *missing_plugins;
 
81
        GSList *downloads;
 
82
 
 
83
        GMutex *mutex;
 
84
        GSList *conditions;
 
85
 
 
86
        gint listeners;
 
87
 
 
88
        BraseroMetadataGetXidCb xid_callback;
 
89
        gpointer xid_user_data;
 
90
 
 
91
        guint started:1;
 
92
        guint moved_forward:1;
 
93
        guint prev_level_mes:1;
 
94
        guint video_linked:1;
 
95
        guint audio_linked:1;
 
96
        guint snapshot_started:1;
 
97
};
 
98
typedef struct BraseroMetadataPrivate BraseroMetadataPrivate;
 
99
#define BRASERO_METADATA_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), BRASERO_TYPE_METADATA, BraseroMetadataPrivate))
 
100
 
 
101
enum {
 
102
        PROP_NONE,
 
103
        PROP_URI
 
104
};
 
105
 
 
106
typedef enum {
 
107
        COMPLETED_SIGNAL,
 
108
        LAST_SIGNAL
 
109
} BraseroMetadataSignalType;
 
110
 
 
111
static guint brasero_metadata_signals [LAST_SIGNAL] = { 0 };
 
112
 
 
113
#define BRASERO_METADATA_IS_FAST(flags)                                         \
 
114
        (!((flags) & BRASERO_METADATA_FLAG_SILENCES) &&                         \
 
115
        ((flags) & BRASERO_METADATA_FLAG_FAST))
 
116
 
 
117
G_DEFINE_TYPE (BraseroMetadata, brasero_metadata, G_TYPE_OBJECT)
 
118
 
 
119
static GSList *downloading = NULL;
 
120
static GSList *downloaded = NULL;
 
121
 
 
122
static gboolean
 
123
brasero_metadata_completed (BraseroMetadata *self);
 
124
 
 
125
static int
 
126
brasero_metadata_get_xid (BraseroMetadata *metadata)
 
127
{
 
128
        BraseroMetadataPrivate *priv;
 
129
 
 
130
        priv = BRASERO_METADATA_PRIVATE (metadata);
 
131
        if (!priv->xid_callback)
 
132
                return 0;
 
133
 
 
134
        return priv->xid_callback (priv->xid_user_data);
 
135
}
 
136
 
 
137
void
 
138
brasero_metadata_set_get_xid_callback (BraseroMetadata *metadata,
 
139
                                       BraseroMetadataGetXidCb callback,
 
140
                                       gpointer user_data)
 
141
{
 
142
        BraseroMetadataPrivate *priv;
 
143
 
 
144
        priv = BRASERO_METADATA_PRIVATE (metadata);
 
145
        priv->xid_callback = callback;
 
146
        priv->xid_user_data = user_data;
 
147
}
 
148
 
 
149
struct _BraseroMetadataGstDownload {
 
150
        gchar *detail;
 
151
 
 
152
        /* These are all metadata objects waiting */
 
153
        GSList *objects;
 
154
};
 
155
typedef struct _BraseroMetadataGstDownload BraseroMetadataGstDownload;
 
156
 
 
157
void
 
158
brasero_metadata_info_clear (BraseroMetadataInfo *info)
 
159
{
 
160
        if (!info)
 
161
                return;
 
162
 
 
163
        if (info->snapshot) {
 
164
                g_object_unref (info->snapshot);
 
165
                info->snapshot = NULL;
 
166
        }
 
167
 
 
168
        if (info->uri)
 
169
                g_free (info->uri);
 
170
 
 
171
        if (info->type)
 
172
                g_free (info->type);
 
173
 
 
174
        if (info->title)
 
175
                g_free (info->title);
 
176
 
 
177
        if (info->artist)
 
178
                g_free (info->artist);
 
179
 
 
180
        if (info->album)
 
181
                g_free (info->album);
 
182
 
 
183
        if (info->genre)
 
184
                g_free (info->genre);
 
185
 
 
186
        if (info->musicbrainz_id)
 
187
                g_free (info->musicbrainz_id);
 
188
 
 
189
        if (info->silences) {
 
190
                g_slist_foreach (info->silences, (GFunc) g_free, NULL);
 
191
                g_slist_free (info->silences);
 
192
                info->silences = NULL;
 
193
        }
 
194
}
 
195
 
 
196
void
 
197
brasero_metadata_info_free (BraseroMetadataInfo *info)
 
198
{
 
199
        if (!info)
 
200
                return;
 
201
 
 
202
        brasero_metadata_info_clear (info);
 
203
 
 
204
        g_free (info);
 
205
}
 
206
 
 
207
void
 
208
brasero_metadata_info_copy (BraseroMetadataInfo *dest,
 
209
                            BraseroMetadataInfo *src)
 
210
{
 
211
        GSList *iter;
 
212
 
 
213
        if (!dest || !src)
 
214
                return;
 
215
 
 
216
        dest->has_dts = src->has_dts;
 
217
        dest->rate = src->rate;
 
218
        dest->channels = src->channels;
 
219
        dest->isrc = src->isrc;
 
220
        dest->len = src->len;
 
221
        dest->is_seekable = src->is_seekable;
 
222
        dest->has_audio = src->has_audio;
 
223
        dest->has_video = src->has_video;
 
224
 
 
225
        if (src->uri)
 
226
                dest->uri = g_strdup (src->uri);
 
227
 
 
228
        if (src->type)
 
229
                dest->type = g_strdup (src->type);
 
230
 
 
231
        if (src->title)
 
232
                dest->title = g_strdup (src->title);
 
233
 
 
234
        if (src->artist)
 
235
                dest->artist = g_strdup (src->artist);
 
236
 
 
237
        if (src->album)
 
238
                dest->album = g_strdup (src->album);
 
239
 
 
240
        if (src->genre)
 
241
                dest->genre = g_strdup (src->genre);
 
242
 
 
243
        if (src->musicbrainz_id)
 
244
                dest->musicbrainz_id = g_strdup (src->musicbrainz_id);
 
245
 
 
246
        if (src->snapshot) {
 
247
                dest->snapshot = src->snapshot;
 
248
                g_object_ref (dest->snapshot);
 
249
        }
 
250
 
 
251
        for (iter = src->silences; iter; iter = iter->next) {
 
252
                BraseroMetadataSilence *silence, *copy;
 
253
 
 
254
                silence = iter->data;
 
255
 
 
256
                copy = g_new0 (BraseroMetadataSilence, 1);
 
257
                copy->start = silence->start;
 
258
                copy->end = silence->end;
 
259
 
 
260
                dest->silences = g_slist_append (dest->silences, copy);
 
261
        }
 
262
}
 
263
 
 
264
static void
 
265
brasero_metadata_stop_pipeline (GstElement *pipeline)
 
266
{
 
267
        GstState state;
 
268
        GstStateChangeReturn change;
 
269
 
 
270
        change = gst_element_set_state (GST_ELEMENT (pipeline),
 
271
                                        GST_STATE_NULL);
 
272
 
 
273
        change = gst_element_get_state (pipeline,
 
274
                                        &state,
 
275
                                        NULL,
 
276
                                        GST_MSECOND);
 
277
 
 
278
        /* better wait for the state change to be completed */
 
279
        while (change == GST_STATE_CHANGE_ASYNC && state != GST_STATE_NULL) {
 
280
                GstState pending;
 
281
 
 
282
                change = gst_element_get_state (pipeline,
 
283
                                                &state,
 
284
                                                &pending,
 
285
                                                GST_MSECOND);
 
286
                BRASERO_UTILS_LOG ("Get state (current = %i pending = %i) returned %i",
 
287
                                   state, pending, change);
 
288
        }
 
289
 
 
290
        if (change == GST_STATE_CHANGE_FAILURE)
 
291
                g_warning ("State change failure");
 
292
 
 
293
}
 
294
 
 
295
static void
 
296
brasero_metadata_destroy_pipeline (BraseroMetadata *self)
 
297
{
 
298
        BraseroMetadataPrivate *priv;
 
299
 
 
300
        priv = BRASERO_METADATA_PRIVATE (self);
 
301
 
 
302
        priv->started = 0;
 
303
 
 
304
        if (priv->pipeline_mp3) {
 
305
                brasero_metadata_stop_pipeline (priv->pipeline_mp3);
 
306
                gst_object_unref (GST_OBJECT (priv->pipeline_mp3));
 
307
                priv->pipeline_mp3 = NULL;
 
308
        }
 
309
 
 
310
        if (priv->watch_mp3) {
 
311
                g_source_remove (priv->watch_mp3);
 
312
                priv->watch_mp3 = 0;
 
313
        }
 
314
 
 
315
        if (!priv->pipeline)
 
316
                return;
 
317
 
 
318
        brasero_metadata_stop_pipeline (priv->pipeline);
 
319
 
 
320
        if (priv->audio) {
 
321
                gst_bin_remove (GST_BIN (priv->pipeline), priv->audio);
 
322
                priv->audio = NULL;
 
323
        }
 
324
 
 
325
        if (priv->video) {
 
326
                gst_bin_remove (GST_BIN (priv->pipeline), priv->video);
 
327
                priv->snapshot = NULL;
 
328
                priv->video = NULL;
 
329
        }
 
330
 
 
331
        gst_object_unref (GST_OBJECT (priv->pipeline));
 
332
        priv->pipeline = NULL;
 
333
 
 
334
        if (priv->level) {
 
335
                gst_object_unref (GST_OBJECT (priv->level));
 
336
                priv->level = NULL;
 
337
        }
 
338
 
 
339
        if (priv->sink) {
 
340
                gst_object_unref (GST_OBJECT (priv->sink));
 
341
                priv->sink = NULL;
 
342
        }
 
343
 
 
344
        if (priv->convert) {
 
345
                gst_object_unref (GST_OBJECT (priv->convert));
 
346
                priv->convert = NULL;
 
347
        }
 
348
}
 
349
 
 
350
static void
 
351
brasero_metadata_stop (BraseroMetadata *self)
 
352
{
 
353
        BraseroMetadataPrivate *priv;
 
354
        GSList *iter;
 
355
 
 
356
        priv = BRASERO_METADATA_PRIVATE (self);
 
357
 
 
358
        BRASERO_UTILS_LOG ("Retrieval ended for %s %p",
 
359
                           priv->info ? priv->info->uri:"Unknown",
 
360
                           self);
 
361
 
 
362
        g_mutex_lock (priv->mutex);
 
363
 
 
364
        /* Destroy the pipeline as it has become un-re-usable */
 
365
        if (priv->watch) {
 
366
                g_source_remove (priv->watch);
 
367
                priv->watch = 0;
 
368
        }
 
369
 
 
370
        if (priv->pipeline)
 
371
                brasero_metadata_destroy_pipeline (self);
 
372
 
 
373
        /* That's automatic missing plugin installation */
 
374
        if (priv->missing_plugins) {
 
375
                g_slist_foreach (priv->missing_plugins,
 
376
                                 (GFunc) gst_mini_object_unref,
 
377
                                 NULL);
 
378
                g_slist_free (priv->missing_plugins);
 
379
                priv->missing_plugins = NULL;
 
380
        }
 
381
 
 
382
        if (priv->downloads) {
 
383
                GSList *iter;
 
384
 
 
385
                for (iter = priv->downloads; iter; iter = iter->next) {
 
386
                        BraseroMetadataGstDownload *download;
 
387
 
 
388
                        download = iter->data;
 
389
                        download->objects = g_slist_remove (download->objects, self);
 
390
                }
 
391
 
 
392
                g_slist_free (priv->downloads);
 
393
                priv->downloads = NULL;
 
394
        }
 
395
 
 
396
        /* stop the pipeline */
 
397
        priv->started = 0;
 
398
 
 
399
        /* Tell all the waiting threads that we're done */
 
400
        for (iter = priv->conditions; iter; iter = iter->next) {
 
401
                GCond *condition;
 
402
 
 
403
                condition = iter->data;
 
404
                g_cond_broadcast (condition);
 
405
        }
 
406
 
 
407
        g_mutex_unlock (priv->mutex);
 
408
}
 
409
 
 
410
void
 
411
brasero_metadata_cancel (BraseroMetadata *self)
 
412
{
 
413
        BraseroMetadataPrivate *priv;
 
414
 
 
415
        priv = BRASERO_METADATA_PRIVATE (self);
 
416
 
 
417
        BRASERO_UTILS_LOG ("Metadata retrieval cancelled for %s %p",
 
418
                           priv->info ? priv->info->uri:"Unknown",
 
419
                           self);
 
420
        
 
421
        brasero_metadata_stop (self);
 
422
        if (priv->error) {
 
423
                g_error_free (priv->error);
 
424
                priv->error = NULL;
 
425
        }
 
426
}
 
427
static void
 
428
brasero_metadata_install_plugins_add_downloaded (GSList *downloads)
 
429
{
 
430
        GSList *iter;
 
431
 
 
432
        for (iter = downloads; iter; iter = iter->next) {
 
433
                BraseroMetadataGstDownload *download;
 
434
 
 
435
                download = iter->data;
 
436
                downloaded = g_slist_prepend (downloaded, download->detail);
 
437
                download->detail = NULL;
 
438
        }
 
439
}
 
440
 
 
441
static void
 
442
brasero_metadata_install_plugins_free_data (GSList *downloads)
 
443
{
 
444
        GSList *iter;
 
445
 
 
446
        for (iter = downloads; iter; iter = iter->next) {
 
447
                BraseroMetadataGstDownload *download;
 
448
                GSList *meta;
 
449
 
 
450
                download = iter->data;
 
451
                if (download->detail)
 
452
                        g_free (download->detail);
 
453
 
 
454
                for (meta = download->objects; meta; meta = meta->next) {
 
455
                        BraseroMetadataPrivate *priv;
 
456
 
 
457
                        priv = BRASERO_METADATA_PRIVATE (meta->data);
 
458
                        priv->downloads = g_slist_remove (priv->downloads, download);
 
459
                }
 
460
                g_slist_free (download->objects);
 
461
 
 
462
                downloading = g_slist_remove (downloading, download);
 
463
                g_free (download);
 
464
        }
 
465
 
 
466
        g_slist_free (downloads);
 
467
}
 
468
 
 
469
static void
 
470
brasero_metadata_install_plugins_success (BraseroMetadataGstDownload *download)
 
471
{
 
472
        GSList *iter;
 
473
 
 
474
        for (iter = download->objects; iter; iter = iter->next) {
 
475
                BraseroMetadataPrivate *priv;
 
476
 
 
477
                priv = BRASERO_METADATA_PRIVATE (iter->data);
 
478
 
 
479
                if (priv->error) {
 
480
                        /* free previously saved error message */
 
481
                        g_error_free (priv->error);
 
482
                        priv->error = NULL;
 
483
                }
 
484
 
 
485
                gst_element_set_state (GST_ELEMENT (priv->pipeline), GST_STATE_NULL);
 
486
                gst_element_set_state (GST_ELEMENT (priv->pipeline), GST_STATE_PLAYING);
 
487
        }
 
488
}
 
489
 
 
490
static void
 
491
brasero_metadata_install_plugins_abort (BraseroMetadataGstDownload *download)
 
492
{
 
493
        GSList *iter;
 
494
        GSList *next;
 
495
 
 
496
        for (iter = download->objects; iter; iter = next) {
 
497
                BraseroMetadataPrivate *priv;
 
498
 
 
499
                next = iter->next;
 
500
 
 
501
                priv = BRASERO_METADATA_PRIVATE (iter->data);
 
502
 
 
503
                if (priv->error) {
 
504
                        g_error_free (priv->error);
 
505
                        priv->error = NULL;
 
506
                }
 
507
 
 
508
                brasero_metadata_completed (BRASERO_METADATA (iter->data));
 
509
        }
 
510
}
 
511
 
 
512
static void
 
513
brasero_metadata_install_plugins_completed (BraseroMetadataGstDownload *download)
 
514
{
 
515
        GSList *iter;
 
516
        GSList *next;
 
517
 
 
518
        for (iter = download->objects; iter; iter = next) {
 
519
                next = iter->next;
 
520
                brasero_metadata_completed (BRASERO_METADATA (iter->data));
 
521
        }
 
522
}
 
523
 
 
524
static void
 
525
brasero_metadata_install_plugins_result (GstInstallPluginsReturn res,
 
526
                                         gpointer data)
 
527
{
 
528
        GSList *downloads = data;
 
529
        GSList *iter;
 
530
 
 
531
        switch (res) {
 
532
        case GST_INSTALL_PLUGINS_PARTIAL_SUCCESS:
 
533
        case GST_INSTALL_PLUGINS_SUCCESS:
 
534
                brasero_metadata_install_plugins_add_downloaded (downloads);
 
535
 
 
536
                /* force gst to update plugin list */
 
537
                gst_update_registry ();
 
538
 
 
539
                /* restart metadata search */
 
540
                for (iter = downloads; iter; iter = iter->next) {
 
541
                        BraseroMetadataGstDownload *download;
 
542
 
 
543
                        download = iter->data;
 
544
                        brasero_metadata_install_plugins_success (download);
 
545
                }
 
546
                break;
 
547
 
 
548
        case GST_INSTALL_PLUGINS_NOT_FOUND:
 
549
                brasero_metadata_install_plugins_add_downloaded (downloads);
 
550
 
 
551
                /* stop everything */
 
552
                for (iter = downloads; iter; iter = iter->next)
 
553
                        brasero_metadata_install_plugins_completed (iter->data);
 
554
                break;
 
555
 
 
556
        case GST_INSTALL_PLUGINS_USER_ABORT:
 
557
                brasero_metadata_install_plugins_add_downloaded (downloads);
 
558
 
 
559
                /* free previously saved error message */
 
560
                for (iter = downloads; iter; iter = iter->next) {
 
561
                        BraseroMetadataGstDownload *download;
 
562
 
 
563
                        download = iter->data;
 
564
                        brasero_metadata_install_plugins_abort (download);
 
565
                }
 
566
                break;
 
567
 
 
568
        case GST_INSTALL_PLUGINS_ERROR:
 
569
        case GST_INSTALL_PLUGINS_CRASHED:
 
570
        default:
 
571
                for (iter = downloads; iter; iter = iter->next)
 
572
                        brasero_metadata_install_plugins_completed (iter->data);
 
573
 
 
574
                break;
 
575
        }
 
576
 
 
577
        brasero_metadata_install_plugins_free_data (downloads);
 
578
}
 
579
 
 
580
static BraseroMetadataGstDownload *
 
581
brasero_metadata_is_downloading (const gchar *detail)
 
582
{
 
583
        GSList *iter;
 
584
 
 
585
        for (iter = downloading; iter; iter = iter->next) {
 
586
                BraseroMetadataGstDownload *download;
 
587
 
 
588
                download = iter->data;
 
589
                if (!strcmp (download->detail, detail))
 
590
                        return download;
 
591
        }
 
592
 
 
593
        return NULL;
 
594
}
 
595
 
 
596
static gboolean
 
597
brasero_metadata_install_missing_plugins (BraseroMetadata *self)
 
598
{
 
599
        GstInstallPluginsContext *context;
 
600
        GstInstallPluginsReturn status;
 
601
        BraseroMetadataPrivate *priv;
 
602
        GSList *downloads = NULL;
 
603
        GPtrArray *details;
 
604
        GSList *iter;
 
605
 
 
606
        priv = BRASERO_METADATA_PRIVATE (self);
 
607
 
 
608
        BRASERO_UTILS_LOG ("Starting to download missing plugins");
 
609
 
 
610
        details = g_ptr_array_new ();
 
611
        for (iter = priv->missing_plugins; iter; iter = iter->next) {
 
612
                gchar *detail;
 
613
                BraseroMetadataGstDownload *download;
 
614
 
 
615
                /* Check if this plugin:
 
616
                 * - has already been downloaded (whether it was successful or not)
 
617
                 * - is being downloaded
 
618
                 * If so don't do anything. */
 
619
                detail = gst_missing_plugin_message_get_installer_detail (iter->data);
 
620
                gst_mini_object_unref (iter->data);
 
621
 
 
622
                download = brasero_metadata_is_downloading (detail);
 
623
                if (download) {
 
624
                        download->objects = g_slist_prepend (download->objects, self);
 
625
                        g_free (detail);
 
626
                        continue;
 
627
                }
 
628
 
 
629
                if (g_slist_find_custom (downloaded, detail, (GCompareFunc) strcmp)) {
 
630
                        g_free (detail);
 
631
                        continue;
 
632
                }
 
633
 
 
634
                download = g_new0 (BraseroMetadataGstDownload, 1);
 
635
                download->detail = detail;
 
636
                download->objects = g_slist_prepend (download->objects, self);
 
637
                priv->downloads = g_slist_prepend (priv->downloads, download);
 
638
 
 
639
                downloads = g_slist_prepend (downloads, download);
 
640
                downloading = g_slist_prepend (downloading, download);
 
641
 
 
642
                g_ptr_array_add (details, detail);
 
643
        }
 
644
 
 
645
        g_slist_free (priv->missing_plugins);
 
646
        priv->missing_plugins = NULL;
 
647
 
 
648
        if (!details->len) {
 
649
                /* either these plugins were downloaded or are being downloaded */
 
650
                g_ptr_array_free (details, TRUE);
 
651
                if (!priv->downloads)
 
652
                        return FALSE;
 
653
 
 
654
                return TRUE;
 
655
        }
 
656
 
 
657
        g_ptr_array_add (details, NULL);
 
658
 
 
659
        /* FIXME: we'd need the main window here to set it modal */
 
660
 
 
661
        context = gst_install_plugins_context_new ();
 
662
        gst_install_plugins_context_set_xid (context, brasero_metadata_get_xid (self));
 
663
        status = gst_install_plugins_async ((gchar **) details->pdata,
 
664
                                            context,
 
665
                                            brasero_metadata_install_plugins_result,
 
666
                                            downloads);
 
667
 
 
668
        gst_install_plugins_context_free (context);
 
669
        g_ptr_array_free (details, TRUE);
 
670
 
 
671
        BRASERO_UTILS_LOG ("Download status %i", status);
 
672
 
 
673
        if (status != GST_INSTALL_PLUGINS_STARTED_OK) {
 
674
                brasero_metadata_install_plugins_free_data (downloads);
 
675
                return FALSE;
 
676
        }
 
677
 
 
678
        return TRUE;
 
679
}
 
680
 
 
681
static gboolean
 
682
brasero_metadata_completed (BraseroMetadata *self)
 
683
{
 
684
        BraseroMetadataPrivate *priv;
 
685
 
 
686
        priv = BRASERO_METADATA_PRIVATE (self);
 
687
 
 
688
        if (priv->error) {
 
689
                BRASERO_UTILS_LOG ("Operation completed with an error %s", priv->error->message);
 
690
        }
 
691
 
 
692
        /* See if we have missing plugins */
 
693
        if (priv->missing_plugins) {
 
694
                if (brasero_metadata_install_missing_plugins (self))
 
695
                        return TRUE;
 
696
        }
 
697
 
 
698
        /* we send a message only if we haven't got a loop (= async mode) */
 
699
        g_object_ref (self);
 
700
        g_signal_emit (G_OBJECT (self),
 
701
                       brasero_metadata_signals [COMPLETED_SIGNAL],
 
702
                       0,
 
703
                       priv->error);
 
704
 
 
705
        brasero_metadata_stop (self);
 
706
 
 
707
        g_object_unref (self);
 
708
 
 
709
        /* Return FALSE on purpose here as it will stop the bus callback 
 
710
         * It's not whether we succeeded or not. */
 
711
        return FALSE;
 
712
}
 
713
 
 
714
static gboolean
 
715
brasero_metadata_thumbnail (BraseroMetadata *self)
 
716
{
 
717
        BraseroMetadataPrivate *priv;
 
718
        gint64 position;
 
719
        gboolean res;
 
720
 
 
721
        priv = BRASERO_METADATA_PRIVATE (self);
 
722
 
 
723
        /* find the right position and move forward */
 
724
        position = 15 * GST_SECOND;
 
725
        while (position > 0 && position >= priv->info->len)
 
726
                position -= 5 * GST_SECOND;
 
727
 
 
728
        if (position <= 0)
 
729
                return FALSE;
 
730
 
 
731
        gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
 
732
 
 
733
        priv->snapshot_started = 1;
 
734
        if (position < GST_SECOND)
 
735
                position = GST_SECOND;
 
736
 
 
737
        res = gst_element_seek_simple (priv->pipeline,
 
738
                                       GST_FORMAT_TIME,
 
739
                                       GST_SEEK_FLAG_FLUSH,
 
740
                                       position);
 
741
 
 
742
        BRASERO_UTILS_LOG ("Seeking forward %i for %s", res, priv->info->uri);
 
743
        if (!res)
 
744
                return brasero_metadata_completed (self);
 
745
 
 
746
        g_object_set (priv->snapshot,
 
747
                      "send-messages", TRUE,
 
748
                      NULL);
 
749
 
 
750
        return TRUE;
 
751
}
 
752
 
 
753
static void
 
754
brasero_metadata_is_seekable (BraseroMetadata *self)
 
755
{
 
756
        GstQuery *query;
 
757
        GstFormat format;
 
758
        gboolean seekable;
 
759
        BraseroMetadataPrivate *priv;
 
760
 
 
761
        priv = BRASERO_METADATA_PRIVATE (self);
 
762
 
 
763
        priv->info->is_seekable = FALSE;
 
764
 
 
765
        /* NOTE: apparently GST_FORMAT_DEFAULT does not work here */
 
766
        query = gst_query_new_seeking (GST_FORMAT_TIME);
 
767
 
 
768
        /* NOTE: it works better now on the pipeline than on the source as we
 
769
         * used to do */
 
770
        if (!gst_element_query (priv->pipeline, query))
 
771
                goto end;
 
772
 
 
773
        gst_query_parse_seeking (query,
 
774
                                 &format,
 
775
                                 &seekable,
 
776
                                 NULL,
 
777
                                 NULL);
 
778
 
 
779
        priv->info->is_seekable = seekable;
 
780
 
 
781
end:
 
782
 
 
783
        gst_query_unref (query);
 
784
}
 
785
 
 
786
/* FIXME: use GstDiscoverer ? */
 
787
static gboolean
 
788
brasero_metadata_get_mime_type (BraseroMetadata *self)
 
789
{
 
790
        BraseroMetadataPrivate *priv;
 
791
        GstElement *typefind;
 
792
        GstCaps *caps = NULL;
 
793
        const gchar *mime;
 
794
 
 
795
        priv = BRASERO_METADATA_PRIVATE (self);
 
796
 
 
797
        if (priv->info->type) {
 
798
                g_free (priv->info->type);
 
799
                priv->info->type = NULL;
 
800
        }
 
801
 
 
802
        /* find the type of the file */
 
803
        typefind = gst_bin_get_by_name (GST_BIN (priv->decode),
 
804
                                        "typefind");
 
805
 
 
806
        g_object_get (typefind, "caps", &caps, NULL);
 
807
        if (!caps) {
 
808
                gst_object_unref (typefind);
 
809
                return FALSE;
 
810
        }
 
811
 
 
812
        if (gst_caps_get_size (caps) <= 0) {
 
813
                gst_object_unref (typefind);
 
814
                return FALSE;
 
815
        }
 
816
 
 
817
        mime = gst_structure_get_name (gst_caps_get_structure (caps, 0));
 
818
        gst_object_unref (typefind);
 
819
 
 
820
        BRASERO_UTILS_LOG ("Mime type %s", mime);
 
821
 
 
822
        if (!mime)
 
823
                return FALSE;
 
824
 
 
825
        if (!strcmp (mime, "application/x-id3"))
 
826
                priv->info->type = g_strdup ("audio/mpeg");
 
827
        else if (!strcmp (mime, "audio/x-wav")) {
 
828
                GstElement *wavparse = NULL;
 
829
                GstIteratorResult res;
 
830
                GstIterator *iter;
 
831
                GValue value = { 0, };
 
832
 
 
833
                priv->info->type = g_strdup (mime);
 
834
 
 
835
                /* make sure it doesn't have dts inside */
 
836
                iter = gst_bin_iterate_recurse (GST_BIN (priv->decode));
 
837
 
 
838
                res = gst_iterator_next (iter, &value);
 
839
                while (res == GST_ITERATOR_OK) {
 
840
                        GstElement *element;
 
841
                        gchar *name;
 
842
 
 
843
                        element = GST_ELEMENT (g_value_get_object (&value));
 
844
                        name = gst_object_get_name (GST_OBJECT (element));
 
845
                        if (name) {
 
846
                                if (!strncmp (name, "wavparse", 8)) {
 
847
                                        wavparse = gst_object_ref (element);
 
848
                                        g_value_unset (&value);
 
849
                                        g_free (name);
 
850
                                        break;
 
851
                                }
 
852
                                g_free (name);
 
853
                        }
 
854
 
 
855
                        g_value_unset (&value);
 
856
                        element = NULL;
 
857
 
 
858
                        res = gst_iterator_next (iter, &value);
 
859
                }
 
860
                gst_iterator_free (iter);
 
861
 
 
862
                if (wavparse) {
 
863
                        GstCaps *src_caps;
 
864
                        GstPad *src_pad;
 
865
 
 
866
                        src_pad = gst_element_get_static_pad (wavparse, "src");
 
867
                        src_caps = gst_pad_get_current_caps (src_pad);
 
868
                        gst_object_unref (src_pad);
 
869
                        src_pad = NULL;
 
870
 
 
871
                        if (src_caps) {
 
872
                                GstStructure *structure;
 
873
 
 
874
                                /* negotiated caps will always have one structure */
 
875
                                structure = gst_caps_get_structure (src_caps, 0);
 
876
                                priv->info->has_dts = gst_structure_has_name (structure, "audio/x-dts");
 
877
                                gst_caps_unref (src_caps);
 
878
                        }
 
879
                        gst_object_unref (wavparse);
 
880
                }
 
881
 
 
882
                BRASERO_UTILS_LOG ("Wav file has dts: %s", priv->info->has_dts? "yes":"no");
 
883
        }
 
884
        else
 
885
                priv->info->type = g_strdup (mime);
 
886
 
 
887
        return TRUE;
 
888
}
 
889
 
 
890
static gboolean
 
891
brasero_metadata_is_mp3 (BraseroMetadata *self)
 
892
{
 
893
        BraseroMetadataPrivate *priv;
 
894
 
 
895
        priv = BRASERO_METADATA_PRIVATE (self);
 
896
 
 
897
        if (!priv->info->type
 
898
        &&  !brasero_metadata_get_mime_type (self))
 
899
                return FALSE;
 
900
 
 
901
        if (!strcmp (priv->info->type, "audio/mpeg"))
 
902
                return TRUE;
 
903
 
 
904
        return FALSE;
 
905
}
 
906
 
 
907
static void
 
908
foreach_tag (const GstTagList *list,
 
909
             const gchar *tag,
 
910
             BraseroMetadata *self)
 
911
{
 
912
        BraseroMetadataPrivate *priv;
 
913
        priv = BRASERO_METADATA_PRIVATE (self);
 
914
 
 
915
        if (!strcmp (tag, GST_TAG_TITLE)) {
 
916
                if (priv->info->title)
 
917
                        g_free (priv->info->title);
 
918
 
 
919
                gst_tag_list_get_string (list, tag, &(priv->info->title));
 
920
        } else if (!strcmp (tag, GST_TAG_ARTIST)
 
921
               ||  !strcmp (tag, GST_TAG_PERFORMER)) {
 
922
                if (priv->info->artist)
 
923
                        g_free (priv->info->artist);
 
924
 
 
925
                gst_tag_list_get_string (list, tag, &(priv->info->artist));
 
926
        }
 
927
        else if (!strcmp (tag, GST_TAG_ALBUM)) {
 
928
                if (priv->info->album)
 
929
                        g_free (priv->info->album);
 
930
 
 
931
                gst_tag_list_get_string (list, tag, &(priv->info->album));
 
932
        }
 
933
        else if (!strcmp (tag, GST_TAG_GENRE)) {
 
934
                if (priv->info->genre)
 
935
                        g_free (priv->info->genre);
 
936
 
 
937
                gst_tag_list_get_string (list, tag, &(priv->info->genre));
 
938
        }
 
939
/*      else if (!strcmp (tag, GST_TAG_COMPOSER)) {
 
940
                if (self->composer)
 
941
                        g_free (self->composer);
 
942
 
 
943
                gst_tag_list_get_string (list, tag, &(self->composer));
 
944
        }
 
945
*/      else if (!strcmp (tag, GST_TAG_ISRC)) {
 
946
                gchar *isrc = NULL;
 
947
                gst_tag_list_get_string (list, tag, &isrc);
 
948
 
 
949
                if (isrc)
 
950
                        priv->info->isrc = (int) g_ascii_strtoull (isrc, NULL, 10);
 
951
        }
 
952
        else if (!strcmp (tag, GST_TAG_MUSICBRAINZ_TRACKID)) {
 
953
                gst_tag_list_get_string (list, tag, &(priv->info->musicbrainz_id));
 
954
        }
 
955
}
 
956
 
 
957
static gboolean
 
958
brasero_metadata_process_element_messages (BraseroMetadata *self,
 
959
                                           GstMessage *msg)
 
960
{
 
961
        BraseroMetadataPrivate *priv;
 
962
        const GstStructure *s;
 
963
 
 
964
        priv = BRASERO_METADATA_PRIVATE (self);
 
965
 
 
966
        s = gst_message_get_structure (msg);
 
967
 
 
968
        /* This is for snapshot function */
 
969
        if (gst_message_has_name (msg, "preroll-pixbuf")
 
970
        ||  gst_message_has_name (msg, "pixbuf")) {
 
971
                const GValue *value;
 
972
 
 
973
                value = gst_structure_get_value (s, "pixbuf");
 
974
                priv->info->snapshot = g_value_get_object (value);
 
975
                g_object_ref (priv->info->snapshot);
 
976
 
 
977
                BRASERO_UTILS_LOG ("Received pixbuf snapshot sink (%p) for %s", priv->info->snapshot, priv->info->uri);
 
978
 
 
979
                /* Now we can stop */
 
980
                return brasero_metadata_completed (self);
 
981
        }
 
982
 
 
983
        /* here we just want to check if that's a missing codec */
 
984
        if ((priv->flags & BRASERO_METADATA_FLAG_MISSING)
 
985
        &&   gst_is_missing_plugin_message (msg)) {
 
986
                priv->missing_plugins = g_slist_prepend (priv->missing_plugins, gst_message_ref (msg));
 
987
        }
 
988
        else if (gst_message_has_name (msg, "level")
 
989
        &&   gst_structure_has_field (s, "peak")) {
 
990
                const GValue *value;
 
991
                const GValue *list;
 
992
                gdouble peak;
 
993
 
 
994
                /* FIXME: this might still be changed to GValueArray before 1.0 release */
 
995
                list = gst_structure_get_value (s, "peak");
 
996
                value = gst_value_list_get_value (list, 0);
 
997
                peak = g_value_get_double (value);
 
998
 
 
999
                /* detection of silence */
 
1000
                if (peak < -50.0) {
 
1001
                        gint64 pos = -1;
 
1002
 
 
1003
                        /* was there a silence last time we check ?
 
1004
                         * NOTE: if that's the first signal we receive
 
1005
                         * then consider that silence started from 0 */
 
1006
                        gst_element_query_position (priv->pipeline, GST_FORMAT_TIME, &pos);
 
1007
                        if (pos == -1) {
 
1008
                                BRASERO_UTILS_LOG ("impossible to retrieve position");
 
1009
                                return TRUE;
 
1010
                        }
 
1011
 
 
1012
                        if (!priv->silence) {
 
1013
                                priv->silence = g_new0 (BraseroMetadataSilence, 1);
 
1014
                                if (priv->prev_level_mes) {
 
1015
                                        priv->silence->start = pos;
 
1016
                                        priv->silence->end = pos;
 
1017
                                }
 
1018
                                else {
 
1019
                                        priv->silence->start = 0;
 
1020
                                        priv->silence->end = pos;
 
1021
                                }
 
1022
                        }                               
 
1023
                        else
 
1024
                                priv->silence->end = pos;
 
1025
 
 
1026
                        BRASERO_UTILS_LOG ("silence detected at %lli", pos);
 
1027
                }
 
1028
                else if (priv->silence) {
 
1029
                        BRASERO_UTILS_LOG ("silence finished");
 
1030
 
 
1031
                        priv->info->silences = g_slist_append (priv->info->silences,
 
1032
                                                               priv->silence);
 
1033
                        priv->silence = NULL;
 
1034
                }
 
1035
                priv->prev_level_mes = 1;
 
1036
        }
 
1037
 
 
1038
        return TRUE;
 
1039
}
 
1040
 
 
1041
static void
 
1042
brasero_metadata_process_pending_messages (BraseroMetadata *self)
 
1043
{
 
1044
        GstBus *bus;
 
1045
        GstMessage *msg;
 
1046
        BraseroMetadataPrivate *priv;
 
1047
 
 
1048
        priv = BRASERO_METADATA_PRIVATE (self);
 
1049
 
 
1050
        bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
 
1051
        while ((msg = gst_bus_pop (bus))) {
 
1052
                GstTagList *tags = NULL;
 
1053
 
 
1054
                if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_TAG) {
 
1055
                        gst_message_parse_tag (msg, &tags);
 
1056
                        gst_tag_list_foreach (tags, (GstTagForeachFunc) foreach_tag, self);
 
1057
                        gst_tag_list_free (tags);
 
1058
                }
 
1059
                else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT)
 
1060
                        brasero_metadata_process_element_messages (self, msg);
 
1061
 
 
1062
                gst_message_unref (msg);
 
1063
        }
 
1064
 
 
1065
        g_object_unref (bus);
 
1066
}
 
1067
 
 
1068
static gboolean
 
1069
brasero_metadata_success (BraseroMetadata *self)
 
1070
{
 
1071
        BraseroMetadataPrivate *priv;
 
1072
 
 
1073
        priv = BRASERO_METADATA_PRIVATE (self);
 
1074
 
 
1075
        BRASERO_UTILS_LOG ("Metadata retrieval completed for %s", priv->info->uri);
 
1076
 
 
1077
        /* check if that's a seekable one */
 
1078
        brasero_metadata_is_seekable (self);
 
1079
 
 
1080
        if (priv->silence) {
 
1081
                priv->silence->end = priv->info->len;
 
1082
                priv->info->silences = g_slist_append (priv->info->silences, priv->silence);
 
1083
                priv->silence = NULL;
 
1084
        }
 
1085
 
 
1086
        /* before leaving, check if we need a snapshot */
 
1087
        if (priv->info->len > 0
 
1088
        &&  priv->snapshot
 
1089
        &&  priv->video_linked
 
1090
        && !priv->snapshot_started)
 
1091
                return brasero_metadata_thumbnail (self);
 
1092
 
 
1093
        return brasero_metadata_completed (self);
 
1094
}
 
1095
 
 
1096
static gboolean
 
1097
brasero_metadata_get_duration (BraseroMetadata *self,
 
1098
                               GstElement *pipeline,
 
1099
                               gboolean use_duration)
 
1100
{
 
1101
        BraseroMetadataPrivate *priv;
 
1102
        gint64 duration = -1;
 
1103
 
 
1104
        priv = BRASERO_METADATA_PRIVATE (self);
 
1105
 
 
1106
        if (!use_duration)
 
1107
                gst_element_query_position (GST_ELEMENT (pipeline),
 
1108
                                            GST_FORMAT_TIME,
 
1109
                                            &duration);
 
1110
        else
 
1111
                gst_element_query_duration (GST_ELEMENT (pipeline),
 
1112
                                            GST_FORMAT_TIME,
 
1113
                                            &duration);
 
1114
 
 
1115
        if (duration == -1) {
 
1116
                if (!priv->error) {
 
1117
                        gchar *name;
 
1118
 
 
1119
                        BRASERO_GET_BASENAME_FOR_DISPLAY (priv->info->uri, name);
 
1120
                        priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1121
                                                   BRASERO_UTILS_ERROR_GENERAL,
 
1122
                                                   _("\"%s\" could not be handled by GStreamer."),
 
1123
                                                   name);
 
1124
                        g_free (name);
 
1125
                }
 
1126
 
 
1127
                return brasero_metadata_completed (self);
 
1128
        }
 
1129
 
 
1130
        BRASERO_UTILS_LOG ("Found duration %lli for %s", duration, priv->info->uri);
 
1131
 
 
1132
        priv->info->len = duration;
 
1133
        return brasero_metadata_success (self);
 
1134
}
 
1135
 
 
1136
/**
 
1137
 * This is to deal with mp3 more particularly the vbrs
 
1138
 **/
 
1139
 
 
1140
static gboolean
 
1141
brasero_metadata_mp3_bus_messages (GstBus *bus,
 
1142
                                   GstMessage *msg,
 
1143
                                   BraseroMetadata *self)
 
1144
{
 
1145
        BraseroMetadataPrivate *priv;
 
1146
        gchar *debug_string = NULL;
 
1147
        GError *error = NULL;
 
1148
 
 
1149
        priv = BRASERO_METADATA_PRIVATE (self);
 
1150
 
 
1151
        switch (GST_MESSAGE_TYPE (msg)) {
 
1152
        case GST_MESSAGE_ERROR:
 
1153
                /* save the error message */
 
1154
                gst_message_parse_error (msg, &error, &debug_string);
 
1155
                BRASERO_UTILS_LOG ("GStreamer error - mp3 - (%s)", debug_string);
 
1156
                g_free (debug_string);
 
1157
                if (!priv->error && error)
 
1158
                        priv->error = error;
 
1159
 
 
1160
                brasero_metadata_completed (self);
 
1161
                return FALSE;
 
1162
 
 
1163
        case GST_MESSAGE_EOS:
 
1164
                BRASERO_UTILS_LOG ("End of stream reached - mp3 - for %s", priv->info->uri);
 
1165
                brasero_metadata_get_duration (self, priv->pipeline_mp3, FALSE);
 
1166
                return FALSE;
 
1167
 
 
1168
        default:
 
1169
                break;
 
1170
        }
 
1171
 
 
1172
        return TRUE;
 
1173
}
 
1174
 
 
1175
static gboolean
 
1176
brasero_metadata_create_mp3_pipeline (BraseroMetadata *self)
 
1177
{
 
1178
        BraseroMetadataPrivate *priv;
 
1179
        GstElement *source;
 
1180
        GstElement *parse;
 
1181
        GstElement *sink;
 
1182
        GstBus *bus;
 
1183
 
 
1184
        priv = BRASERO_METADATA_PRIVATE (self);
 
1185
 
 
1186
        priv->pipeline_mp3 = gst_pipeline_new (NULL);
 
1187
 
 
1188
        source = gst_element_make_from_uri (GST_URI_SRC,
 
1189
                                            priv->info->uri,
 
1190
                                            NULL, NULL);
 
1191
        if (!source) {
 
1192
                priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1193
                                           BRASERO_UTILS_ERROR_GENERAL,
 
1194
                                           _("%s element could not be created"),
 
1195
                                           "\"Source\"");
 
1196
 
 
1197
                g_object_unref (priv->pipeline_mp3);
 
1198
                priv->pipeline_mp3 = NULL;
 
1199
                return FALSE;
 
1200
        }
 
1201
        gst_bin_add (GST_BIN (priv->pipeline_mp3), source);
 
1202
 
 
1203
        parse = gst_element_factory_make ("mp3parse", NULL);
 
1204
        if (!parse) {
 
1205
                priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1206
                                           BRASERO_UTILS_ERROR_GENERAL,
 
1207
                                           _("%s element could not be created"),
 
1208
                                           "\"mp3parse\"");
 
1209
 
 
1210
                g_object_unref (priv->pipeline_mp3);
 
1211
                priv->pipeline_mp3 = NULL;
 
1212
                return FALSE;
 
1213
        }
 
1214
        gst_bin_add (GST_BIN (priv->pipeline_mp3), parse);
 
1215
 
 
1216
        sink = gst_element_factory_make ("fakesink", NULL);
 
1217
        if (!sink) {
 
1218
                priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1219
                                           BRASERO_UTILS_ERROR_GENERAL,
 
1220
                                           _("%s element could not be created"),
 
1221
                                           "\"Fakesink\"");
 
1222
 
 
1223
                g_object_unref (priv->pipeline_mp3);
 
1224
                priv->pipeline_mp3 = NULL;
 
1225
                return FALSE;
 
1226
        }
 
1227
        gst_bin_add (GST_BIN (priv->pipeline_mp3), sink);
 
1228
 
 
1229
        /* Link */
 
1230
        if (!gst_element_link_many (source, parse, sink, NULL)) {
 
1231
                g_object_unref (priv->pipeline_mp3);
 
1232
                priv->pipeline_mp3 = NULL;
 
1233
                return FALSE;
 
1234
        }
 
1235
 
 
1236
        /* Bus */
 
1237
        bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline_mp3));
 
1238
        priv->watch_mp3 = gst_bus_add_watch (bus,
 
1239
                                             (GstBusFunc) brasero_metadata_mp3_bus_messages,
 
1240
                                             self);
 
1241
        gst_object_unref (bus);
 
1242
 
 
1243
        gst_element_set_state (priv->pipeline_mp3, GST_STATE_PLAYING);
 
1244
        return TRUE;
 
1245
}
 
1246
 
 
1247
static gboolean
 
1248
brasero_metadata_success_main (BraseroMetadata *self)
 
1249
{
 
1250
        BraseroMetadataPrivate *priv;
 
1251
 
 
1252
        priv = BRASERO_METADATA_PRIVATE (self);
 
1253
 
 
1254
        BRASERO_UTILS_LOG ("Metadata retrieval successfully completed for %s", priv->info->uri);
 
1255
 
 
1256
        /* find the type of the file */
 
1257
        brasero_metadata_get_mime_type (self);
 
1258
 
 
1259
        /* empty the bus of any pending message */
 
1260
        brasero_metadata_process_pending_messages (self);
 
1261
 
 
1262
        /* get the size */
 
1263
        if (brasero_metadata_is_mp3 (self)) {
 
1264
                if (!brasero_metadata_create_mp3_pipeline (self)) {
 
1265
                        BRASERO_UTILS_LOG ("Impossible to run mp3 specific pipeline");
 
1266
                        return brasero_metadata_completed (self);
 
1267
                }
 
1268
 
 
1269
                /* Return FALSE here not because we failed but to stop the Bus callback */
 
1270
                return FALSE;
 
1271
        }
 
1272
 
 
1273
        return brasero_metadata_get_duration (self, priv->pipeline, TRUE);
 
1274
}
 
1275
 
 
1276
static gboolean
 
1277
brasero_metadata_bus_messages (GstBus *bus,
 
1278
                               GstMessage *msg,
 
1279
                               BraseroMetadata *self)
 
1280
{
 
1281
        BraseroMetadataPrivate *priv;
 
1282
        GstStateChangeReturn result;
 
1283
        gchar *debug_string = NULL;
 
1284
        GstTagList *tags = NULL;
 
1285
        GError *error = NULL;
 
1286
        GstState newstate;
 
1287
 
 
1288
        priv = BRASERO_METADATA_PRIVATE (self);
 
1289
 
 
1290
        switch (GST_MESSAGE_TYPE (msg)) {
 
1291
        case GST_MESSAGE_ELEMENT:
 
1292
                return brasero_metadata_process_element_messages (self, msg);
 
1293
 
 
1294
        case GST_MESSAGE_ERROR:
 
1295
                /* save the error message */
 
1296
                gst_message_parse_error (msg, &error, &debug_string);
 
1297
                BRASERO_UTILS_LOG ("GStreamer error (%s)", debug_string);
 
1298
                g_free (debug_string);
 
1299
                if (!priv->error && error)
 
1300
                        priv->error = error;
 
1301
 
 
1302
                return brasero_metadata_completed (self);
 
1303
 
 
1304
        case GST_MESSAGE_EOS:
 
1305
                BRASERO_UTILS_LOG ("End of stream reached for %s", priv->info->uri);
 
1306
                return brasero_metadata_success_main (self);
 
1307
 
 
1308
        case GST_MESSAGE_TAG:
 
1309
                gst_message_parse_tag (msg, &tags);
 
1310
                gst_tag_list_foreach (tags, (GstTagForeachFunc) foreach_tag, self);
 
1311
                gst_tag_list_free (tags);
 
1312
                break;
 
1313
 
 
1314
        case GST_MESSAGE_STATE_CHANGED:
 
1315
                /* when stopping the pipeline we are only interested in TAGS */
 
1316
                result = gst_element_get_state (GST_ELEMENT (priv->pipeline),
 
1317
                                                &newstate,
 
1318
                                                NULL,
 
1319
                                                0);
 
1320
 
 
1321
                if (result != GST_STATE_CHANGE_SUCCESS)
 
1322
                        break;
 
1323
 
 
1324
                if (newstate != GST_STATE_PAUSED && newstate != GST_STATE_PLAYING)
 
1325
                        break;
 
1326
 
 
1327
                if (!priv->snapshot_started)
 
1328
                        return brasero_metadata_success_main (self);
 
1329
 
 
1330
                break;
 
1331
 
 
1332
        default:
 
1333
                break;
 
1334
        }
 
1335
 
 
1336
        return TRUE;
 
1337
}
 
1338
 
 
1339
static gboolean
 
1340
brasero_metadata_create_audio_pipeline (BraseroMetadata *self)
 
1341
{
 
1342
        BraseroMetadataPrivate *priv;
 
1343
        GstPad *audio_pad;
 
1344
 
 
1345
        priv = BRASERO_METADATA_PRIVATE (self);
 
1346
 
 
1347
        priv->audio = gst_bin_new (NULL);
 
1348
 
 
1349
        /* set up the pipeline according to flags */
 
1350
        if (priv->flags & BRASERO_METADATA_FLAG_SILENCES) {
 
1351
                priv->prev_level_mes = 0;
 
1352
 
 
1353
                /* Add a reference to these objects as we want to keep them
 
1354
                 * around after the bin they've been added to is destroyed
 
1355
                 * NOTE: now we destroy the pipeline every time which means
 
1356
                 * that it doesn't really matter. */
 
1357
                if (!priv->level) {
 
1358
                        priv->level = gst_element_factory_make ("level", NULL);
 
1359
                        if (!priv->level) {
 
1360
                                priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1361
                                                           BRASERO_UTILS_ERROR_GENERAL,
 
1362
                                                           _("%s element could not be created"),
 
1363
                                                           "\"Level\"");
 
1364
                                gst_object_unref (priv->audio);
 
1365
                                priv->audio = NULL;
 
1366
                                return FALSE;
 
1367
                        }
 
1368
                        g_object_set (priv->level,
 
1369
                                      "message", TRUE,
 
1370
                                      "interval", (guint64) BRASERO_METADATA_SILENCE_INTERVAL,
 
1371
                                      NULL);
 
1372
                }
 
1373
 
 
1374
                gst_object_ref (priv->convert);
 
1375
                gst_object_ref (priv->level);
 
1376
                gst_object_ref (priv->sink);
 
1377
 
 
1378
                gst_bin_add_many (GST_BIN (priv->audio),
 
1379
                                  priv->convert,
 
1380
                                  priv->level,
 
1381
                                  priv->sink,
 
1382
                                  NULL);
 
1383
 
 
1384
                if (!gst_element_link_many (priv->convert,
 
1385
                                            priv->level,
 
1386
                                            priv->sink,
 
1387
                                            NULL)) {
 
1388
                        BRASERO_UTILS_LOG ("Impossible to link elements");
 
1389
                        gst_object_unref (priv->audio);
 
1390
                        priv->audio = NULL;
 
1391
                        return FALSE;
 
1392
                }
 
1393
 
 
1394
                audio_pad = gst_element_get_static_pad (priv->convert, "sink");
 
1395
        }
 
1396
        else if (priv->flags & BRASERO_METADATA_FLAG_THUMBNAIL) {
 
1397
                GstElement *queue;
 
1398
 
 
1399
                queue = gst_element_factory_make ("queue", NULL);
 
1400
                gst_object_ref (priv->convert);
 
1401
                gst_object_ref (priv->sink);
 
1402
 
 
1403
                gst_bin_add_many (GST_BIN (priv->audio),
 
1404
                                  queue,
 
1405
                                  priv->convert,
 
1406
                                  priv->sink,
 
1407
                                  NULL);
 
1408
                if (!gst_element_link_many (queue,
 
1409
                                            priv->convert,
 
1410
                                            priv->sink,
 
1411
                                            NULL)) {
 
1412
                        BRASERO_UTILS_LOG ("Impossible to link elements");
 
1413
                        gst_object_unref (priv->audio);
 
1414
                        priv->audio = NULL;
 
1415
                        return FALSE;
 
1416
                }
 
1417
 
 
1418
                audio_pad = gst_element_get_static_pad (queue, "sink");
 
1419
        }
 
1420
        else {
 
1421
                GstElement *queue;
 
1422
 
 
1423
                queue = gst_element_factory_make ("queue", NULL);
 
1424
                gst_bin_add (GST_BIN (priv->audio), queue);
 
1425
 
 
1426
                gst_object_ref (priv->sink);
 
1427
                gst_bin_add (GST_BIN (priv->audio), priv->sink);
 
1428
 
 
1429
                if (!gst_element_link (queue, priv->sink)) {
 
1430
                        BRASERO_UTILS_LOG ("Impossible to link elements");
 
1431
                        gst_object_unref (priv->audio);
 
1432
                        priv->audio = NULL;
 
1433
                        return FALSE;
 
1434
                }
 
1435
 
 
1436
                audio_pad = gst_element_get_static_pad (queue, "sink");
 
1437
        }
 
1438
 
 
1439
        gst_element_add_pad (priv->audio, gst_ghost_pad_new ("sink", audio_pad));
 
1440
        gst_object_unref (audio_pad);
 
1441
 
 
1442
        gst_bin_add (GST_BIN (priv->pipeline), priv->audio);
 
1443
        BRASERO_UTILS_LOG ("Adding audio pipeline for %s", priv->info->uri);
 
1444
 
 
1445
        return TRUE;
 
1446
}
 
1447
 
 
1448
static gboolean
 
1449
brasero_metadata_create_video_pipeline (BraseroMetadata *self)
 
1450
{
 
1451
        BraseroMetadataPrivate *priv;
 
1452
        GstElement *colorspace;
 
1453
        GstPad *video_pad;
 
1454
        GstElement *queue;
 
1455
 
 
1456
        priv = BRASERO_METADATA_PRIVATE (self);
 
1457
        priv->video = gst_bin_new (NULL);
 
1458
 
 
1459
        priv->snapshot = gst_element_factory_make ("gdkpixbufsink", NULL);
 
1460
        if (!priv->snapshot) {
 
1461
                gst_object_unref (priv->video);
 
1462
                priv->video = NULL;
 
1463
 
 
1464
                BRASERO_UTILS_LOG ("gdkpixbufsink is not installed");
 
1465
                return FALSE;
 
1466
        }
 
1467
        gst_bin_add (GST_BIN (priv->video), priv->snapshot);
 
1468
 
 
1469
        g_object_set (priv->snapshot,
 
1470
                      "qos", FALSE,
 
1471
                      "send-messages", FALSE,
 
1472
                      "max-lateness", (gint64) - 1,
 
1473
                      NULL);
 
1474
 
 
1475
        colorspace = gst_element_factory_make ("videoconvert", NULL);
 
1476
        if (!colorspace) {
 
1477
                gst_object_unref (priv->video);
 
1478
                priv->video = NULL;
 
1479
 
 
1480
                BRASERO_UTILS_LOG ("videoconvert is not installed");
 
1481
                return FALSE;
 
1482
        }
 
1483
        gst_bin_add (GST_BIN (priv->video), colorspace);
 
1484
 
 
1485
        queue = gst_element_factory_make ("queue", NULL);
 
1486
        gst_bin_add (GST_BIN (priv->video), queue);
 
1487
 
 
1488
        /* link elements */
 
1489
        if (!gst_element_link_many (queue,
 
1490
                                    colorspace,
 
1491
                                    priv->snapshot,
 
1492
                                    NULL)) {
 
1493
                gst_object_unref (priv->video);
 
1494
                priv->video = NULL;
 
1495
 
 
1496
                BRASERO_UTILS_LOG ("Impossible to link elements");
 
1497
                return FALSE;
 
1498
        }
 
1499
 
 
1500
        video_pad = gst_element_get_static_pad (queue, "sink");
 
1501
        gst_element_add_pad (priv->video, gst_ghost_pad_new ("sink", video_pad));
 
1502
        gst_object_unref (video_pad);
 
1503
 
 
1504
        gst_bin_add (GST_BIN (priv->pipeline), priv->video);
 
1505
        BRASERO_UTILS_LOG ("Adding pixbuf snapshot sink for %s", priv->info->uri);
 
1506
 
 
1507
        return TRUE;
 
1508
}
 
1509
 
 
1510
static void
 
1511
brasero_metadata_error_on_pad_linking (BraseroMetadata *self)
 
1512
{
 
1513
        BraseroMetadataPrivate *priv;
 
1514
        GstMessage *message;
 
1515
        GstBus *bus;
 
1516
 
 
1517
        priv = BRASERO_METADATA_PRIVATE (self);
 
1518
 
 
1519
        message = gst_message_new_error (GST_OBJECT (priv->pipeline),
 
1520
                                         priv->error,
 
1521
                                         "Sent by brasero_metadata_error_on_pad_linking");
 
1522
 
 
1523
        bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
 
1524
        gst_bus_post (bus, message);
 
1525
        g_object_unref (bus);
 
1526
}
 
1527
 
 
1528
static gboolean
 
1529
brasero_metadata_link_dummy_pad (BraseroMetadata *self,
 
1530
                                 GstPad *pad)
 
1531
{
 
1532
        BraseroMetadataPrivate *priv;
 
1533
        GstElement *fakesink;
 
1534
        GstPadLinkReturn res;
 
1535
        GstPad *sink;
 
1536
 
 
1537
        priv = BRASERO_METADATA_PRIVATE (self);
 
1538
 
 
1539
        BRASERO_UTILS_LOG ("Linking to a fake sink");
 
1540
 
 
1541
        /* It doesn't hurt to link to a fakesink and can avoid some deadlocks.
 
1542
         * I don't know why but some demuxers in particular will lock (probably
 
1543
         * because they can't output video) if only their audio streams are
 
1544
         * linked and not their video streams (one example is dv demuxer).
 
1545
         * NOTE: there must also be a queue for audio streams. */
 
1546
        fakesink = gst_element_factory_make ("fakesink", NULL);
 
1547
        if (!fakesink)
 
1548
                return FALSE;
 
1549
 
 
1550
        gst_bin_add (GST_BIN (priv->pipeline), fakesink);
 
1551
        sink = gst_element_get_static_pad (fakesink, "sink");
 
1552
        if (!sink)
 
1553
                return FALSE;
 
1554
 
 
1555
        res = gst_pad_link (pad, sink);
 
1556
 
 
1557
        if (res == GST_PAD_LINK_OK) {
 
1558
                gst_element_set_state (fakesink, BRASERO_METADATA_INITIAL_STATE);
 
1559
                return TRUE;
 
1560
        }
 
1561
 
 
1562
        return FALSE;
 
1563
}
 
1564
 
 
1565
static void
 
1566
brasero_metadata_audio_caps (BraseroMetadata *self,
 
1567
                             GstCaps *caps)
 
1568
{
 
1569
        int i;
 
1570
        int num_caps;
 
1571
        BraseroMetadataPrivate *priv;
 
1572
 
 
1573
        priv = BRASERO_METADATA_PRIVATE (self);
 
1574
 
 
1575
        num_caps = gst_caps_get_size (caps);
 
1576
        for (i = 0; i < num_caps; i++) {
 
1577
                const GstStructure *structure;
 
1578
 
 
1579
                structure = gst_caps_get_structure (caps, i);
 
1580
                if (!structure)
 
1581
                        continue;
 
1582
 
 
1583
                if (gst_structure_has_field (structure, "channels")) {
 
1584
                        if (gst_structure_get_field_type (structure, "channels") == G_TYPE_INT) {
 
1585
                                priv->info->channels = 0;
 
1586
                                gst_structure_get_int (structure, "channels", &priv->info->channels);
 
1587
 
 
1588
                                BRASERO_UTILS_LOG ("Number of channels %i", priv->info->channels);
 
1589
                        }
 
1590
                        else if (gst_structure_get_field_type (structure, "channels") == GST_TYPE_INT_RANGE) {
 
1591
                                const GValue *value;
 
1592
 
 
1593
                                value = gst_structure_get_value (structure, "channels");
 
1594
                                if (value) {
 
1595
                                        priv->info->channels = gst_value_get_int_range_max (value);
 
1596
                                        BRASERO_UTILS_LOG ("Number of channels %i", priv->info->channels);
 
1597
                                }
 
1598
                        }
 
1599
                        else if (gst_structure_get_field_type (structure, "channels") != G_TYPE_INVALID) {
 
1600
                                BRASERO_UTILS_LOG ("Unhandled type for channel prop %s",
 
1601
                                                   g_type_name (gst_structure_get_field_type (structure, "channels")));
 
1602
                        }
 
1603
                }
 
1604
 
 
1605
                if (gst_structure_has_field (structure, "rate")) {
 
1606
                        if (gst_structure_get_field_type (structure, "rate") == G_TYPE_INT) {
 
1607
                                priv->info->rate = 0;
 
1608
                                gst_structure_get_int (structure, "rate", &priv->info->rate);
 
1609
 
 
1610
                                BRASERO_UTILS_LOG ("Rate %i", priv->info->rate);
 
1611
                        }
 
1612
                        else if (gst_structure_get_field_type (structure, "rate") == GST_TYPE_INT_RANGE) {
 
1613
                                const GValue *value;
 
1614
 
 
1615
                                value = gst_structure_get_value (structure, "rate");
 
1616
                                if (value) {
 
1617
                                        priv->info->rate = gst_value_get_int_range_max (value);
 
1618
                                        BRASERO_UTILS_LOG ("Rate %i", priv->info->rate);
 
1619
                                }
 
1620
                        }
 
1621
                        else if (gst_structure_get_field_type (structure, "rate") != G_TYPE_INVALID) {
 
1622
                                BRASERO_UTILS_LOG ("Unhandled type for rate prop %s",
 
1623
                                                   g_type_name (gst_structure_get_field_type (structure, "rate")));
 
1624
                        }
 
1625
                }
 
1626
        }
 
1627
}
 
1628
 
 
1629
static void
 
1630
brasero_metadata_new_decoded_pad_cb (GstElement *decode,
 
1631
                                     GstPad *pad,
 
1632
                                     BraseroMetadata *self)
 
1633
{
 
1634
        GstPad *sink;
 
1635
        GstCaps *caps;
 
1636
        const gchar *name;
 
1637
        gboolean has_audio;
 
1638
        gboolean has_video;
 
1639
        GstPadLinkReturn res;
 
1640
        GstStructure *structure;
 
1641
        BraseroMetadataPrivate *priv;
 
1642
 
 
1643
        priv = BRASERO_METADATA_PRIVATE (self);
 
1644
 
 
1645
        res = GST_PAD_LINK_REFUSED;
 
1646
 
 
1647
        BRASERO_UTILS_LOG ("New pad for %s", priv->info->uri);
 
1648
 
 
1649
        /* make sure that this is audio / video */
 
1650
        /* FIXME: get_current_caps() doesn't always seem to work yet here */
 
1651
        caps = gst_pad_query_caps (pad, NULL);
 
1652
        if (!caps) {
 
1653
                g_warning ("Expected caps on decodebin pad %s", GST_PAD_NAME (pad));
 
1654
                return;
 
1655
        }
 
1656
        structure = gst_caps_get_structure (caps, 0);
 
1657
        name = gst_structure_get_name (structure);
 
1658
 
 
1659
        has_audio = (g_strrstr (name, "audio") != NULL);
 
1660
        has_video = (g_strrstr (name, "video") != NULL);
 
1661
        priv->info->has_audio |= has_audio;
 
1662
        priv->info->has_video |= has_video;
 
1663
 
 
1664
        if (has_audio && !priv->audio_linked) {
 
1665
                brasero_metadata_audio_caps (self, caps);
 
1666
                brasero_metadata_create_audio_pipeline (self);
 
1667
                sink = gst_element_get_static_pad (priv->audio, "sink");
 
1668
                if (sink && !GST_PAD_IS_LINKED (sink)) {
 
1669
                        res = gst_pad_link (pad, sink);
 
1670
                        BRASERO_UTILS_LOG ("Audio stream link %i for %s", res, priv->info->uri);
 
1671
                        gst_object_unref (sink);
 
1672
 
 
1673
                        priv->audio_linked = (res == GST_PAD_LINK_OK);
 
1674
                        gst_element_set_state (priv->audio, BRASERO_METADATA_INITIAL_STATE);
 
1675
                }
 
1676
        }
 
1677
 
 
1678
        if (!strcmp (name, "video/x-raw") && !priv->video_linked) {
 
1679
                BRASERO_UTILS_LOG ("RAW video stream found");
 
1680
 
 
1681
                if (!priv->video && (priv->flags & BRASERO_METADATA_FLAG_THUMBNAIL)) {
 
1682
                        /* we shouldn't error out if we can't create a video
 
1683
                         * pipeline (mostly used for snapshots) */
 
1684
                        /* FIXME: we should nevertheless tell the user what
 
1685
                         * plugin he is missing. */
 
1686
                        if (!brasero_metadata_create_video_pipeline (self)) {
 
1687
                                BRASERO_UTILS_LOG ("Impossible to create video pipeline");
 
1688
 
 
1689
                                gst_caps_unref (caps);
 
1690
 
 
1691
                                if (!brasero_metadata_link_dummy_pad (self, pad))
 
1692
                                        brasero_metadata_error_on_pad_linking (self);
 
1693
 
 
1694
                                return;
 
1695
                        }
 
1696
 
 
1697
                        sink = gst_element_get_static_pad (priv->video, "sink");
 
1698
                        if (!sink || GST_PAD_IS_LINKED (sink)) {
 
1699
                                gst_object_unref (sink);
 
1700
                                gst_caps_unref (caps);
 
1701
                                return;
 
1702
                        }
 
1703
 
 
1704
                        res = gst_pad_link (pad, sink);
 
1705
                        priv->video_linked = (res == GST_PAD_LINK_OK);
 
1706
                        gst_object_unref (sink);
 
1707
 
 
1708
                        gst_element_set_state (priv->video, BRASERO_METADATA_INITIAL_STATE);
 
1709
 
 
1710
                        BRASERO_UTILS_LOG ("Video stream link %i for %s", res, priv->info->uri);
 
1711
                }
 
1712
                else if (!brasero_metadata_link_dummy_pad (self, pad))
 
1713
                        brasero_metadata_error_on_pad_linking (self);
 
1714
        }
 
1715
        else if (has_video && !brasero_metadata_link_dummy_pad (self, pad))
 
1716
                brasero_metadata_error_on_pad_linking (self);
 
1717
 
 
1718
        gst_caps_unref (caps);
 
1719
}
 
1720
 
 
1721
static gboolean
 
1722
brasero_metadata_create_pipeline (BraseroMetadata *self)
 
1723
{
 
1724
        BraseroMetadataPrivate *priv;
 
1725
 
 
1726
        priv = BRASERO_METADATA_PRIVATE (self);
 
1727
 
 
1728
        priv->pipeline = gst_pipeline_new (NULL);
 
1729
 
 
1730
        priv->decode = gst_element_factory_make ("decodebin", NULL);
 
1731
        if (priv->decode == NULL) {
 
1732
                priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1733
                                           BRASERO_UTILS_ERROR_GENERAL,
 
1734
                                           _("%s element could not be created"),
 
1735
                                           "\"Decodebin\"");
 
1736
                return FALSE;
 
1737
        }
 
1738
        g_signal_connect (G_OBJECT (priv->decode), "pad-added",
 
1739
                          G_CALLBACK (brasero_metadata_new_decoded_pad_cb),
 
1740
                          self);
 
1741
 
 
1742
        gst_bin_add (GST_BIN (priv->pipeline), priv->decode);
 
1743
 
 
1744
        /* the two following objects don't always run */
 
1745
        priv->convert = gst_element_factory_make ("audioconvert", NULL);
 
1746
        if (!priv->convert) {
 
1747
                priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1748
                                           BRASERO_UTILS_ERROR_GENERAL,
 
1749
                                           _("%s element could not be created"),
 
1750
                                           "\"Audioconvert\"");
 
1751
                return FALSE;
 
1752
        }
 
1753
 
 
1754
        priv->sink = gst_element_factory_make ("fakesink", NULL);
 
1755
        if (priv->sink == NULL) {
 
1756
                priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1757
                                           BRASERO_UTILS_ERROR_GENERAL,
 
1758
                                           _("%s element could not be created"),
 
1759
                                           "\"Fakesink\"");
 
1760
                return FALSE;
 
1761
        }
 
1762
 
 
1763
        return TRUE;
 
1764
}
 
1765
 
 
1766
static gboolean
 
1767
brasero_metadata_set_new_uri (BraseroMetadata *self,
 
1768
                              const gchar *uri)
 
1769
{
 
1770
        BraseroMetadataPrivate *priv;
 
1771
        GstBus *bus;
 
1772
 
 
1773
        priv = BRASERO_METADATA_PRIVATE (self);
 
1774
 
 
1775
        BRASERO_UTILS_LOG ("New retrieval for %s %p", uri, self);
 
1776
 
 
1777
        if (priv->error) {
 
1778
                g_error_free (priv->error);
 
1779
                priv->error = NULL;
 
1780
        }
 
1781
 
 
1782
        brasero_metadata_info_free (priv->info);
 
1783
        priv->info = NULL;
 
1784
 
 
1785
        if (priv->silence) {
 
1786
                g_free (priv->silence);
 
1787
                priv->silence = NULL;
 
1788
        }
 
1789
 
 
1790
        priv->info = g_new0 (BraseroMetadataInfo, 1);
 
1791
        priv->info->uri = g_strdup (uri);
 
1792
 
 
1793
        if (priv->pipeline){
 
1794
                gst_element_set_state (priv->pipeline, GST_STATE_NULL);
 
1795
                if (priv->source) {
 
1796
                        gst_bin_remove (GST_BIN (priv->pipeline), priv->source);
 
1797
                        priv->source = NULL;
 
1798
                }
 
1799
 
 
1800
                if (priv->audio) {
 
1801
                        gst_bin_remove (GST_BIN (priv->pipeline), priv->audio);
 
1802
                        priv->audio = NULL;
 
1803
                }
 
1804
 
 
1805
                if (priv->video) {
 
1806
                        gst_bin_remove (GST_BIN (priv->pipeline), priv->video);
 
1807
                        priv->snapshot = NULL;
 
1808
                        priv->video = NULL;
 
1809
                }
 
1810
        }
 
1811
        else if (!brasero_metadata_create_pipeline (self))
 
1812
                return FALSE;
 
1813
 
 
1814
        if (!gst_uri_is_valid (uri))
 
1815
                return FALSE;
 
1816
 
 
1817
        priv->video_linked = 0;
 
1818
        priv->audio_linked = 0;
 
1819
        priv->snapshot_started = 0;
 
1820
 
 
1821
        /* create a necessary source */
 
1822
        priv->source = gst_element_make_from_uri (GST_URI_SRC,
 
1823
                                                  uri,
 
1824
                                                  NULL, NULL);
 
1825
        if (!priv->source) {
 
1826
                priv->error = g_error_new (BRASERO_UTILS_ERROR,
 
1827
                                           BRASERO_UTILS_ERROR_GENERAL,
 
1828
                                           "Can't create file source");
 
1829
                return FALSE;
 
1830
        }
 
1831
 
 
1832
        gst_bin_add (GST_BIN (priv->pipeline), priv->source);
 
1833
        gst_element_link (priv->source, priv->decode);
 
1834
 
 
1835
        /* apparently we need to reconnect to the bus every time */
 
1836
        if (priv->watch)
 
1837
                g_source_remove (priv->watch);
 
1838
 
 
1839
        bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
 
1840
        priv->watch = gst_bus_add_watch (bus,
 
1841
                                         (GstBusFunc) brasero_metadata_bus_messages,
 
1842
                                         self);
 
1843
        gst_object_unref (bus);
 
1844
 
 
1845
        return TRUE;
 
1846
}
 
1847
 
 
1848
gboolean
 
1849
brasero_metadata_set_uri (BraseroMetadata *self,
 
1850
                          BraseroMetadataFlag flags,
 
1851
                          const gchar *uri,
 
1852
                          GError **error)
 
1853
{
 
1854
        GstStateChangeReturn state_change;
 
1855
        BraseroMetadataPrivate *priv;
 
1856
 
 
1857
        priv = BRASERO_METADATA_PRIVATE (self);
 
1858
 
 
1859
        g_mutex_lock (priv->mutex);
 
1860
 
 
1861
        priv->flags = flags;
 
1862
        if (!brasero_metadata_set_new_uri (self, uri)) {
 
1863
                if (priv->error) {
 
1864
                        BRASERO_UTILS_LOG ("Failed to set new URI %s", priv->error->message);
 
1865
                        g_propagate_error (error, priv->error);
 
1866
                        priv->error = NULL;
 
1867
                }
 
1868
 
 
1869
                brasero_metadata_info_free (priv->info);
 
1870
                priv->info = NULL;
 
1871
 
 
1872
                g_mutex_unlock (priv->mutex);
 
1873
                return FALSE;
 
1874
        }
 
1875
 
 
1876
        priv->started = 1;
 
1877
        state_change = gst_element_set_state (GST_ELEMENT (priv->pipeline),
 
1878
                                              BRASERO_METADATA_INITIAL_STATE);
 
1879
 
 
1880
        g_mutex_unlock (priv->mutex);
 
1881
 
 
1882
        if (state_change == GST_STATE_CHANGE_FAILURE)
 
1883
                brasero_metadata_stop (self);
 
1884
 
 
1885
        return (state_change != GST_STATE_CHANGE_FAILURE);
 
1886
}
 
1887
 
 
1888
gboolean
 
1889
brasero_metadata_get_info_async (BraseroMetadata *self,
 
1890
                                 const gchar *uri,
 
1891
                                 BraseroMetadataFlag flags)
 
1892
{
 
1893
        BraseroMetadataPrivate *priv;
 
1894
        GstStateChangeReturn state_change;
 
1895
 
 
1896
        priv = BRASERO_METADATA_PRIVATE (self);
 
1897
 
 
1898
        priv->flags = flags;
 
1899
 
 
1900
        if (!brasero_metadata_set_new_uri (self, uri)) {
 
1901
                g_object_ref (self);
 
1902
                g_signal_emit (G_OBJECT (self),
 
1903
                               brasero_metadata_signals [COMPLETED_SIGNAL],
 
1904
                               0,
 
1905
                               priv->error);
 
1906
                g_object_unref (self);
 
1907
 
 
1908
                if (priv->error) {
 
1909
                        BRASERO_UTILS_LOG ("Failed to set new URI %s", priv->error->message);
 
1910
                        g_error_free (priv->error);
 
1911
                        priv->error = NULL;
 
1912
                }
 
1913
                return FALSE;
 
1914
        }
 
1915
 
 
1916
        state_change = gst_element_set_state (GST_ELEMENT (priv->pipeline),
 
1917
                                              BRASERO_METADATA_INITIAL_STATE);
 
1918
 
 
1919
        priv->started = (state_change != GST_STATE_CHANGE_FAILURE);
 
1920
        return priv->started;
 
1921
}
 
1922
 
 
1923
static void
 
1924
brasero_metadata_wait_cancelled (GCancellable *cancel,
 
1925
                                 GCond *condition)
 
1926
{
 
1927
        BRASERO_UTILS_LOG ("Thread waiting for retrieval end cancelled");
 
1928
        g_cond_broadcast (condition);
 
1929
}
 
1930
 
 
1931
void
 
1932
brasero_metadata_wait (BraseroMetadata *self,
 
1933
                       GCancellable *cancel)
 
1934
{
 
1935
        BraseroMetadataPrivate *priv;
 
1936
        GCond *condition;
 
1937
        gulong sig;
 
1938
 
 
1939
        priv = BRASERO_METADATA_PRIVATE (self);
 
1940
 
 
1941
        BRASERO_UTILS_LOG ("Metadata lock and wait %p", self);
 
1942
 
 
1943
        g_mutex_lock (priv->mutex);
 
1944
 
 
1945
        if (!priv->started) {
 
1946
                /* Maybe we were waiting for the lock which was held by the 
 
1947
                 * finish function. That's why we check if it didn't finish in
 
1948
                 * the mean time. */
 
1949
                g_mutex_unlock (priv->mutex);
 
1950
                return;
 
1951
        }
 
1952
 
 
1953
        condition = g_cond_new ();
 
1954
        priv->conditions = g_slist_prepend (priv->conditions, condition);
 
1955
 
 
1956
        sig = g_signal_connect (cancel,
 
1957
                                "cancelled",
 
1958
                                G_CALLBACK (brasero_metadata_wait_cancelled),
 
1959
                                condition);
 
1960
 
 
1961
        if (!g_cancellable_is_cancelled (cancel))
 
1962
                g_cond_wait (condition, priv->mutex);
 
1963
 
 
1964
        priv->conditions = g_slist_remove (priv->conditions, condition);
 
1965
        g_cond_free (condition);
 
1966
 
 
1967
        g_mutex_unlock (priv->mutex);
 
1968
 
 
1969
        g_signal_handler_disconnect (cancel, sig);
 
1970
}
 
1971
 
 
1972
void
 
1973
brasero_metadata_increase_listener_number (BraseroMetadata *self)
 
1974
{
 
1975
        BraseroMetadataPrivate *priv;
 
1976
 
 
1977
        priv = BRASERO_METADATA_PRIVATE (self);
 
1978
        g_atomic_int_inc (&priv->listeners);
 
1979
}
 
1980
 
 
1981
gboolean
 
1982
brasero_metadata_decrease_listener_number (BraseroMetadata *self)
 
1983
{
 
1984
        BraseroMetadataPrivate *priv;
 
1985
 
 
1986
        priv = BRASERO_METADATA_PRIVATE (self);
 
1987
        return g_atomic_int_dec_and_test (&priv->listeners);
 
1988
}
 
1989
 
 
1990
const gchar *
 
1991
brasero_metadata_get_uri (BraseroMetadata *self)
 
1992
{
 
1993
        BraseroMetadataPrivate *priv;
 
1994
 
 
1995
        priv = BRASERO_METADATA_PRIVATE (self);
 
1996
        return priv->info?priv->info->uri:NULL;
 
1997
}
 
1998
 
 
1999
BraseroMetadataFlag
 
2000
brasero_metadata_get_flags (BraseroMetadata *self)
 
2001
{
 
2002
        BraseroMetadataPrivate *priv;
 
2003
 
 
2004
        priv = BRASERO_METADATA_PRIVATE (self);
 
2005
        return priv->flags;
 
2006
}
 
2007
 
 
2008
gboolean
 
2009
brasero_metadata_get_result (BraseroMetadata *self,
 
2010
                             BraseroMetadataInfo *info,
 
2011
                             GError **error)
 
2012
{
 
2013
        BraseroMetadataPrivate *priv;
 
2014
 
 
2015
        priv = BRASERO_METADATA_PRIVATE (self);
 
2016
 
 
2017
        if (priv->error) {
 
2018
                if (error)
 
2019
                        *error = g_error_copy (priv->error);
 
2020
 
 
2021
                return FALSE;
 
2022
        }
 
2023
 
 
2024
        if (!priv->info)
 
2025
                return FALSE;
 
2026
 
 
2027
        if (priv->started)
 
2028
                return FALSE;
 
2029
 
 
2030
        memset (info, 0, sizeof (BraseroMetadataInfo));
 
2031
        brasero_metadata_info_copy (info, priv->info);
 
2032
        return TRUE;
 
2033
}
 
2034
 
 
2035
static void
 
2036
brasero_metadata_init (BraseroMetadata *obj)
 
2037
{
 
2038
        BraseroMetadataPrivate *priv;
 
2039
 
 
2040
        priv = BRASERO_METADATA_PRIVATE (obj);
 
2041
 
 
2042
        priv->mutex = g_mutex_new ();
 
2043
}
 
2044
 
 
2045
static void
 
2046
brasero_metadata_finalize (GObject *object)
 
2047
{
 
2048
        BraseroMetadataPrivate *priv;
 
2049
        GSList *iter;
 
2050
 
 
2051
        priv = BRASERO_METADATA_PRIVATE (object);
 
2052
 
 
2053
        brasero_metadata_destroy_pipeline (BRASERO_METADATA (object));
 
2054
 
 
2055
        if (priv->silence) {
 
2056
                g_free (priv->silence);
 
2057
                priv->silence = NULL;
 
2058
        }
 
2059
 
 
2060
        if (priv->error) {
 
2061
                g_error_free (priv->error);
 
2062
                priv->error = NULL;
 
2063
        }
 
2064
 
 
2065
        if (priv->watch) {
 
2066
                g_source_remove (priv->watch);
 
2067
                priv->watch = 0;
 
2068
        }
 
2069
 
 
2070
        if (priv->info) {
 
2071
                brasero_metadata_info_free (priv->info);
 
2072
                priv->info = NULL;
 
2073
        }
 
2074
 
 
2075
        for (iter = priv->conditions; iter; iter = iter->next) {
 
2076
                GCond *condition;
 
2077
 
 
2078
                condition = iter->data;
 
2079
                g_cond_broadcast (condition);
 
2080
                g_cond_free (condition);
 
2081
        }
 
2082
        g_slist_free (priv->conditions);
 
2083
        priv->conditions = NULL;
 
2084
 
 
2085
        if (priv->mutex) {
 
2086
                g_mutex_free (priv->mutex);
 
2087
                priv->mutex = NULL;
 
2088
        }
 
2089
 
 
2090
        G_OBJECT_CLASS (brasero_metadata_parent_class)->finalize (object);
 
2091
}
 
2092
 
 
2093
static void
 
2094
brasero_metadata_get_property (GObject *obj,
 
2095
                               guint prop_id,
 
2096
                               GValue *value,
 
2097
                               GParamSpec *pspec)
 
2098
{
 
2099
        gchar *uri;
 
2100
        BraseroMetadata *self;
 
2101
        BraseroMetadataPrivate *priv;
 
2102
 
 
2103
        self = BRASERO_METADATA (obj);
 
2104
        priv = BRASERO_METADATA_PRIVATE (self);
 
2105
 
 
2106
        switch (prop_id) {
 
2107
        case PROP_URI:
 
2108
                g_object_get (G_OBJECT (priv->source), "location",
 
2109
                              &uri, NULL);
 
2110
                g_value_set_string (value, uri);
 
2111
                g_free (uri);
 
2112
                break;
 
2113
        default:
 
2114
                G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
 
2115
                break;
 
2116
        }
 
2117
}
 
2118
 
 
2119
static void
 
2120
brasero_metadata_set_property (GObject *obj,
 
2121
                               guint prop_id,
 
2122
                               const GValue *value,
 
2123
                               GParamSpec *pspec)
 
2124
{
 
2125
        const gchar *uri;
 
2126
        BraseroMetadata *self;
 
2127
        BraseroMetadataPrivate *priv;
 
2128
 
 
2129
        self = BRASERO_METADATA (obj);
 
2130
        priv = BRASERO_METADATA_PRIVATE (self);
 
2131
 
 
2132
        switch (prop_id) {
 
2133
        case PROP_URI:
 
2134
                uri = g_value_get_string (value);
 
2135
                gst_element_set_state (GST_ELEMENT (priv->pipeline), GST_STATE_NULL);
 
2136
                if (priv->source)
 
2137
                        g_object_set (G_OBJECT (priv->source),
 
2138
                                      "location", uri,
 
2139
                                      NULL);
 
2140
                gst_element_set_state (GST_ELEMENT (priv->pipeline), GST_STATE_PAUSED);
 
2141
                priv->started = 1;
 
2142
                break;
 
2143
        default:
 
2144
                G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
 
2145
                break;
 
2146
        }
 
2147
}
 
2148
 
 
2149
static void
 
2150
brasero_metadata_class_init (BraseroMetadataClass *klass)
 
2151
{
 
2152
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
2153
 
 
2154
        g_type_class_add_private (klass, sizeof (BraseroMetadataPrivate));
 
2155
 
 
2156
        object_class->finalize = brasero_metadata_finalize;
 
2157
        object_class->set_property = brasero_metadata_set_property;
 
2158
        object_class->get_property = brasero_metadata_get_property;
 
2159
 
 
2160
        brasero_metadata_signals[COMPLETED_SIGNAL] =
 
2161
            g_signal_new ("completed",
 
2162
                          G_TYPE_FROM_CLASS (klass),
 
2163
                          G_SIGNAL_RUN_LAST,
 
2164
                          G_STRUCT_OFFSET (BraseroMetadataClass,
 
2165
                                           completed),
 
2166
                          NULL, NULL,
 
2167
                          g_cclosure_marshal_VOID__POINTER,
 
2168
                          G_TYPE_NONE,
 
2169
                          1,
 
2170
                          G_TYPE_POINTER);
 
2171
        g_object_class_install_property (object_class,
 
2172
                                         PROP_URI,
 
2173
                                         g_param_spec_string ("uri",
 
2174
                                                              "The uri of the song",
 
2175
                                                              "The uri of the song",
 
2176
                                                              NULL,
 
2177
                                                              G_PARAM_READWRITE));
 
2178
}
 
2179
 
 
2180
BraseroMetadata *
 
2181
brasero_metadata_new (void)
 
2182
{
 
2183
        return BRASERO_METADATA (g_object_new (BRASERO_TYPE_METADATA, NULL));
 
2184
}