From da4ae7be4cfc6f97116ca5bf1ef34f5df7ca0839 Mon Sep 17 00:00:00 2001 From: Anthony Minessale <anthony.minessale@gmail.com> Date: Tue, 25 Jul 2006 22:37:52 +0000 Subject: [PATCH] add metadata functions to sound file api git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2139 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_core.h | 19 +++++ src/include/switch_module_interfaces.h | 4 ++ src/include/switch_types.h | 10 +++ src/mod/formats/mod_sndfile/mod_sndfile.c | 22 ++++++ src/switch_core.c | 17 +++++ src/switch_ivr.c | 87 ++++++++++++++++++++++- 6 files changed, 157 insertions(+), 2 deletions(-) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 4183bde96c..4fec365024 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -866,6 +866,25 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_write(switch_file_handle_t *fh, */ SWITCH_DECLARE(switch_status_t) switch_core_file_seek(switch_file_handle_t *fh, unsigned int *cur_pos, int64_t samples, int whence); +/*! + \brief Set metadata to the desired string + \param fh the file handle to set data to + \param col the enum of the col name + \param string the string to add + \return SWITCH_STATUS_SUCCESS with cur_pos adjusted to new position +*/ +SWITCH_DECLARE(switch_status_t) switch_core_file_set_string(switch_file_handle_t *fh, switch_audio_col_t col, const char *string); + +/*! + \brief get metadata of the desired string + \param fh the file handle to get data from + \param col the enum of the col name + \param string pointer to the string to fetch + \return SWITCH_STATUS_SUCCESS with cur_pos adjusted to new position +*/ +SWITCH_DECLARE(switch_status_t) switch_core_file_get_string(switch_file_handle_t *fh, switch_audio_col_t col, const char **string); + + /*! \brief Close an open file handle \param fh the file handle to close diff --git a/src/include/switch_module_interfaces.h b/src/include/switch_module_interfaces.h index 10f88e45c8..69a672f749 100644 --- a/src/include/switch_module_interfaces.h +++ b/src/include/switch_module_interfaces.h @@ -266,6 +266,10 @@ struct switch_file_interface { switch_status_t (*file_write)(switch_file_handle_t *, void *data, switch_size_t *len); /*! function to seek to a certian position in the file */ switch_status_t (*file_seek)(switch_file_handle_t *, unsigned int *cur_pos, int64_t samples, int whence); + /*! function to set meta data */ + switch_status_t (*file_set_string)(switch_file_handle_t *fh, switch_audio_col_t col, const char *string); + /*! function to get meta data */ + switch_status_t (*file_get_string)(switch_file_handle_t *fh, switch_audio_col_t col, const char **string); /*! list of supported file extensions */ char **extens; const struct switch_file_interface *next; diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 727bf4e9d8..864519ce56 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -94,6 +94,16 @@ SWITCH_DECLARE_DATA extern switch_directories SWITCH_GLOBAL_dirs; #define SWITCH_FALSE 0 #define SWITCH_CORE_QUEUE_LEN 100000 + +typedef enum { + SWITCH_AUDIO_COL_STR_TITLE = 0x01, + SWITCH_AUDIO_COL_STR_COPYRIGHT = 0x02, + SWITCH_AUDIO_COL_STR_SOFTWARE = 0x03, + SWITCH_AUDIO_COL_STR_ARTIST = 0x04, + SWITCH_AUDIO_COL_STR_COMMENT = 0x05, + SWITCH_AUDIO_COL_STR_DATE = 0x06 +} switch_audio_col_t; + typedef enum { SWITCH_XML_SECTION_RESULT = 0, SWITCH_XML_SECTION_CONFIG = (1 << 0), diff --git a/src/mod/formats/mod_sndfile/mod_sndfile.c b/src/mod/formats/mod_sndfile/mod_sndfile.c index 9096cbd60a..9b579205a4 100644 --- a/src/mod/formats/mod_sndfile/mod_sndfile.c +++ b/src/mod/formats/mod_sndfile/mod_sndfile.c @@ -213,6 +213,26 @@ static switch_status_t sndfile_file_write(switch_file_handle_t *handle, void *da return SWITCH_STATUS_SUCCESS; } +static switch_status_t sndfile_file_set_string(switch_file_handle_t *handle, switch_audio_col_t col, const char *string) +{ + sndfile_context *context = handle->private_info; + + return sf_set_string(context->handle, (int)col, string) ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS; +} + +static switch_status_t sndfile_file_get_string(switch_file_handle_t *handle, switch_audio_col_t col, const char **string) +{ + sndfile_context *context = handle->private_info; + const char *s; + + if ((s = sf_get_string(context->handle, (int)col))) { + *string = s; + return SWITCH_STATUS_SUCCESS; + } + + return SWITCH_STATUS_FALSE; +} + /* Registration */ static char **supported_formats; @@ -224,6 +244,8 @@ static switch_file_interface_t sndfile_file_interface = { /*.file_read */ sndfile_file_read, /*.file_write */ sndfile_file_write, /*.file_seek */ sndfile_file_seek, + /*.file_set_string */ sndfile_file_set_string, + /*.file_get_string */ sndfile_file_get_string, /*.extens */ NULL, /*.next */ NULL, }; diff --git a/src/switch_core.c b/src/switch_core.c index 886a4af344..894e66ee70 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -543,9 +543,26 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_write(switch_file_handle_t *fh, SWITCH_DECLARE(switch_status_t) switch_core_file_seek(switch_file_handle_t *fh, unsigned int *cur_pos, int64_t samples, int whence) { + assert(fh != NULL); return fh->file_interface->file_seek(fh, cur_pos, samples, whence); } +SWITCH_DECLARE(switch_status_t) switch_core_file_set_string(switch_file_handle_t *fh, switch_audio_col_t col, const char *string) +{ + assert(fh != NULL); + + return fh->file_interface->file_set_string(fh, col, string); +} + +SWITCH_DECLARE(switch_status_t) switch_core_file_get_string(switch_file_handle_t *fh, switch_audio_col_t col, const char **string) +{ + assert(fh != NULL); + + return fh->file_interface->file_get_string(fh, col, string); + +} + + SWITCH_DECLARE(switch_status_t) switch_core_file_close(switch_file_handle_t *fh) { return fh->file_interface->file_close(fh); diff --git a/src/switch_ivr.c b/src/switch_ivr.c index a2fec65758..142d55af66 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -214,6 +214,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se switch_codec_t codec, *read_codec; char *codec_name; switch_status_t status = SWITCH_STATUS_SUCCESS; + char *p; + const char *vval; if (!fh) { fh = &lfh; @@ -241,6 +243,41 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se switch_channel_answer(channel); + if ((p = switch_channel_get_variable(channel, "RECORD_TITLE"))) { + vval = (const char *) switch_core_session_strdup(session, p); + switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_TITLE, vval); + switch_channel_set_variable(channel, "RECORD_TITLE", NULL); + } + + if ((p = switch_channel_get_variable(channel, "RECORD_COPYRIGHT"))) { + vval = (const char *) switch_core_session_strdup(session, p); + switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_COPYRIGHT, vval); + switch_channel_set_variable(channel, "RECORD_COPYRIGHT", NULL); + } + + if ((p = switch_channel_get_variable(channel, "RECORD_SOFTWARE"))) { + vval = (const char *) switch_core_session_strdup(session, p); + switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_SOFTWARE, vval); + switch_channel_set_variable(channel, "RECORD_SOFTWARE", NULL); + } + + if ((p = switch_channel_get_variable(channel, "RECORD_ARTIST"))) { + vval = (const char *) switch_core_session_strdup(session, p); + switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_ARTIST, vval); + switch_channel_set_variable(channel, "RECORD_ARTIST", NULL); + } + + if ((p = switch_channel_get_variable(channel, "RECORD_COMMENT"))) { + vval = (const char *) switch_core_session_strdup(session, p); + switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_COMMENT, vval); + switch_channel_set_variable(channel, "RECORD_COMMENT", NULL); + } + + if ((p = switch_channel_get_variable(channel, "RECORD_DATE"))) { + vval = (const char *) switch_core_session_strdup(session, p); + switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_DATE, vval); + switch_channel_set_variable(channel, "RECORD_DATE", NULL); + } codec_name = "L16"; if (switch_core_codec_init(&codec, @@ -333,7 +370,9 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess switch_status_t status = SWITCH_STATUS_SUCCESS; switch_file_handle_t lfh; switch_codec_t *read_codec = switch_core_session_get_read_codec(session); - + const char *p; + char *title = "", *copyright = "", *software = "", *artist = "", *comment = "", *date = ""; + if (!fh) { fh = &lfh; memset(fh, 0, sizeof(lfh)); @@ -354,8 +393,52 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess write_frame.data = abuf; write_frame.buflen = sizeof(abuf); + + if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_TITLE, &p) == SWITCH_STATUS_SUCCESS) { + title = (char *) switch_core_session_strdup(session, (char *)p); + switch_channel_set_variable(channel, "RECORD_TITLE", (char *)p); + } + + if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_COPYRIGHT, &p) == SWITCH_STATUS_SUCCESS) { + copyright = (char *) switch_core_session_strdup(session, (char *)p); + switch_channel_set_variable(channel, "RECORD_COPYRIGHT", (char *)p); + } + + if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_SOFTWARE, &p) == SWITCH_STATUS_SUCCESS) { + software = (char *) switch_core_session_strdup(session, (char *)p); + switch_channel_set_variable(channel, "RECORD_SOFTWARE", (char *)p); + } + + if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_ARTIST, &p) == SWITCH_STATUS_SUCCESS) { + artist = (char *) switch_core_session_strdup(session, (char *)p); + switch_channel_set_variable(channel, "RECORD_ARTIST", (char *)p); + } + + if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_COMMENT, &p) == SWITCH_STATUS_SUCCESS) { + comment = (char *) switch_core_session_strdup(session, (char *)p); + switch_channel_set_variable(channel, "RECORD_COMMENT", (char *)p); + } + + if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_DATE, &p) == SWITCH_STATUS_SUCCESS) { + date = (char *) switch_core_session_strdup(session, (char *)p); + switch_channel_set_variable(channel, "RECORD_DATE", (char *)p); + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "OPEN FILE %s %uhz %u channels\n" + "TITLE=%s\n" + "COPYRIGHT=%s\n" + "SOFTWARE=%s\n" + "ARTIST=%s\n" + "COMMENT=%s\n" + "DATE=%s\n", file, fh->samplerate, fh->channels, + title, + copyright, + software, + artist, + comment, + date); - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "OPEN FILE %s %uhz %u channels\n", file, fh->samplerate, fh->channels); interval = read_codec->implementation->microseconds_per_frame / 1000;