mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-07-24 05:02:10 +00:00
add append and truncate to audio file api
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15503 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
@@ -57,7 +57,13 @@ static switch_status_t native_file_file_open(switch_file_handle_t *handle, const
|
||||
}
|
||||
|
||||
if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
|
||||
flags |= SWITCH_FOPEN_WRITE | SWITCH_FOPEN_CREATE | SWITCH_FOPEN_TRUNCATE;
|
||||
flags |= SWITCH_FOPEN_WRITE | SWITCH_FOPEN_CREATE;
|
||||
if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) {
|
||||
flags |= SWITCH_FOPEN_READ;
|
||||
} else {
|
||||
flags |= SWITCH_FOPEN_TRUNCATE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (switch_test_flag(handle, SWITCH_FILE_FLAG_READ)) {
|
||||
@@ -69,6 +75,12 @@ static switch_status_t native_file_file_open(switch_file_handle_t *handle, const
|
||||
return SWITCH_STATUS_GENERR;
|
||||
}
|
||||
|
||||
if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) {
|
||||
int64_t samples = 0;
|
||||
switch_file_seek(context->fd, SEEK_END, &samples);
|
||||
handle->pos = samples;
|
||||
}
|
||||
|
||||
handle->samples = 0;
|
||||
handle->samplerate = 8000;
|
||||
handle->channels = 1;
|
||||
@@ -84,6 +96,19 @@ static switch_status_t native_file_file_open(switch_file_handle_t *handle, const
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static switch_status_t native_file_file_truncate(switch_file_handle_t *handle, int64_t offset)
|
||||
{
|
||||
native_file_context *context = handle->private_info;
|
||||
switch_status_t status;
|
||||
|
||||
if ((status = switch_file_trunc(context->fd, offset)) == SWITCH_STATUS_SUCCESS) {
|
||||
handle->pos = 0;
|
||||
}
|
||||
|
||||
return status;
|
||||
|
||||
}
|
||||
|
||||
static switch_status_t native_file_file_close(switch_file_handle_t *handle)
|
||||
{
|
||||
native_file_context *context = handle->private_info;
|
||||
@@ -161,6 +186,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_native_file_load)
|
||||
file_interface->extens = supported_formats;
|
||||
file_interface->file_open = native_file_file_open;
|
||||
file_interface->file_close = native_file_file_close;
|
||||
file_interface->file_truncate = native_file_file_truncate;
|
||||
file_interface->file_read = native_file_file_read;
|
||||
file_interface->file_write = native_file_file_write;
|
||||
file_interface->file_seek = native_file_file_seek;
|
||||
|
||||
@@ -626,6 +626,9 @@ static switch_status_t shout_file_open(switch_file_handle_t *handle, const char
|
||||
|
||||
}
|
||||
} else if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
|
||||
if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Appending to MP3 not supported.\n");
|
||||
}
|
||||
if (!(context->gfp = lame_init())) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not allocate lame\n");
|
||||
goto error;
|
||||
|
||||
@@ -83,7 +83,11 @@ static switch_status_t sndfile_file_open(switch_file_handle_t *handle, const cha
|
||||
}
|
||||
|
||||
if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
|
||||
mode += SFM_WRITE;
|
||||
if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) {
|
||||
mode += SFM_RDWR;
|
||||
} else {
|
||||
mode += SFM_WRITE;
|
||||
}
|
||||
}
|
||||
|
||||
if (!mode) {
|
||||
@@ -98,8 +102,6 @@ static switch_status_t sndfile_file_open(switch_file_handle_t *handle, const cha
|
||||
map = switch_core_hash_find(globals.format_hash, ext);
|
||||
|
||||
if (mode & SFM_WRITE) {
|
||||
sf_count_t frames = 0;
|
||||
|
||||
context->sfinfo.channels = handle->channels;
|
||||
context->sfinfo.samplerate = handle->samplerate;
|
||||
if (handle->samplerate == 8000 || handle->samplerate == 16000 ||
|
||||
@@ -107,8 +109,6 @@ static switch_status_t sndfile_file_open(switch_file_handle_t *handle, const cha
|
||||
handle->samplerate == 11025 || handle->samplerate == 22050 || handle->samplerate == 44100) {
|
||||
context->sfinfo.format |= SF_FORMAT_PCM_16;
|
||||
}
|
||||
|
||||
sf_command(context->handle, SFC_FILE_TRUNCATE, &frames, sizeof(frames));
|
||||
}
|
||||
|
||||
if (map) {
|
||||
@@ -206,6 +206,14 @@ static switch_status_t sndfile_file_open(switch_file_handle_t *handle, const cha
|
||||
handle->speed = 0;
|
||||
handle->private_info = context;
|
||||
|
||||
if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) {
|
||||
handle->pos = sf_seek(context->handle, 0, SEEK_END);
|
||||
} else {
|
||||
sf_count_t frames = 0;
|
||||
sf_command(context->handle, SFC_FILE_TRUNCATE, &frames, sizeof(frames));
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
|
||||
switch_safe_free(alt_path);
|
||||
@@ -214,6 +222,14 @@ static switch_status_t sndfile_file_open(switch_file_handle_t *handle, const cha
|
||||
return status;
|
||||
}
|
||||
|
||||
static switch_status_t sndfile_file_truncate(switch_file_handle_t *handle, int64_t offset)
|
||||
{
|
||||
sndfile_context *context = handle->private_info;
|
||||
sf_command(context->handle, SFC_FILE_TRUNCATE, &offset, sizeof(offset));
|
||||
handle->pos = 0;
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static switch_status_t sndfile_file_close(switch_file_handle_t *handle)
|
||||
{
|
||||
sndfile_context *context = handle->private_info;
|
||||
@@ -416,6 +432,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sndfile_load)
|
||||
file_interface->extens = supported_formats;
|
||||
file_interface->file_open = sndfile_file_open;
|
||||
file_interface->file_close = sndfile_file_close;
|
||||
file_interface->file_truncate = sndfile_file_truncate;
|
||||
file_interface->file_read = sndfile_file_read;
|
||||
file_interface->file_write = sndfile_file_write;
|
||||
file_interface->file_seek = sndfile_file_seek;
|
||||
|
||||
@@ -1247,6 +1247,8 @@ static switch_status_t js_stream_input_callback(switch_core_session_t *session,
|
||||
switch_set_flag(fh, SWITCH_FILE_PAUSE);
|
||||
}
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
} else if (!strcasecmp(ret, "truncate")) {
|
||||
switch_core_file_truncate(fh, 0);
|
||||
} else if (!strcasecmp(ret, "restart")) {
|
||||
uint32_t pos = 0;
|
||||
fh->speed = 0;
|
||||
|
||||
Reference in New Issue
Block a user