annotate src/portaudio_20161030/test/patest_write_stop_hang_illegal.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 59a8758c56b1
children
rev   line source
cannam@140 1 /** @file patest_write_stop_threads.c
cannam@140 2 @brief Call Pa_StopStream() from another thread to see if PortAudio hangs.
cannam@140 3 @author Bjorn Roche of XO Audio (www.xoaudio.com)
cannam@140 4 @author Ross Bencina
cannam@140 5 @author Phil Burk
cannam@140 6 */
cannam@140 7 /*
cannam@140 8 * $Id$
cannam@140 9 *
cannam@140 10 * This program uses the PortAudio Portable Audio Library.
cannam@140 11 * For more information see: http://www.portaudio.com/
cannam@140 12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
cannam@140 13 *
cannam@140 14 * Permission is hereby granted, free of charge, to any person obtaining
cannam@140 15 * a copy of this software and associated documentation files
cannam@140 16 * (the "Software"), to deal in the Software without restriction,
cannam@140 17 * including without limitation the rights to use, copy, modify, merge,
cannam@140 18 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@140 19 * and to permit persons to whom the Software is furnished to do so,
cannam@140 20 * subject to the following conditions:
cannam@140 21 *
cannam@140 22 * The above copyright notice and this permission notice shall be
cannam@140 23 * included in all copies or substantial portions of the Software.
cannam@140 24 *
cannam@140 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@140 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@140 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@140 28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@140 29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@140 30 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@140 31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@140 32 */
cannam@140 33
cannam@140 34 /*
cannam@140 35 * The text above constitutes the entire PortAudio license; however,
cannam@140 36 * the PortAudio community also makes the following non-binding requests:
cannam@140 37 *
cannam@140 38 * Any person wishing to distribute modifications to the Software is
cannam@140 39 * requested to send the modifications to the original developer so that
cannam@140 40 * they can be incorporated into the canonical version. It is also
cannam@140 41 * requested that these non-binding requests be included along with the
cannam@140 42 * license above.
cannam@140 43 */
cannam@140 44
cannam@140 45 #include <stdio.h>
cannam@140 46 #include <unistd.h>
cannam@140 47 #include <math.h>
cannam@140 48 #include <memory.h>
cannam@140 49 /* pthread may only be available on Mac and Linux. */
cannam@140 50 #include <pthread.h>
cannam@140 51 #include "portaudio.h"
cannam@140 52
cannam@140 53 #define SAMPLE_RATE (44100)
cannam@140 54 #define FRAMES_PER_BUFFER (2048)
cannam@140 55
cannam@140 56 static float s_buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */
cannam@140 57
cannam@140 58 /**
cannam@140 59 * WARNING: PortAudio is NOT thread safe. DO NOT call PortAudio
cannam@140 60 * from multiple threads without synchronization. This test uses
cannam@140 61 * PA in an ILLEGAL WAY in order to try to flush out potential hang bugs.
cannam@140 62 * The test calls Pa_WriteStream() and Pa_StopStream() simultaneously
cannam@140 63 * from separate threads in order to try to cause Pa_StopStream() to hang.
cannam@140 64 * In the main thread we write to the stream in a loop.
cannam@140 65 * Then try stopping PA from another thread to see if it hangs.
cannam@140 66 *
cannam@140 67 * @note: Do not expect this test to pass. The test is only here
cannam@140 68 * as a debugging aid for hang bugs. Since this test uses PA in an
cannam@140 69 * illegal way, it may fail for reasons that are not PA bugs.
cannam@140 70 */
cannam@140 71
cannam@140 72 /* Wait for awhile then abort the stream. */
cannam@140 73 void *stop_thread_proc(void *arg)
cannam@140 74 {
cannam@140 75 PaStream *stream = (PaStream *)arg;
cannam@140 76 PaTime time;
cannam@140 77 for (int i = 0; i < 20; i++)
cannam@140 78 {
cannam@140 79 /* ILLEGAL unsynchronised call to PA, see comment above */
cannam@140 80 time = Pa_GetStreamTime( stream );
cannam@140 81 printf("Stream time = %f\n", time);
cannam@140 82 fflush(stdout);
cannam@140 83 usleep(100 * 1000);
cannam@140 84 }
cannam@140 85 printf("Call Pa_StopStream()\n");
cannam@140 86 fflush(stdout);
cannam@140 87 /* ILLEGAL unsynchronised call to PA, see comment above */
cannam@140 88 PaError err = Pa_StopStream( stream );
cannam@140 89 printf("Pa_StopStream() returned %d\n", err);
cannam@140 90 fflush(stdout);
cannam@140 91
cannam@140 92 return stream;
cannam@140 93 }
cannam@140 94
cannam@140 95 int main(void);
cannam@140 96 int main(void)
cannam@140 97 {
cannam@140 98 PaStreamParameters outputParameters;
cannam@140 99 PaStream *stream;
cannam@140 100 PaError err;
cannam@140 101 int result;
cannam@140 102 pthread_t thread;
cannam@140 103
cannam@140 104 printf( "PortAudio Test: output silence and stop from another thread. SR = %d, BufSize = %d\n",
cannam@140 105 SAMPLE_RATE, FRAMES_PER_BUFFER);
cannam@140 106
cannam@140 107 err = Pa_Initialize();
cannam@140 108 if( err != paNoError ) goto error;
cannam@140 109
cannam@140 110 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
cannam@140 111 outputParameters.channelCount = 2; /* stereo output */
cannam@140 112 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
cannam@140 113 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency * 5;
cannam@140 114 outputParameters.hostApiSpecificStreamInfo = NULL;
cannam@140 115
cannam@140 116 /* open the stream */
cannam@140 117 err = Pa_OpenStream(
cannam@140 118 &stream,
cannam@140 119 NULL, /* no input */
cannam@140 120 &outputParameters,
cannam@140 121 SAMPLE_RATE,
cannam@140 122 FRAMES_PER_BUFFER,
cannam@140 123 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@140 124 NULL, /* no callback, use blocking API */
cannam@140 125 NULL ); /* no callback, so no callback userData */
cannam@140 126 if( err != paNoError ) goto error;
cannam@140 127
cannam@140 128 result = pthread_create(&thread, NULL /* attributes */, stop_thread_proc, stream);
cannam@140 129
cannam@140 130 /* start the stream */
cannam@140 131 err = Pa_StartStream( stream );
cannam@140 132 if( err != paNoError ) goto error;
cannam@140 133
cannam@140 134 /* clear buffer */
cannam@140 135 memset( s_buffer, 0, sizeof(s_buffer) );
cannam@140 136
cannam@140 137 /* play the silent buffer many times */
cannam@140 138 while( Pa_IsStreamActive(stream) > 0 )
cannam@140 139 {
cannam@140 140 err = Pa_WriteStream( stream, s_buffer, FRAMES_PER_BUFFER );
cannam@140 141 printf("Pa_WriteStream returns %d = %s\n", err, Pa_GetErrorText( err ));
cannam@140 142 if( err != paNoError )
cannam@140 143 {
cannam@140 144 err = paNoError;
cannam@140 145 break;
cannam@140 146 };
cannam@140 147 }
cannam@140 148
cannam@140 149 printf("Try to join the thread that called Pa_StopStream().\n");
cannam@140 150 result = pthread_join( thread, NULL );
cannam@140 151 printf("pthread_join returned %d\n", result);
cannam@140 152
cannam@140 153 /* close, and terminate */
cannam@140 154 printf("Call Pa_CloseStream\n");
cannam@140 155 err = Pa_CloseStream( stream );
cannam@140 156 if( err != paNoError ) goto error;
cannam@140 157
cannam@140 158 Pa_Terminate();
cannam@140 159 printf("Test finished.\n");
cannam@140 160
cannam@140 161 return err;
cannam@140 162 error:
cannam@140 163 Pa_Terminate();
cannam@140 164 fprintf( stderr, "An error occured while using the portaudio stream\n" );
cannam@140 165 fprintf( stderr, "Error number: %d\n", err );
cannam@140 166 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
cannam@140 167 return err;
cannam@140 168 }