# HG changeset patch # User Calin Crisan ccrisan@gmail.com # Date 1184978937 -10800 # Node ID 28df54b3eaea7eafe0ac86e5d0339f74dc8e77e2 # Parent c0b5b291bc64883d6b98b2555e00422211932673 Ejecting the cd while playing no longer crashes the player. diff -r c0b5b291bc64 -r 28df54b3eaea src/cdaudio-ng/cdaudio-ng.c --- a/src/cdaudio-ng/cdaudio-ng.c Fri Jul 20 14:43:09 2007 -0500 +++ b/src/cdaudio-ng/cdaudio-ng.c Sat Jul 21 03:48:57 2007 +0300 @@ -1,7 +1,6 @@ /* todo: - - fileinfo dialog - about dialog */ @@ -49,6 +48,7 @@ static gboolean debug = FALSE; static char cddb_server[DEF_STRING_LEN]; static int cddb_port; +static InputPlayback *pglobalinputplayback = NULL; static void cdaudio_init(); static void cdaudio_about(); @@ -64,7 +64,6 @@ static gint cdaudio_set_volume(gint l, gint r); static void cdaudio_cleanup(); static void cdaudio_get_song_info(gchar *filename, gchar **title, gint *length); -static void cdaudio_file_info_box(gchar *filename); static TitleInput *cdaudio_get_song_tuple(gchar *filename); static void *dae_playing_thread_core(dae_params_t *pdae_params); @@ -96,7 +95,7 @@ NULL, NULL, cdaudio_get_song_info, - NULL /*cdaudio_file_info_box*/, // todo: implement a file info dialog + NULL, NULL, cdaudio_get_song_tuple }; @@ -184,11 +183,17 @@ } /* reload the cd information if the media has changed */ - if (cdio_get_media_changed(pcdio)) { + if (cdio_get_media_changed(pcdio) && pcdio != NULL) { if (debug) printf("cdaudio-ng: cd changed, rescanning\n"); cdaudio_scan_dir(CDDA_DEFAULT); } + + if (pcdio == NULL) { + if (debug) + printf("cdaudio-ng: \"%s\" is not our file\n", filename); + return FALSE; + } /* check if the requested track actually exists on the current audio cd */ int trackno = find_trackno_from_filename(filename); @@ -231,21 +236,22 @@ } else { char **ppcd_drives = cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false); - if (ppcd_drives != NULL) { /* we have at least one audio capable cd drive */ + pcdio = NULL; + if (ppcd_drives != NULL && *ppcd_drives != NULL) { /* we have at least one audio capable cd drive */ pcdio = cdio_open(*ppcd_drives, DRIVER_UNKNOWN); if (pcdio == NULL) { fprintf(stderr, "cdaudio-ng: failed to open cd\n"); cleanup_on_error(); return NULL; } + if (debug) + printf("cdaudio-ng: found cd drive \"%s\" with audio capable media\n", *ppcd_drives); } else { fprintf(stderr, "cdaudio-ng: unable find or access a cdda capable drive\n"); cleanup_on_error(); return NULL; } - if (debug) - printf("cdaudio-ng: found cd drive \"%s\" with audio capable media\n", *ppcd_drives); cdio_free_device_list(ppcd_drives); } @@ -425,6 +431,8 @@ { if (debug) printf("cdaudio-ng: cdaudio_play_file(\"%s\")\n", pinputplayback->filename); + + pglobalinputplayback = pinputplayback; if (trackinfo == NULL) { if (debug) @@ -505,6 +513,11 @@ { if (debug) printf("cdaudio-ng: cdaudio_stop(\"%s\")\n", pinputplayback != NULL ? pinputplayback->filename : "N/A"); + + pglobalinputplayback = NULL; + + if (playing_track == -1) + return; if (pinputplayback != NULL) pinputplayback->playing = FALSE; @@ -521,7 +534,6 @@ else { if (cdio_audio_stop(pcdio) != DRIVER_OP_SUCCESS) { fprintf(stderr, "cdaudio-ng: failed to stop analog cd\n"); - cleanup_on_error(); return; } } @@ -591,13 +603,13 @@ if (cdio_audio_read_subchannel(pcdio, &subchannel) != DRIVER_OP_SUCCESS) { fprintf(stderr, "cdaudio-ng: failed to read analog cd subchannel\n"); cleanup_on_error(); - return -1; + return 0; } int currlsn = cdio_msf_to_lsn(&subchannel.abs_addr); /* check to see if we have reached the end of the song */ if (currlsn == trackinfo[playing_track].endlsn) { - cdaudio_stop(pinputplayback); + //cdaudio_stop(pinputplayback); return -1; } @@ -707,12 +719,6 @@ *length = calculate_track_length(trackinfo[trackno].startlsn, trackinfo[trackno].endlsn); } -void cdaudio_file_info_box(gchar *filename) -{ - if (debug) - printf("cdaudio-ng: cdaudio_file_info_box(\"%s\")\n", filename); -} - TitleInput *cdaudio_get_song_tuple(gchar *filename) { if (debug) @@ -751,6 +757,7 @@ cdio_lseek(pcdio, pdae_params->startlsn * CDIO_CD_FRAMESIZE_RAW, SEEK_SET); gboolean output_paused = FALSE; + int read_error_counter = 0; while (pdae_params->pplayback->playing) { /* handle pause status */ @@ -792,8 +799,14 @@ if (cdio_read_audio_sectors(pcdio, buffer, pdae_params->currlsn, lsncount) != DRIVER_OP_SUCCESS) { fprintf(stderr, "cdaudio-ng: failed to read audio sector\n"); - /* ok, that's it, we go on */ + read_error_counter++; + if (read_error_counter >= 2) { + fprintf(stderr, "cdaudio-ng: this cd can no longer be played, stopping\n"); + break; + } } + else + read_error_counter = 0; int remainingbytes = lsncount * CDIO_CD_FRAMESIZE_RAW; unsigned char *bytebuff = buffer; @@ -843,14 +856,20 @@ void cleanup_on_error() { - if (pcdio != NULL) { - if (playing_track != -1 && !use_dae) + if (playing_track != -1) { + playing_track = -1; + //playback_stop(); + if (pglobalinputplayback != NULL) { + pglobalinputplayback->error = 1; + playback_stop(); + } + if (pcdio != NULL && !use_dae) cdio_audio_stop(pcdio); } + if (trackinfo != NULL) { free(trackinfo); trackinfo = NULL; } - playing_track = -1; }