No longer crash or exit(1) all open MediaPlayer windows when a bad mp3 file is played.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6964 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
beveloper
2004-03-14 02:08:51 +00:00
parent 190c52aac5
commit 62ffde749f
4 changed files with 26 additions and 9 deletions
@@ -14,6 +14,9 @@
#define DECODE_BUFFER_SIZE (32 * 1024) #define DECODE_BUFFER_SIZE (32 * 1024)
// reference:
// http://www.mp3-tech.org/programmer/frame_header.html
inline size_t inline size_t
AudioBufferSize(const media_raw_audio_format &raf, bigtime_t buffer_duration = 50000 /* 50 ms */) AudioBufferSize(const media_raw_audio_format &raf, bigtime_t buffer_duration = 50000 /* 50 ms */)
{ {
@@ -73,6 +76,7 @@ mp3Decoder::mp3Decoder()
fChannelCount = 0; fChannelCount = 0;
fOutputBufferSize = 0; fOutputBufferSize = 0;
fNeedSync = false; fNeedSync = false;
fDecodingError = false;
} }
@@ -158,6 +162,7 @@ mp3Decoder::Seek(uint32 seekTo,
InitMP3(&fMpgLibPrivate); InitMP3(&fMpgLibPrivate);
fResidualBytes = 0; fResidualBytes = 0;
fNeedSync = true; fNeedSync = true;
fDecodingError = false;
return B_OK; return B_OK;
} }
@@ -171,6 +176,9 @@ mp3Decoder::Decode(void *buffer, int64 *frameCount,
int32 out_bytes_needed = fOutputBufferSize; int32 out_bytes_needed = fOutputBufferSize;
int32 out_bytes = 0; int32 out_bytes = 0;
if (fDecodingError)
return B_ERROR;
mediaHeader->start_time = fStartTime; mediaHeader->start_time = fStartTime;
//TRACE("mp3Decoder: Decoding start time %.6f\n", fStartTime / 1000000.0); //TRACE("mp3Decoder: Decoding start time %.6f\n", fStartTime / 1000000.0);
@@ -191,8 +199,10 @@ mp3Decoder::Decode(void *buffer, int64 *frameCount,
} }
last_err = DecodeNextChunk(); last_err = DecodeNextChunk();
if (last_err != B_OK) if (last_err != B_OK) {
fDecodingError = true;
break; break;
}
} }
*frameCount = out_bytes / fFrameSize; *frameCount = out_bytes / fFrameSize;
@@ -40,6 +40,7 @@ private:
int fChannelCount; int fChannelCount;
int fOutputBufferSize; int fOutputBufferSize;
bool fNeedSync; bool fNeedSync;
bool fDecodingError;
}; };
@@ -58,8 +58,8 @@ int decode_header(struct frame *fr,unsigned long newhead)
fr->lay = 4-((newhead>>17)&3); fr->lay = 4-((newhead>>17)&3);
if( ((newhead>>10)&0x3) == 0x3) { if( ((newhead>>10)&0x3) == 0x3) {
fprintf(stderr,"Stream error\n"); fprintf(stderr,"Stream error, reserved sampling rate\n");
exit(1); return 0;
} }
if(fr->mpeg25) { if(fr->mpeg25) {
fr->sampling_frequency = 6 + ((newhead>>10)&0x3); fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
@@ -84,8 +84,8 @@ int decode_header(struct frame *fr,unsigned long newhead)
if(!fr->bitrate_index) if(!fr->bitrate_index)
{ {
fprintf(stderr,"Free format not supported.\n"); fprintf(stderr, "Stream error, free format bitrate index not supported\n");
return (0); return 0;
} }
switch(fr->lay) switch(fr->lay)
@@ -126,7 +126,7 @@ int decode_header(struct frame *fr,unsigned long newhead)
fr->framesize = fr->framesize + fr->padding - 4; fr->framesize = fr->framesize + fr->padding - 4;
break; break;
default: default:
fprintf(stderr,"Sorry, unknown layer type.\n"); fprintf(stderr,"Stream error, unknown layer type.\n");
return (0); return (0);
} }
return 1; return 1;
@@ -94,14 +94,19 @@ static int read_buf_byte(struct mpstr *mp)
int pos; int pos;
if(!mp->tail) {
fprintf(stderr,"Fatal error!\n");
return 0;
}
pos = mp->tail->pos; pos = mp->tail->pos;
while(pos >= mp->tail->size) { while(pos >= mp->tail->size) {
remove_buf(mp); remove_buf(mp);
pos = mp->tail->pos;
if(!mp->tail) { if(!mp->tail) {
fprintf(stderr,"Fatal error!\n"); fprintf(stderr,"Fatal error!\n");
exit(1); return 0;
} }
pos = mp->tail->pos;
} }
b = mp->tail->pnt[pos]; b = mp->tail->pnt[pos];
@@ -149,7 +154,8 @@ int decodeMP3(struct mpstr *mp,char *in,int isize,char *out,
return MP3_NEED_MORE; return MP3_NEED_MORE;
} }
read_head(mp); read_head(mp);
decode_header(&mp->fr,mp->header); if (decode_header(&mp->fr,mp->header) == 0)
return MP3_ERR;
mp->framesize = mp->fr.framesize; mp->framesize = mp->fr.framesize;
} }