Skip to content

Commit

Permalink
Map PaStream to PaUtilStreamRepresentation
Browse files Browse the repository at this point in the history
This change tells the compiler that a PaStream is really a
PaUtilStreamRepresentation. Note the struct is still opaque to users, as
they do not have access to the definition of that struct.

The upside is reduced need for casts in the PortAudio implementation, as
well as a possibly nicer debugging experience, as debuggers will now
understand the contents of a PaStream.

The downside is the typedef is now slightly uglier as it names a type
that only the implementation is supposed to know about. This is purely
cosmetic though as users do not have access to the definition of the
struct.
  • Loading branch information
dechamps committed May 28, 2024
1 parent 0d8c084 commit b11341b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 46 deletions.
2 changes: 1 addition & 1 deletion include/portaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ PaError Pa_IsFormatSupported( const PaStreamParameters *inputParameters,
Pa_GetStreamTime, Pa_GetStreamCpuLoad
*/
typedef struct PaStream PaStream;
typedef struct PaUtilStreamRepresentation PaStream;


/** Can be passed as the framesPerBuffer parameter to Pa_OpenStream()
Expand Down
46 changes: 23 additions & 23 deletions src/common/pa_front.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ static int FindHostApi( PaDeviceIndex device, int *hostSpecificDeviceIndex )

static void AddOpenStream( PaStream* stream )
{
((PaUtilStreamRepresentation*)stream)->nextOpenStream = firstOpenStream_;
firstOpenStream_ = (PaUtilStreamRepresentation*)stream;
(stream)->nextOpenStream = firstOpenStream_;
firstOpenStream_ = stream;
}


Expand Down Expand Up @@ -1372,7 +1372,7 @@ PaError PaUtil_ValidateStreamPointer( PaStream* stream )

if( stream == NULL ) return paBadStreamPtr;

if( ((PaUtilStreamRepresentation*)stream)->magic != PA_STREAM_MAGIC )
if( (stream)->magic != PA_STREAM_MAGIC )
return paBadStreamPtr;

return paNoError;
Expand All @@ -1394,7 +1394,7 @@ PaError Pa_CloseStream( PaStream* stream )

if( result == paNoError )
{
interface = PA_STREAM_INTERFACE(stream);
interface = stream->streamInterface;

/* abort the stream if it isn't stopped */
result = interface->IsStopped( stream );
Expand Down Expand Up @@ -1423,14 +1423,14 @@ PaError Pa_SetStreamFinishedCallback( PaStream *stream, PaStreamFinishedCallback

if( result == paNoError )
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
result = stream->streamInterface->IsStopped( stream );
if( result == 0 )
{
result = paStreamIsNotStopped ;
}
if( result == 1 )
{
PA_STREAM_REP( stream )->streamFinishedCallback = streamFinishedCallback;
stream->streamFinishedCallback = streamFinishedCallback;
result = paNoError;
}
}
Expand All @@ -1451,14 +1451,14 @@ PaError Pa_StartStream( PaStream *stream )

if( result == paNoError )
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
result = stream->streamInterface->IsStopped( stream );
if( result == 0 )
{
result = paStreamIsNotStopped ;
}
else if( result == 1 )
{
result = PA_STREAM_INTERFACE(stream)->Start( stream );
result = stream->streamInterface->Start( stream );
}
}

Expand All @@ -1477,10 +1477,10 @@ PaError Pa_StopStream( PaStream *stream )

if( result == paNoError )
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
result = stream->streamInterface->IsStopped( stream );
if( result == 0 )
{
result = PA_STREAM_INTERFACE(stream)->Stop( stream );
result = stream->streamInterface->Stop( stream );
}
else if( result == 1 )
{
Expand All @@ -1503,10 +1503,10 @@ PaError Pa_AbortStream( PaStream *stream )

if( result == paNoError )
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
result = stream->streamInterface->IsStopped( stream );
if( result == 0 )
{
result = PA_STREAM_INTERFACE(stream)->Abort( stream );
result = stream->streamInterface->Abort( stream );
}
else if( result == 1 )
{
Expand All @@ -1528,7 +1528,7 @@ PaError Pa_IsStreamStopped( PaStream *stream )
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));

if( result == paNoError )
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
result = stream->streamInterface->IsStopped( stream );

PA_LOGAPI_EXIT_PAERROR( "Pa_IsStreamStopped", result );

Expand All @@ -1544,7 +1544,7 @@ PaError Pa_IsStreamActive( PaStream *stream )
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));

if( result == paNoError )
result = PA_STREAM_INTERFACE(stream)->IsActive( stream );
result = stream->streamInterface->IsActive( stream );


