2 * This file is part of DisOrder.
3 * Copyright (C) 2018 Mark Wooding
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file plugins/tracklength-gstreamer.c
19 * @brief Plugin to compute track lengths using GStreamer
21 #include "tracklength.h"
23 /* Ugh. It turns out that libxml tries to define a function called
24 * `attribute', and it's included by GStreamer for some unimaginable reason.
25 * So undefine it here. We'll want GCC attributes for special effects, but
26 * can take care of ourselves.
32 #include <gst/pbutils/gstdiscoverer.h>
34 /* The only application we have for `attribute' is declaring function
35 * arguments as being unused, because we have a lot of callback functions
36 * which are meant to comply with an externally defined interface.
39 # define UNUSED __attribute__((unused))
42 #define END ((void *)0)
44 static GstDiscoverer
*disco
= 0;
46 long disorder_tracklength(const char UNUSED
*track
, const char *path
) {
48 gchar
*dir
= 0, *abs
= 0, *uri
= 0;
49 GstDiscovererInfo
*info
= 0;
57 disco
= gst_discoverer_new(5*GST_SECOND
, &err
); if(!disco
) goto end
;
60 if (g_path_is_absolute(path
))
61 uri
= g_filename_to_uri(path
, 0, &err
);
63 dir
= g_get_current_dir();
64 abs
= g_build_filename(dir
, path
, END
);
65 uri
= g_filename_to_uri(abs
, 0, &err
);
69 info
= gst_discoverer_discover_uri(disco
, uri
, &err
); if(!info
) goto end
;
70 switch(gst_discoverer_info_get_result(info
)) {
71 case GST_DISCOVERER_OK
:
72 t
= gst_discoverer_info_get_duration(info
);
73 length
= (t
+ GST_SECOND
/2)/GST_SECOND
;
75 case GST_DISCOVERER_TIMEOUT
:
76 disorder_info("discovery timed out probing `%s'", path
);
78 case GST_DISCOVERER_MISSING_PLUGINS
:
79 disorder_info("unrecognized file `%s' (missing plugins?)", path
);
82 if(err
) { g_error_free(err
); err
= 0; }
91 disorder_error(0, "error probing `%s': %s", path
, err
->message
);
94 if(info
) gst_discoverer_info_unref(info
);
95 g_free(dir
); g_free(abs
); g_free(uri
);
100 int main(int argc
, char *argv
[]) {
103 for(i
= 1; i
< argc
; i
++)
104 printf("%s: %ld\n", argv
[i
], disorder_tracklength(0, argv
[i
]));