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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "OggStream.h"
#include <string.h> //memset
#include "System/FileSystem/FileHandler.h"
#include "System/Sound/SoundLog.h"
#include "ALShared.h"
#include "VorbisShared.h"
namespace VorbisCallbacks {
size_t VorbisStreamRead(void* ptr, size_t size, size_t nmemb, void* datasource)
{
CFileHandler* buffer = static_cast<CFileHandler*>(datasource);
return buffer->Read(ptr, size * nmemb);
}
int VorbisStreamClose(void* datasource)
{
CFileHandler* buffer = static_cast<CFileHandler*>(datasource);
delete buffer;
return 0;
}
int VorbisStreamSeek(void* datasource, ogg_int64_t offset, int whence)
{
CFileHandler* buffer = static_cast<CFileHandler*>(datasource);
if (whence == SEEK_SET)
{
buffer->Seek(offset, std::ios_base::beg);
}
else if (whence == SEEK_CUR)
{
buffer->Seek(offset, std::ios_base::cur);
}
else if (whence == SEEK_END)
{
buffer->Seek(offset, std::ios_base::end);
}
return 0;
}
long VorbisStreamTell(void* datasource)
{
CFileHandler* buffer = static_cast<CFileHandler*>(datasource);
return buffer->GetPos();
}
}
COggStream::COggStream(ALuint _source)
: vorbisInfo(NULL)
, source(_source)
, format(AL_FORMAT_MONO16)
, stopped(true)
, paused(false)
{
for (unsigned i = 0; i < NUM_BUFFERS; ++i) {
buffers[i] = 0;
}
for (unsigned i = 0; i < BUFFER_SIZE; ++i) {
pcmDecodeBuffer[i] = 0;
}
}
COggStream::~COggStream()
{
Stop();
}
// open an Ogg stream from a given file and start playing it
void COggStream::Play(const std::string& path, float volume)
{
if (!stopped) {
// we're already playing another stream
return;
}
vorbisTags.clear();
ov_callbacks vorbisCallbacks;
vorbisCallbacks.read_func = VorbisCallbacks::VorbisStreamRead;
vorbisCallbacks.close_func = VorbisCallbacks::VorbisStreamClose;
vorbisCallbacks.seek_func = VorbisCallbacks::VorbisStreamSeek;
vorbisCallbacks.tell_func = VorbisCallbacks::VorbisStreamTell;
CFileHandler* buf = new CFileHandler(path);
const int result = ov_open_callbacks(buf, &oggStream, NULL, 0, vorbisCallbacks);
if (result < 0) {
LOG_L(L_WARNING, "Could not open Ogg stream (reason: %s).",
ErrorString(result).c_str());
return;
}
vorbisInfo = ov_info(&oggStream, -1);
{
vorbis_comment* vorbisComment;
vorbisComment = ov_comment(&oggStream, -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");
if (!StartPlaying()) {
ReleaseBuffers();
} else {
stopped = false;
paused = false;
}
CheckError("COggStream::Play");
}
float COggStream::GetPlayTime() const
{
return msecsPlayed.toSecsf();
}
float COggStream::GetTotalTime()
{
return ov_time_total(&oggStream, -1);
}
bool COggStream::Valid() const
{
return (vorbisInfo != 0);
}
bool COggStream::IsFinished()
{
return !Valid() || (GetPlayTime() >= GetTotalTime());
}
const COggStream::TagVector& COggStream::VorbisTags() const
{
return vorbisTags;
}
// display Ogg info and comments
void COggStream::DisplayInfo()
{
LOG("version: %d", vorbisInfo->version);
LOG("channels: %d", vorbisInfo->channels);
LOG("time (sec): %lf", ov_time_total(&oggStream,-1));
LOG("rate (Hz): %ld", vorbisInfo->rate);
LOG("bitrate (upper): %ld", vorbisInfo->bitrate_upper);
LOG("bitrate (nominal): %ld", vorbisInfo->bitrate_nominal);
LOG("bitrate (lower): %ld", vorbisInfo->bitrate_lower);
LOG("bitrate (window): %ld", vorbisInfo->bitrate_window);
LOG("vendor: %s", vendor.c_str());
for (TagVector::const_iterator it = vorbisTags.begin(); it != vorbisTags.end(); ++it) {
LOG("%s", it->c_str());
}
}
// clean up the OpenAL resources
void COggStream::ReleaseBuffers()
{
stopped = true;
paused = false;
EmptyBuffers();
alDeleteBuffers(2, buffers);
CheckError("COggStream::ReleaseBuffers");
ov_clear(&oggStream);
}
// 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("COggStream::StartPlaying");
alSourcePlay(source); CheckError("COggStream::StartPlaying");
return true;
}
// returns true if we're still playing
bool COggStream::IsPlaying()
{
ALenum state = 0;
alGetSourcei(source, AL_SOURCE_STATE, &state);
return (state == AL_PLAYING);
}
// stops the currently playing stream
void COggStream::Stop()
{
if (stopped) {
return;
}
ReleaseBuffers();
msecsPlayed = spring_nulltime;
vorbisInfo = NULL;
lastTick = spring_gettime();
}
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");
// false if we've reached end of stream
active = DecodeStream(buffer);
if (active) {
alSourceQueueBuffers(source, 1, &buffer); CheckError("COggStream::UpdateBuffers");
}
}
CheckError("COggStream::UpdateBuffers");
return active;
}
void COggStream::Update()
{
if (stopped) {
return;
}
spring_time tick = spring_gettime();
if (!paused) {
UpdateBuffers();
if (!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(&oggStream, pcmDecodeBuffer + size, BUFFER_SIZE - size, 0, 2, 1, §ion);
if (result > 0) {
size += result;
} else {
if (result < 0) {
LOG_L(L_WARNING, "Error reading Ogg stream (%s)",
ErrorString(result).c_str());
} else {
break;
}
}
}
if (size == 0) {
return false;
}
alBufferData(buffer, format, pcmDecodeBuffer, size, vorbisInfo->rate);
CheckError("COggStream::DecodeStream");
return true;
}
// dequeue any buffers pending on source
void COggStream::EmptyBuffers()
{
int queuedBuffers = 0;
alGetSourcei(source, AL_BUFFERS_QUEUED, &queuedBuffers); CheckError("COggStream::EmptyBuffers");
while (queuedBuffers-- > 0) {
ALuint buffer;
alSourceUnqueueBuffers(source, 1, &buffer); CheckError("COggStream::EmptyBuffers");
}
}
|