PA_LOGAPI_EXIT_PAERROR( "Pa_IsStreamActive", result );
Expand All @@ -1571,7 +1571,7 @@ const PaStreamInfo* Pa_GetStreamInfo( PaStream *stream )
}
else
{
result = &PA_STREAM_REP( stream )->streamInfo;
result = &stream->streamInfo;

PA_LOGAPI(("Pa_GetStreamInfo returned:\n" ));
PA_LOGAPI(("\tconst PaStreamInfo*: 0x%p:\n", result ));
Expand Down Expand Up @@ -1607,7 +1607,7 @@ PaTime Pa_GetStreamTime( PaStream *stream )
}
else
{
result = PA_STREAM_INTERFACE(stream)->GetTime( stream );
result = stream->streamInterface->GetTime( stream );

PA_LOGAPI(("Pa_GetStreamTime returned:\n" ));
PA_LOGAPI(("\tPaTime: %g\n", result ));
Expand Down Expand Up @@ -1637,7 +1637,7 @@ double Pa_GetStreamCpuLoad( PaStream* stream )
}
else
{
result = PA_STREAM_INTERFACE(stream)->GetCpuLoad( stream );
result = stream->streamInterface->GetCpuLoad( stream );

PA_LOGAPI(("Pa_GetStreamCpuLoad returned:\n" ));
PA_LOGAPI(("\tdouble: %g\n", result ));
Expand Down Expand Up @@ -1670,10 +1670,10 @@ PaError Pa_ReadStream( PaStream* stream,
}
else
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
result = stream->streamInterface->IsStopped( stream );
if( result == 0 )
{
result = PA_STREAM_INTERFACE(stream)->Read( stream, buffer, frames );
result = stream->streamInterface->Read( stream, buffer, frames );
}
else if( result == 1 )
{
Expand Down Expand Up @@ -1710,10 +1710,10 @@ PaError Pa_WriteStream( PaStream* stream,
}
else
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
result = stream->streamInterface->IsStopped( stream );
if( result == 0 )
{
result = PA_STREAM_INTERFACE(stream)->Write( stream, buffer, frames );
result = stream->streamInterface->Write( stream, buffer, frames );
}
else if( result == 1 )
{
Expand Down Expand Up @@ -1745,7 +1745,7 @@ signed long Pa_GetStreamReadAvailable( PaStream* stream )
}
else
{
result = PA_STREAM_INTERFACE(stream)->GetReadAvailable( stream );
result = stream->streamInterface->GetReadAvailable( stream );

PA_LOGAPI(("Pa_GetStreamReadAvailable returned:\n" ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
Expand Down Expand Up @@ -1774,7 +1774,7 @@ signed long Pa_GetStreamWriteAvailable( PaStream* stream )
}
else
{
result = PA_STREAM_INTERFACE(stream)->GetWriteAvailable( stream );
result = stream->streamInterface->GetWriteAvailable( stream );

PA_LOGAPI(("Pa_GetStreamWriteAvailable returned:\n" ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
Expand Down
16 changes: 0 additions & 16 deletions src/common/pa_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,6 @@ void PaUtil_TerminateStreamRepresentation( PaUtilStreamRepresentation *streamRep
PaError PaUtil_ValidateStreamPointer( PaStream *stream );


/** Cast an opaque stream pointer into a pointer to a PaUtilStreamRepresentation.
@see PaUtilStreamRepresentation
*/
#define PA_STREAM_REP( stream )\
((PaUtilStreamRepresentation*) (stream) )


/** Cast an opaque stream pointer into a pointer to a PaUtilStreamInterface.
@see PaUtilStreamRepresentation, PaUtilStreamInterface
*/
#define PA_STREAM_INTERFACE( stream )\
PA_STREAM_REP( (stream) )->streamInterface



#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions src/hostapi/alsa/pa_linux_alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -4651,8 +4651,8 @@ static PaError GetAlsaStreamPointer( PaStream* s, PaAlsaStream** stream )
PA_ENSURE( PaUtil_GetHostApiRepresentation( &hostApi, paALSA ) );
alsaHostApi = (PaAlsaHostApiRepresentation*)hostApi;

PA_UNLESS( PA_STREAM_REP( s )->streamInterface == &alsaHostApi->callbackStreamInterface
|| PA_STREAM_REP( s )->streamInterface == &alsaHostApi->blockingStreamInterface,
PA_UNLESS( s->streamInterface == &alsaHostApi->callbackStreamInterface
|| s->streamInterface == &alsaHostApi->blockingStreamInterface,
paIncompatibleStreamHostApi );

*stream = (PaAlsaStream*)s;
Expand Down
4 changes: 2 additions & 2 deletions src/hostapi/asio/pa_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4215,8 +4215,8 @@ static PaError GetAsioStreamPointer( PaAsioStream **stream, PaStream *s )

asioHostApi = (PaAsioHostApiRepresentation*)hostApi;

if( PA_STREAM_REP( s )->streamInterface == &asioHostApi->callbackStreamInterface
|| PA_STREAM_REP( s )->streamInterface == &asioHostApi->blockingStreamInterface )
if( s->streamInterface == &asioHostApi->callbackStreamInterface
|| s->streamInterface == &asioHostApi->blockingStreamInterface )
{
/* s is an ASIO stream */
*stream = (PaAsioStream *)s;
Expand Down
4 changes: 2 additions & 2 deletions src/hostapi/wmme/pa_win_wmme.c
Original file line number Diff line number Diff line change
Expand Up @@ -3966,8 +3966,8 @@ static PaError GetWinMMEStreamPointer( PaWinMmeStream **stream, PaStream *s )
/* note, the following would be easier if there was a generic way of testing
that a stream belongs to a specific host API */

if( PA_STREAM_REP( s )->streamInterface == &winMmeHostApi->callbackStreamInterface
|| PA_STREAM_REP( s )->streamInterface == &winMmeHostApi->blockingStreamInterface )
if( s->streamInterface == &winMmeHostApi->callbackStreamInterface
|| s->streamInterface == &winMmeHostApi->blockingStreamInterface )
{
/* s is a WinMME stream */
*stream = (PaWinMmeStream *)s;
Expand Down

0 comments on commit b11341b

Please sign in to comment.