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 #include "speaker-protocol.h"
25 /* Ugh. It turns out that libxml tries to define a function called
26 * `attribute', and it's included by GStreamer for some unimaginable reason.
27 * So undefine it here. We'll want GCC attributes for special effects, but
28 * can take care of ourselves.
34 #include <gst/pbutils/gstdiscoverer.h>
36 /* The only application we have for `attribute' is declaring function
37 * arguments as being unused, because we have a lot of callback functions
38 * which are meant to comply with an externally defined interface.
41 # define UNUSED __attribute__((unused))
44 #define END ((void *)0)
46 static GstDiscoverer
*disco
= 0;
48 long disorder_tracklength(const char UNUSED
*track
, const char *path
) {
50 gchar
*dir
= 0, *abs
= 0, *uri
= 0;
51 GstDiscovererInfo
*info
= 0;
59 disco
= gst_discoverer_new(5*GST_SECOND
, &err
); if(!disco
) goto end
;
62 if (g_path_is_absolute(path
))
63 uri
= g_filename_to_uri(path
, 0, &err
);
65 dir
= g_get_current_dir();
66 abs
= g_build_filename(dir
, path
, END
);
67 uri
= g_filename_to_uri(abs
, 0, &err
);
71 info
= gst_discoverer_discover_uri(disco
, uri
, &err
); if(!info
) goto end
;
72 switch(gst_discoverer_info_get_result(info
)) {
73 case GST_DISCOVERER_OK
:
74 t
= gst_discoverer_info_get_duration(info
);
75 length
= (t
+ GST_SECOND
/2)/GST_SECOND
;
77 case GST_DISCOVERER_TIMEOUT
:
78 disorder_info("discovery timed out probing `%s'", path
);
80 case GST_DISCOVERER_MISSING_PLUGINS
:
81 disorder_info("unrecognized file `%s' (missing plugins?)", path
);
84 if(err
) { g_error_free(err
); err
= 0; }
93 disorder_error(0, "error probing `%s': %s", path
, err
->message
);
96 if(info
) gst_discoverer_info_unref(info
);
97 g_free(dir
); g_free(abs
); g_free(uri
);
102 int main(int argc
, char *argv
[]) {
105 for(i
= 1; i
< argc
; i
++)
106 printf("%s: %ld\n", argv
[i
], disorder_tracklength(0, argv
[i
]));