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)
{
dir = FileFinder.addSlash(dir);
char[][] res = listdir(dir);
char[][] res = ((exists(dir) && isdir(dir)) ? listdir(dir) : null);
foreach(ref char[] fn; res)
fn = dir ~ fn;
return res;

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

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

Loading…
Cancel
Save