Group PJMED_SND

group PJMED_SND

PJMEDIA abstraction for sound device hardware.

Warning: this sound device API has been deprecated and replaced by PJMEDIA Audio Device API. Please see 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:

  • callback to be called when it has finished capturing one media frame, and

  • 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.

Typedefs

typedef struct pjmedia_snd_stream pjmedia_snd_stream

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_data – User data associated with the stream.

  • timestamp – Timestamp, in samples.

  • output – Buffer to be filled out by application.

  • size – The 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_data – User data associated with the stream.

  • timestamp – Timestamp, in samples.

  • output – Buffer containing the captured audio samples.

  • size – The size of the data in the buffer, in bytes.

Returns

Non-zero to stop the stream.

Functions

pj_status_t pjmedia_snd_init(pj_pool_factory *factory)

Init the sound library.

Parameters

factory – The sound factory.

Returns

Zero on success.

int pjmedia_snd_get_dev_count(void)

Get the number of devices detected by the library.

Returns

Number of devices.

const pjmedia_snd_dev_info *pjmedia_snd_get_dev_info(unsigned index)

Get device info.

Parameters

index – The 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,

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.

See

PJMEDIA_SND_DEFAULT_REC_LATENCY & PJMEDIA_SND_DEFAULT_PLAY_LATENCY.

Parameters
  • input_latency – The latency of input device, in ms, set to 0 for default PJMEDIA_SND_DEFAULT_REC_LATENCY.

  • output_latency – The 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_id – Device index for recorder/capture stream, or -1 to use the first capable device.

  • play_id – Device index for playback stream, or -1 to use the first capable device.

  • clock_rate – Sound device’s clock rate to set.

  • channel_count – Set number of channels, 1 for mono, or 2 for stereo. The channel count determines the format of the frame.

  • samples_per_frame – Number of samples per frame.

  • bits_per_sample – Set the number of bits per sample. The normal value for this parameter is 16 bits per sample.

  • rec_cb – Callback to handle captured audio samples.

  • play_cb – Callback to be called when the sound player needs more audio samples to play.

  • user_data – User data to be associated with the stream.

  • p_snd_strm – Pointer 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
  • index – Device index, or -1 to let the library choose the first available device.

  • clock_rate – Sound device’s clock rate to set.

  • channel_count – Set number of channels, 1 for mono, or 2 for stereo. The channel count determines the format of the frame.

  • samples_per_frame – Number of samples per frame.

  • bits_per_sample – Set the number of bits per sample. The normal value for this parameter is 16 bits per sample.

  • rec_cb – Callback to handle captured audio samples.

  • user_data – User data to be associated with the stream.

  • p_snd_strm – Pointer 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
  • index – Device index, or -1 to let the library choose the first available device.

  • clock_rate – Sound device’s clock rate to set.

  • channel_count – Set number of channels, 1 for mono, or 2 for stereo. The channel count determines the format of the frame.

  • samples_per_frame – Number of samples per frame.

  • bits_per_sample – Set the number of bits per sample. The normal value for this parameter is 16 bits per sample.

  • play_cb – Callback to be called when the sound player needs more audio samples to play.

  • user_data – User data to be associated with the stream.

  • p_snd_strm – Pointer 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
  • strm – The stream to be queried.

  • pi – Pointer 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

stream – The recorder or player stream.

Returns

Zero on success.

pj_status_t pjmedia_snd_stream_stop(pjmedia_snd_stream *stream)

Stop the stream.

Parameters

stream – The recorder or player stream.

Returns

Zero on success.

pj_status_t pjmedia_snd_stream_close(pjmedia_snd_stream *stream)

Destroy the stream.

Parameters

stream – The recorder of player stream.

Returns

Zero on success.

pj_status_t pjmedia_snd_deinit(void)

Deinitialize sound library.

Returns

Zero on success.

struct pjmedia_snd_dev_info
#include <sound.h>

Device information structure returned by pjmedia_snd_get_dev_info.

struct pjmedia_snd_stream_info
#include <sound.h>

Stream information, can be retrieved from a live stream by calling pjmedia_snd_stream_get_info().