diff options
author | Benny Prijono <bennylp@teluu.com> | 2009-03-12 18:11:37 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2009-03-12 18:11:37 +0000 |
commit | 1dacdee696b7591a6dcc0b3c1d0f41573e473168 (patch) | |
tree | 302b09dcd989c0c05cf09f6aebaa63d870b421b9 /pjsip | |
parent | ba9d8ca28eb209571c0bd6a080a8bb03d0fa2d33 (diff) |
(Major) Task #737 and #738: integration of APS-Direct and Audiodev from aps-direct branch to trunk.
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2506 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjsip')
-rw-r--r-- | pjsip/include/pjsua-lib/pjsua.h | 118 | ||||
-rw-r--r-- | pjsip/include/pjsua-lib/pjsua_internal.h | 11 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_call.c | 10 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_core.c | 7 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_media.c | 658 |
5 files changed, 650 insertions, 154 deletions
diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h index ca00bb18..99869344 100644 --- a/pjsip/include/pjsua-lib/pjsua.h +++ b/pjsip/include/pjsua-lib/pjsua.h @@ -4200,6 +4200,20 @@ struct pjsua_media_config */ unsigned ec_tail_len; + /** + * Audio capture buffer length, in milliseconds. + * + * Default: PJMEDIA_SND_DEFAULT_REC_LATENCY + */ + unsigned snd_rec_latency; + + /** + * Audio playback buffer length, in milliseconds. + * + * Default: PJMEDIA_SND_DEFAULT_PLAY_LATENCY + */ + unsigned snd_play_latency; + /** * Jitter buffer initial prefetch delay in msec. The value must be * between jb_min_pre and jb_max_pre below. @@ -4272,9 +4286,10 @@ struct pjsua_media_config /** * Specify idle time of sound device before it is automatically closed, - * in seconds. + * in seconds. Use value -1 to disable the auto-close feature of sound + * device * - * Default : -1 (Disable the auto-close feature of sound device) + * Default : 1 */ int snd_auto_close_time; }; @@ -4787,7 +4802,20 @@ PJ_DECL(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id); */ /** - * Enum all sound devices installed in the system. + * Enum all audio devices installed in the system. + * + * @param info Array of info to be initialized. + * @param count On input, specifies max elements in the array. + * On return, it contains actual number of elements + * that have been initialized. + * + * @return PJ_SUCCESS on success, or the appropriate error code. + */ +PJ_DECL(pj_status_t) pjsua_enum_aud_devs(pjmedia_aud_dev_info info[], + unsigned *count); + +/** + * Enum all sound devices installed in the system (old API). * * @param info Array of info to be initialized. * @param count On input, specifies max elements in the array. @@ -4807,8 +4835,6 @@ PJ_DECL(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id); PJ_DECL(pj_status_t) pjsua_enum_snd_devs(pjmedia_snd_dev_info info[], unsigned *count); - - /** * Get currently active sound devices. If sound devices has not been created * (for example when pjsua_start() is not called), it is possible that @@ -4879,7 +4905,22 @@ PJ_DECL(pjmedia_port*) pjsua_set_no_snd_dev(void); /** - * Configure the echo canceller tail length of the sound port. + * Change the echo cancellation settings. + * + * The behavior of this function depends on whether the sound device is + * currently active, and if it is, whether device or software AEC is + * being used. + * + * If the sound device is currently active, and if the device supports AEC, + * this function will forward the change request to the device and it will + * be up to the device on whether support the request. If software AEC is + * being used (the software EC will be used if the device does not support + * AEC), this function will change the software EC settings. In all cases, + * the setting will be saved for future opening of the sound device. + * + * If the sound device is not currently active, this will only change the + * default AEC settings and the setting will be applied next time the + * sound device is opened. * * @param tail_ms The tail length, in miliseconds. Set to zero to * disable AEC. @@ -4897,7 +4938,7 @@ PJ_DECL(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options); /** - * Get current echo canceller tail length. + * Get current echo canceller tail length. * * @param p_tail_ms Pointer to receive the tail length, in miliseconds. * If AEC is disabled, the value will be zero. @@ -4912,6 +4953,69 @@ PJ_DECL(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options); PJ_DECL(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms); +/** + * Check whether the sound device is currently active. The sound device + * may be inactive if the application has set the auto close feature to + * non-zero (the snd_auto_close_time setting in #pjsua_media_config), or + * if null sound device or no sound device has been configured via the + * #pjsua_set_no_snd_dev() function. + */ +PJ_DECL(pj_bool_t) pjsua_snd_is_active(void); + + +/** + * Configure sound device setting to the sound device being used. If sound + * device is currently active, the function will forward the setting to the + * sound device instance to be applied immediately, if it supports it. + * + * The setting will be saved for future opening of the sound device, if the + * "keep" argument is set to non-zero. If the sound device is currently + * inactive, and the "keep" argument is false, this function will return + * error. + * + * Note that in case the setting is kept for future use, it will be applied + * to any devices, even when application has changed the sound device to be + * used. + * + * Note also that the echo cancellation setting should be set with + * #pjsua_set_ec() API instead. + * + * See also #pjmedia_aud_stream_set_cap() for more information about setting + * an audio device capability. + * + * @param cap The sound device setting to change. + * @param pval Pointer to value. Please see #pjmedia_aud_dev_cap + * documentation about the type of value to be + * supplied for each setting. + * @param keep Specify whether the setting is to be kept for future + * use. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) pjsua_snd_set_setting(pjmedia_aud_dev_cap cap, + const void *pval, + pj_bool_t keep); + +/** + * Retrieve a sound device setting. If sound device is currently active, + * the function will forward the request to the sound device. If sound device + * is currently inactive, and if application had previously set the setting + * and mark the setting as kept, then that setting will be returned. + * Otherwise, this function will return error. + * + * Note that echo cancellation settings should be retrieved with + * #pjsua_get_ec_tail() API instead. + * + * @param cap The sound device setting to retrieve. + * @param pval Pointer to receive the value. + * Please see #pjmedia_aud_dev_cap documentation about + * the type of value to be supplied for each setting. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) pjsua_snd_get_setting(pjmedia_aud_dev_cap cap, + void *pval); + /***************************************************************************** * Codecs. diff --git a/pjsip/include/pjsua-lib/pjsua_internal.h b/pjsip/include/pjsua-lib/pjsua_internal.h index a4af71bc..a144c97d 100644 --- a/pjsip/include/pjsua-lib/pjsua_internal.h +++ b/pjsip/include/pjsua-lib/pjsua_internal.h @@ -272,8 +272,15 @@ struct pjsua_data pjmedia_endpt *med_endpt; /**< Media endpoint. */ pjsua_conf_setting mconf_cfg; /**< Additionan conf. bridge. param */ pjmedia_conf *mconf; /**< Conference bridge. */ - int cap_dev; /**< Capture device ID. */ - int play_dev; /**< Playback device ID. */ + pj_bool_t is_mswitch;/**< Are we using audio switchboard + (a.k.a APS-Direct) */ + + /* Sound device */ + pjmedia_aud_dev_index cap_dev; /**< Capture device ID. */ + pjmedia_aud_dev_index play_dev; /**< Playback device ID. */ + pj_uint32_t aud_svmask;/**< Which settings to save */ + pjmedia_aud_param aud_param; /**< User settings to sound dev */ + pj_bool_t aud_open_cnt;/**< How many # device is opened */ pj_bool_t no_snd; /**< No sound (app will manage it) */ pj_pool_t *snd_pool; /**< Sound's private pool. */ pjmedia_snd_port *snd_port; /**< Sound port. */ diff --git a/pjsip/src/pjsua-lib/pjsua_call.c b/pjsip/src/pjsua-lib/pjsua_call.c index c2f9ae2d..87dcfdea 100644 --- a/pjsip/src/pjsua-lib/pjsua_call.c +++ b/pjsip/src/pjsua-lib/pjsua_call.c @@ -370,9 +370,13 @@ PJ_DEF(pj_status_t) pjsua_call_make_call( pjsua_acc_id acc_id, PJSUA_LOCK(); - /* Create sound port if none is instantiated */ - if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && - !pjsua_var.no_snd) + /* Create sound port if none is instantiated, to check if sound device + * can be used. But only do this with the conference bridge, as with + * audio switchboard (i.e. APS-Direct), we can only open the sound + * device once the correct format has been known + */ + if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL && + pjsua_var.null_snd==NULL && !pjsua_var.no_snd) { pj_status_t status; diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c index 4b7c4baa..7430fc12 100644 --- a/pjsip/src/pjsua-lib/pjsua_core.c +++ b/pjsip/src/pjsua-lib/pjsua_core.c @@ -177,8 +177,10 @@ PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg) cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY; cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE; cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN; + cfg->snd_rec_latency = PJMEDIA_SND_DEFAULT_REC_LATENCY; + cfg->snd_play_latency = PJMEDIA_SND_DEFAULT_PLAY_LATENCY; cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1; - cfg->snd_auto_close_time = -1; + cfg->snd_auto_close_time = 1; cfg->turn_conn_type = PJ_TURN_TP_UDP; } @@ -581,7 +583,8 @@ PJ_DEF(pj_status_t) pjsua_create(void) PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Set default sound device ID */ - pjsua_var.cap_dev = pjsua_var.play_dev = -1; + pjsua_var.cap_dev = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV; + pjsua_var.play_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV; /* Init caching pool. */ pj_caching_pool_init(&pjsua_var.cp, NULL, 0); diff --git a/pjsip/src/pjsua-lib/pjsua_media.c b/pjsip/src/pjsua-lib/pjsua_media.c index 8d2d8a8a..f49214a9 100644 --- a/pjsip/src/pjsua-lib/pjsua_media.c +++ b/pjsip/src/pjsua-lib/pjsua_media.c @@ -35,8 +35,18 @@ /* Next RTP port to be used */ static pj_uint16_t next_rtp_port; +/* Open sound dev */ +static pj_status_t open_snd_dev(pjmedia_aud_param *param); /* Close existing sound device */ static void close_snd_dev(void); +/* Create audio device param */ +static pj_status_t create_aud_param(pjmedia_aud_param *param, + pjmedia_aud_dev_index capture_dev, + pjmedia_aud_dev_index playback_dev, + unsigned clock_rate, + unsigned channel_count, + unsigned samples_per_frame, + unsigned bits_per_sample); static void pjsua_media_config_dup(pj_pool_t *pool, @@ -60,6 +70,16 @@ pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) /* To suppress warning about unused var when all codecs are disabled */ PJ_UNUSED_ARG(codec_id); + /* Specify which audio device settings are save-able */ + pjsua_var.aud_svmask = 0xFFFFFFFF; + /* These are not-settable */ + pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EXT_FORMAT | + PJMEDIA_AUD_DEV_CAP_INPUT_SIGNAL_METER | + PJMEDIA_AUD_DEV_CAP_OUTPUT_SIGNAL_METER); + /* EC settings use different API */ + pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EC | + PJMEDIA_AUD_DEV_CAP_EC_TAIL); + /* Copy configuration */ pjsua_media_config_dup(pjsua_var.pool, &pjsua_var.media_cfg, cfg); @@ -172,6 +192,16 @@ pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) #endif /* PJMEDIA_HAS_INTEL_IPP */ +#if PJMEDIA_HAS_PASSTHROUGH_CODECS + /* Register passthrough codecs */ + status = pjmedia_codec_passthrough_init(pjsua_var.med_endpt); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Error initializing passthrough codecs", + status); + return status; + } +#endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */ + #if PJMEDIA_HAS_L16_CODEC /* Register L16 family codecs, but disable all */ status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0); @@ -226,6 +256,10 @@ pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) return status; } + /* Are we using the audio switchboard (a.k.a APS-Direct)? */ + pjsua_var.is_mswitch = pjmedia_conf_get_master_port(pjsua_var.mconf) + ->info.signature == PJMEDIA_CONF_SWITCH_SIGNATURE; + /* Create null port just in case user wants to use null sound. */ status = pjmedia_null_port_create(pjsua_var.pool, pjsua_var.media_cfg.clock_rate, @@ -574,6 +608,10 @@ pj_status_t pjsua_media_subsys_destroy(void) pjmedia_codec_ipp_deinit(); # endif /* PJMEDIA_HAS_INTEL_IPP */ +# if PJMEDIA_HAS_PASSTHROUGH_CODECS + pjmedia_codec_passthrough_deinit(); +# endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */ + # if PJMEDIA_HAS_L16_CODEC pjmedia_codec_l16_deinit(); # endif /* PJMEDIA_HAS_L16_CODEC */ @@ -1495,7 +1533,6 @@ pj_status_t pjsua_media_channel_update(pjsua_call_id call_id, return PJ_SUCCESS; } - /* * Get maxinum number of conference ports. */ @@ -1610,17 +1647,85 @@ PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source, pjsua_var.snd_idle_timer.id = PJ_FALSE; } - /* Create sound port if none is instantiated */ - if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && - !pjsua_var.no_snd) - { + + /* For audio switchboard (i.e. APS-Direct): + * Check if sound device need to be reopened, i.e: its attributes + * (format, clock rate, channel count) must match to peer's. + * Note that sound device can be reopened only if it doesn't have + * any connection. + */ + if (pjsua_var.is_mswitch) { + pjmedia_conf_port_info port0_info; + pjmedia_conf_port_info peer_info; + unsigned peer_id; + pj_bool_t need_reopen = PJ_FALSE; pj_status_t status; - status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Error opening sound device", status); - return status; + peer_id = (source!=0)? source : sink; + status = pjmedia_conf_get_port_info(pjsua_var.mconf, peer_id, + &peer_info); + pj_assert(status == PJ_SUCCESS); + + status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &port0_info); + pj_assert(status == PJ_SUCCESS); + + /* Check if sound device is instantiated. */ + need_reopen = (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && + !pjsua_var.no_snd); + + /* Check if sound device need to reopen because it needs to modify + * settings to match its peer. Sound device must be idle in this case + * though. + */ + if (!need_reopen && + port0_info.listener_cnt==0 && port0_info.transmitter_cnt==0) + { + need_reopen = (peer_info.format.id != port0_info.format.id || + peer_info.format.bitrate != port0_info.format.bitrate || + peer_info.clock_rate != port0_info.clock_rate || + peer_info.channel_count != port0_info.channel_count); + } + + if (need_reopen) { + pjmedia_aud_param param; + + /* Create parameter based on peer info */ + status = create_aud_param(¶m, pjsua_var.cap_dev, + pjsua_var.play_dev, + peer_info.clock_rate, + peer_info.channel_count, + peer_info.samples_per_frame, + peer_info.bits_per_sample); + + /* And peer format */ + if (peer_info.format.id != PJMEDIA_FORMAT_PCM) { + param.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT; + param.ext_fmt = peer_info.format; + } + + status = open_snd_dev(¶m); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Error opening sound device", status); + return status; + } } + + } else { + /* The bridge version */ + + /* Create sound port if none is instantiated */ + if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && + !pjsua_var.no_snd) + { + pj_status_t status; + + status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Error opening sound device", status); + return status; + } + } + } return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0); @@ -2097,20 +2202,22 @@ PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id) /* * Enum sound devices. */ -PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[], + +PJ_DEF(pj_status_t) pjsua_enum_aud_devs( pjmedia_aud_dev_info info[], unsigned *count) { unsigned i, dev_count; - dev_count = pjmedia_snd_get_dev_count(); + dev_count = pjmedia_aud_dev_count(); if (dev_count > *count) dev_count = *count; for (i=0; i<dev_count; ++i) { - const pjmedia_snd_dev_info *ci; + pj_status_t status; - ci = pjmedia_snd_get_dev_info(i); - pj_memcpy(&info[i], ci, sizeof(*ci)); + status = pjmedia_aud_dev_get_info(i, &info[i]); + if (status != PJ_SUCCESS) + return status; } *count = dev_count; @@ -2119,56 +2226,154 @@ PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[], } -/* Close existing sound device */ -static void close_snd_dev(void) +PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[], + unsigned *count) { - /* Close sound device */ - if (pjsua_var.snd_port) { - const pjmedia_snd_dev_info *cap_info, *play_info; + unsigned i, dev_count; + + dev_count = pjmedia_aud_dev_count(); + + if (dev_count > *count) dev_count = *count; + pj_bzero(info, dev_count * sizeof(pjmedia_snd_dev_info)); - cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev); - play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev); + for (i=0; i<dev_count; ++i) { + pjmedia_aud_dev_info ai; + pj_status_t status; - PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and " - "%s sound capture device", - play_info->name, cap_info->name)); + status = pjmedia_aud_dev_get_info(i, &ai); + if (status != PJ_SUCCESS) + return status; - pjmedia_snd_port_disconnect(pjsua_var.snd_port); - pjmedia_snd_port_destroy(pjsua_var.snd_port); - pjsua_var.snd_port = NULL; + strncpy(info[i].name, ai.name, sizeof(info[i].name)); + info[i].name[sizeof(info[i].name)-1] = '\0'; + info[i].input_count = ai.input_count; + info[i].output_count = ai.output_count; + info[i].default_samples_per_sec = ai.default_samples_per_sec; } - /* Close null sound device */ - if (pjsua_var.null_snd) { - PJ_LOG(4,(THIS_FILE, "Closing null sound device..")); - pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE); - pjsua_var.null_snd = NULL; + *count = dev_count; + + return PJ_SUCCESS; +} + +/* Create audio device parameter to open the device */ +static pj_status_t create_aud_param(pjmedia_aud_param *param, + pjmedia_aud_dev_index capture_dev, + pjmedia_aud_dev_index playback_dev, + unsigned clock_rate, + unsigned channel_count, + unsigned samples_per_frame, + unsigned bits_per_sample) +{ + pj_status_t status; + + /* Normalize device ID with new convention about default device ID */ + if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV) + playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV; + + /* Create default parameters for the device */ + status = pjmedia_aud_dev_default_param(capture_dev, param); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Error retrieving default audio " + "device parameters", status); + return status; + } + param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; + param->rec_id = capture_dev; + param->play_id = playback_dev; + param->clock_rate = clock_rate; + param->channel_count = channel_count; + param->samples_per_frame = samples_per_frame; + param->bits_per_sample = bits_per_sample; + + /* Update the setting with user preference */ +#define update_param(cap, field) \ + if (pjsua_var.aud_param.flags & cap) { \ + param->flags |= cap; \ + param->field = pjsua_var.aud_param.field; \ + } + update_param( PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol); + update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol); + update_param( PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route); + update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route); +#undef update_param + + /* Latency settings */ + param->flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | + PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY); + param->input_latency_ms = pjsua_var.media_cfg.snd_rec_latency; + param->output_latency_ms = pjsua_var.media_cfg.snd_play_latency; + + /* EC settings */ + if (pjsua_var.media_cfg.ec_tail_len) { + param->flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL); + param->ec_enabled = PJ_TRUE; + param->ec_tail_ms = pjsua_var.media_cfg.ec_tail_len; + } else { + param->flags &= ~(PJMEDIA_AUD_DEV_CAP_EC|PJMEDIA_AUD_DEV_CAP_EC_TAIL); } - if (pjsua_var.snd_pool) - pj_pool_release(pjsua_var.snd_pool); - pjsua_var.snd_pool = NULL; + return PJ_SUCCESS; } -/* - * Select or change sound device. Application may call this function at - * any time to replace current sound device. +/* Internal: the first time the audio device is opened (during app + * startup), retrieve the audio settings such as volume level + * so that aud_get_settings() will work. */ -PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, - int playback_dev) +static pj_status_t update_initial_aud_param() +{ + pjmedia_aud_stream *strm; + pjmedia_aud_param param; + pj_status_t status; + + PJ_ASSERT_RETURN(pjsua_var.snd_port != NULL, PJ_EBUG); + + strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); + + status = pjmedia_aud_stream_get_param(strm, ¶m); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Error audio stream " + "device parameters", status); + return status; + } + +#define update_saved_param(cap, field) \ + if (param.flags & cap) { \ + pjsua_var.aud_param.flags |= cap; \ + pjsua_var.aud_param.field = param.field; \ + } + + update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol); + update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol); + update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route); + update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route); +#undef update_saved_param + + return PJ_SUCCESS; +} + +/* Get format name */ +static const char *get_fmt_name(pj_uint32_t id) +{ + static char name[8]; + + if (id == PJMEDIA_FORMAT_L16) + return "PCM"; + pj_memcpy(name, &id, 4); + name[4] = '\0'; + return name; +} + +/* Open sound device with the setting. */ +static pj_status_t open_snd_dev(pjmedia_aud_param *param) { pjmedia_port *conf_port; - const pjmedia_snd_dev_info *play_info; - unsigned clock_rates[] = {0, 44100, 48000, 32000, 16000, 8000}; - unsigned selected_clock_rate = 0; - unsigned i; - pjmedia_snd_stream *strm; - pjmedia_snd_stream_info si; - pj_str_t tmp; - pj_status_t status = -1; + pj_status_t status; + + PJ_ASSERT_RETURN(param, PJ_EINVAL); /* Check if NULL sound device is used */ - if (NULL_SND_DEV_ID == capture_dev || NULL_SND_DEV_ID == playback_dev) { + if (NULL_SND_DEV_ID==param->rec_id || NULL_SND_DEV_ID==param->play_id) { return pjsua_set_null_snd_dev(); } @@ -2179,94 +2384,72 @@ PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000); PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM); - /* Set default clock rate */ - clock_rates[0] = pjsua_var.media_cfg.snd_clock_rate; - if (clock_rates[0] == 0) - clock_rates[0] = pjsua_var.media_cfg.clock_rate; + + PJ_LOG(4,(THIS_FILE, "Opening sound device %s@%d/%d/%dms", + get_fmt_name(param->ext_fmt.id), + param->clock_rate, param->channel_count, + param->samples_per_frame / param->channel_count * 1000 / + param->clock_rate)); + + status = pjmedia_snd_port_create2( pjsua_var.snd_pool, + param, &pjsua_var.snd_port); + if (status != PJ_SUCCESS) + return status; /* Get the port0 of the conference bridge. */ conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf); pj_assert(conf_port != NULL); - /* Attempts to open the sound device with different clock rates */ - for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) { - char errmsg[PJ_ERR_MSG_SIZE]; - unsigned samples_per_frame; - - PJ_LOG(4,(THIS_FILE, - "pjsua_set_snd_dev(): attempting to open devices " - "@%d Hz", clock_rates[i])); - - samples_per_frame = clock_rates[i] * - pjsua_var.media_cfg.audio_frame_ptime * - pjsua_var.media_cfg.channel_count / 1000; - - /* Create the sound device. Sound port will start immediately. */ - status = pjmedia_snd_port_create(pjsua_var.snd_pool, capture_dev, - playback_dev, - clock_rates[i], - pjsua_var.media_cfg.channel_count, - samples_per_frame, - 16, 0, &pjsua_var.snd_port); - - if (status == PJ_SUCCESS) { - selected_clock_rate = clock_rates[i]; - - /* If there's mismatch between sound port and conference's port, - * create a resample port to bridge them. - */ - if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) { - pjmedia_port *resample_port; - unsigned resample_opt = 0; - - if (pjsua_var.media_cfg.quality >= 3 && - pjsua_var.media_cfg.quality <= 4) - { - resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER; - } - else if (pjsua_var.media_cfg.quality < 3) { - resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR; - } - - status = pjmedia_resample_port_create(pjsua_var.snd_pool, - conf_port, - selected_clock_rate, - resample_opt, - &resample_port); - if (status != PJ_SUCCESS) { - pj_strerror(status, errmsg, sizeof(errmsg)); - PJ_LOG(4, (THIS_FILE, - "Error creating resample port, trying next " - "clock rate", - errmsg)); - pjmedia_snd_port_destroy(pjsua_var.snd_port); - pjsua_var.snd_port = NULL; - continue; - } else { - conf_port = resample_port; - break; - } + /* For conference bridge, resample if necessary if the bridge's + * clock rate is different than the sound device's clock rate. + */ + if (!pjsua_var.is_mswitch && + param->ext_fmt.id == PJMEDIA_FORMAT_PCM && + conf_port->info.clock_rate != param->clock_rate) + { + pjmedia_port *resample_port; + unsigned resample_opt = 0; - } else { - break; - } + if (pjsua_var.media_cfg.quality >= 3 && + pjsua_var.media_cfg.quality <= 4) + { + resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER; } - - pj_strerror(status, errmsg, sizeof(errmsg)); - PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg)); + else if (pjsua_var.media_cfg.quality < 3) { + resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR; + } + + status = pjmedia_resample_port_create(pjsua_var.snd_pool, + conf_port, + param->clock_rate, + resample_opt, + &resample_port); + if (status != PJ_SUCCESS) { + char errmsg[PJ_ERR_MSG_SIZE]; + pj_strerror(status, errmsg, sizeof(errmsg)); + PJ_LOG(4, (THIS_FILE, + "Error creating resample port: %s", + errmsg)); + close_snd_dev(); + return status; + } + + conf_port = resample_port; } - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Unable to open sound device", status); - return status; + /* Otherwise for audio switchboard, the switch's port0 setting is + * derived from the sound device setting, so update the setting. + */ + if (pjsua_var.is_mswitch) { + pj_memcpy(&conf_port->info.format, ¶m->ext_fmt, + sizeof(conf_port->info.format)); + conf_port->info.clock_rate = param->clock_rate; + conf_port->info.samples_per_frame = param->samples_per_frame; + conf_port->info.channel_count = param->channel_count; + conf_port->info.bits_per_sample = 16; } - /* Set AEC */ - pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.snd_pool, - pjsua_var.media_cfg.ec_tail_len, - pjsua_var.media_cfg.ec_options); - - /* Connect sound port to the bridge */ + /* Connect sound port to the bridge */ status = pjmedia_snd_port_connect(pjsua_var.snd_port, conf_port ); if (status != PJ_SUCCESS) { @@ -2278,25 +2461,145 @@ PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, } /* Save the device IDs */ - pjsua_var.cap_dev = capture_dev; - pjsua_var.play_dev = playback_dev; + pjsua_var.cap_dev = param->rec_id; + pjsua_var.play_dev = param->play_id; /* Update sound device name. */ - strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); - pjmedia_snd_stream_get_info(strm, &si); - play_info = pjmedia_snd_get_dev_info(si.rec_id); + { + pjmedia_aud_dev_info rec_info; + pjmedia_aud_stream *strm; + pjmedia_aud_param si; + pj_str_t tmp; + + strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); + status = pjmedia_aud_stream_get_param(strm, &si); + if (status == PJ_SUCCESS) + status = pjmedia_aud_dev_get_info(si.rec_id, &rec_info); + + if (status==PJ_SUCCESS) { + if (param->clock_rate != pjsua_var.media_cfg.clock_rate) { + char tmp_buf[128]; + int tmp_buf_len = sizeof(tmp_buf); + + tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, + "%s (%dKHz)", + rec_info.name, + param->clock_rate/1000); + pj_strset(&tmp, tmp_buf, tmp_buf_len); + pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp); + } else { + pjmedia_conf_set_port0_name(pjsua_var.mconf, + pj_cstr(&tmp, rec_info.name)); + } + } + + /* Any error is not major, let it through */ + status = PJ_SUCCESS; + }; + + /* If this is the first time the audio device is open, retrieve some + * settings from the device (such as volume settings) so that the + * pjsua_snd_get_setting() work. + */ + if (pjsua_var.aud_open_cnt == 0) { + update_initial_aud_param(); + ++pjsua_var.aud_open_cnt; + } + + return PJ_SUCCESS; +} + + +/* Close existing sound device */ +static void close_snd_dev(void) +{ + /* Close sound device */ + if (pjsua_var.snd_port) { + pjmedia_aud_dev_info cap_info, play_info; + pjmedia_aud_stream *strm; + pjmedia_aud_param param; + + strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); + pjmedia_aud_stream_get_param(strm, ¶m); + + if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS) + cap_info.name[0] = '\0'; + if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS) + play_info.name[0] = '\0'; + + PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and " + "%s sound capture device", + play_info.name, cap_info.name)); + + pjmedia_snd_port_disconnect(pjsua_var.snd_port); + pjmedia_snd_port_destroy(pjsua_var.snd_port); + pjsua_var.snd_port = NULL; + } + + /* Close null sound device */ + if (pjsua_var.null_snd) { + PJ_LOG(4,(THIS_FILE, "Closing null sound device..")); + pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE); + pjsua_var.null_snd = NULL; + } + + if (pjsua_var.snd_pool) + pj_pool_release(pjsua_var.snd_pool); + pjsua_var.snd_pool = NULL; +} + + +/* + * Select or change sound device. Application may call this function at + * any time to replace current sound device. + */ +PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, + int playback_dev) +{ + unsigned alt_cr_cnt = 1; + unsigned alt_cr[] = {0, 44100, 48000, 32000, 16000, 8000}; + unsigned i; + pj_status_t status = -1; - if (si.clock_rate != pjsua_var.media_cfg.clock_rate) { - char tmp_buf[128]; - int tmp_buf_len = sizeof(tmp_buf); + /* Set default clock rate */ + alt_cr[0] = pjsua_var.media_cfg.snd_clock_rate; + if (alt_cr[0] == 0) + alt_cr[0] = pjsua_var.media_cfg.clock_rate; - tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, "%s (%dKHz)", - play_info->name, si.clock_rate/1000); - pj_strset(&tmp, tmp_buf, tmp_buf_len); - pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp); + /* Allow retrying of different clock rate if we're using conference + * bridge (meaning audio format is always PCM), otherwise lock on + * to one clock rate. + */ + if (pjsua_var.is_mswitch) { + alt_cr_cnt = 1; } else { - pjmedia_conf_set_port0_name(pjsua_var.mconf, - pj_cstr(&tmp, play_info->name)); + alt_cr_cnt = PJ_ARRAY_SIZE(alt_cr); + } + + /* Attempts to open the sound device with different clock rates */ + for (i=0; i<alt_cr_cnt; ++i) { + pjmedia_aud_param param; + unsigned samples_per_frame; + + /* Create the default audio param */ + samples_per_frame = alt_cr[i] * + pjsua_var.media_cfg.audio_frame_ptime * + pjsua_var.media_cfg.channel_count / 1000; + status = create_aud_param(¶m, capture_dev, playback_dev, + alt_cr[i], pjsua_var.media_cfg.channel_count, + samples_per_frame, 16); + if (status != PJ_SUCCESS) + return status; + + /* Open! */ + status = open_snd_dev(¶m); + if (status == PJ_SUCCESS) + break; + } + + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Unable to open sound device", status); + return status; } return PJ_SUCCESS; @@ -2404,6 +2707,81 @@ PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms) } +/* + * Check whether the sound device is currently active. + */ +PJ_DEF(pj_bool_t) pjsua_snd_is_active(void) +{ + return pjsua_var.snd_port != NULL; +} + + +/* + * Configure sound device setting to the sound device being used. + */ +PJ_DEF(pj_status_t) pjsua_snd_set_setting( pjmedia_aud_dev_cap cap, + const void *pval, + pj_bool_t keep) +{ + pj_status_t status; + + /* Check if we are allowed to set the cap */ + if ((cap & pjsua_var.aud_svmask) == 0) { + return PJMEDIA_EAUD_INVCAP; + } + + /* If sound is active, set it immediately */ + if (pjsua_snd_is_active()) { + pjmedia_aud_stream *strm; + + strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); + status = pjmedia_aud_stream_set_cap(strm, cap, pval); + } else { + status = PJ_SUCCESS; + } + + if (status != PJ_SUCCESS) + return status; + + /* Save in internal param for later device open */ + if (keep) { + status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param, + cap, pval); + } + + return status; +} + +/* + * Retrieve a sound device setting. + */ +PJ_DEF(pj_status_t) pjsua_snd_get_setting( pjmedia_aud_dev_cap cap, + void *pval) +{ + /* If sound device has never been opened before, open it to + * retrieve the initial setting from the device (e.g. audio + * volume) + */ + if (pjsua_var.aud_open_cnt==0) { + PJ_LOG(4,(THIS_FILE, "Opening sound device to get initial settings")); + pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); + close_snd_dev(); + } + + if (pjsua_snd_is_active()) { + /* Sound is active, retrieve from device directly */ + pjmedia_aud_stream *strm; + + strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); + return pjmedia_aud_stream_get_cap(strm, cap, pval); + } else { + /* Otherwise retrieve from internal param */ + return pjmedia_aud_param_get_cap(&pjsua_var.aud_param, + cap, pval); + } +} + + /***************************************************************************** * Codecs. */ |