Newer
Older
Richard Mudgett
committed
frame = &ast_null_frame;
break;
case AST_JB_IMPL_INTERP:
if (framedata->last_format) {
Richard Mudgett
committed
struct ast_frame tmp = { 0, };
Richard Mudgett
committed
tmp.frametype = AST_FRAME_VOICE;
tmp.subclass.format = framedata->last_format;
Richard Mudgett
committed
/* example: 8000hz / (1000 / 20ms) = 160 samples */
tmp.samples = ast_format_get_sample_rate(framedata->last_format) / (1000 / framedata->timer_interval);
Richard Mudgett
committed
tmp.delivery = ast_tvadd(framedata->start_tv, ast_samp2tv(next, 1000));
tmp.offset = AST_FRIENDLY_OFFSET;
tmp.src = "func_jitterbuffer interpolation";
Richard Mudgett
committed
frame = ast_frdup(&tmp);
break;
}
/* else fall through */
case AST_JB_IMPL_NOFRAME:
Richard Mudgett
committed
frame = &ast_null_frame;
break;
}
}
if (frame->frametype == AST_FRAME_CONTROL) {
switch(frame->subclass.integer) {
case AST_CONTROL_HOLD:
case AST_CONTROL_UNHOLD:
case AST_CONTROL_T38_PARAMETERS:
Richard Mudgett
committed
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
case AST_CONTROL_SRCUPDATE:
case AST_CONTROL_SRCCHANGE:
framedata->jb_impl->force_resync(framedata->jb_obj);
break;
default:
break;
}
}
return frame;
}
/* set defaults */
static int jb_framedata_init(struct jb_framedata *framedata, struct ast_jb_conf *jb_conf)
{
int jb_impl_type = DEFAULT_TYPE;
/* Initialize defaults */
framedata->timer_fd = -1;
memcpy(&framedata->jb_conf, jb_conf, sizeof(*jb_conf));
/* Figure out implementation type from the configuration implementation string */
if (!ast_strlen_zero(jb_conf->impl)) {
if (!strcasecmp(jb_conf->impl, "fixed")) {
jb_impl_type = AST_JB_FIXED;
} else if (!strcasecmp(jb_conf->impl, "adaptive")) {
jb_impl_type = AST_JB_ADAPTIVE;
} else {
ast_log(LOG_WARNING, "Unknown Jitterbuffer type %s. Failed to create jitterbuffer.\n", jb_conf->impl);
return -1;
}
}
if (!(framedata->jb_impl = ast_jb_get_impl(jb_impl_type))) {
return -1;
}
if (!(framedata->timer = ast_timer_open())) {
return -1;
}
framedata->timer_fd = ast_timer_fd(framedata->timer);
framedata->timer_interval = DEFAULT_TIMER_INTERVAL;
ast_timer_set_rate(framedata->timer, 1000 / framedata->timer_interval);
framedata->start_tv = ast_tvnow();
framedata->jb_obj = framedata->jb_impl->create(&framedata->jb_conf);
return 0;
}
void ast_jb_create_framehook(struct ast_channel *chan, struct ast_jb_conf *jb_conf, int prefer_existing)
{
struct jb_framedata *framedata;
struct ast_datastore *datastore = NULL;
struct ast_framehook_interface interface = {
.version = AST_FRAMEHOOK_INTERFACE_VERSION,
.event_cb = hook_event_cb,
.destroy_cb = hook_destroy_cb,
};
int i = 0;
/* If disabled, strip any existing jitterbuffer and don't replace it. */
if (!strcasecmp(jb_conf->impl, "disabled")) {
int *id;
ast_channel_lock(chan);
if ((datastore = ast_channel_datastore_find(chan, &jb_datastore, NULL))) {
id = datastore->data;
ast_framehook_detach(chan, *id);
ast_channel_datastore_remove(chan, datastore);
ast_datastore_free(datastore);
Richard Mudgett
committed
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
}
ast_channel_unlock(chan);
return;
}
if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
return;
}
if (jb_framedata_init(framedata, jb_conf)) {
jb_framedata_destroy(framedata);
return;
}
interface.data = framedata;
ast_channel_lock(chan);
i = ast_framehook_attach(chan, &interface);
if (i >= 0) {
int *id;
if ((datastore = ast_channel_datastore_find(chan, &jb_datastore, NULL))) {
/* There is already a jitterbuffer on the channel. */
if (prefer_existing) {
/* We prefer the existing jitterbuffer, so remove the new one and keep the old one. */
ast_framehook_detach(chan, i);
ast_channel_unlock(chan);
return;
}
/* We prefer the new jitterbuffer, so strip the old one. */
id = datastore->data;
ast_framehook_detach(chan, *id);
ast_channel_datastore_remove(chan, datastore);
ast_datastore_free(datastore);
Richard Mudgett
committed
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
}
if (!(datastore = ast_datastore_alloc(&jb_datastore, NULL))) {
ast_framehook_detach(chan, i);
ast_channel_unlock(chan);
return;
}
if (!(id = ast_calloc(1, sizeof(int)))) {
ast_datastore_free(datastore);
ast_framehook_detach(chan, i);
ast_channel_unlock(chan);
return;
}
*id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
datastore->data = id;
ast_channel_datastore_add(chan, datastore);
ast_channel_set_fd(chan, AST_JITTERBUFFER_FD, framedata->timer_fd);
} else {
jb_framedata_destroy(framedata);
framedata = NULL;
}
ast_channel_unlock(chan);
}