wiki:Audio_Dev_API

Version 7 (modified by bennylp, 15 years ago) (diff)

--

PJMEDIA Audio Device API

Table of Contents

  1. Background
  2. Features
  3. Migration Path
    1. Common Tasks
    2. Ported Devices
    3. Compatibility settings
    4. Migration strategy
  4. Porting applications to new API
    1. PJSUA-LIB applications
    2. PJMEDIA applications

The PJMEDIA Audio Device API is a new sound device abstraction API/library in PJMEDIA, introduced in PJSIP version 1.1, deprecating the existing sound device API.


Background

The PJMEDIA Audio Device API was introduced as part of the implementation of APS-Direct implementation in PJSIP version 1.1. During the design and implementation of APS-Direct project, it was clear that the existing sound device API lacks many features that are needed to support the project. We had the choice to either patch the existing API with new features that are specific to Nokia APS, and potentially break existing applications anyway, or design a new sound device API to support all these new features as well as future enhancements, while providing some support for the old sound API and a clear migration path to the new API.


Features

The new audio device API contains the following major features.

Forward compatibility:
The new API has been designed to be extensible, it will support new API's as well as new features that may be introduced in the future without breaking compatibility with applications that use this API as well as compatibility with existing device implementations.
Device capabilities:
At the heart of the API is device capabilities management, where all possible audio capabilities of audio devices should be able to be handled in a generic manner. With this framework, new capabilities that may be discovered in the future can be handled in manner without breaking existing applications.
Built-in features:
The device capabilities framework enables applications to use audio features built-in in the device, such as echo cancellation, built-in codecs, audio routing, and volume control.
Codec support:
Some audio devices such as Nokia/Symbian? Audio Proxy Server (APS) and Nokia VoIP Audio Services (VAS) support built-in hardware audio codecs (e.g. G.729, iLBC, and AMR), and this feature is supported by the new audio device API.
Multiple backends:
The new API supports multiple audio backends (may be called factories or drivers in the code) to be active simultaneously, and audio backends may be added or removed during run-time.


Migration Path

Common Tasks

  1. The Audio Device API is implemented as a new (static) library called pjmedia-audiodev, under pjmedia directory. Please add this library into link specifications of your application.

Ported Devices

The following audio device backends have been ported to the new API:

  • PortAudio (previously pasound.c)
  • WMME (previously wmme_sound.c)
  • Null implementation (previously nullsound.c)
  • APS (previously symbian_sound_aps.cpp)

Compatibility settings

The following compile time settings/macros have introduced to manage compatibility between old and new API and to assist migration of application and/or sound device implementation to the new API.

Setting 1) PJMEDIA_AUDIO_API = PJMEDIA_AUDIO_API_NEW_ONLY

This setting will completely deprecate the use of old API, and inclusion of <pjmedia/sound.h> in the code will raise compilation error.

Use this setting if:

  • you are accessing sound devices which have been implemented using the new API
  • you have completely ported your application to use the new API
Setting 2) PJMEDIA_AUDIO_API = PJMEDIA_AUDIO_API_HAS_OLD_API

With this setting (this is the default setting), application can use the old sound device API to access audio devices provided by the new audio device API.

Use this setting if:

  • you are accessing sound devices which have been ported to the new API
  • you want to access the sound device using both old or new API's.

What this setting does:

  • it creates a sound device implementation based on the old API, but the implementation uses/accesses the new audio device API.
Setting 3) PJMEDIA_AUDIO_API = PJMEDIA_AUDIO_API_HAS_OLD_DEVICE

This setting enables old sound device implementation to be accessed via both old and new API's.

Use this setting if:

  • you have your own sound device implementation, using the old API
  • you want to access the sound device using both old or new API's.

What this setting does:

  • the new audio device API will create a wrapper for the old sound device implementation, effectively enabling the sound device implementation to be accessed via old or new API.

Migration strategy

