/**
 * Transforms Rayo document into sub-format and opens file_string.
 * @param handle
 * @param path the inline Rayo document
 * @return SWITCH_STATUS_SUCCESS if opened
 */
static switch_status_t rayo_file_open(switch_file_handle_t *handle, const char *path)
{
	switch_status_t status = SWITCH_STATUS_FALSE;
	struct rayo_file_context *context = switch_core_alloc(handle->memory_pool, sizeof(*context));

	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Got path %s\n", path);

	context->component = RAYO_COMPONENT_LOCATE(path);

	if (context->component) {
		handle->private_info = context;
		context->cur_doc = NULL;
		context->play_count = 0;
		context->could_open = 0;
		status = next_file(handle);
	} else {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "File error! %s\n", path);
		return SWITCH_STATUS_FALSE;
	}

	if (status != SWITCH_STATUS_SUCCESS && context->component) {
		/* complete error event will be sent by calling thread */
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Status = %i\n", status);
		RAYO_RELEASE(context->component);
	}

	return status;
}
/**
 * Handle RECORD_STOP event from FreeSWITCH.
 * @param event received from FreeSWITCH core.  It will be destroyed by the core after this function returns.
 */
static void on_call_record_stop_event(switch_event_t *event)
{
	const char *file_path = switch_event_get_header(event, "Record-File-Path");
	struct rayo_component *component = RAYO_COMPONENT_LOCATE(file_path);

	if (component) {
		RECORD_COMPONENT(component)->duration_ms += (switch_micro_time_now() - RECORD_COMPONENT(component)->start_time) / 1000;
		if (RECORD_COMPONENT(component)->stop) {
			complete_record(component, COMPONENT_COMPLETE_STOP);
		} else {
			/* TODO assume final timeout, for now */
			complete_record(component, RECORD_COMPLETE_FINAL_TIMEOUT);
		}
	}
}
/**
 * Handle conference events from FreeSWITCH.
 * @param event received from FreeSWITCH core.  It will be destroyed by the core after this function returns.
 */
static void on_mixer_record_event(switch_event_t *event)
{
	const char *file_path = switch_event_get_header(event, "Path");
	const char *action = switch_event_get_header(event, "Action");
	struct rayo_component *component = RAYO_COMPONENT_LOCATE(file_path);

	if (component) {
		struct record_component *record = RECORD_COMPONENT(component);
		if (!strcmp("stop-recording", action)) {
			record->duration_ms += (switch_micro_time_now() - record->start_time) / 1000;
			if (record->stop) {
				complete_record(component, COMPONENT_COMPLETE_STOP);
			} else {
				/* TODO assume final timeout, for now */
				complete_record(component, RECORD_COMPLETE_FINAL_TIMEOUT);
			}
		}
	}
}