view EXAMPLES/playstream.c @ 48:00b770b0d2aa

Workaround: There is a terrible OpenAL regression bug in iOS 5 dealing with streaming sources. alSourcei(source_id, AL_BUFFER, AL_NONE); fails to clear queued buffer queues on a streaming source. The workaround involves manually dequeuing the individual buffers before calling alSourcei(source_id, AL_BUFFER, AL_NONE);. But there is an additional race condition bug where the unqueue fails to take, so the included workaround keeps looping until the buffers finally report as cleared. The current check is compiled only for iOS and does a runtime version check against CoreFoundation version 674.0. When Apple finally ships a fix, this fix should be amended to not run on fixed versions. Also ironically, it was originally the Apple Mac OpenAL implementation that had a problem with buffer unqueuing which ultimately led to the use of alSourcei(source_id, AL_BUFFER, AL_NONE); This is another reason the workaround is only constrained to iOS. In some cases I've seen, the buffer unqueue also triggers OpenAL errors, however, the current workaround seems to usually avoid those OpenAL errors (even though the buffers fail to unqueue some times). The 20ms sleep seems to avoid the race condition entirely, but when it doesn't, the sleep seems to do something that magically avoids tripping OpenAL errors.
author Eric Wing <ewing@anscamobile.com>
date Fri, 30 Sep 2011 16:44:08 -0700
parents ee50db043251
children b1e13d5688d1
line wrap: on
line source

#include "ALmixer.h"
#include <stdio.h>
#include <stdlib.h>

#ifdef ALMIXER_COMPILE_WITHOUT_SDL
	#if defined(_WIN32)
		#define WIN32_LEAN_AND_MEAN
		#include <windows.h>
	#else
   	  	#include <unistd.h>
	#endif

	static void Internal_Delay(ALuint milliseconds_delay)
	{
		#if defined(_WIN32)
		Sleep(milliseconds_delay);
		#else
		usleep(milliseconds_delay);
		#endif
	}
#else
	#include "SDL.h"
	#define Internal_Delay SDL_Delay
#endif
#define MAX_SOURCES 16

ALboolean g_PlayingAudio[MAX_SOURCES];

void Internal_SoundFinished_CallbackIntercept(ALint which_channel, ALuint al_source, ALmixer_Data* almixer_data, ALboolean finished_naturally, void* user_data)
{
	fprintf(stderr, "Channel %d finished\n", which_channel);
	g_PlayingAudio[which_channel] = AL_FALSE;
}

int main(int argc, char* argv[])
{
	ALint i;
	ALboolean still_playing = AL_TRUE;

	ALmixer_Data* audio_data[MAX_SOURCES];

	if(argc < 1)
	{
		printf("Pass a sound file (or files) as a parameter\n");
	}
	else if(argc-1 > MAX_SOURCES)
	{
		printf("Maximum supported files is %d\n", MAX_SOURCES);
	}
	ALmixer_Init(ALMIXER_DEFAULT_FREQUENCY, ALMIXER_DEFAULT_NUM_SOURCES, ALMIXER_DEFAULT_REFRESH);
	
	for(i=1; i<argc; i++)
	{
		if(!(audio_data[i-1]=ALmixer_LoadStream( argv[i], ALMIXER_DEFAULT_BUFFERSIZE, ALMIXER_DEFAULT_QUEUE_BUFFERS, ALMIXER_DEFAULT_STARTUP_BUFFERS, AL_FALSE) ))
/*
		if(!(audio_data[i-1]=ALmixer_LoadStream( argv[i], 4096, 2, 1, AL_FALSE) ))
		if(!(audio_data[i-1]=ALmixer_LoadStream( argv[i], 4096, 5 * 4, 2, AL_FALSE) ))
*/
		{
			printf("%s. Quiting program.\n", ALmixer_GetError());
			exit(0);
		}
	}

	ALmixer_SetPlaybackFinishedCallback(Internal_SoundFinished_CallbackIntercept, NULL);

	for(i=1; i<argc; i++)
	{
		g_PlayingAudio[i-1] = AL_TRUE;
		ALmixer_PlayChannel(i-1, audio_data[i-1], 0);
	}

	while(still_playing)
	{
		still_playing = AL_FALSE;
		for(i=1; i<argc; i++)
		{
			still_playing |= g_PlayingAudio[i-1];
		}
		ALmixer_Update();
		Internal_Delay(10);
	}
		
	for(i=1; i<argc; i++)
	{
		ALmixer_FreeData(audio_data[i-1]);
	}

	ALmixer_Quit();

	return 0;
}