1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <cstring> //memset
#include "OggStream.h"
#include "System/FileSystem/FileHandler.h"
#include "System/Sound/SoundLog.h"
#include "ALShared.h"
#include "VorbisShared.h"
namespace VorbisCallbacks {
// NOTE:
// this buffer gets recycled by each new stream, across *all* audio-channels
// as a result streams are limited to only ever being played within a single
// channel (currently BGMusic), but cause far less memory fragmentation
// TODO:
// can easily be fixed if necessary by giving each channel its own index and
// passing that along to the callbacks via COggStream{::Play}
// CFileHandler fileBuffers[NUM_AUDIO_CHANNELS];
CFileHandler fileBuffer("", "");
size_t VorbisStreamReadCB(void* ptr, size_t size, size_t nmemb, void* datasource)
{
assert(datasource == &fileBuffer);
return fileBuffer.Read(ptr, size * nmemb);
}
int VorbisStreamCloseCB(void* datasource)
{
assert(datasource == &fileBuffer);
fileBuffer.Close();
return 0;
}
int VorbisStreamSeekCB(void* datasource, ogg_int64_t offset, int whence)
{
assert(datasource == &fileBuffer);
switch (whence) {
case SEEK_SET: { fileBuffer.Seek(offset, std::ios_base::beg); } break;
case SEEK_CUR: { fileBuffer.Seek(offset, std::ios_base::cur); } break;
case SEEK_END: { fileBuffer.Seek(offset, std::ios_base::end); } break;
default: {} break;
}
return 0;
}
long VorbisStreamTellCB(void* datasource)
{
assert(datasource == &fileBuffer);
return (fileBuffer.GetPos());
}
}
COggStream::COggStream(ALuint _source)
: vorbisInfo(nullptr)
, source(_source)
, format(AL_FORMAT_MONO16)
, stopped(true)
, paused(false)
{
memset(buffers, 0, NUM_BUFFERS * sizeof(buffers[0]));
memset(pcmDecodeBuffer, 0, BUFFER_SIZE * sizeof(pcmDecodeBuffer[0]));
}
// open an Ogg stream from a given file and start playing it
void COggStream::Play(const std::string& path, float volume)
{
// we're already playing another stream
if (!stopped)
return;
vorbisTags.clear();
ov_callbacks vorbisCallbacks;
vorbisCallbacks.read_func = VorbisCallbacks::VorbisStreamReadCB;
vorbisCallbacks.close_func = VorbisCallbacks::VorbisStreamCloseCB;
vorbisCallbacks.seek_func = VorbisCallbacks::VorbisStreamSeekCB;
vorbisCallbacks.tell_func = VorbisCallbacks::VorbisStreamTellCB;
VorbisCallbacks::fileBuffer.Open(path);
const int result = ov_open_callbacks(&VorbisCallbacks::fileBuffer, &ovFile, nullptr, 0, vorbisCallbacks);
if (result < 0) {
LOG_L(L_WARNING, "Could not open Ogg stream (reason: %s).", ErrorString(result).c_str());
VorbisCallbacks::fileBuffer.Close();
return;
}
vorbisInfo = ov_info(&ovFile, -1);
{
vorbis_comment* vorbisComment = ov_comment(&ovFile, -1);
vorbisTags.resize(vorbisComment->comments);
for (unsigned i = 0; i < vorbisComment->comments; ++i) {
vorbisTags[i] = std::string(vorbisComment->user_comments[i], vorbisComment->comment_lengths[i]);
}
vendor = std::string(vorbisComment->vendor);
// DisplayInfo();
}
if (vorbisInfo->channels == 1) {
format = AL_FORMAT_MONO16;
} else {
format = AL_FORMAT_STEREO16;
}
alGenBuffers(2, buffers);
CheckError("[COggStream::Play][1]");
if (!StartPlaying()) {
ReleaseBuffers();
} else {
stopped = false;
paused = false;
}
CheckError("[COggStream::Play][2]");
}
// stops the currently playing stream
void COggStream::Stop()
{
if (stopped)
return;
ReleaseBuffers();
msecsPlayed = spring_nulltime;
lastTick = spring_gettime();
source = 0;
format = 0;
vorbisInfo = nullptr;
assert(!Valid());
}
float COggStream::GetTotalTime()
{
return ov_time_total(&ovFile, -1);
}
// display Ogg info and comments
void COggStream::DisplayInfo()
{
LOG("[OggStream::%s]", __func__);
LOG("\tversion: %d", vorbisInfo->version);
LOG("\tchannels: %d", vorbisInfo->channels);
LOG("\ttime (sec): %lf", ov_time_total(&ovFile, -1));
LOG("\trate (Hz): %ld", vorbisInfo->rate);
LOG("\tbitrate (upper): %ld", vorbisInfo->bitrate_upper);
LOG("\tbitrate (nominal): %ld", vorbisInfo->bitrate_nominal);
LOG("\tbitrate (lower): %ld", vorbisInfo->bitrate_lower);
LOG("\tbitrate (window): %ld", vorbisInfo->bitrate_window);
LOG("\tvendor: %s", vendor.c_str());
LOG("\ttags: %lu", static_cast<unsigned long>(vorbisTags.size()));
for (const std::string& s: vorbisTags) {
LOG("\t\t%s", s.c_str());
}
}
// clean up the OpenAL resources
void COggStream::ReleaseBuffers()
{
stopped = true;
paused = false;
#if 0
EmptyBuffers();
#else
// alDeleteBuffers fails with AL_INVALID_OPERATION if either buffer
// is still bound to source, while alSourceUnqueueBuffers sometimes
// generates an AL_INVALID_VALUE but doesn't appear to be necessary
// since we can just detach both of them directly
alSourcei(source, AL_BUFFER, AL_NONE);
CheckError("[COggStream::ReleaseBuffers][1]");
#endif
alDeleteBuffers(2, buffers);
CheckError("[COggStream::ReleaseBuffers][2]");
memset(buffers, 0, sizeof(buffers));
ov_clear(&ovFile);
}
// returns true if both buffers were
// filled with data from the stream
bool COggStream::StartPlaying()
{
msecsPlayed = spring_nulltime;
lastTick = spring_gettime();
if (!DecodeStream(buffers[0]))
return false;
if (!DecodeStream(buffers[1]))
return false;
alSourceQueueBuffers(source, 2, buffers);
// CheckError returns true if *no* error occurred
if (!CheckError("[COggStream::StartPlaying][1]"))
return false;
alSourcePlay(source);
return (CheckError("[COggStream::StartPlaying][2]"));
}
// returns true if we're still playing
bool COggStream::IsPlaying()
{
ALenum state = 0;
alGetSourcei(source, AL_SOURCE_STATE, &state);
return (state == AL_PLAYING);
}
bool COggStream::TogglePause()
{
if (!stopped)
paused = !paused;
return paused;
}
// pop the processed buffers from the queue,
// refill them, and push them back in line
bool COggStream::UpdateBuffers()
{
int buffersProcessed = 0;
bool active = true;
alGetSourcei(source, AL_BUFFERS_PROCESSED, &buffersProcessed);
while (buffersProcessed-- > 0) {
ALuint buffer;
alSourceUnqueueBuffers(source, 1, &buffer);
CheckError("[COggStream::UpdateBuffers][1]");
// false if we've reached end of stream
if ((active = DecodeStream(buffer))) {
alSourceQueueBuffers(source, 1, &buffer);
CheckError("[COggStream::UpdateBuffers][2]");
}
}
return (active && CheckError("[COggStream::UpdateBuffers][3]"));
}
void COggStream::Update()
{
if (stopped)
return;
const spring_time tick = spring_gettime();
if (!paused) {
// releasing buffers is only allowed once the source has actually
// stopped playing, since it might still be reading from the last
// decoded chunk
if (UpdateBuffers(), !IsPlaying())
ReleaseBuffers();
msecsPlayed += (tick - lastTick);
}
lastTick = tick;
}
// read decoded data from audio stream into PCM buffer
bool COggStream::DecodeStream(ALuint buffer)
{
memset(pcmDecodeBuffer, 0, BUFFER_SIZE);
int size = 0;
int section = 0;
int result = 0;
while (size < BUFFER_SIZE) {
result = ov_read(&ovFile, pcmDecodeBuffer + size, BUFFER_SIZE - size, 0, 2, 1, §ion);
if (result > 0) {
size += result;
continue;
}
if (result < 0) {
LOG_L(L_WARNING, "Error reading Ogg stream (%s)", ErrorString(result).c_str());
continue;
}
break;
}
if (size == 0)
return false;
alBufferData(buffer, format, pcmDecodeBuffer, size, vorbisInfo->rate);
return (CheckError("[COggStream::DecodeStream]"));
}
// dequeue any buffers pending on source (unused, see ReleaseBuffers)
void COggStream::EmptyBuffers()
{
assert(source != 0);
#if 1
int queuedBuffers = 0;
alGetSourcei(source, AL_BUFFERS_QUEUED, &queuedBuffers);
CheckError("[COggStream::EmptyBuffers][1]");
while (queuedBuffers-- > 0) {
ALuint buffer;
alSourceUnqueueBuffers(source, 1, &buffer);
CheckError("[COggStream::EmptyBuffers][2]");
// done by caller
// alDeleteBuffers(1, &buffer);
}
#else
// assumes both are still pending
alSourceUnqueueBuffers(source, 2, buffers);
CheckError("[COggStream::EmptyBuffers]");
#endif
}
|