/
OAL_Stream.cpp
187 lines (151 loc) · 4.4 KB
/
OAL_Stream.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
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
/*
* Copyright 2007-2010 (C) - Frictional Games
*
* This file is part of OALWrapper
*
* For conditions of distribution and use, see copyright notice in LICENSE
*/
#include "OALWrapper/OAL_Types.h"
#include "OALWrapper/OAL_Device.h"
#include "OALWrapper/OAL_Helper.h"
#include "OALWrapper/OAL_Stream.h"
#include "OALWrapper/OAL_Source.h"
#include "OALWrapper/OAL_Buffer.h"
#include <cstdlib>
//-----------------------------------------------------------------------------------
unsigned int cOAL_Stream::mlBufferSize = STREAMING_BLOCK_SIZE;
unsigned int cOAL_Stream::mlBufferCount = 4;
//-----------------------------------------------------------------------------------
cOAL_Stream::cOAL_Stream() : iOAL_AudioData(eOAL_AudioDataType_Stream, mlBufferCount), mpBoundSource(NULL)
{
mpPCMBuffer = (char*)malloc(mlBufferSize*sizeof(char));
mvOALBufferIDs = (ALuint*)malloc(mlBufferCount*sizeof(ALuint));
for(int i=0;i<(int)mlBufferCount;++i)
mvOALBufferIDs[i] = mvBuffers[i]->GetObjectID();
mfProcessedBuffersTime = 0;
}
cOAL_Stream::~cOAL_Stream()
{
if(mpBoundSource != NULL)
mpBoundSource->Stop();
free(mpPCMBuffer);
free(mvOALBufferIDs);
}
//-----------------------------------------------------------------------------------
void cOAL_Stream::Update()
{
if(mpBoundSource==NULL)
return;
//unsigned long lTimeStart = hpl::cPlatform::GetApplicationTime();
//unsigned long lTimeToRebuffer = 0;
if(mbNeedsRebuffering)
{
bool bPlaying = mpBoundSource->mbPlaying;
bool bPaused = mpBoundSource->mbPaused;
mpBoundSource->LowLevelStop();
mlBuffersUsed=0;
mfProcessedBuffersTime=GetTime();
int lQueuedBuffers = mpBoundSource->GetQueuedBuffers();
for(int i=0;i<lQueuedBuffers;++i)
mpBoundSource->Unqueue();
for(int i=0;i<(int)mvBuffers.size();++i)
{
cOAL_Buffer* pBuffer = mvBuffers[i];
if(Stream(pBuffer))
{
++mlBuffersUsed;
mpBoundSource->Queue(pBuffer);
}
}
if(mlBuffersUsed!=0)
mbNeedsRebuffering = false;
if(bPlaying && bPaused==false)
mpBoundSource->LowLevelPlay();
//lTimeToRebuffer = hpl::cPlatform::GetApplicationTime()-lTimeStart;
//hpl::Log("Stream rebuffering took %d ms\n", lTimeToRebuffer);
}
if(mlBuffersUsed==0)
return;
int lProcessedBuffers = mpBoundSource->GetProcessedBuffers();
// For every buffer that has been played, unqueue it, refill it with streamed data and enqueue it again
while(lProcessedBuffers)
{
// Then perform the actual streaming
cOAL_Buffer* pBuffer = mpBoundSource->Unqueue();
mfProcessedBuffersTime += pBuffer->GetBufferTime();
if(mfProcessedBuffersTime > mfTotalTime)
mfProcessedBuffersTime-=mfTotalTime;
if(Stream(pBuffer))
{
mpBoundSource->Queue(pBuffer);
}
else
{
if(mbEOF && (mbLoop || mpBoundSource->mbLoop))
{
Seek(0, false);
Stream(pBuffer);
mpBoundSource->Queue(pBuffer);
}
}
lProcessedBuffers--;
}
//hpl::Log("Stream update took %d ms\n", hpl::cPlatform::GetApplicationTime()-(lTimeStart+lTimeToRebuffer));
}
//-----------------------------------------------------------------------------------
void cOAL_Stream::DoBuffering()
{
}
//-----------------------------------------------------------------------------------
bool cOAL_Stream::HasBufferUnderrun()
{
if(mpBoundSource==NULL)
return false;
if(mbEOF==false)
{
//hpl::Log("Have not reached end of file\n");
return true;
}
if(mpBoundSource->GetQueuedBuffers() != mpBoundSource->GetProcessedBuffers())
{
//hpl::Log("Source still has %d buffers to play\n", mpBoundSource->GetQueuedBuffers());
return true;
}
return false;
}
//-----------------------------------------------------------------------------------
/*
void cOAL_Stream::Log( eOAL_LogVerbose aVerboseLevelReq, eOAL_LogMsg aMessageType, const char* asMessage, ...)
{
if(asMessage == NULL)
return;
if(!mbLogEnabled)
return;
if(aVerboseLevelReq < mLogVerboseLevel)
return;
string sMessage;
char text[2048];
va_list ap;
va_start(ap, asMessage);
vsprintf(text, asMessage, ap);
va_end(ap);
char streamId[20];
sprintf(streamId, "Stream %s - ", msFilename.c_str());
switch(aMessageType)
{
case eOAL_LogMsg_Command:
sMessage.append("[COMMAND] ");
break;
case eOAL_LogMsg_Info:
sMessage.append("[INFO] ");
break;
case eOAL_LogMsg_Error:
sMessage.append("[ERROR] ");
default:
break;
}
sMessage.append(streamId).append(text);
Write(sMessage);
}
*/
//-----------------------------------------------------------------------------------