Mercurial > audlegacy-plugins
annotate src/wma/wma.c @ 1404:ab7d89bb8fcb
Switched to new threading model
author | Christian Birchinger <joker@netswarm.net> |
---|---|
date | Sat, 04 Aug 2007 21:37:11 +0200 |
parents | 761e17b23e0c |
children | a25c2cfcce83 |
rev | line source |
---|---|
878 | 1 /* |
2 * Audacious WMA input plugin | |
1072 | 3 * (C) 2005, 2006, 2007 Audacious development team |
878 | 4 * |
5 * Based on: | |
6 * xmms-wma - WMA player for BMP | |
7 * Copyright (C) 2004,2005 McMCC <mcmcc@mail.ru> | |
8 * bmp-wma - WMA player for BMP | |
9 * Copyright (C) 2004 Roman Bogorodskiy <bogorodskiy@inbox.ru> | |
10 * | |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
24 */ | |
25 #define _XOPEN_SOURCE 600 | |
26 #include <stdlib.h> | |
27 #include <unistd.h> | |
28 #include <math.h> | |
29 #include <stdbool.h> | |
30 #include <stdio.h> | |
31 #include <string.h> | |
32 #include <strings.h> | |
33 #include <glib.h> | |
34 | |
35 #include <audacious/plugin.h> | |
36 #include <audacious/output.h> | |
37 #include <audacious/util.h> | |
38 #include <audacious/titlestring.h> | |
39 #include <audacious/vfs.h> | |
40 #include <audacious/strings.h> | |
41 #include <audacious/i18n.h> | |
42 | |
43 #include "avcodec.h" | |
44 #include "avformat.h" | |
45 | |
46 #define ABOUT_TXT "Adapted for use in audacious by Tony Vroon (chainsaw@gentoo.org) from\n \ | |
47 the BEEP-WMA plugin which is Copyright (C) 2004,2005 Mokrushin I.V. aka McMCC (mcmcc@mail.ru)\n \ | |
48 and the BMP-WMA plugin which is Copyright (C) 2004 Roman Bogorodskiy <bogorodskiy@inbox.ru>.\n \ | |
49 This plugin based on source code " LIBAVCODEC_IDENT "\nby Fabrice Bellard from \ | |
50 http://ffmpeg.sourceforge.net.\n\n \ | |
51 This program is free software; you can redistribute it and/or modify \n \ | |
52 it under the terms of the GNU General Public License as published by \n \ | |
53 the Free Software Foundation; either version 2 of the License, or \n \ | |
54 (at your option) any later version. \n\n \ | |
55 This program is distributed in the hope that it will be useful, \n \ | |
56 but WITHOUT ANY WARRANTY; without even the implied warranty of \n \ | |
57 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. \n \ | |
58 See the GNU General Public License for more details.\n" | |
59 #define PLUGIN_NAME "Audacious-WMA" | |
60 #define PLUGIN_VERSION "v.1.0.5" | |
61 #define ST_BUFF 1024 | |
62 | |
63 static int wma_decode = 0; | |
64 static gboolean wma_pause = 0; | |
65 static int wma_seekpos = -1; | |
66 static int wma_st_buff, wma_idx, wma_idx2; | |
67 static GThread *wma_decode_thread; | |
68 GStaticMutex wma_mutex = G_STATIC_MUTEX_INIT; | |
69 static AVCodecContext *c = NULL; | |
70 static AVFormatContext *ic = NULL; | |
71 static AVCodecContext *c2 = NULL; | |
72 static AVFormatContext *ic2 = NULL; | |
73 static uint8_t *wma_outbuf, *wma_s_outbuf; | |
74 | |
75 char description[64]; | |
76 static void wma_about(void); | |
77 static void wma_init(void); | |
78 static int wma_is_our_file(char *filename); | |
79 static int wma_is_our_fd(char *filename, VFSFile *fd); | |
80 static void wma_play_file(InputPlayback *data); | |
81 static void wma_stop(InputPlayback *data); | |
82 static void wma_seek(InputPlayback *data, int time); | |
83 static void wma_do_pause(InputPlayback *data, short p); | |
84 static void wma_get_song_info(char *filename, char **title, int *length); | |
85 static TitleInput *wma_get_song_tuple(char *filename); | |
86 static char *wsong_title; | |
87 static int wsong_time; | |
88 | |
89 static GtkWidget *dialog1, *button1, *label1; | |
1072 | 90 static gchar *fmts[] = { "wma", NULL }; |
878 | 91 |
92 InputPlugin wma_ip = | |
93 { | |
1072 | 94 .description = "Windows Media Audio (WMA) Plugin", |
95 .init = wma_init, | |
96 .about = wma_about, | |
97 .is_our_file = wma_is_our_file, | |
98 .play_file = wma_play_file, | |
99 .stop = wma_stop, | |
100 .pause = wma_do_pause, | |
101 .seek = wma_seek, | |
102 .get_song_info = wma_get_song_info, | |
103 .get_song_tuple = wma_get_song_tuple, | |
104 .is_our_file_from_vfs = wma_is_our_fd, | |
105 .vfs_extensions = fmts, | |
878 | 106 }; |
107 | |
1072 | 108 InputPlugin *wma_iplist[] = { &wma_ip, NULL }; |
109 | |
1395
761e17b23e0c
added Discovery plugin type
Cristi Magherusan <majeru@atheme-project.org>
parents:
1390
diff
changeset
|
110 DECLARE_PLUGIN(wma, NULL, NULL, wma_iplist, NULL, NULL, NULL, NULL, NULL); |
1072 | 111 |
878 | 112 static gchar *str_twenty_to_space(gchar * str) |
113 { | |
114 gchar *match, *match_end; | |
115 | |
116 g_return_val_if_fail(str != NULL, NULL); | |
117 | |
118 while ((match = strstr(str, "%20"))) { | |
119 match_end = match + 3; | |
120 *match++ = ' '; | |
121 while (*match_end) | |
122 *match++ = *match_end++; | |
123 *match = 0; | |
124 } | |
125 | |
126 return str; | |
127 } | |
128 | |
129 static void wma_about(void) | |
130 { | |
131 char *title; | |
132 char *message; | |
133 | |
134 if (dialog1) return; | |
135 | |
136 title = (char *)g_malloc(80); | |
137 message = (char *)g_malloc(1000); | |
138 memset(title, 0, 80); | |
139 memset(message, 0, 1000); | |
140 | |
141 sprintf(title, _("About %s"), PLUGIN_NAME); | |
142 sprintf(message, "%s %s\n\n%s", PLUGIN_NAME, PLUGIN_VERSION, ABOUT_TXT); | |
143 | |
144 dialog1 = gtk_dialog_new(); | |
145 g_signal_connect(G_OBJECT(dialog1), "destroy", | |
146 G_CALLBACK(gtk_widget_destroyed), &dialog1); | |
147 gtk_window_set_title(GTK_WINDOW(dialog1), title); | |
148 gtk_window_set_policy(GTK_WINDOW(dialog1), FALSE, FALSE, FALSE); | |
149 gtk_container_border_width(GTK_CONTAINER(dialog1), 5); | |
150 label1 = gtk_label_new(message); | |
151 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog1)->vbox), label1, TRUE, TRUE, 0); | |
152 gtk_widget_show(label1); | |
153 | |
154 button1 = gtk_button_new_with_label(_(" Close ")); | |
155 g_signal_connect_swapped(G_OBJECT(button1), "clicked", | |
156 G_CALLBACK(gtk_widget_destroy), | |
157 GTK_OBJECT(dialog1)); | |
158 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog1)->action_area), button1, | |
159 FALSE, FALSE, 0); | |
160 | |
161 gtk_widget_show(button1); | |
162 gtk_widget_show(dialog1); | |
163 gtk_widget_grab_focus(button1); | |
164 g_free(title); | |
165 g_free(message); | |
166 } | |
167 | |
168 static void wma_init(void) | |
169 { | |
170 avcodec_init(); | |
171 avcodec_register_all(); | |
172 av_register_all(); | |
173 } | |
174 | |
175 static int wma_is_our_file(char *filename) | |
176 { | |
177 AVCodec *codec2; | |
178 | |
179 if(av_open_input_file(&ic2, str_twenty_to_space(filename), NULL, 0, NULL) < 0) return 0; | |
180 | |
181 for(wma_idx2 = 0; wma_idx2 < ic2->nb_streams; wma_idx2++) { | |
182 c2 = &ic2->streams[wma_idx2]->codec; | |
183 if(c2->codec_type == CODEC_TYPE_AUDIO) break; | |
184 } | |
185 | |
186 av_find_stream_info(ic2); | |
187 | |
188 codec2 = avcodec_find_decoder(c2->codec_id); | |
189 | |
190 if(!codec2) { | |
191 av_close_input_file(ic2); | |
192 return 0; | |
193 } | |
194 | |
195 av_close_input_file(ic2); | |
196 return 1; | |
197 } | |
198 | |
199 static int wma_is_our_fd(char *filename, VFSFile *fd) | |
200 { | |
201 AVCodec *codec2; | |
202 | |
203 if(av_open_input_vfsfile(&ic2, filename, fd, NULL, 0, NULL) < 0) return 0; | |
204 | |
205 for(wma_idx2 = 0; wma_idx2 < ic2->nb_streams; wma_idx2++) { | |
206 c2 = &ic2->streams[wma_idx2]->codec; | |
207 if(c2->codec_type == CODEC_TYPE_AUDIO) break; | |
208 } | |
209 | |
210 av_find_stream_info(ic2); | |
211 | |
212 codec2 = avcodec_find_decoder(c2->codec_id); | |
213 | |
1156 | 214 if (!codec2) { |
215 av_close_input_vfsfile(ic2); | |
216 return 0; | |
217 } | |
218 | |
219 av_close_input_vfsfile(ic2); | |
878 | 220 return 1; |
221 } | |
222 | |
223 static void wma_do_pause(InputPlayback *playback, short p) | |
224 { | |
225 wma_pause = p; | |
226 playback->output->pause(wma_pause); | |
227 } | |
228 | |
229 static void wma_seek(InputPlayback *playback, int time) | |
230 { | |
231 wma_seekpos = time; | |
232 if(wma_pause) playback->output->pause(0); | |
233 while(wma_decode && wma_seekpos!=-1) xmms_usleep(10000); | |
234 if(wma_pause) playback->output->pause(1); | |
235 } | |
236 | |
237 static gchar *extname(const char *filename) | |
238 { | |
239 gchar *ext = strrchr(filename, '.'); | |
240 if(ext != NULL) ++ext; | |
241 return ext; | |
242 } | |
243 | |
244 static TitleInput *wma_get_song_tuple(gchar * filename) | |
245 { | |
246 TitleInput *tuple = NULL; | |
247 AVFormatContext *in = NULL; | |
248 gchar *filename_proxy = g_strdup(filename); | |
249 | |
250 if (av_open_input_file(&in, str_twenty_to_space(filename), NULL, 0, NULL) < 0) | |
251 return NULL; | |
252 | |
253 tuple = bmp_title_input_new(); | |
254 | |
255 tuple->file_name = g_path_get_basename(filename_proxy); | |
256 tuple->file_path = g_path_get_dirname(filename_proxy); | |
257 tuple->file_ext = extname(filename_proxy); | |
258 | |
259 av_find_stream_info(in); | |
260 | |
895
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
261 if(strlen(in->title)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
262 tuple->track_name = strdup(in->title); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
263 if(strlen(in->author)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
264 tuple->performer = strdup(in->author); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
265 if(strlen(in->album)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
266 tuple->album_name = strdup(in->album); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
267 if(strlen(in->comment)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
268 tuple->comment = strdup(in->comment); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
269 if(strlen(in->genre)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
270 tuple->genre = strdup(in->genre); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
271 if(in->year > 0) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
272 tuple->year = in->year; |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
273 if(in->track > 0) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
274 tuple->track_number = in->track; |
878 | 275 if (in->duration) |
276 tuple->length = in->duration / 1000; | |
277 | |
278 av_close_input_file(in); | |
279 | |
280 return tuple; | |
281 } | |
282 | |
283 static gchar *get_song_title(AVFormatContext *in, gchar * filename) | |
284 { | |
285 gchar *ret = NULL; | |
286 TitleInput *input; | |
287 | |
288 input = bmp_title_input_new(); | |
289 | |
895
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
290 if(strlen(in->title)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
291 input->track_name = strdup(in->title); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
292 if(strlen(in->author)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
293 input->performer = strdup(in->author); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
294 if(strlen(in->album)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
295 input->album_name = strdup(in->album); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
296 if(strlen(in->comment)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
297 input->comment = strdup(in->comment); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
298 if(strlen(in->genre)) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
299 input->genre = strdup(in->genre); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
300 if(in->year > 0) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
301 input->year = in->year; |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
302 if(in->track > 0) |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
303 input->track_number = in->track; |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
304 |
878 | 305 input->file_name = g_path_get_basename(filename); |
306 input->file_path = g_path_get_dirname(filename); | |
307 input->file_ext = extname(filename); | |
308 ret = xmms_get_titlestring(xmms_get_gentitle_format(), input); | |
309 if(input) g_free(input); | |
310 | |
311 if(!ret) | |
312 { | |
313 ret = g_strdup(input->file_name); | |
314 if (extname(ret) != NULL) | |
315 *(extname(ret) - 1) = '\0'; | |
316 } | |
317 return ret; | |
318 } | |
319 | |
320 static guint get_song_time(AVFormatContext *in) | |
321 { | |
322 if(in->duration) | |
323 return in->duration/1000; | |
324 else | |
325 return 0; | |
326 } | |
327 | |
328 static void wma_get_song_info(char *filename, char **title_real, int *len_real) | |
329 { | |
330 TitleInput *tuple = wma_get_song_tuple(filename); | |
331 | |
332 if (tuple == NULL) | |
333 return; | |
334 | |
335 (*len_real) = tuple->length; | |
336 (*title_real) = xmms_get_titlestring(xmms_get_gentitle_format(), tuple); | |
337 } | |
338 | |
339 static void wma_playbuff(InputPlayback *playback, int out_size) | |
340 { | |
341 FifoBuffer f; | |
342 int sst_buff; | |
343 | |
344 fifo_init(&f, out_size*2); | |
345 fifo_write(&f, wma_outbuf, out_size, &f.wptr); | |
346 while(!fifo_read(&f, wma_s_outbuf, wma_st_buff, &f.rptr) && wma_decode) | |
347 { | |
348 sst_buff = wma_st_buff; | |
349 if(wma_pause) memset(wma_s_outbuf, 0, sst_buff); | |
350 while(playback->output->buffer_free() < wma_st_buff) xmms_usleep(20000); | |
351 produce_audio(playback->output->written_time(), FMT_S16_NE, | |
352 c->channels, sst_buff, (short *)wma_s_outbuf, NULL); | |
353 memset(wma_s_outbuf, 0, sst_buff); | |
354 } | |
355 fifo_free(&f); | |
356 return; | |
357 } | |
358 | |
359 static void *wma_play_loop(void *arg) | |
360 { | |
361 InputPlayback *playback = arg; | |
362 uint8_t *inbuf_ptr; | |
363 int out_size, size, len; | |
364 AVPacket pkt; | |
365 | |
366 g_static_mutex_lock(&wma_mutex); | |
1390
095595555e7b
Fix WMA playback.
William Pitcock <nenolod@atheme-project.org>
parents:
1190
diff
changeset
|
367 while(playback->playing) |
095595555e7b
Fix WMA playback.
William Pitcock <nenolod@atheme-project.org>
parents:
1190
diff
changeset
|
368 { |
878 | 369 if(wma_seekpos != -1) |
370 { | |
371 av_seek_frame(ic, wma_idx, wma_seekpos * 1000000LL); | |
372 playback->output->flush(wma_seekpos * 1000); | |
373 wma_seekpos = -1; | |
374 } | |
375 | |
376 if(av_read_frame(ic, &pkt) < 0) break; | |
377 | |
378 size = pkt.size; | |
379 inbuf_ptr = pkt.data; | |
380 | |
381 if(size == 0) break; | |
382 | |
383 while(size > 0){ | |
384 len = avcodec_decode_audio(c, (short *)wma_outbuf, &out_size, | |
385 inbuf_ptr, size); | |
386 if(len < 0) break; | |
387 | |
388 if(out_size <= 0) continue; | |
389 | |
390 wma_playbuff(playback, out_size); | |
391 | |
392 size -= len; | |
393 inbuf_ptr += len; | |
394 if(pkt.data) av_free_packet(&pkt); | |
395 } | |
396 } | |
1390
095595555e7b
Fix WMA playback.
William Pitcock <nenolod@atheme-project.org>
parents:
1190
diff
changeset
|
397 while(playback->playing && playback->output->buffer_playing()) xmms_usleep(30000); |
1168 | 398 playback->playing = 0; |
878 | 399 if(wma_s_outbuf) g_free(wma_s_outbuf); |
400 if(wma_outbuf) g_free(wma_outbuf); | |
401 if(pkt.data) av_free_packet(&pkt); | |
402 if(c) avcodec_close(c); | |
403 if(ic) av_close_input_file(ic); | |
404 g_static_mutex_unlock(&wma_mutex); | |
405 return(NULL); | |
406 } | |
407 | |
408 static void wma_play_file(InputPlayback *playback) | |
409 { | |
410 char *filename = playback->filename; | |
411 AVCodec *codec; | |
412 | |
1390
095595555e7b
Fix WMA playback.
William Pitcock <nenolod@atheme-project.org>
parents:
1190
diff
changeset
|
413 if(av_open_input_file(&ic, filename, NULL, 0, NULL) < 0) return; |
878 | 414 |
415 for(wma_idx = 0; wma_idx < ic->nb_streams; wma_idx++) { | |
416 c = &ic->streams[wma_idx]->codec; | |
417 if(c->codec_type == CODEC_TYPE_AUDIO) break; | |
418 } | |
419 | |
420 av_find_stream_info(ic); | |
421 | |
422 codec = avcodec_find_decoder(c->codec_id); | |
423 | |
424 if(!codec) return; | |
425 | |
426 if(avcodec_open(c, codec) < 0) return; | |
427 | |
428 wsong_title = get_song_title(ic, filename); | |
429 wsong_time = get_song_time(ic); | |
430 | |
431 if(playback->output->open_audio(FMT_S16_NE, c->sample_rate, c->channels) <= 0) return; | |
432 | |
433 wma_st_buff = ST_BUFF; | |
434 | |
1075
37abd9b3de4b
[svn] - conversion to yaz's style was not needed, just API v2 ;p
nenolod
parents:
1072
diff
changeset
|
435 wma_ip.set_info(wsong_title, wsong_time, c->bit_rate, c->sample_rate, c->channels); |
878 | 436 |
437 /* av_malloc() will wrap posix_memalign() if necessary -nenolod */ | |
438 wma_s_outbuf = av_malloc(wma_st_buff); | |
439 wma_outbuf = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); | |
440 | |
441 wma_seekpos = -1; | |
442 wma_decode = 1; | |
1168 | 443 playback->playing = 1; |
1390
095595555e7b
Fix WMA playback.
William Pitcock <nenolod@atheme-project.org>
parents:
1190
diff
changeset
|
444 wma_decode_thread = g_thread_self(); |
095595555e7b
Fix WMA playback.
William Pitcock <nenolod@atheme-project.org>
parents:
1190
diff
changeset
|
445 wma_play_loop(playback); |
878 | 446 } |
447 | |
448 static void wma_stop(InputPlayback *playback) | |
449 { | |
450 wma_decode = 0; | |
1168 | 451 playback->playing = 0; |
878 | 452 if(wma_pause) wma_do_pause(playback, 0); |
453 g_thread_join(wma_decode_thread); | |
454 playback->output->close_audio(); | |
455 } |