Pointer safety patch to avcoded, fixed a music volume bug

git-svn-id: https://openmw.svn.sourceforge.net/svnroot/openmw/trunk@29 ea6a568a-9f4f-0410-981a-c910a81bb256
actorid
nkorslund 17 years ago
parent 3e30014b9b
commit 3c1e340aac

@ -144,7 +144,7 @@ struct ResourceManager
char[][] getDir(char[] dir) char[][] getDir(char[] dir)
{ {
dir = FileFinder.addSlash(dir); dir = FileFinder.addSlash(dir);
char[][] res = listdir(dir); char[][] res = ((exists(dir) && isdir(dir)) ? listdir(dir) : null);
foreach(ref char[] fn; res) foreach(ref char[] fn; res)
fn = dir ~ fn; fn = dir ~ fn;
return res; return res;

@ -43,7 +43,7 @@ struct MyFile {
vector<uint8_t> Data; vector<uint8_t> Data;
vector<char> DecodedData; vector<char> DecodedData;
}; };
vector<MyStream> Streams; vector<MyStream*> Streams;
}; };
// TODO: // TODO:
@ -75,9 +75,10 @@ extern "C" void cpp_closeAVFile(MyFile *file)
for(size_t i = 0;i < file->Streams.size();i++) for(size_t i = 0;i < file->Streams.size();i++)
{ {
avcodec_close(file->Streams[i].CodecCtx); avcodec_close(file->Streams[i]->CodecCtx);
file->Streams[i].Data.clear(); file->Streams[i]->Data.clear();
file->Streams[i].DecodedData.clear(); file->Streams[i]->DecodedData.clear();
delete file->Streams[i];
} }
file->Streams.clear(); file->Streams.clear();
@ -95,18 +96,20 @@ extern "C" MyFile::MyStream *cpp_getAVAudioStream(MyFile *file, int streamnum)
if(streamnum == 0) if(streamnum == 0)
{ {
MyFile::MyStream stream; MyFile::MyStream *stream = new MyFile::MyStream;
stream.parent = file; stream->parent = file;
stream.CodecCtx = file->FmtCtx->streams[i]->codec; stream->CodecCtx = file->FmtCtx->streams[i]->codec;
stream.StreamNum = i; stream->StreamNum = i;
AVCodec *codec = avcodec_find_decoder(stream.CodecCtx->codec_id); AVCodec *codec = avcodec_find_decoder(stream->CodecCtx->codec_id);
if(!codec) return NULL; if(!codec || avcodec_open(stream->CodecCtx, codec) < 0)
if(avcodec_open(stream.CodecCtx, codec) < 0) {
delete stream;
return NULL; return NULL;
}
file->Streams.push_back(stream); file->Streams.push_back(stream);
return &file->Streams[file->Streams.size()-1]; return stream;
} }
streamnum--; streamnum--;
} }
@ -130,14 +133,14 @@ static int getNextPacket(MyFile *file)
AVPacket packet; AVPacket packet;
while(av_read_frame(file->FmtCtx, &packet) >= 0) while(av_read_frame(file->FmtCtx, &packet) >= 0)
{ {
for(vector<MyFile::MyStream>::iterator i = file->Streams.begin(); for(vector<MyFile::MyStream*>::iterator i = file->Streams.begin();
i != file->Streams.end();i++) i != file->Streams.end();i++)
{ {
if(i->StreamNum == packet.stream_index) if((*i)->StreamNum == packet.stream_index)
{ {
size_t idx = i->Data.size(); size_t idx = (*i)->Data.size();
i->Data.resize(idx + packet.size); (*i)->Data.resize(idx + packet.size);
memcpy(&i->Data[idx], packet.data, packet.size); memcpy(&(*i)->Data[idx], packet.data, packet.size);
av_free_packet(&packet); av_free_packet(&packet);
return 0; return 0;
} }

@ -40,7 +40,7 @@ struct MusicManager
// How much to add to the volume each second when fading // How much to add to the volume each second when fading
const float fadeInRate = 0.10; const float fadeInRate = 0.10;
const float fadeOutRate = 0.35; const float fadeOutRate = 0.20;
// Maximum buffer length, divided up among OpenAL buffers // Maximum buffer length, divided up among OpenAL buffers
const uint bufLength = 128*1024; const uint bufLength = 128*1024;
@ -85,7 +85,6 @@ struct MusicManager
outData.length = bufLength / bIDs.length; outData.length = bufLength / bIDs.length;
fileHandle = null; fileHandle = null;
musicOn = false; musicOn = false;
updateVolume();
} }
// Get the new volume setting. // Get the new volume setting.
@ -99,6 +98,7 @@ struct MusicManager
// a fade. Even if we are fading, though, the volume should never // a fade. Even if we are fading, though, the volume should never
// be over the max. // be over the max.
if(fading == Fade.None || volume > maxVolume) volume = maxVolume; if(fading == Fade.None || volume > maxVolume) volume = maxVolume;
if(sID)
alSourcef(sID, AL_GAIN, volume); alSourcef(sID, AL_GAIN, volume);
} }
@ -154,6 +154,8 @@ struct MusicManager
if(checkALError()) if(checkALError())
fail("Couldn't generate music sources"); fail("Couldn't generate music sources");
alSourcei(sID, AL_SOURCE_RELATIVE, AL_TRUE); alSourcei(sID, AL_SOURCE_RELATIVE, AL_TRUE);
updateVolume();
} }
else else
{ {
@ -263,7 +265,6 @@ struct MusicManager
if(!config.useMusic) return; if(!config.useMusic) return;
musicOn = true; musicOn = true;
volume = maxVolume;
fading = Fade.None; fading = Fade.None;
playNext(); playNext();
} }

Loading…
Cancel
Save