mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-08-14 01:49:05 +00:00
FS-9708 #resolve [RTP timing when doing repacketization]
This commit is contained in:
@@ -945,708 +945,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi
|
||||
return status;
|
||||
}
|
||||
|
||||
static switch_status_t perform_write(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id)
|
||||
{
|
||||
switch_io_event_hook_write_frame_t *ptr;
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
|
||||
|
||||
if (session->bugs && !(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_WRITE)) {
|
||||
if (bp->callback) {
|
||||
bp->native_write_frame = frame;
|
||||
ok = bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_TAP_NATIVE_WRITE);
|
||||
bp->native_write_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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (session->endpoint_interface->io_routines->write_frame) {
|
||||
if ((status = session->endpoint_interface->io_routines->write_frame(session, frame, flags, stream_id)) == SWITCH_STATUS_SUCCESS) {
|
||||
for (ptr = session->event_hooks.write_frame; ptr; ptr = ptr->next) {
|
||||
if ((status = ptr->write_frame(session, frame, flags, stream_id)) != SWITCH_STATUS_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags,
|
||||
int stream_id)
|
||||
{
|
||||
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
switch_frame_t *enc_frame = NULL, *write_frame = frame;
|
||||
unsigned int flag = 0, need_codec = 0, perfect = 0, do_bugs = 0, do_write = 0, do_resample = 0, ptime_mismatch = 0, pass_cng = 0, resample = 0;
|
||||
int did_write_resample = 0;
|
||||
|
||||
switch_assert(session != NULL);
|
||||
switch_assert(frame != NULL);
|
||||
|
||||
if (!switch_channel_ready(session->channel)) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if (switch_mutex_trylock(session->codec_write_mutex) == SWITCH_STATUS_SUCCESS) {
|
||||
switch_mutex_unlock(session->codec_write_mutex);
|
||||
} else {
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (switch_test_flag(frame, SFF_CNG)) {
|
||||
if (switch_channel_test_flag(session->channel, CF_ACCEPT_CNG)) {
|
||||
pass_cng = 1;
|
||||
} else {
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
if (switch_channel_test_flag(session->channel, CF_AUDIO_PAUSE_WRITE)) {
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (!(session->write_codec && switch_core_codec_ready(session->write_codec)) && !pass_cng) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "%s has no write codec.\n", switch_channel_get_name(session->channel));
|
||||
switch_channel_hangup(session->channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if (switch_channel_test_flag(session->channel, CF_HOLD)) {
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (switch_test_flag(frame, SFF_PROXY_PACKET) || pass_cng) {
|
||||
/* Fast PASS! */
|
||||
switch_mutex_lock(session->codec_write_mutex);
|
||||
status = perform_write(session, frame, flag, stream_id);
|
||||
switch_mutex_unlock(session->codec_write_mutex);
|
||||
return status;
|
||||
}
|
||||
|
||||
switch_mutex_lock(session->codec_write_mutex);
|
||||
|
||||
if (!(frame->codec && frame->codec->implementation)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "%s has received a bad frame with no codec!\n",
|
||||
switch_channel_get_name(session->channel));
|
||||
switch_channel_hangup(session->channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
|
||||
switch_mutex_unlock(session->codec_write_mutex);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
switch_assert(frame->codec != NULL);
|
||||
switch_assert(frame->codec->implementation != NULL);
|
||||
|
||||
if (!(switch_core_codec_ready(session->write_codec) && frame->codec) ||
|
||||
!switch_channel_ready(session->channel) || !switch_channel_media_ready(session->channel)) {
|
||||
switch_mutex_unlock(session->codec_write_mutex);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
switch_mutex_lock(session->write_codec->mutex);
|
||||
switch_mutex_lock(frame->codec->mutex);
|
||||
|
||||
if (!(switch_core_codec_ready(session->write_codec) && switch_core_codec_ready(frame->codec))) goto error;
|
||||
|
||||
if ((session->write_codec && frame->codec && session->write_codec->implementation != frame->codec->implementation)) {
|
||||
if (session->write_impl.codec_id == frame->codec->implementation->codec_id ||
|
||||
session->write_impl.microseconds_per_packet != frame->codec->implementation->microseconds_per_packet) {
|
||||
ptime_mismatch = TRUE;
|
||||
if ((switch_test_flag(frame->codec, SWITCH_CODEC_FLAG_PASSTHROUGH) || switch_test_flag(session->read_codec, SWITCH_CODEC_FLAG_PASSTHROUGH)) ||
|
||||
switch_channel_test_flag(session->channel, CF_PASSTHRU_PTIME_MISMATCH)) {
|
||||
status = perform_write(session, frame, flags, stream_id);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
need_codec = TRUE;
|
||||
}
|
||||
|
||||
if (session->write_codec && !frame->codec) {
|
||||
need_codec = TRUE;
|
||||
}
|
||||
|
||||
if (session->bugs && !need_codec && !switch_test_flag(session, SSF_MEDIA_BUG_TAP_ONLY)) {
|
||||
do_bugs = TRUE;
|
||||
need_codec = TRUE;
|
||||
}
|
||||
|
||||
if (frame->codec->implementation->actual_samples_per_second != session->write_impl.actual_samples_per_second) {
|
||||
need_codec = TRUE;
|
||||
do_resample = TRUE;
|
||||
}
|
||||
|
||||
|
||||
if ((frame->flags & SFF_NOT_AUDIO)) {
|
||||
do_resample = 0;
|
||||
do_bugs = 0;
|
||||
need_codec = 0;
|
||||
}
|
||||
|
||||
if (switch_test_flag(session, SSF_WRITE_TRANSCODE) && !need_codec && switch_core_codec_ready(session->write_codec)) {
|
||||
switch_core_session_t *other_session;
|
||||
const char *uuid = switch_channel_get_partner_uuid(switch_core_session_get_channel(session));
|
||||
|
||||
if (uuid && (other_session = switch_core_session_locate(uuid))) {
|
||||
switch_set_flag(other_session, SSF_READ_CODEC_RESET);
|
||||
switch_set_flag(other_session, SSF_READ_CODEC_RESET);
|
||||
switch_set_flag(other_session, SSF_WRITE_CODEC_RESET);
|
||||
switch_core_session_rwunlock(other_session);
|
||||
}
|
||||
|
||||
switch_clear_flag(session, SSF_WRITE_TRANSCODE);
|
||||
}
|
||||
|
||||
|
||||
if (switch_test_flag(session, SSF_WRITE_CODEC_RESET)) {
|
||||
switch_core_codec_reset(session->write_codec);
|
||||
switch_clear_flag(session, SSF_WRITE_CODEC_RESET);
|
||||
}
|
||||
|
||||
if (!need_codec) {
|
||||
do_write = TRUE;
|
||||
write_frame = frame;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!switch_test_flag(session, SSF_WARN_TRANSCODE)) {
|
||||
switch_core_session_message_t msg = { 0 };
|
||||
|
||||
msg.message_id = SWITCH_MESSAGE_INDICATE_TRANSCODING_NECESSARY;
|
||||
switch_core_session_receive_message(session, &msg);
|
||||
switch_set_flag(session, SSF_WARN_TRANSCODE);
|
||||
}
|
||||
|
||||
if (frame->codec) {
|
||||
session->raw_write_frame.datalen = session->raw_write_frame.buflen;
|
||||
frame->codec->cur_frame = frame;
|
||||
session->write_codec->cur_frame = frame;
|
||||
status = switch_core_codec_decode(frame->codec,
|
||||
session->write_codec,
|
||||
frame->data,
|
||||
frame->datalen,
|
||||
session->write_impl.actual_samples_per_second,
|
||||
session->raw_write_frame.data, &session->raw_write_frame.datalen, &session->raw_write_frame.rate, &frame->flags);
|
||||
frame->codec->cur_frame = NULL;
|
||||
session->write_codec->cur_frame = NULL;
|
||||
if (do_resample && status == SWITCH_STATUS_SUCCESS) {
|
||||
status = SWITCH_STATUS_RESAMPLE;
|
||||
}
|
||||
|
||||
/* mux or demux to match */
|
||||
if (session->write_impl.number_of_channels != frame->codec->implementation->number_of_channels) {
|
||||
uint32_t rlen = session->raw_write_frame.datalen / 2 / frame->codec->implementation->number_of_channels;
|
||||
switch_mux_channels((int16_t *) session->raw_write_frame.data, rlen,
|
||||
frame->codec->implementation->number_of_channels, session->write_impl.number_of_channels);
|
||||
session->raw_write_frame.datalen = rlen * 2 * session->write_impl.number_of_channels;
|
||||
}
|
||||
|
||||
switch (status) {
|
||||
case SWITCH_STATUS_RESAMPLE:
|
||||
resample++;
|
||||
write_frame = &session->raw_write_frame;
|
||||
write_frame->rate = frame->codec->implementation->actual_samples_per_second;
|
||||
if (!session->write_resampler) {
|
||||
switch_mutex_lock(session->resample_mutex);
|
||||
status = switch_resample_create(&session->write_resampler,
|
||||
frame->codec->implementation->actual_samples_per_second,
|
||||
session->write_impl.actual_samples_per_second,
|
||||
session->write_impl.decoded_bytes_per_packet, SWITCH_RESAMPLE_QUALITY, session->write_impl.number_of_channels);
|
||||
|
||||
|
||||
switch_mutex_unlock(session->resample_mutex);
|
||||
if (status != SWITCH_STATUS_SUCCESS) {
|
||||
goto done;
|
||||
} else {
|
||||
switch_core_session_message_t msg = { 0 };
|
||||
msg.numeric_arg = 1;
|
||||
msg.message_id = SWITCH_MESSAGE_RESAMPLE_EVENT;
|
||||
switch_core_session_receive_message(session, &msg);
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Activating write resampler\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SWITCH_STATUS_SUCCESS:
|
||||
session->raw_write_frame.samples = session->raw_write_frame.datalen / sizeof(int16_t) / session->write_impl.number_of_channels;
|
||||
session->raw_write_frame.channels = session->write_impl.number_of_channels;
|
||||
session->raw_write_frame.timestamp = frame->timestamp;
|
||||
session->raw_write_frame.rate = frame->rate;
|
||||
session->raw_write_frame.m = frame->m;
|
||||
session->raw_write_frame.ssrc = frame->ssrc;
|
||||
session->raw_write_frame.seq = frame->seq;
|
||||
session->raw_write_frame.payload = frame->payload;
|
||||
session->raw_write_frame.flags = 0;
|
||||
if (switch_test_flag(frame, SFF_PLC)) {
|
||||
session->raw_write_frame.flags |= SFF_PLC;
|
||||
}
|
||||
|
||||
write_frame = &session->raw_write_frame;
|
||||
break;
|
||||
case SWITCH_STATUS_BREAK:
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
goto error;
|
||||
case SWITCH_STATUS_NOOP:
|
||||
if (session->write_resampler) {
|
||||
switch_mutex_lock(session->resample_mutex);
|
||||
switch_resample_destroy(&session->write_resampler);
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Deactivating write resampler\n");
|
||||
switch_mutex_unlock(session->resample_mutex);
|
||||
|
||||
{
|
||||
switch_core_session_message_t msg = { 0 };
|
||||
msg.numeric_arg = 0;
|
||||
msg.message_id = SWITCH_MESSAGE_RESAMPLE_EVENT;
|
||||
switch_core_session_receive_message(session, &msg);
|
||||
}
|
||||
|
||||
}
|
||||
write_frame = frame;
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
break;
|
||||
default:
|
||||
|
||||
if (status == SWITCH_STATUS_NOT_INITALIZED) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec init error!\n");
|
||||
goto error;
|
||||
}
|
||||
if (ptime_mismatch && status != SWITCH_STATUS_GENERR) {
|
||||
status = perform_write(session, frame, flags, stream_id);
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
goto error;
|
||||
}
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec %s decoder error!\n",
|
||||
frame->codec->codec_interface->interface_name);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (session->write_resampler) {
|
||||
short *data = write_frame->data;
|
||||
|
||||
switch_mutex_lock(session->resample_mutex);
|
||||
if (session->write_resampler) {
|
||||
|
||||
if (switch_resample_calc_buffer_size(session->write_resampler->to_rate, session->write_resampler->from_rate,
|
||||
write_frame->datalen / 2 / session->write_resampler->channels) > SWITCH_RECOMMENDED_BUFFER_SIZE) {
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "%s not enough buffer space for required resample operation!\n",
|
||||
switch_channel_get_name(session->channel));
|
||||
switch_channel_hangup(session->channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
|
||||
switch_mutex_unlock(session->resample_mutex);
|
||||
goto error;
|
||||
}
|
||||
|
||||
|
||||
switch_resample_process(session->write_resampler, data, write_frame->datalen / 2 / session->write_resampler->channels);
|
||||
|
||||
memcpy(data, session->write_resampler->to, session->write_resampler->to_len * 2 * session->write_resampler->channels);
|
||||
|
||||
write_frame->samples = session->write_resampler->to_len;
|
||||
write_frame->channels = session->write_resampler->channels;
|
||||
write_frame->datalen = write_frame->samples * 2 * session->write_resampler->channels;
|
||||
|
||||
write_frame->rate = session->write_resampler->to_rate;
|
||||
|
||||
did_write_resample = 1;
|
||||
}
|
||||
switch_mutex_unlock(session->resample_mutex);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (session->bugs) {
|
||||
switch_media_bug_t *bp;
|
||||
int prune = 0;
|
||||
|
||||
switch_thread_rwlock_rdlock(session->bug_rwlock);
|
||||
for (bp = session->bugs; bp; bp = bp->next) {
|
||||
switch_bool_t ok = SWITCH_TRUE;
|
||||
|
||||
if (!bp->ready) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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 (switch_test_flag(bp, SMBF_WRITE_STREAM)) {
|
||||
switch_mutex_lock(bp->write_mutex);
|
||||
switch_buffer_write(bp->raw_write_buffer, write_frame->data, write_frame->datalen);
|
||||
switch_mutex_unlock(bp->write_mutex);
|
||||
|
||||
if (bp->callback) {
|
||||
ok = bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_WRITE);
|
||||
}
|
||||
}
|
||||
|
||||
if (switch_test_flag(bp, SMBF_WRITE_REPLACE)) {
|
||||
do_bugs = 0;
|
||||
if (bp->callback) {
|
||||
bp->write_replace_frame_in = write_frame;
|
||||
bp->write_replace_frame_out = write_frame;
|
||||
if ((ok = bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_WRITE_REPLACE)) == SWITCH_TRUE) {
|
||||
write_frame = bp->write_replace_frame_out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bp->stop_time && bp->stop_time <= switch_epoch_time_now(NULL)) {
|
||||
ok = SWITCH_FALSE;
|
||||
}
|
||||
|
||||
|
||||
if (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);
|
||||
}
|
||||
}
|
||||
|
||||
if (do_bugs) {
|
||||
do_write = TRUE;
|
||||
write_frame = frame;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (session->write_codec) {
|
||||
if (!ptime_mismatch && write_frame->codec && write_frame->codec->implementation &&
|
||||
write_frame->codec->implementation->decoded_bytes_per_packet == session->write_impl.decoded_bytes_per_packet) {
|
||||
perfect = TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (perfect) {
|
||||
|
||||
if (write_frame->datalen < session->write_impl.decoded_bytes_per_packet) {
|
||||
memset(write_frame->data, 255, session->write_impl.decoded_bytes_per_packet - write_frame->datalen);
|
||||
write_frame->datalen = session->write_impl.decoded_bytes_per_packet;
|
||||
}
|
||||
|
||||
enc_frame = write_frame;
|
||||
session->enc_write_frame.datalen = session->enc_write_frame.buflen;
|
||||
session->write_codec->cur_frame = frame;
|
||||
frame->codec->cur_frame = frame;
|
||||
switch_assert(enc_frame->datalen <= SWITCH_RECOMMENDED_BUFFER_SIZE);
|
||||
switch_assert(session->enc_read_frame.datalen <= SWITCH_RECOMMENDED_BUFFER_SIZE);
|
||||
status = switch_core_codec_encode(session->write_codec,
|
||||
frame->codec,
|
||||
enc_frame->data,
|
||||
enc_frame->datalen,
|
||||
session->write_impl.actual_samples_per_second,
|
||||
session->enc_write_frame.data, &session->enc_write_frame.datalen, &session->enc_write_frame.rate, &flag);
|
||||
|
||||
switch_assert(session->enc_read_frame.datalen <= SWITCH_RECOMMENDED_BUFFER_SIZE);
|
||||
|
||||
session->write_codec->cur_frame = NULL;
|
||||
frame->codec->cur_frame = NULL;
|
||||
switch (status) {
|
||||
case SWITCH_STATUS_RESAMPLE:
|
||||
resample++;
|
||||
/* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Fixme 2\n"); */
|
||||
case SWITCH_STATUS_SUCCESS:
|
||||
session->enc_write_frame.codec = session->write_codec;
|
||||
session->enc_write_frame.samples = enc_frame->datalen / sizeof(int16_t) / session->write_impl.number_of_channels;
|
||||
session->enc_write_frame.channels = session->write_impl.number_of_channels;
|
||||
if (frame->codec->implementation->samples_per_packet != session->write_impl.samples_per_packet) {
|
||||
session->enc_write_frame.timestamp = 0;
|
||||
} else {
|
||||
session->enc_write_frame.timestamp = frame->timestamp;
|
||||
}
|
||||
session->enc_write_frame.payload = session->write_impl.ianacode;
|
||||
session->enc_write_frame.m = frame->m;
|
||||
session->enc_write_frame.ssrc = frame->ssrc;
|
||||
session->enc_write_frame.seq = frame->seq;
|
||||
session->enc_write_frame.flags = 0;
|
||||
write_frame = &session->enc_write_frame;
|
||||
break;
|
||||
case SWITCH_STATUS_NOOP:
|
||||
enc_frame->codec = session->write_codec;
|
||||
enc_frame->samples = enc_frame->datalen / sizeof(int16_t) / session->write_impl.number_of_channels;
|
||||
enc_frame->channels = session->write_impl.number_of_channels;
|
||||
enc_frame->timestamp = frame->timestamp;
|
||||
enc_frame->m = frame->m;
|
||||
enc_frame->seq = frame->seq;
|
||||
enc_frame->ssrc = frame->ssrc;
|
||||
enc_frame->payload = enc_frame->codec->implementation->ianacode;
|
||||
write_frame = enc_frame;
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
break;
|
||||
case SWITCH_STATUS_NOT_INITALIZED:
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec init error!\n");
|
||||
write_frame = NULL;
|
||||
goto error;
|
||||
default:
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec %s encoder error!\n",
|
||||
session->read_codec->codec_interface->interface_name);
|
||||
write_frame = NULL;
|
||||
goto error;
|
||||
}
|
||||
if (flag & SFF_CNG) {
|
||||
switch_set_flag(write_frame, SFF_CNG);
|
||||
}
|
||||
|
||||
status = perform_write(session, write_frame, flags, stream_id);
|
||||
goto error;
|
||||
} else {
|
||||
if (!session->raw_write_buffer) {
|
||||
switch_size_t bytes_per_packet = session->write_impl.decoded_bytes_per_packet;
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
|
||||
"Engaging Write Buffer at %u bytes to accommodate %u->%u\n",
|
||||
(uint32_t) bytes_per_packet, write_frame->datalen, session->write_impl.decoded_bytes_per_packet);
|
||||
if ((status = switch_buffer_create_dynamic(&session->raw_write_buffer,
|
||||
bytes_per_packet * SWITCH_BUFFER_BLOCK_FRAMES,
|
||||
bytes_per_packet * SWITCH_BUFFER_START_FRAMES, 0)) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Write Buffer Failed!\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Need to retrain the recording data */
|
||||
switch_core_media_bug_flush_all(session);
|
||||
}
|
||||
|
||||
if (!(switch_buffer_write(session->raw_write_buffer, write_frame->data, write_frame->datalen))) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Write Buffer %u bytes Failed!\n", write_frame->datalen);
|
||||
status = SWITCH_STATUS_MEMERR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
|
||||
while (switch_buffer_inuse(session->raw_write_buffer) >= session->write_impl.decoded_bytes_per_packet) {
|
||||
int rate;
|
||||
|
||||
if (switch_channel_down(session->channel) || !session->raw_write_buffer) {
|
||||
goto error;
|
||||
}
|
||||
if ((session->raw_write_frame.datalen = (uint32_t)
|
||||
switch_buffer_read(session->raw_write_buffer, session->raw_write_frame.data, session->write_impl.decoded_bytes_per_packet)) == 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
enc_frame = &session->raw_write_frame;
|
||||
session->raw_write_frame.rate = session->write_impl.actual_samples_per_second;
|
||||
session->enc_write_frame.datalen = session->enc_write_frame.buflen;
|
||||
session->enc_write_frame.timestamp = 0;
|
||||
|
||||
|
||||
if (frame->codec && frame->codec->implementation && switch_core_codec_ready(frame->codec)) {
|
||||
rate = frame->codec->implementation->actual_samples_per_second;
|
||||
} else {
|
||||
rate = session->write_impl.actual_samples_per_second;
|
||||
}
|
||||
|
||||
session->write_codec->cur_frame = frame;
|
||||
frame->codec->cur_frame = frame;
|
||||
switch_assert(enc_frame->datalen <= SWITCH_RECOMMENDED_BUFFER_SIZE);
|
||||
switch_assert(session->enc_read_frame.datalen <= SWITCH_RECOMMENDED_BUFFER_SIZE);
|
||||
status = switch_core_codec_encode(session->write_codec,
|
||||
frame->codec,
|
||||
enc_frame->data,
|
||||
enc_frame->datalen,
|
||||
rate,
|
||||
session->enc_write_frame.data, &session->enc_write_frame.datalen, &session->enc_write_frame.rate, &flag);
|
||||
|
||||
switch_assert(session->enc_read_frame.datalen <= SWITCH_RECOMMENDED_BUFFER_SIZE);
|
||||
|
||||
session->write_codec->cur_frame = NULL;
|
||||
frame->codec->cur_frame = NULL;
|
||||
switch (status) {
|
||||
case SWITCH_STATUS_RESAMPLE:
|
||||
resample++;
|
||||
session->enc_write_frame.codec = session->write_codec;
|
||||
session->enc_write_frame.samples = enc_frame->datalen / sizeof(int16_t) / session->write_impl.number_of_channels;
|
||||
session->enc_write_frame.channels = session->write_impl.number_of_channels;
|
||||
session->enc_write_frame.m = frame->m;
|
||||
session->enc_write_frame.ssrc = frame->ssrc;
|
||||
session->enc_write_frame.payload = session->write_impl.ianacode;
|
||||
write_frame = &session->enc_write_frame;
|
||||
if (!session->write_resampler) {
|
||||
switch_mutex_lock(session->resample_mutex);
|
||||
if (!session->write_resampler) {
|
||||
status = switch_resample_create(&session->write_resampler,
|
||||
frame->codec->implementation->actual_samples_per_second,
|
||||
session->write_impl.actual_samples_per_second,
|
||||
session->write_impl.decoded_bytes_per_packet, SWITCH_RESAMPLE_QUALITY,
|
||||
session->write_impl.number_of_channels);
|
||||
}
|
||||
switch_mutex_unlock(session->resample_mutex);
|
||||
|
||||
|
||||
|
||||
if (status != SWITCH_STATUS_SUCCESS) {
|
||||
goto done;
|
||||
} else {
|
||||
switch_core_session_message_t msg = { 0 };
|
||||
msg.numeric_arg = 1;
|
||||
msg.message_id = SWITCH_MESSAGE_RESAMPLE_EVENT;
|
||||
switch_core_session_receive_message(session, &msg);
|
||||
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Activating write resampler\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SWITCH_STATUS_SUCCESS:
|
||||
session->enc_write_frame.codec = session->write_codec;
|
||||
session->enc_write_frame.samples = enc_frame->datalen / sizeof(int16_t) / session->write_impl.number_of_channels;
|
||||
session->enc_write_frame.channels = session->write_impl.number_of_channels;
|
||||
session->enc_write_frame.m = frame->m;
|
||||
session->enc_write_frame.ssrc = frame->ssrc;
|
||||
session->enc_write_frame.payload = session->write_impl.ianacode;
|
||||
session->enc_write_frame.flags = 0;
|
||||
write_frame = &session->enc_write_frame;
|
||||
break;
|
||||
case SWITCH_STATUS_NOOP:
|
||||
if (session->write_resampler) {
|
||||
switch_core_session_message_t msg = { 0 };
|
||||
int ok = 0;
|
||||
|
||||
switch_mutex_lock(session->resample_mutex);
|
||||
if (session->write_resampler) {
|
||||
switch_resample_destroy(&session->write_resampler);
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Deactivating write resampler\n");
|
||||
ok = 1;
|
||||
}
|
||||
switch_mutex_unlock(session->resample_mutex);
|
||||
|
||||
if (ok) {
|
||||
msg.numeric_arg = 0;
|
||||
msg.message_id = SWITCH_MESSAGE_RESAMPLE_EVENT;
|
||||
switch_core_session_receive_message(session, &msg);
|
||||
}
|
||||
|
||||
}
|
||||
enc_frame->codec = session->write_codec;
|
||||
enc_frame->samples = enc_frame->datalen / sizeof(int16_t) / session->read_impl.number_of_channels;
|
||||
enc_frame->channels = session->read_impl.number_of_channels;
|
||||
enc_frame->m = frame->m;
|
||||
enc_frame->ssrc = frame->ssrc;
|
||||
enc_frame->payload = enc_frame->codec->implementation->ianacode;
|
||||
write_frame = enc_frame;
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
break;
|
||||
case SWITCH_STATUS_NOT_INITALIZED:
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec init error!\n");
|
||||
write_frame = NULL;
|
||||
goto error;
|
||||
default:
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec %s encoder error %d!\n",
|
||||
session->read_codec->codec_interface->interface_name, status);
|
||||
write_frame = NULL;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!did_write_resample && session->read_resampler) {
|
||||
short *data = write_frame->data;
|
||||
switch_mutex_lock(session->resample_mutex);
|
||||
if (session->read_resampler) {
|
||||
switch_resample_process(session->read_resampler, data, write_frame->datalen / 2 / session->read_resampler->channels);
|
||||
memcpy(data, session->read_resampler->to, session->read_resampler->to_len * 2 * session->read_resampler->channels);
|
||||
write_frame->samples = session->read_resampler->to_len;
|
||||
write_frame->channels = session->read_resampler->channels;
|
||||
write_frame->datalen = session->read_resampler->to_len * 2 * session->read_resampler->channels;
|
||||
write_frame->rate = session->read_resampler->to_rate;
|
||||
}
|
||||
switch_mutex_unlock(session->resample_mutex);
|
||||
|
||||
}
|
||||
|
||||
if (flag & SFF_CNG) {
|
||||
switch_set_flag(write_frame, SFF_CNG);
|
||||
}
|
||||
|
||||
if (ptime_mismatch || resample) {
|
||||
write_frame->timestamp = 0;
|
||||
}
|
||||
|
||||
if ((status = perform_write(session, write_frame, flags, stream_id)) != SWITCH_STATUS_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
done:
|
||||
|
||||
if (ptime_mismatch || resample) {
|
||||
write_frame->timestamp = 0;
|
||||
}
|
||||
|
||||
if (do_write) {
|
||||
status = perform_write(session, write_frame, flags, stream_id);
|
||||
}
|
||||
|
||||
error:
|
||||
|
||||
switch_mutex_unlock(session->write_codec->mutex);
|
||||
switch_mutex_unlock(frame->codec->mutex);
|
||||
switch_mutex_unlock(session->codec_write_mutex);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static char *SIG_NAMES[] = {
|
||||
"NONE",
|
||||
"KILL",
|
||||
|
Reference in New Issue
Block a user