BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJMEDIA Reference

PJMEDIA abstraction for sound device hardware. More...

Data Structures

struct  pjmedia_snd_dev_info
 
struct  pjmedia_snd_stream_info
 

Typedefs

typedef struct pjmedia_snd_stream pjmedia_snd_stream
 
typedef pj_status_t(* pjmedia_snd_play_cb )(void *user_data, pj_uint32_t timestamp, void *output, unsigned size)
 
typedef pj_status_t(* pjmedia_snd_rec_cb )(void *user_data, pj_uint32_t timestamp, void *input, unsigned size)
 

Functions

pj_status_t pjmedia_snd_init (pj_pool_factory *factory)
 
int pjmedia_snd_get_dev_count (void)
 
const pjmedia_snd_dev_infopjmedia_snd_get_dev_info (unsigned index)
 
pj_status_t pjmedia_snd_set_latency (unsigned input_latency, unsigned output_latency)
 
pj_status_t pjmedia_snd_open (int rec_id, int play_id, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, pjmedia_snd_rec_cb rec_cb, pjmedia_snd_play_cb play_cb, void *user_data, pjmedia_snd_stream **p_snd_strm)
 
pj_status_t pjmedia_snd_open_rec (int index, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, pjmedia_snd_rec_cb rec_cb, void *user_data, pjmedia_snd_stream **p_snd_strm)
 
pj_status_t pjmedia_snd_open_player (int index, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, pjmedia_snd_play_cb play_cb, void *user_data, pjmedia_snd_stream **p_snd_strm)
 
pj_status_t pjmedia_snd_stream_get_info (pjmedia_snd_stream *strm, pjmedia_snd_stream_info *pi)
 
pj_status_t pjmedia_snd_stream_start (pjmedia_snd_stream *stream)
 
pj_status_t pjmedia_snd_stream_stop (pjmedia_snd_stream *stream)
 
pj_status_t pjmedia_snd_stream_close (pjmedia_snd_stream *stream)
 
pj_status_t pjmedia_snd_deinit (void)
 

Detailed Description

Warning: this sound device API has been deprecated and replaced by PJMEDIA Audio Device API. Please see http://trac.pjsip.org/repos/wiki/Audio_Dev_API for more information.

This section describes lower level abstraction for sound device hardware. Application normally uses the higher layer Sound Device Port abstraction since it works seamlessly with Media Ports Framework.

The sound hardware abstraction basically runs asychronously, and application must register callbacks to be called to receive/ supply audio frames from/to the sound hardware.

A full duplex sound stream (created with pjmedia_snd_open()) requires application to supply two callbacks:

  • rec_cb callback to be called when it has finished capturing one media frame, and
  • play_cb callback to be called when it needs media frame to be played to the sound playback hardware.

Half duplex sound stream (created with pjmedia_snd_open_rec() or pjmedia_snd_open_player()) will only need one of the callback to be specified.

After sound stream is created, application need to call pjmedia_snd_stream_start() to start capturing/playing back media frames from/to the sound device.

Typedef Documentation

Opaque declaration for pjmedia_snd_stream.

typedef pj_status_t(* pjmedia_snd_play_cb)(void *user_data,pj_uint32_t timestamp,void *output,unsigned size)

This callback is called by player stream when it needs additional data to be played by the device. Application must fill in the whole of output buffer with sound samples.

Parameters
user_dataUser data associated with the stream.
timestampTimestamp, in samples.
outputBuffer to be filled out by application.
sizeThe size requested in bytes, which will be equal to the size of one whole packet.
Returns
Non-zero to stop the stream.
typedef pj_status_t(* pjmedia_snd_rec_cb)(void *user_data,pj_uint32_t timestamp,void *input,unsigned size)

This callback is called by recorder stream when it has captured the whole packet worth of audio samples.

Parameters
user_dataUser data associated with the stream.
timestampTimestamp, in samples.
outputBuffer containing the captured audio samples.
sizeThe size of the data in the buffer, in bytes.
Returns
Non-zero to stop the stream.

Function Documentation

pj_status_t pjmedia_snd_init ( pj_pool_factory factory)

Init the sound library.

Parameters
factoryThe sound factory.
Returns
Zero on success.

References PJ_INLINE, and pjmedia_aud_subsys_init().

int pjmedia_snd_get_dev_count ( void  )

Get the number of devices detected by the library.

Returns
Number of devices.

References PJ_INLINE, and pjmedia_aud_dev_count().

const pjmedia_snd_dev_info* pjmedia_snd_get_dev_info ( unsigned  index)

Get device info.

Parameters
indexThe index of the device, which should be in the range from zero to pjmedia_snd_get_dev_count - 1.
pj_status_t pjmedia_snd_set_latency ( unsigned  input_latency,
unsigned  output_latency 
)

Set sound device latency, this function must be called before sound device opened, or otherwise default latency setting will be used,

See also
PJMEDIA_SND_DEFAULT_REC_LATENCY & PJMEDIA_SND_DEFAULT_PLAY_LATENCY.

