-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaudiostream.cpp
148 lines (123 loc) · 4.05 KB
/
audiostream.cpp
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
/* smushplay - A simple LucasArts SMUSH video player
*
* smushplay is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
// Based on the xoreos code of the same name, which is in turn
// based on the RawStream code of ScummVM (GPLv3+ and GPLv2+,
// respectively).
#include <assert.h>
#include <SDL_thread.h>
#include <queue>
#include "audiostream.h"
class QueuingAudioStreamImpl : public QueuingAudioStream {
private:
/**
* We queue a number of (pointers to) audio stream objects.
* In addition, we need to remember for each stream whether
* to dispose it after all data has been read from it.
* Hence, we don't store pointers to stream objects directly,
* but rather StreamHolder structs.
*/
struct StreamHolder {
AudioStream *_stream;
bool _disposeAfterUse;
StreamHolder(AudioStream *stream, bool disposeAfterUse)
: _stream(stream),
_disposeAfterUse(disposeAfterUse) {}
};
/**
* The sampling rate of this audio stream.
*/
const int _rate;
/**
* The number of channels in the stream.
*/
const int _channels;
/**
* This flag is set by the finish() method only. See there for more details.
*/
bool _finished;
/**
* A mutex to avoid access problems (causing e.g. corruption of
* the linked list) in thread aware environments.
*/
SDL_mutex *_mutex;
/**
* The queue of audio streams.
*/
std::queue<StreamHolder> _queue;
public:
QueuingAudioStreamImpl(int rate, int channels) : _rate(rate), _channels(channels), _finished(false) {
_mutex = SDL_CreateMutex();
}
~QueuingAudioStreamImpl();
// Implement the AudioStream API
virtual int readBuffer(int16 *buffer, const int numSamples);
virtual int getChannels() const { return _channels; }
virtual int getRate() const { return _rate; }
virtual bool endOfData() const {
// TODO: Lock mutex?
return _queue.empty();
}
virtual bool endOfStream() const { return _finished && _queue.empty(); }
// Implement the QueuingAudioStream API
virtual void queueAudioStream(AudioStream *stream, bool disposeAfterUse);
virtual void finish() { _finished = true; }
uint32 getQueuedStreamCount() const {
// TODO: Lock mutex?
return _queue.size();
}
};
QueuingAudioStreamImpl::~QueuingAudioStreamImpl() {
SDL_DestroyMutex(_mutex);
while (!_queue.empty()) {
StreamHolder tmp = _queue.front();
_queue.pop();
if (tmp._disposeAfterUse)
delete tmp._stream;
}
}
void QueuingAudioStreamImpl::queueAudioStream(AudioStream *stream, bool disposeAfterUse) {
assert(!_finished);
assert(stream->getRate() == getRate());
assert(stream->getChannels() == getChannels());
SDL_mutexP(_mutex);
_queue.push(StreamHolder(stream, disposeAfterUse));
SDL_mutexV(_mutex);
}
int QueuingAudioStreamImpl::readBuffer(int16 *buffer, const int numSamples) {
int samplesDecoded = 0;
SDL_mutexP(_mutex);
while (samplesDecoded < numSamples && !_queue.empty()) {
AudioStream *stream = _queue.front()._stream;
samplesDecoded += stream->readBuffer(buffer + samplesDecoded, numSamples - samplesDecoded);
if (stream->endOfData()) {
StreamHolder tmp = _queue.front();
_queue.pop();
if (tmp._disposeAfterUse)
delete stream;
}
}
SDL_mutexV(_mutex);
return samplesDecoded;
}
QueuingAudioStream *makeQueuingAudioStream(int rate, int channels) {
return new QueuingAudioStreamImpl(rate, channels);
}