PageRenderTime 72ms CodeModel.GetById 39ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llaudio/llstreamingaudio_fmod.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 356 lines | 242 code | 53 blank | 61 comment | 27 complexity | 726681fcfdb7f76fb029389bfd6b7fa0 MD5 | raw file
  1/** 
  2 * @file streamingaudio_fmod.cpp
  3 * @brief LLStreamingAudio_FMOD implementation
  4 *
  5 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "llmath.h"
 30
 31#include "fmod.h"
 32#include "fmod_errors.h"
 33
 34#include "llstreamingaudio_fmod.h"
 35
 36
 37class LLAudioStreamManagerFMOD
 38{
 39public:
 40	LLAudioStreamManagerFMOD(const std::string& url);
 41	int	startStream();
 42	bool stopStream(); // Returns true if the stream was successfully stopped.
 43	bool ready();
 44
 45	const std::string& getURL() 	{ return mInternetStreamURL; }
 46
 47	int getOpenState();
 48protected:
 49	FSOUND_STREAM* mInternetStream;
 50	bool mReady;
 51
 52	std::string mInternetStreamURL;
 53};
 54
 55
 56
 57//---------------------------------------------------------------------------
 58// Internet Streaming
 59//---------------------------------------------------------------------------
 60LLStreamingAudio_FMOD::LLStreamingAudio_FMOD() :
 61	mCurrentInternetStreamp(NULL),
 62	mFMODInternetStreamChannel(-1),
 63	mGain(1.0f)
 64{
 65	// Number of milliseconds of audio to buffer for the audio card.
 66	// Must be larger than the usual Second Life frame stutter time.
 67	FSOUND_Stream_SetBufferSize(200);
 68
 69	// Here's where we set the size of the network buffer and some buffering 
 70	// parameters.  In this case we want a network buffer of 16k, we want it 
 71	// to prebuffer 40% of that when we first connect, and we want it 
 72	// to rebuffer 80% of that whenever we encounter a buffer underrun.
 73
 74	// Leave the net buffer properties at the default.
 75	//FSOUND_Stream_Net_SetBufferProperties(20000, 40, 80);
 76}
 77
 78
 79LLStreamingAudio_FMOD::~LLStreamingAudio_FMOD()
 80{
 81	// nothing interesting/safe to do.
 82}
 83
 84
 85void LLStreamingAudio_FMOD::start(const std::string& url)
 86{
 87	//if (!mInited)
 88	//{
 89	//	llwarns << "startInternetStream before audio initialized" << llendl;
 90	//	return;
 91	//}
 92
 93	// "stop" stream but don't clear url, etc. in case url == mInternetStreamURL
 94	stop();
 95
 96	if (!url.empty())
 97	{
 98		llinfos << "Starting internet stream: " << url << llendl;
 99		mCurrentInternetStreamp = new LLAudioStreamManagerFMOD(url);
100		mURL = url;
101	}
102	else
103	{
104		llinfos << "Set internet stream to null" << llendl;
105		mURL.clear();
106	}
107}
108
109
110void LLStreamingAudio_FMOD::update()
111{
112	// Kill dead internet streams, if possible
113	std::list<LLAudioStreamManagerFMOD *>::iterator iter;
114	for (iter = mDeadStreams.begin(); iter != mDeadStreams.end();)
115	{
116		LLAudioStreamManagerFMOD *streamp = *iter;
117		if (streamp->stopStream())
118		{
119			llinfos << "Closed dead stream" << llendl;
120			delete streamp;
121			mDeadStreams.erase(iter++);
122		}
123		else
124		{
125			iter++;
126		}
127	}
128
129	// Don't do anything if there are no streams playing
130	if (!mCurrentInternetStreamp)
131	{
132		return;
133	}
134
135	int open_state = mCurrentInternetStreamp->getOpenState();
136
137	if (!open_state)
138	{
139		// Stream is live
140
141		// start the stream if it's ready
142		if (mFMODInternetStreamChannel < 0)
143		{
144			mFMODInternetStreamChannel = mCurrentInternetStreamp->startStream();
145
146			if (mFMODInternetStreamChannel != -1)
147			{
148				// Reset volume to previously set volume
149				setGain(getGain());
150				FSOUND_SetPaused(mFMODInternetStreamChannel, false);
151			}
152		}
153	}
154		
155	switch(open_state)
156	{
157	default:
158	case 0:
159		// success
160		break;
161	case -1:
162		// stream handle is invalid
163		llwarns << "InternetStream - invalid handle" << llendl;
164		stop();
165		return;
166	case -2:
167		// opening
168		break;
169	case -3:
170		// failed to open, file not found, perhaps
171		llwarns << "InternetStream - failed to open" << llendl;
172		stop();
173		return;
174	case -4:
175		// connecting
176		break;
177	case -5:
178		// buffering
179		break;
180	}
181
182}
183
184void LLStreamingAudio_FMOD::stop()
185{
186	if (mFMODInternetStreamChannel != -1)
187	{
188		FSOUND_SetPaused(mFMODInternetStreamChannel, true);
189		FSOUND_SetPriority(mFMODInternetStreamChannel, 0);
190		mFMODInternetStreamChannel = -1;
191	}
192
193	if (mCurrentInternetStreamp)
194	{
195		llinfos << "Stopping internet stream: " << mCurrentInternetStreamp->getURL() << llendl;
196		if (mCurrentInternetStreamp->stopStream())
197		{
198			delete mCurrentInternetStreamp;
199		}
200		else
201		{
202			llwarns << "Pushing stream to dead list: " << mCurrentInternetStreamp->getURL() << llendl;
203			mDeadStreams.push_back(mCurrentInternetStreamp);
204		}
205		mCurrentInternetStreamp = NULL;
206		//mURL.clear();
207	}
208}
209
210void LLStreamingAudio_FMOD::pause(int pauseopt)
211{
212	if (pauseopt < 0)
213	{
214		pauseopt = mCurrentInternetStreamp ? 1 : 0;
215	}
216
217	if (pauseopt)
218	{
219		if (mCurrentInternetStreamp)
220		{
221			stop();
222		}
223	}
224	else
225	{
226		start(getURL());
227	}
228}
229
230
231// A stream is "playing" if it has been requested to start.  That
232// doesn't necessarily mean audio is coming out of the speakers.
233int LLStreamingAudio_FMOD::isPlaying()
234{
235	if (mCurrentInternetStreamp)
236	{
237		return 1; // Active and playing
238	}
239	else if (!mURL.empty())
240	{
241		return 2; // "Paused"
242	}
243	else
244	{
245		return 0;
246	}
247}
248
249
250F32 LLStreamingAudio_FMOD::getGain()
251{
252	return mGain;
253}
254
255
256std::string LLStreamingAudio_FMOD::getURL()
257{
258	return mURL;
259}
260
261
262void LLStreamingAudio_FMOD::setGain(F32 vol)
263{
264	mGain = vol;
265
266	if (mFMODInternetStreamChannel != -1)
267	{
268		vol = llclamp(vol * vol, 0.f, 1.f);
269		int vol_int = llround(vol * 255.f);
270		FSOUND_SetVolumeAbsolute(mFMODInternetStreamChannel, vol_int);
271	}
272}
273
274
275///////////////////////////////////////////////////////
276// manager of possibly-multiple internet audio streams
277
278LLAudioStreamManagerFMOD::LLAudioStreamManagerFMOD(const std::string& url) :
279	mInternetStream(NULL),
280	mReady(false)
281{
282	mInternetStreamURL = url;
283	mInternetStream = FSOUND_Stream_Open(url.c_str(), FSOUND_NORMAL | FSOUND_NONBLOCKING, 0, 0);
284	if (!mInternetStream)
285	{
286		llwarns << "Couldn't open fmod stream, error "
287			<< FMOD_ErrorString(FSOUND_GetError())
288			<< llendl;
289		mReady = false;
290		return;
291	}
292
293	mReady = true;
294}
295
296int LLAudioStreamManagerFMOD::startStream()
297{
298	// We need a live and opened stream before we try and play it.
299	if (!mInternetStream || getOpenState())
300	{
301		llwarns << "No internet stream to start playing!" << llendl;
302		return -1;
303	}
304
305	// Make sure the stream is set to 2D mode.
306	FSOUND_Stream_SetMode(mInternetStream, FSOUND_2D);
307
308	return FSOUND_Stream_PlayEx(FSOUND_FREE, mInternetStream, NULL, true);
309}
310
311bool LLAudioStreamManagerFMOD::stopStream()
312{
313	if (mInternetStream)
314	{
315		int read_percent = 0;
316		int status = 0;
317		int bitrate = 0;
318		unsigned int flags = 0x0;
319		FSOUND_Stream_Net_GetStatus(mInternetStream, &status, &read_percent, &bitrate, &flags);
320
321		bool close = true;
322		switch (status)
323		{
324		case FSOUND_STREAM_NET_CONNECTING:
325			close = false;
326			break;
327		case FSOUND_STREAM_NET_NOTCONNECTED:
328		case FSOUND_STREAM_NET_BUFFERING:
329		case FSOUND_STREAM_NET_READY:
330		case FSOUND_STREAM_NET_ERROR:
331		default:
332			close = true;
333		}
334
335		if (close)
336		{
337			FSOUND_Stream_Close(mInternetStream);
338			mInternetStream = NULL;
339			return true;
340		}
341		else
342		{
343			return false;
344		}
345	}
346	else
347	{
348		return true;
349	}
350}
351
352int LLAudioStreamManagerFMOD::getOpenState()
353{
354	int open_state = FSOUND_Stream_GetOpenState(mInternetStream);
355	return open_state;
356}