方法一:diaplan配置APP
打开会话录音:
action application=“record_session” data=“xxx.wav”
app源码
SWITCH_STANDARD_APP(record_session_function)
{
char *path = NULL;
char *path_end;
uint32_t limit = 0;
if (zstr(data)) {
return;
}
path = switch_core_session_strdup(session, data);
/* Search for a space then a plus followed by only numbers at the end of the path,
if found trim any spaces to the left/right of the plus use the left side as the
path and right side as a time limit on the recording
*/
/* if we find a + and the character before it is a space */
if ((path_end = strrchr(path, '+')) && path_end > path && *(path_end - 1) == ' ') {
char *limit_start = path_end + 1;
/* not at the end and the rest is numbers lets parse out the limit and fix up the path */
if (*limit_start != '\0' && switch_is_number(limit_start) == SWITCH_TRUE) {
limit = atoi(limit_start);
/* back it off by one character to the char before the + */
path_end--;
/* trim spaces to the left of the plus */
while (path_end > path && *path_end == ' ') {
path_end--;
}
*(path_end + 1) = '\0';
}
}
switch_ivr_record_session(session, path, limit, NULL);
}
可以看到次app调用的是switch_ivr_record_session函数,继续看switch_ivr_record_session函数,此函数核心逻辑是添加了一个回调函数
if ((status = switch_core_media_bug_add(session, "session_record", file,
record_callback, rh, to, flags, &bug)) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error adding media bug for file %s\n", file);
if (rh->native) {
switch_core_file_close(&rh->in_fh);
switch_core_file_close(&rh->out_fh);
} else {
switch_core_file_close(fh);
}
return status;
}
然后通话过程中每一帧处理都会调用此回调函数,调用处:
switch_core_io.c:
if (session->bugs && !((*frame)->flags & SFF_CNG) && !((*frame)->flags & SFF_NOT_AUDIO)) {
switch_media_bug_t *bp;
switch_bool_t ok = SWITCH_TRUE;
int prune = 0;
switch_thread_rwlock_rdlock(session->bug_rwlock);
for (bp = session->bugs; bp; bp = bp->next) {
ok = SWITCH_TRUE;
if (switch_channel_test_flag(session->channel, CF_PAUSE_BUGS) && !switch_core_media_bug_test_flag(bp, SMBF_NO_PAUSE)) {
continue;
}
if (!switch_channel_test_flag(session->channel, CF_ANSWERED) && switch_core_media_bug_test_flag(bp, SMBF_ANSWER_REQ)) {
continue;
}
if (switch_test_flag(bp, SMBF_PRUNE)) {
prune++;
continue;
}
if (bp->ready) {
if (switch_test_flag(bp, SMBF_TAP_NATIVE_READ)) {
if ((*frame)->codec && (*frame)->codec->implementation &&
(*frame)->codec->implementation->encoded_bytes_per_packet &&
(*frame)->datalen != (*frame)->codec->implementation->encoded_bytes_per_packet) {
switch_set_flag((*frame), SFF_CNG);
break;
}
if (bp->callback) {
bp->native_read_frame = *frame;
ok = bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_TAP_NATIVE_READ);
bp->native_read_frame = NULL;
}
}
}
if ((bp->stop_time && bp->stop_time <= switch_epoch_time_now(NULL)) || ok == SWITCH_FALSE) {
switch_set_flag(bp, SMBF_PRUNE);
prune++;
}
}
switch_thread_rwlock_unlock(session->bug_rwlock);
if (prune) {
switch_core_media_bug_prune(session);
}
}
设置双轨录音
action application=“set” data=“RECORD_STEREO=true”/
源码switch_ivr_async.c:检测RECORD_STEREO字段,设置channels为2
if (channels == 1) { /* if leg is already stereo this feature is not available */
if ((p = switch_channel_get_variable(channel, "RECORD_STEREO")) && switch_true(p)) {
flags |= SMBF_STEREO;
flags &= ~SMBF_STEREO_SWAP;
channels = 2;
}
if ((p = switch_channel_get_variable(channel, "RECORD_STEREO_SWAP")) && switch_true(p)) {
flags |= SMBF_STEREO;
flags |= SMBF_STEREO_SWAP;
channels = 2;
}
}
方法二:ESL 模式API
开始录音API:
uuid_record < uuid> [start|stop|mask|unmask] < path> [< limit>]
分析源码可见,其调用的也是switch_ivr_record_session函数,所以再在开始录音前设置RECORD_STEREO即可实现双规录音。
if (!strcasecmp(action, "start")) {
if (switch_ivr_record_session(rsession, path, limit, NULL) != SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "-ERR Cannot record session!\n");
} else {
stream->write_function(stream, "+OK Success\n");
}
} else if (!strcasecmp(action, "stop")) {
设置参数API:
uuid_setvar < uuid> < var> [value]