1 | //roar.c: |
---|
2 | |
---|
3 | #include <roaraudio.h> |
---|
4 | |
---|
5 | //#include "xmms/i18n.h" |
---|
6 | #include <gtk/gtk.h> |
---|
7 | #include <stdio.h> |
---|
8 | #include <string.h> |
---|
9 | |
---|
10 | #include "xmms/plugin.h" |
---|
11 | #include "xmms/xmmsctrl.h" |
---|
12 | #include "xmms/dirbrowser.h" |
---|
13 | #include "xmms/configfile.h" |
---|
14 | #include "xmms/util.h" |
---|
15 | |
---|
16 | #define _(x) (x) |
---|
17 | |
---|
18 | gint ctrlsocket_get_session_id(void); |
---|
19 | |
---|
20 | void roar_init(void); |
---|
21 | void roar_about(void); |
---|
22 | void roar_configure(void); |
---|
23 | |
---|
24 | void roar_get_volume(int *l, int *r); |
---|
25 | void roar_fetch_volume(int *l, int *r); |
---|
26 | void roar_set_volume(int l, int r); |
---|
27 | void roar_mixer_init(void); |
---|
28 | void roar_mixer_init_vol(int l, int r); |
---|
29 | |
---|
30 | int roar_playing(void); |
---|
31 | int roar_free(void); |
---|
32 | void roar_write(void *ptr, int length); |
---|
33 | void roar_close(void); |
---|
34 | void roar_flush(int time); |
---|
35 | void roar_pause(short p); |
---|
36 | int roar_open(AFormat fmt, int rate, int nch); |
---|
37 | int roar_get_output_time(void); |
---|
38 | int roar_get_written_time(void); |
---|
39 | |
---|
40 | int roar_update_metadata(void); |
---|
41 | int roar_chk_metadata(void); |
---|
42 | |
---|
43 | OutputPlugin roar_op = { |
---|
44 | NULL, |
---|
45 | NULL, |
---|
46 | "RoarAudio XMMS Plugin", /* Description */ |
---|
47 | roar_init, |
---|
48 | roar_about, |
---|
49 | NULL, //roar_configure, |
---|
50 | NULL, //roar_get_volume, |
---|
51 | NULL, //roar_set_volume, |
---|
52 | roar_open, |
---|
53 | roar_write, |
---|
54 | roar_close, |
---|
55 | NULL, //roar_flush, |
---|
56 | NULL, //roar_pause, |
---|
57 | roar_free, |
---|
58 | roar_playing, |
---|
59 | roar_get_output_time, |
---|
60 | roar_get_written_time, |
---|
61 | }; |
---|
62 | |
---|
63 | #define STATE_CONNECTED 1 |
---|
64 | #define STATE_PLAYING 2 |
---|
65 | |
---|
66 | struct xmms_roar_out { |
---|
67 | int state; |
---|
68 | char * server; |
---|
69 | struct roar_connection con; |
---|
70 | struct roar_stream stream; |
---|
71 | int data_fh; |
---|
72 | long unsigned int written; |
---|
73 | long unsigned int bps; |
---|
74 | int session; |
---|
75 | int next_test; |
---|
76 | } g_inst; |
---|
77 | |
---|
78 | OutputPlugin *get_oplugin_info(void) { |
---|
79 | return &roar_op; |
---|
80 | } |
---|
81 | |
---|
82 | void roar_init(void) { |
---|
83 | g_inst.state = 0; |
---|
84 | g_inst.server = NULL; |
---|
85 | g_inst.session = ctrlsocket_get_session_id(); |
---|
86 | ROAR_DBG("roar_init(*) = (void)"); |
---|
87 | } |
---|
88 | |
---|
89 | int roar_playing(void) { |
---|
90 | return g_inst.state & STATE_PLAYING ? TRUE : FALSE; |
---|
91 | } |
---|
92 | |
---|
93 | void roar_write(void *ptr, int length) { |
---|
94 | int r; |
---|
95 | |
---|
96 | while (length) { |
---|
97 | if ( (r = write(g_inst.data_fh, ptr, length >= 1764 ? 1764 : length)) != -1 ) { |
---|
98 | g_inst.written += r; |
---|
99 | ptr += r; |
---|
100 | length -= r; |
---|
101 | g_inst.next_test -= r; |
---|
102 | } else { |
---|
103 | return; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | if ( g_inst.next_test < 1 ) { |
---|
109 | roar_chk_metadata(); |
---|
110 | g_inst.next_test = g_inst.bps; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | int roar_open(AFormat fmt, int rate, int nch) { |
---|
115 | int codec = ROAR_CODEC_DEFAULT; |
---|
116 | int bits; |
---|
117 | |
---|
118 | if ( !(g_inst.state & STATE_CONNECTED) ) { |
---|
119 | if ( roar_simple_connect(&(g_inst.con), g_inst.server, "XMMS") == -1 ) { |
---|
120 | return FALSE; |
---|
121 | } |
---|
122 | g_inst.state |= STATE_CONNECTED; |
---|
123 | } |
---|
124 | |
---|
125 | bits = 16; |
---|
126 | switch (fmt) { |
---|
127 | case FMT_S8: |
---|
128 | bits = 8; |
---|
129 | codec = ROAR_CODEC_DEFAULT; |
---|
130 | break; |
---|
131 | case FMT_U8: |
---|
132 | bits = 8; |
---|
133 | codec = ROAR_CODEC_PCM_S_LE; // _LE, _BE, _PDP,... all the same for 8 bit output |
---|
134 | break; |
---|
135 | case FMT_U16_LE: |
---|
136 | codec = ROAR_CODEC_PCM_U_LE; |
---|
137 | break; |
---|
138 | case FMT_U16_BE: |
---|
139 | codec = ROAR_CODEC_PCM_U_BE; |
---|
140 | break; |
---|
141 | case FMT_U16_NE: |
---|
142 | #if BYTE_ORDER == BIG_ENDIAN |
---|
143 | codec = ROAR_CODEC_PCM_U_BE; |
---|
144 | #elif BYTE_ORDER == LITTLE_ENDIAN |
---|
145 | codec = ROAR_CODEC_PCM_U_LE; |
---|
146 | #else |
---|
147 | codec = ROAR_CODEC_PCM_U_PDP; |
---|
148 | #endif |
---|
149 | break; |
---|
150 | case FMT_S16_LE: |
---|
151 | codec = ROAR_CODEC_PCM_S_LE; |
---|
152 | break; |
---|
153 | case FMT_S16_BE: |
---|
154 | codec = ROAR_CODEC_PCM_S_BE; |
---|
155 | break; |
---|
156 | case FMT_S16_NE: |
---|
157 | codec = ROAR_CODEC_DEFAULT; |
---|
158 | break; |
---|
159 | } |
---|
160 | |
---|
161 | g_inst.bps = nch * rate * bits / 8; |
---|
162 | g_inst.next_test = g_inst.bps; |
---|
163 | |
---|
164 | if ( (g_inst.data_fh = roar_simple_new_stream_obj(&(g_inst.con), &(g_inst.stream), |
---|
165 | rate, nch, bits, codec, ROAR_DIR_PLAY)) == -1) { |
---|
166 | return FALSE; |
---|
167 | } |
---|
168 | g_inst.state |= STATE_PLAYING; |
---|
169 | |
---|
170 | g_inst.written = 0; |
---|
171 | |
---|
172 | roar_update_metadata(); |
---|
173 | |
---|
174 | return TRUE; |
---|
175 | } |
---|
176 | |
---|
177 | void roar_close(void) { |
---|
178 | close(g_inst.data_fh); |
---|
179 | g_inst.state |= STATE_PLAYING; |
---|
180 | g_inst.state -= STATE_PLAYING; |
---|
181 | g_inst.written = 0; |
---|
182 | } |
---|
183 | |
---|
184 | void roar_pause(short p); |
---|
185 | |
---|
186 | int roar_free(void) { |
---|
187 | return 1000000; // ??? |
---|
188 | } |
---|
189 | |
---|
190 | int roar_get_output_time(void) { |
---|
191 | return roar_get_written_time(); |
---|
192 | } |
---|
193 | |
---|
194 | int roar_get_written_time(void) { |
---|
195 | gint64 r; |
---|
196 | |
---|
197 | if ( !g_inst.bps ) { |
---|
198 | ROAR_DBG("roar_get_written_time(void) = 0"); |
---|
199 | return 0; |
---|
200 | } |
---|
201 | |
---|
202 | r = g_inst.written; |
---|
203 | r *= 1000; // sec -> msec |
---|
204 | r /= g_inst.bps; |
---|
205 | ROAR_DBG("roar_get_written_time(void) = %lu", r); |
---|
206 | |
---|
207 | return r; |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | // ABOUT: |
---|
212 | |
---|
213 | void roar_about(void) { |
---|
214 | static GtkWidget *dialog; |
---|
215 | |
---|
216 | if (dialog != NULL) |
---|
217 | return; |
---|
218 | |
---|
219 | dialog = xmms_show_message( |
---|
220 | _("About RoarAudio Plugin"), |
---|
221 | _("RoarAudio XMMS Plugin..." |
---|
222 | ), _("OK"), FALSE, NULL, NULL); |
---|
223 | gtk_signal_connect(GTK_OBJECT(dialog), "destroy", |
---|
224 | GTK_SIGNAL_FUNC(gtk_widget_destroyed), |
---|
225 | &dialog); |
---|
226 | } |
---|
227 | |
---|
228 | // CONFIG: |
---|
229 | |
---|
230 | // META DATA: |
---|
231 | |
---|
232 | int roar_update_metadata(void) { |
---|
233 | struct roar_meta meta; |
---|
234 | char empty = 0; |
---|
235 | char * info; |
---|
236 | int pos; |
---|
237 | |
---|
238 | pos = xmms_remote_get_playlist_pos(g_inst.session); |
---|
239 | |
---|
240 | meta.value = ∅ |
---|
241 | meta.key[0] = 0; |
---|
242 | meta.type = ROAR_META_TYPE_NONE; |
---|
243 | |
---|
244 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_CLEAR, &meta); |
---|
245 | |
---|
246 | info = xmms_remote_get_playlist_file(g_inst.session, pos); |
---|
247 | |
---|
248 | if ( info ) { |
---|
249 | if ( strncmp(info, "http:", 5) == 0 ) |
---|
250 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
251 | else |
---|
252 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
253 | |
---|
254 | meta.value = info; |
---|
255 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta); |
---|
256 | |
---|
257 | free(info); |
---|
258 | } |
---|
259 | |
---|
260 | info = xmms_remote_get_playlist_title(g_inst.session, pos); |
---|
261 | |
---|
262 | if ( info ) { |
---|
263 | meta.type = ROAR_META_TYPE_TITLE; |
---|
264 | |
---|
265 | meta.value = info; |
---|
266 | roar_stream_meta_set(&(g_inst.con), &(g_inst.stream), ROAR_META_MODE_SET, &meta); |
---|
267 | |
---|
268 | free(info); |
---|
269 | } |
---|
270 | |
---|
271 | return 0; |
---|
272 | } |
---|
273 | |
---|
274 | int roar_chk_metadata(void) { |
---|
275 | static int old_pos = -1; |
---|
276 | static char * old_title = "NEW TITLE"; |
---|
277 | int pos; |
---|
278 | char * title; |
---|
279 | int need_update = 0; |
---|
280 | |
---|
281 | pos = xmms_remote_get_playlist_pos(g_inst.session); |
---|
282 | |
---|
283 | if ( pos != old_pos ) { |
---|
284 | old_pos = pos; |
---|
285 | need_update = 1; |
---|
286 | } else { |
---|
287 | title = xmms_remote_get_playlist_title(g_inst.session, pos); |
---|
288 | |
---|
289 | if ( strcmp(title, old_title) ) { |
---|
290 | free(old_title); |
---|
291 | old_title = title; |
---|
292 | need_update = 1; |
---|
293 | } else { |
---|
294 | free(title); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | if ( need_update ) { |
---|
299 | roar_update_metadata(); |
---|
300 | } |
---|
301 | |
---|
302 | return 0; |
---|
303 | } |
---|
304 | |
---|
305 | //ll |
---|