Commit | Line | Data |
---|---|---|
f8a3bdc8 MW |
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 | ||
f8a3bdc8 MW |
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. | |
27 | */ | |
28 | #undef attribute | |
29 | ||
30 | #include <glib.h> | |
31 | #include <gst/gst.h> | |
5dcfc065 | 32 | #include <gst/pbutils/gstdiscoverer.h> |
f8a3bdc8 MW |
33 | |
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. | |
37 | */ | |
38 | #ifdef __GNUC__ | |
39 | # define UNUSED __attribute__((unused)) | |
40 | #endif | |
41 | ||
42 | #define END ((void *)0) | |
43 | ||
5dcfc065 | 44 | static GstDiscoverer *disco = 0; |
f8a3bdc8 | 45 | |
5dcfc065 MW |
46 | long disorder_tracklength(const char UNUSED *track, const char *path) { |
47 | GError *err = 0; | |
48 | gchar *dir = 0, *abs = 0, *uri = 0; | |
49 | GstDiscovererInfo *info = 0; | |
50 | long length = -1; | |
51 | GstClockTime t; | |
f8a3bdc8 | 52 | |
5dcfc065 | 53 | if (!path) goto end; |
f8a3bdc8 | 54 | |
5dcfc065 MW |
55 | if(!disco) { |
56 | gst_init(0, 0); | |
57 | disco = gst_discoverer_new(5*GST_SECOND, &err); if(!disco) goto end; | |
f8a3bdc8 | 58 | } |
f8a3bdc8 | 59 | |
5dcfc065 MW |
60 | if (g_path_is_absolute(path)) |
61 | uri = g_filename_to_uri(path, 0, &err); | |
62 | else { | |
63 | dir = g_get_current_dir(); | |
64 | abs = g_build_filename(dir, path, END); | |
65 | uri = g_filename_to_uri(abs, 0, &err); | |
f8a3bdc8 | 66 | } |
5dcfc065 | 67 | if(!uri) goto end; |
f8a3bdc8 | 68 | |
5dcfc065 MW |
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); | |
83f5e5c2 | 73 | length = (t + GST_SECOND/2)/GST_SECOND; |
f8a3bdc8 | 74 | break; |
5dcfc065 MW |
75 | case GST_DISCOVERER_TIMEOUT: |
76 | disorder_info("discovery timed out probing `%s'", path); | |
77 | goto swallow_error; | |
78 | case GST_DISCOVERER_MISSING_PLUGINS: | |
79 | disorder_info("unrecognized file `%s' (missing plugins?)", path); | |
80 | goto swallow_error; | |
81 | swallow_error: | |
82 | if(err) { g_error_free(err); err = 0; } | |
83 | length = 0; | |
f8a3bdc8 MW |
84 | break; |
85 | default: | |
f8a3bdc8 MW |
86 | goto end; |
87 | } | |
f8a3bdc8 MW |
88 | |
89 | end: | |
5dcfc065 MW |
90 | if(err) { |
91 | disorder_error(0, "error probing `%s': %s", path, err->message); | |
92 | g_error_free(err); | |
f8a3bdc8 | 93 | } |
5dcfc065 MW |
94 | if(info) gst_discoverer_info_unref(info); |
95 | g_free(dir); g_free(abs); g_free(uri); | |
f8a3bdc8 MW |
96 | return length; |
97 | } | |
98 | ||
99 | #ifdef STANDALONE | |
100 | int main(int argc, char *argv[]) { | |
101 | int i; | |
102 | ||
103 | for(i = 1; i < argc; i++) | |
104 | printf("%s: %ld\n", argv[i], disorder_tracklength(0, argv[i])); | |
105 | return (0); | |
106 | } | |
107 | #endif | |
108 | ||
109 | /* | |
110 | Local Variables: | |
111 | c-basic-offset:2 | |
112 | comment-column:40 | |
113 | fill-column:79 | |
114 | End: | |
115 | */ |