From 4f80e2f36bcee0862f1360cae2b69ba7f0ad5732 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 29 Jun 2004 23:27:18 +0000 Subject: Fix for bug #981991 (VORBIS: Crash when using Ogg Vorbis CD tracks) svn-id: r14128 --- sound/vorbis.cpp | 57 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'sound') diff --git a/sound/vorbis.cpp b/sound/vorbis.cpp index 3d48d12b1b..26b04599ca 100644 --- a/sound/vorbis.cpp +++ b/sound/vorbis.cpp @@ -48,6 +48,7 @@ private: public: VorbisTrackInfo(File *file); ~VorbisTrackInfo(); + bool openTrack(); bool error() { return _error_flag; } void play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration); }; @@ -119,21 +120,45 @@ static ov_callbacks g_File_wrap = { VorbisTrackInfo::VorbisTrackInfo(File *file) { + + _file = file; + if (openTrack()) { + warning("Invalid file format"); + _error_flag = true; + _file = 0; + } else { + _error_flag = false; + _file->incRef(); + ov_clear(&_ov_file); + } +} + +bool VorbisTrackInfo::openTrack() { + assert(_file); + file_info *f = new file_info; - f->file = file; + f->file = _file; f->start = 0; - f->len = file->size(); - f->curr_pos = file->pos(); - - if (ov_open_callbacks((void *) f, &_ov_file, NULL, 0, g_File_wrap) < 0) { - warning("Invalid file format"); - _error_flag = true; + f->len = _file->size(); + f->curr_pos = 0; + _file->seek(0); + + bool err = (ov_open_callbacks((void *) f, &_ov_file, NULL, 0, g_File_wrap) < 0); + + if (err) { delete f; - delete file; } else { - _error_flag = false; - _file = file; + _file->incRef(); + } + + return err; +} + +VorbisTrackInfo::~VorbisTrackInfo() { + if (! _error_flag) { + ov_clear(&_ov_file); + _file->decRef(); } } @@ -142,6 +167,10 @@ VorbisTrackInfo::VorbisTrackInfo(File *file) { #endif void VorbisTrackInfo::play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration) { + + bool err = openTrack(); + assert(!err); + #ifdef VORBIS_TREMOR ov_time_seek(&_ov_file, (ogg_int64_t)(startFrame / 75.0 * 1000)); #else @@ -152,13 +181,6 @@ void VorbisTrackInfo::play(SoundMixer *mixer, PlayingSoundHandle *handle, int st mixer->playInputStream(handle, input, true); } -VorbisTrackInfo::~VorbisTrackInfo() { - if (! _error_flag) { - ov_clear(&_ov_file); - delete _file; - } -} - DigitalTrackInfo *getVorbisTrack(int track) { char track_name[32]; File *file = new File(); @@ -168,6 +190,7 @@ DigitalTrackInfo *getVorbisTrack(int track) { if (file->isOpen()) { VorbisTrackInfo *trackInfo = new VorbisTrackInfo(file); + file->decRef(); if (!trackInfo->error()) return trackInfo; delete trackInfo; -- cgit v1.2.3