SWITCH_DECLARE(switch_status_t) switch_core_media_bug_transfer_recordings(switch_core_session_t *orig_session, switch_core_session_t *new_session)
{
	switch_media_bug_t *bp;
	char *list[100] = { 0 };
	int stop_times[100] = { 0 };
	int i = 0, x = 0;

	if (orig_session->bugs) {
		switch_channel_t *new_channel = switch_core_session_get_channel(new_session);
		switch_channel_t *orig_channel = switch_core_session_get_channel(orig_session);
		const char *save_append = switch_channel_get_variable(new_channel, "record_append");
		const char *save_stereo = switch_channel_get_variable(new_channel, "record_stereo");
		const char *orig_stereo = switch_channel_get_variable(orig_channel, "record_stereo");
		const char *new_stereo = orig_stereo;
		
		switch_thread_rwlock_wrlock(orig_session->bug_rwlock);
		switch_channel_set_variable(new_channel, "RECORD_MIN_SEC", "0");
		switch_channel_set_variable(new_channel, "record_append", "true");
		switch_channel_set_variable(new_channel, "record_stereo", new_stereo);

		for (bp = orig_session->bugs; bp; bp = bp->next) {
			if (!strcmp(bp->function, "session_record")) {
				list[x] = switch_core_session_strdup(new_session, bp->target);
				if (bp->stop_time > 0) {
					stop_times[x] = (int)(bp->stop_time - switch_epoch_time_now(NULL));
				}
				x++;
			}
		}

		switch_thread_rwlock_unlock(orig_session->bug_rwlock);

		for(i = 0; i < x; i++) {
			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(orig_session), SWITCH_LOG_DEBUG, "Transfering %s from %s to %s\n", list[i],
							  switch_core_session_get_name(orig_session), switch_core_session_get_name(new_session));
			switch_ivr_stop_record_session(orig_session, list[i]);
			switch_ivr_record_session(new_session, list[i], stop_times[i], NULL);
		}

		switch_channel_set_variable(new_channel, "record_append", save_append);
		switch_channel_set_variable(new_channel, "record_stereo", save_stereo);

	}

	return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
}
/**
 * Start recording call
 * @param session the session to record
 * @param record the record component
 */
static int start_call_record(switch_core_session_t *session, struct rayo_component *component)
{
	struct record_component *record_component = RECORD_COMPONENT(component);
	switch_channel_t *channel = switch_core_session_get_channel(session);
	int max_duration_sec = 0;

	switch_channel_set_variable(channel, "RECORD_HANGUP_ON_ERROR", "false");
	switch_channel_set_variable(channel, "RECORD_TOGGLE_ON_REPEAT", "");
	switch_channel_set_variable(channel, "RECORD_CHECK_BRIDGE", "");
	switch_channel_set_variable(channel, "RECORD_MIN_SEC", "0");
	switch_channel_set_variable(channel, "RECORD_STEREO", "");
	switch_channel_set_variable(channel, "RECORD_READ_ONLY", "");
	switch_channel_set_variable(channel, "RECORD_WRITE_ONLY", "");
	switch_channel_set_variable(channel, "RECORD_APPEND", "");
	switch_channel_set_variable(channel, "RECORD_WRITE_OVER", "true");
	switch_channel_set_variable(channel, "RECORD_ANSWER_REQ", "");
	switch_channel_set_variable(channel, "RECORD_SILENCE_THRESHOLD", "200");
	if (record_component->initial_timeout > 0) {
		switch_channel_set_variable_printf(channel, "RECORD_INITIAL_TIMEOUT_MS", "%i", record_component->initial_timeout);
	} else {
		switch_channel_set_variable(channel, "RECORD_INITIAL_TIMEOUT_MS", "");
	}
	if (record_component->final_timeout > 0) {
		switch_channel_set_variable_printf(channel, "RECORD_FINAL_TIMEOUT_MS", "%i", record_component->final_timeout);
	} else {
		switch_channel_set_variable(channel, "RECORD_FINAL_TIMEOUT_MS", "");
	}
	/* allow dialplan override for these variables */
	//switch_channel_set_variable(channel, "RECORD_PRE_BUFFER_FRAMES", "");
	//switch_channel_set_variable(channel, "record_sample_rate", "");
	//switch_channel_set_variable(channel, "enable_file_write_buffering", "");

	/* max duration attribute is in milliseconds- convert to seconds */
	if (record_component->max_duration > 0) {
		max_duration_sec = ceil((double)(record_component->max_duration - record_component->duration_ms) / 1000.0);
	}

	if (!strcmp(record_component->direction, "duplex")) {
		if (!record_component->mix) {
			/* STEREO */
			switch_channel_set_variable(channel, "RECORD_STEREO", "true");
		} /* else MONO (default) */
	} else if (!strcmp(record_component->direction, "send")) {
		/* record audio sent from the caller */
		switch_channel_set_variable(channel, "RECORD_READ_ONLY", "true");
	} else if (!strcmp(record_component->direction, "recv")) {
		/* record audio received by the caller */
		switch_channel_set_variable(channel, "RECORD_WRITE_ONLY", "true");
	};

	if (record_component->start_beep) {
		switch_ivr_displace_session(session, RECORD_BEEP, 0, "");
		record_component->start_time = switch_micro_time_now();
	}

	if (switch_ivr_record_session(session, (char *)RAYO_ID(component), max_duration_sec, NULL) == SWITCH_STATUS_SUCCESS) {
		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Recording started: file = %s\n", RAYO_ID(component));
		return 1;
	}

	return 0;
}