Use the following checklist to select which setting is appropriate for your build.

  1. I use sound devices provided by PJMEDIA (I don't have my own sound device implementation), or I have ported my sound device implementation to the new API
    1. I haven't changed my application to use the new API
      • use setting 2 (the default)
    2. I have changed my application to use the new API
      • you may use setting 1 to make sure that everything has been ported to the new API, or otherwise use setting 2.
  2. I have my own sound device implementation, based on old API
    1. I haven't changed my application to use the new API
      • use setting 3
    2. I have changed my application to use the new API
      • also use setting 3


Porting applications to new API

As stated above, existing applications should build fine with the new changes. However, it will still be beneficial to port the application to use the new API since the old sound API has been deprecated, and also to make use of the new features in the new Audio Device API.

This section describes how to port applications to use the new Audio Device API.

PJSUA-LIB applications

PJSUA-LIB applications that only make use of the API in <pjsua.h> should require minimum changes.

  1. Change in pjsua_enum_snd_devs() signature. Old API:
    PJ_DECL(pj_status_t) pjsua_enum_snd_devs(pjmedia_snd_dev_info info[], unsigned *count);
    
    New API (notice the type difference in the info argument):
    PJ_DECL(pj_status_t) pjsua_enum_snd_devs(pjmedia_aud_dev_info info[], unsigned *count);
    
  1. Add pjmedia-audiodev library to the link specifications of your application.

PJMEDIA applications

Below are list of changes for applications that directly uses the old sound API as declared in <pjmedia/sound.h>.

  1. Change the header file to include. Old code:
    #include <pjmedia/sound.h>
    
    New code:
    #include <pjmedia_audiodev.h>
    
  2. Add pjmedia-audiodev library to the link specifications of your application.
  3. Sound device subsystem startup and shutdown. Old code:
    pjmedia_snd_init(pf);
     ...
    pjmedia_snd_deinit();
    
    New code:
    pjmedia_aud_subsys_init(pf);
     ...
    pjmedia_aud_subsys_shutdown();
    
  4. Device enumeration. Old code:
    unsigned i, dev_count;
    
    dev_count = pjmedia_snd_get_dev_count();
    for (i=0; i<dev_count; ++i) {
        const pjmedia_snd_dev_info *dev_info = pjmedia_snd_get_dev_info(i);
    }
    
    New code:
    unsigned i, dev_count;
    
    dev_count = pjmedia_aud_dev_count();
    for (i=0; i<dev_count; ++i) {
        pjmedia_aud_dev_info dev_info;
        pj_status_t status;
    
        status = pjmedia_aud_dev_get_info(i, &info);
    }
    
  5. Opening sound device. Old code:
    pjmedia_snd_stream *stream;
    pj_status_t status;
    
    status = pjmedia_snd_open(-1,       // default capture device ID
                              -1,       // default playback device ID
                              8000,     // clock rate
                              1,        // channel count
                              160,      // samples per frame
                              16,       // bits per sample
                              &rec_cb,  // capture callback
                              &play_cb, // playback callback
                              user_data,
                              &stream);
    if (status != PJ_SUCCESS)
       ..
    
    New code:
    pjmedia_aud_dev_param param;
    pjmedia_aud_stream *stream;
    pj_status_t status;
    
    status = pjmedia_aud_dev_default_param(PJMEDIA_AUD_DEV_DEFAULT, &param);
    if (status != PJ_SUCCESS)
       ..
    
    param.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
    param.rec_id = PJMEDIA_AUD_DEV_DEFAULT;
    param.play_id = PJMEDIA_AUD_DEV_DEFAULT;
    param.clock_rate = 8000;
    param.channel_count = 1;
    param.samples_per_frame = 160;
    param.bits_per_sample = 16;
    .. // put additional settings here
    
    status = pjmedia_aud_stream_create(&param,
                                       &rec_cb,
                                       &play_cb,
                                       user_data,
                                       &stream);
    if (status != PJ_SUCCESS)
       ..
    
  6. .. and so on