To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at https://github.com/sonic-visualiser/sv-dependency-builds .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / src / portaudio_20161030_catalina_patch / test / patest_write_stop_hang_illegal.c @ 162:d43aab368df9

History | View | Annotate | Download (5.89 KB)

1
/** @file patest_write_stop_threads.c
2
        @brief Call Pa_StopStream() from another thread to see if PortAudio hangs.
3
        @author Bjorn Roche of XO Audio (www.xoaudio.com)
4
        @author Ross Bencina
5
        @author Phil Burk
6
*/
7
/*
8
 * $Id$
9
 *
10
 * This program uses the PortAudio Portable Audio Library.
11
 * For more information see: http://www.portaudio.com/
12
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining
15
 * a copy of this software and associated documentation files
16
 * (the "Software"), to deal in the Software without restriction,
17
 * including without limitation the rights to use, copy, modify, merge,
18
 * publish, distribute, sublicense, and/or sell copies of the Software,
19
 * and to permit persons to whom the Software is furnished to do so,
20
 * subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be
23
 * included in all copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
 */
33

    
34
/*
35
 * The text above constitutes the entire PortAudio license; however,
36
 * the PortAudio community also makes the following non-binding requests:
37
 *
38
 * Any person wishing to distribute modifications to the Software is
39
 * requested to send the modifications to the original developer so that
40
 * they can be incorporated into the canonical version. It is also
41
 * requested that these non-binding requests be included along with the
42
 * license above.
43
 */
44

    
45
#include <stdio.h>
46
#include <unistd.h>
47
#include <math.h>
48
#include <memory.h>
49
/* pthread may only be available on Mac and Linux. */
50
#include <pthread.h>
51
#include "portaudio.h"
52

    
53
#define SAMPLE_RATE         (44100)
54
#define FRAMES_PER_BUFFER   (2048)
55

    
56
static float s_buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */
57

    
58
/**
59
 * WARNING: PortAudio is NOT thread safe. DO NOT call PortAudio
60
 * from multiple threads without synchronization. This test uses
61
 * PA in an ILLEGAL WAY in order to try to flush out potential hang bugs.
62
 * The test calls Pa_WriteStream() and Pa_StopStream() simultaneously
63
 * from separate threads in order to try to cause Pa_StopStream() to hang.
64
 * In the main thread we write to the stream in a loop.
65
 * Then try stopping PA from another thread to see if it hangs.
66
 *
67
 * @note: Do not expect this test to pass. The test is only here
68
 * as a debugging aid for hang bugs. Since this test uses PA in an
69
 * illegal way, it may fail for reasons that are not PA bugs.
70
 */
71

    
72
/* Wait for awhile then abort the stream. */
73
void *stop_thread_proc(void *arg)
74
{
75
    PaStream *stream = (PaStream *)arg;
76
    PaTime time;
77
    for (int i = 0; i < 20; i++)
78
    {
79
        /* ILLEGAL unsynchronised call to PA, see comment above */
80
        time = Pa_GetStreamTime( stream );
81
        printf("Stream time = %f\n", time);
82
        fflush(stdout);
83
        usleep(100 * 1000);
84
    }
85
    printf("Call Pa_StopStream()\n");
86
    fflush(stdout);
87
    /* ILLEGAL unsynchronised call to PA, see comment above */
88
    PaError err = Pa_StopStream( stream );
89
    printf("Pa_StopStream() returned %d\n", err);
90
    fflush(stdout);
91

    
92
    return stream;
93
}
94

    
95
int main(void);
96
int main(void)
97
{
98
    PaStreamParameters outputParameters;
99
    PaStream *stream;
100
    PaError err;
101
    int result;
102
    pthread_t thread;
103

    
104
    printf( "PortAudio Test: output silence and stop from another thread. SR = %d, BufSize = %d\n",
105
            SAMPLE_RATE, FRAMES_PER_BUFFER);
106

    
107
    err = Pa_Initialize();
108
    if( err != paNoError ) goto error;
109

    
110
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
111
    outputParameters.channelCount = 2;       /* stereo output */
112
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
113
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency * 5;
114
    outputParameters.hostApiSpecificStreamInfo = NULL;
115

    
116
    /* open the stream */
117
    err = Pa_OpenStream(
118
              &stream,
119
              NULL, /* no input */
120
              &outputParameters,
121
              SAMPLE_RATE,
122
              FRAMES_PER_BUFFER,
123
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
124
              NULL, /* no callback, use blocking API */
125
              NULL ); /* no callback, so no callback userData */
126
    if( err != paNoError ) goto error;
127

    
128
    result = pthread_create(&thread, NULL /* attributes */, stop_thread_proc, stream);
129

    
130
    /* start the stream */
131
    err = Pa_StartStream( stream );
132
    if( err != paNoError ) goto error;
133

    
134
    /* clear buffer */
135
    memset( s_buffer, 0, sizeof(s_buffer) );
136

    
137
    /* play the silent buffer many times */
138
    while( Pa_IsStreamActive(stream) > 0 )
139
    {
140
        err = Pa_WriteStream( stream, s_buffer, FRAMES_PER_BUFFER );
141
        printf("Pa_WriteStream returns %d = %s\n", err, Pa_GetErrorText( err ));
142
        if( err != paNoError )
143
        {
144
            err = paNoError;
145
            break;
146
        };
147
    }
148

    
149
    printf("Try to join the thread that called Pa_StopStream().\n");
150
    result = pthread_join( thread, NULL );
151
    printf("pthread_join returned %d\n", result);
152

    
153
    /* close, and terminate */
154
    printf("Call Pa_CloseStream\n");
155
    err = Pa_CloseStream( stream );
156
    if( err != paNoError ) goto error;
157

    
158
    Pa_Terminate();
159
    printf("Test finished.\n");
160

    
161
    return err;
162
error:
163
    Pa_Terminate();
164
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
165
    fprintf( stderr, "Error number: %d\n", err );
166
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
167
    return err;
168
}