Choosing latency value is not straightforward, it should accomodate both minimum latency and stability. Lower latency tends to cause sound device less reliable (producing audio dropouts) on CPU load disturbance. Moreover, the best latency setting may vary based on many aspects, e.g: sound card, CPU, OS, kernel, etc.

Parameters
input_latencyThe latency of input device, in ms, set to 0 for default PJMEDIA_SND_DEFAULT_REC_LATENCY.
output_latencyThe latency of output device, in ms, set to 0 for default PJMEDIA_SND_DEFAULT_PLAY_LATENCY.
Returns
PJ_SUCCESS on success.
pj_status_t pjmedia_snd_open ( int  rec_id,
int  play_id,
unsigned  clock_rate,
unsigned  channel_count,
unsigned  samples_per_frame,
unsigned  bits_per_sample,
pjmedia_snd_rec_cb  rec_cb,
pjmedia_snd_play_cb  play_cb,
void *  user_data,
pjmedia_snd_stream **  p_snd_strm 
)

Create sound stream for both capturing audio and audio playback, from the same device. This is the recommended way to create simultaneous recorder and player streams (instead of creating separate capture and playback streams), because it works on backends that does not allow a device to be opened more than once.

Parameters
rec_idDevice index for recorder/capture stream, or -1 to use the first capable device.
play_idDevice index for playback stream, or -1 to use the first capable device.
clock_rateSound device's clock rate to set.
channel_countSet number of channels, 1 for mono, or 2 for stereo. The channel count determines the format of the frame.
samples_per_frameNumber of samples per frame.
bits_per_sampleSet the number of bits per sample. The normal value for this parameter is 16 bits per sample.
rec_cbCallback to handle captured audio samples.
play_cbCallback to be called when the sound player needs more audio samples to play.
user_dataUser data to be associated with the stream.
p_snd_strmPointer to receive the stream instance.
Returns
PJ_SUCCESS on success.
pj_status_t pjmedia_snd_open_rec ( int  index,
unsigned  clock_rate,
unsigned  channel_count,
unsigned  samples_per_frame,
unsigned  bits_per_sample,
pjmedia_snd_rec_cb  rec_cb,
void *  user_data,
pjmedia_snd_stream **  p_snd_strm 
)

Create a unidirectional audio stream for capturing audio samples from the sound device.

Parameters
indexDevice index, or -1 to let the library choose the first available device.
clock_rateSound device's clock rate to set.
channel_countSet number of channels, 1 for mono, or 2 for stereo. The channel count determines the format of the frame.
samples_per_frameNumber of samples per frame.
bits_per_sampleSet the number of bits per sample. The normal value for this parameter is 16 bits per sample.
rec_cbCallback to handle captured audio samples.
user_dataUser data to be associated with the stream.
p_snd_strmPointer to receive the stream instance.
Returns
PJ_SUCCESS on success.
pj_status_t pjmedia_snd_open_player ( int  index,
unsigned  clock_rate,
unsigned  channel_count,
unsigned  samples_per_frame,
unsigned  bits_per_sample,
pjmedia_snd_play_cb  play_cb,
void *  user_data,
pjmedia_snd_stream **  p_snd_strm 
)

Create a unidirectional audio stream for playing audio samples to the sound device.

Parameters
indexDevice index, or -1 to let the library choose the first available device.
clock_rateSound device's clock rate to set.
channel_countSet number of channels, 1 for mono, or 2 for stereo. The channel count determines the format of the frame.
samples_per_frameNumber of samples per frame.
bits_per_sampleSet the number of bits per sample. The normal value for this parameter is 16 bits per sample.
play_cbCallback to be called when the sound player needs more audio samples to play.
user_dataUser data to be associated with the stream.
p_snd_strmPointer to receive the stream instance.
Returns
PJ_SUCCESS on success.
pj_status_t pjmedia_snd_stream_get_info ( pjmedia_snd_stream strm,
pjmedia_snd_stream_info pi 
)

Get information about live stream.

Parameters
strmThe stream to be queried.
piPointer to stream information to be filled up with information about the stream.
Returns
PJ_SUCCESS on success or the appropriate error code.
pj_status_t pjmedia_snd_stream_start ( pjmedia_snd_stream stream)

Start the stream.

Parameters
streamThe recorder or player stream.
Returns
Zero on success.
pj_status_t pjmedia_snd_stream_stop ( pjmedia_snd_stream stream)

Stop the stream.

Parameters
streamThe recorder or player stream.
Returns
Zero on success.
pj_status_t pjmedia_snd_stream_close ( pjmedia_snd_stream stream)

Destroy the stream.

Parameters
streamThe recorder of player stream.
Returns
Zero on success.
pj_status_t pjmedia_snd_deinit ( void  )

Deinitialize sound library.

Returns
Zero on success.

References pjmedia_aud_subsys_shutdown().

 


PJMEDIA small footprint Open Source media stack
Copyright (C) 2006-2008 Teluu Inc.