comparison src/xmms-sid.c @ 75:653c9b0d1320

SIDPlay2 support "works" now. Borked problems with threads.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 15 Sep 2003 11:10:03 +0000
parents 8cb66a3f75f7
children ab522ab65c85
comparison
equal deleted inserted replaced
74:8cb66a3f75f7 75:653c9b0d1320
134 134
135 /* Initialize and get configuration */ 135 /* Initialize and get configuration */
136 memset(&xs_cfg, 0, sizeof(xs_cfg)); 136 memset(&xs_cfg, 0, sizeof(xs_cfg));
137 xs_read_configuration(); 137 xs_read_configuration();
138 138
139
140 /* Initialize status */ 139 /* Initialize status */
141 memset(&xs_status, 0, sizeof(xs_status)); 140 memset(&xs_status, 0, sizeof(xs_status));
142 xs_status.allowNext = TRUE; 141 xs_status.allowNext = TRUE;
143
144 142
145 /* Try to initialize emulator engine */ 143 /* Try to initialize emulator engine */
146 XSDEBUG("initializing emulator engine...\n"); 144 XSDEBUG("initializing emulator engine...\n");
147 145
148 iPlayer = 0; 146 iPlayer = 0;
247 * Main playing thread loop 245 * Main playing thread loop
248 */ 246 */
249 void *xs_play_loop(void *argPointer) 247 void *xs_play_loop(void *argPointer)
250 { 248 {
251 t_xs_status myStatus; 249 t_xs_status myStatus;
252 gboolean audioOpen; 250 gboolean audioOpen, doPlay;
253 gint audioFreq, audioChannels, songLength, audioFmt; 251 gint audioFreq, audioChannels, songLength, audioFmt;
254 gchar audioBuffer[XS_BUFSIZE]; 252 gchar audioBuffer[XS_BUFSIZE];
255 253
256 254 /* Initialize */
257 pthread_mutex_lock(&xs_mutex); 255 pthread_mutex_lock(&xs_mutex);
258 XSDEBUG("entering play thread\n"); 256 XSDEBUG("entering play thread\n");
259
260 /* No idea, if this is really required here, but better be
261 * careful since we're dealing with EVIL threads ...
262 */
263 if (!xs_status.allowNext)
264 {
265 pthread_mutex_unlock(&xs_mutex);
266 pthread_exit(NULL);
267 }
268
269 /* Don't allow next song to be set yet */
270 xs_status.allowNext = FALSE;
271 xs_status.isPlaying = TRUE; 257 xs_status.isPlaying = TRUE;
272 memcpy(&myStatus, &xs_status, sizeof(t_xs_status)); 258 memcpy(&myStatus, &xs_status, sizeof(t_xs_status));
273 pthread_mutex_unlock(&xs_mutex); 259 pthread_mutex_unlock(&xs_mutex);
274
275 260
276 /* Copy and check audio options here (they might change in config while running) */ 261 /* Copy and check audio options here (they might change in config while running) */
277 #ifdef HAVE_UNSIGNEDPCM 262 #ifdef HAVE_UNSIGNEDPCM
278 audioFmt = (xs_cfg.fmtBitsPerSample == XS_RES_16BIT) ? FMT_U16_NE : FMT_U8; 263 audioFmt = (xs_cfg.fmtBitsPerSample == XS_RES_16BIT) ? FMT_U16_NE : FMT_U8;
279 #else 264 #else
284 audioOpen = FALSE; 269 audioOpen = FALSE;
285 270
286 /* 271 /*
287 * Main player loop: while not stopped, loop here - play subtunes 272 * Main player loop: while not stopped, loop here - play subtunes
288 */ 273 */
289 while (xs_status.isPlaying) 274 doPlay = TRUE;
275 while (xs_status.isPlaying && doPlay)
290 { 276 {
291 pthread_mutex_lock(&xs_mutex); 277 pthread_mutex_lock(&xs_mutex);
292 myStatus.currSong = xs_status.currSong; 278 myStatus.currSong = xs_status.currSong;
293 myStatus.isPlaying = TRUE; 279 myStatus.isPlaying = TRUE;
280 xs_status.allowNext = TRUE;
294 pthread_mutex_unlock(&xs_mutex); 281 pthread_mutex_unlock(&xs_mutex);
295 282
296 XSDEBUG("subtune #%i selected, initializing...\n", myStatus.currSong); 283 XSDEBUG("subtune #%i selected, initializing...\n", myStatus.currSong);
297 284
298 285
382 xs_plugin_ip.output->close_audio(); 369 xs_plugin_ip.output->close_audio();
383 audioOpen = FALSE; 370 audioOpen = FALSE;
384 } 371 }
385 372
386 /* Now determine if we continue by selecting other subtune or something */ 373 /* Now determine if we continue by selecting other subtune or something */
387 if (!myStatus.isPlaying) xs_status.isPlaying = FALSE; 374 if (!myStatus.isPlaying) doPlay = FALSE;
388 } 375 }
389 376
390 /* When exiting, delete data */
391 err_exit: 377 err_exit:
392 pthread_mutex_lock(&xs_mutex); 378 /* Close audio */
393 xs_status.isPlaying = FALSE;
394 pthread_mutex_unlock(&xs_mutex);
395
396 if (audioOpen) 379 if (audioOpen)
397 { 380 {
398 XSDEBUG("close audio #2\n"); 381 XSDEBUG("close audio #2\n");
399 xs_plugin_ip.output->close_audio(); 382 xs_plugin_ip.output->close_audio();
400 } 383 }
401 384
402 xs_player->plrDeleteSID(&myStatus);
403
404 /* Exit the playing thread */ 385 /* Exit the playing thread */
405 XSDEBUG("exiting thread, bye.\n"); 386 XSDEBUG("exiting thread, bye.\n");
406 387
407 /* Last thing we do is set allowNext to TRUE to flag
408 * that we have ended all action in the thread
409 */
410 pthread_mutex_lock(&xs_mutex); 388 pthread_mutex_lock(&xs_mutex);
389 xs_player->plrDeleteSID(&myStatus);
390
391 if (xs_status.currFileName)
392 {
393 g_free(xs_status.currFileName);
394 xs_status.currFileName = NULL;
395 }
396
397 xs_status.isPlaying = FALSE;
411 xs_status.allowNext = TRUE; 398 xs_status.allowNext = TRUE;
412 pthread_mutex_unlock(&xs_mutex); 399 pthread_mutex_unlock(&xs_mutex);
413
414 pthread_exit(NULL); 400 pthread_exit(NULL);
415 } 401 }
416 402
417 403
418 /* 404 /*
419 * Start playing the given file 405 * Start playing the given file
420 */ 406 */
421 void xs_play_file(gchar *pcFileName) 407 void xs_play_file(gchar *pcFileName)
422 { 408 {
409 while (!xs_status.allowNext) usleep(5000);
410
411 pthread_mutex_lock(&xs_mutex);
412 xs_status.allowNext = FALSE;
413 pthread_mutex_unlock(&xs_mutex);
414
415 fprintf(stderr, "\n\n");
416 XSDEBUG("play '%s'\n", pcFileName);
417
423 /* Initialize the tune */ 418 /* Initialize the tune */
424 if (!xs_player->plrLoadSID(&xs_status, pcFileName)) 419 if (!xs_player->plrLoadSID(&xs_status, pcFileName))
425 return; 420 return;
426 421
422 /* Set general status information */
423 xs_status.isPlaying = FALSE;
424 xs_status.isError = FALSE;
425 xs_status.currFileName = g_strdup(pcFileName);
426
427 /* Start the playing thread! */ 427 /* Start the playing thread! */
428 if (pthread_create(&xs_decode_thread, NULL, xs_play_loop, NULL) < 0) 428 if (pthread_create(&xs_decode_thread, NULL, xs_play_loop, NULL) < 0)
429 { 429 {
430 XSERR("Couldn't start playing thread! Possible reason reported by system: %s\n", strerror(errno)); 430 XSERR("Couldn't start playing thread! Possible reason reported by system: %s\n", strerror(errno));
431 xs_player->plrDeleteSID(&xs_status); 431 xs_player->plrDeleteSID(&xs_status);
440 */ 440 */
441 void xs_stop(void) 441 void xs_stop(void)
442 { 442 {
443 /* If playing, stop. */ 443 /* If playing, stop. */
444 XSDEBUG("STOP_REQ\n"); 444 XSDEBUG("STOP_REQ\n");
445
445 if (xs_status.isPlaying) 446 if (xs_status.isPlaying)
446 { 447 {
447 XSDEBUG("stopping...\n"); 448 XSDEBUG("stopping...\n");
448 pthread_mutex_lock(&xs_mutex); 449 pthread_mutex_lock(&xs_mutex);
449 xs_status.isPlaying = FALSE; 450 xs_status.isPlaying = FALSE;
450 pthread_mutex_unlock(&xs_mutex); 451 pthread_mutex_unlock(&xs_mutex);
451 pthread_join(xs_decode_thread, NULL); 452 pthread_join(xs_decode_thread, NULL);
452 } 453 }
454
453 } 455 }
454 456
455 457
456 /* 458 /*
457 * Pause the playing 459 * Pause the playing
488 /* If errorflag is set, return -2 to signal it to XMMS's idle callback */ 490 /* If errorflag is set, return -2 to signal it to XMMS's idle callback */
489 if (xs_status.isError) 491 if (xs_status.isError)
490 return -2; 492 return -2;
491 493
492 /* If tune has ended, return -1 */ 494 /* If tune has ended, return -1 */
493 if (xs_status.allowNext && !xs_status.isPlaying) 495 if (!xs_status.isPlaying)
494 return -1; 496 return -1;
495 497
496 /* Obsolete? */ 498 /* Obsolete? */
497 #ifdef HAVE_SONG_POSITION 499 #ifdef HAVE_SONG_POSITION
498 pthread_mutex_lock(&xs_mutex); 500 pthread_mutex_lock(&xs_mutex);