chiark / gitweb /
plugins/Makefile.am: Build standalone tracklength program.
[disorder] / plugins / tracklength-gstreamer.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2018 Mark Wooding
4  *
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.
9  *
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.
14  * 
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/>.
17  */
18 /** @file plugins/tracklength-gstreamer.c
19  * @brief Plugin to compute track lengths using GStreamer
20  */
21 #include "tracklength.h"
22
23 #include "speaker-protocol.h"
24
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.
29  */
30 #undef attribute
31
32 #include <glib.h>
33 #include <gst/gst.h>
34 #include <gst/pbutils/gstdiscoverer.h>
35
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.
39  */
40 #ifdef __GNUC__
41 #  define UNUSED __attribute__((unused))
42 #endif
43
44 #define END ((void *)0)
45
46 static GstDiscoverer *disco = 0;
47
48 long disorder_tracklength(const char UNUSED *track, const char *path) {
49   GError *err = 0;
50   gchar *dir = 0, *abs = 0, *uri = 0;
51   GstDiscovererInfo *info = 0;
52   long length = -1;
53   GstClockTime t;
54
55   if (!path) goto end;
56
57   if(!disco) {
58     gst_init(0, 0);
59     disco = gst_discoverer_new(5*GST_SECOND, &err); if(!disco) goto end;
60   }
61
62   if (g_path_is_absolute(path))
63     uri = g_filename_to_uri(path, 0, &err);
64   else {
65     dir = g_get_current_dir();
66     abs = g_build_filename(dir, path, END);
67     uri = g_filename_to_uri(abs, 0, &err);
68   }
69   if(!uri) goto end;
70
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 + 500000000)/1000000000;
76     break;
77   case GST_DISCOVERER_TIMEOUT:
78     disorder_info("discovery timed out probing `%s'", path);
79     goto swallow_error;
80   case GST_DISCOVERER_MISSING_PLUGINS:
81     disorder_info("unrecognized file `%s' (missing plugins?)", path);
82     goto swallow_error;
83   swallow_error:
84     if(err) { g_error_free(err); err = 0; }
85     length = 0;
86     break;
87   default:
88     goto end;
89   }
90
91 end:
92   if(err) {
93     disorder_error(0, "error probing `%s': %s", path, err->message);
94     g_error_free(err);
95   }
96   if(info) gst_discoverer_info_unref(info);
97   g_free(dir); g_free(abs); g_free(uri);
98   return length;
99 }
100
101 #ifdef STANDALONE
102 int main(int argc, char *argv[]) {
103   int i;
104
105   for(i = 1; i < argc; i++)
106     printf("%s: %ld\n", argv[i], disorder_tracklength(0, argv[i]));
107   return (0);
108 }
109 #endif
110
111 /*
112 Local Variables:
113 c-basic-offset:2
114 comment-column:40
115 fill-column:79
116 End:
117 */