Chris@55: /** @file patest_write_stop_threads.c Chris@55: @brief Call Pa_StopStream() from another thread to see if PortAudio hangs. Chris@55: @author Bjorn Roche of XO Audio (www.xoaudio.com) Chris@55: @author Ross Bencina Chris@55: @author Phil Burk Chris@55: */ Chris@55: /* Chris@55: * $Id$ Chris@55: * Chris@55: * This program uses the PortAudio Portable Audio Library. Chris@55: * For more information see: http://www.portaudio.com/ Chris@55: * Copyright (c) 1999-2000 Ross Bencina and Phil Burk Chris@55: * Chris@55: * Permission is hereby granted, free of charge, to any person obtaining Chris@55: * a copy of this software and associated documentation files Chris@55: * (the "Software"), to deal in the Software without restriction, Chris@55: * including without limitation the rights to use, copy, modify, merge, Chris@55: * publish, distribute, sublicense, and/or sell copies of the Software, Chris@55: * and to permit persons to whom the Software is furnished to do so, Chris@55: * subject to the following conditions: Chris@55: * Chris@55: * The above copyright notice and this permission notice shall be Chris@55: * included in all copies or substantial portions of the Software. Chris@55: * Chris@55: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@55: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@55: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@55: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR Chris@55: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@55: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@55: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@55: */ Chris@55: Chris@55: /* Chris@55: * The text above constitutes the entire PortAudio license; however, Chris@55: * the PortAudio community also makes the following non-binding requests: Chris@55: * Chris@55: * Any person wishing to distribute modifications to the Software is Chris@55: * requested to send the modifications to the original developer so that Chris@55: * they can be incorporated into the canonical version. It is also Chris@55: * requested that these non-binding requests be included along with the Chris@55: * license above. Chris@55: */ Chris@55: Chris@55: #include Chris@55: #include Chris@55: #include Chris@55: #include Chris@55: /* pthread may only be available on Mac and Linux. */ Chris@55: #include Chris@55: #include "portaudio.h" Chris@55: Chris@55: #define SAMPLE_RATE (44100) Chris@55: #define FRAMES_PER_BUFFER (2048) Chris@55: Chris@55: static float s_buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */ Chris@55: Chris@55: /** Chris@55: * WARNING: PortAudio is NOT thread safe. DO NOT call PortAudio Chris@55: * from multiple threads without synchronization. This test uses Chris@55: * PA in an ILLEGAL WAY in order to try to flush out potential hang bugs. Chris@55: * The test calls Pa_WriteStream() and Pa_StopStream() simultaneously Chris@55: * from separate threads in order to try to cause Pa_StopStream() to hang. Chris@55: * In the main thread we write to the stream in a loop. Chris@55: * Then try stopping PA from another thread to see if it hangs. Chris@55: * Chris@55: * @note: Do not expect this test to pass. The test is only here Chris@55: * as a debugging aid for hang bugs. Since this test uses PA in an Chris@55: * illegal way, it may fail for reasons that are not PA bugs. Chris@55: */ Chris@55: Chris@55: /* Wait for awhile then abort the stream. */ Chris@55: void *stop_thread_proc(void *arg) Chris@55: { Chris@55: PaStream *stream = (PaStream *)arg; Chris@55: PaTime time; Chris@55: for (int i = 0; i < 20; i++) Chris@55: { Chris@55: /* ILLEGAL unsynchronised call to PA, see comment above */ Chris@55: time = Pa_GetStreamTime( stream ); Chris@55: printf("Stream time = %f\n", time); Chris@55: fflush(stdout); Chris@55: usleep(100 * 1000); Chris@55: } Chris@55: printf("Call Pa_StopStream()\n"); Chris@55: fflush(stdout); Chris@55: /* ILLEGAL unsynchronised call to PA, see comment above */ Chris@55: PaError err = Pa_StopStream( stream ); Chris@55: printf("Pa_StopStream() returned %d\n", err); Chris@55: fflush(stdout); Chris@55: Chris@55: return stream; Chris@55: } Chris@55: Chris@55: int main(void); Chris@55: int main(void) Chris@55: { Chris@55: PaStreamParameters outputParameters; Chris@55: PaStream *stream; Chris@55: PaError err; Chris@55: int result; Chris@55: pthread_t thread; Chris@55: Chris@55: printf( "PortAudio Test: output silence and stop from another thread. SR = %d, BufSize = %d\n", Chris@55: SAMPLE_RATE, FRAMES_PER_BUFFER); Chris@55: Chris@55: err = Pa_Initialize(); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ Chris@55: outputParameters.channelCount = 2; /* stereo output */ Chris@55: outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ Chris@55: outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency * 5; Chris@55: outputParameters.hostApiSpecificStreamInfo = NULL; Chris@55: Chris@55: /* open the stream */ Chris@55: err = Pa_OpenStream( Chris@55: &stream, Chris@55: NULL, /* no input */ Chris@55: &outputParameters, Chris@55: SAMPLE_RATE, Chris@55: FRAMES_PER_BUFFER, Chris@55: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@55: NULL, /* no callback, use blocking API */ Chris@55: NULL ); /* no callback, so no callback userData */ Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: result = pthread_create(&thread, NULL /* attributes */, stop_thread_proc, stream); Chris@55: Chris@55: /* start the stream */ Chris@55: err = Pa_StartStream( stream ); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: /* clear buffer */ Chris@55: memset( s_buffer, 0, sizeof(s_buffer) ); Chris@55: Chris@55: /* play the silent buffer many times */ Chris@55: while( Pa_IsStreamActive(stream) > 0 ) Chris@55: { Chris@55: err = Pa_WriteStream( stream, s_buffer, FRAMES_PER_BUFFER ); Chris@55: printf("Pa_WriteStream returns %d = %s\n", err, Pa_GetErrorText( err )); Chris@55: if( err != paNoError ) Chris@55: { Chris@55: err = paNoError; Chris@55: break; Chris@55: }; Chris@55: } Chris@55: Chris@55: printf("Try to join the thread that called Pa_StopStream().\n"); Chris@55: result = pthread_join( thread, NULL ); Chris@55: printf("pthread_join returned %d\n", result); Chris@55: Chris@55: /* close, and terminate */ Chris@55: printf("Call Pa_CloseStream\n"); Chris@55: err = Pa_CloseStream( stream ); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: Pa_Terminate(); Chris@55: printf("Test finished.\n"); Chris@55: Chris@55: return err; Chris@55: error: Chris@55: Pa_Terminate(); Chris@55: fprintf( stderr, "An error occured while using the portaudio stream\n" ); Chris@55: fprintf( stderr, "Error number: %d\n", err ); Chris@55: fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); Chris@55: return err; Chris@55: }