提交 e6a60a20 authored 作者: Anthony Minessale's avatar Anthony Minessale

spring cleaning 2

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@4796 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 3a541262
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
*
*
* switch_core.h -- Core Library Private Data (not to be installed into the system)
* If the last line didn't make sense, stop reading this file, go away!,
* this file does not exist!!!!
*
*/
#ifndef WIN32
#include <switch_private.h>
#endif
/* for apr_pool_create and apr_pool_destroy */
/* functions only used in this file so not exposed */
#include <apr_pools.h>
/* for apr_hash_make, apr_hash_pool_get, apr_hash_set */
/* functions only used in this file so not exposed */
#include <apr_hash.h>
/* for apr_pvsprintf */
/* function only used in this file so not exposed */
#include <apr_strings.h>
/* for apr_initialize and apr_terminate */
/* function only used in this file so not exposed */
#include <apr_general.h>
#include <apr_portable.h>
typedef apr_os_thread_t switch_thread_id_t;
#define switch_thread_self apr_os_thread_current
#ifdef HAVE_MLOCKALL
#include <sys/mman.h>
#endif
/* #define DEBUG_ALLOC */
#define DO_EVENTS
#ifdef CRASH_PROT
#define __CP "ENABLED"
#else
#define __CP "DISABLED"
#endif
#define SWITCH_EVENT_QUEUE_LEN 256
#define SWITCH_MESSAGE_QUEUE_LEN 256
#define SWITCH_SQL_QUEUE_LEN 2000
#define SWITCH_BUFFER_BLOCK_FRAMES 25
#define SWITCH_BUFFER_START_FRAMES 50
typedef enum {
SSF_NONE = 0,
SSF_DESTROYED = (1 << 0)
} switch_session_flag_t;
struct switch_core_session {
uint32_t id;
char name[80];
switch_session_flag_t flags;
int thread_running;
switch_memory_pool_t *pool;
switch_channel_t *channel;
switch_thread_t *thread;
const switch_endpoint_interface_t *endpoint_interface;
switch_io_event_hooks_t event_hooks;
switch_codec_t *read_codec;
switch_codec_t *write_codec;
switch_buffer_t *raw_write_buffer;
switch_frame_t raw_write_frame;
switch_frame_t enc_write_frame;
uint8_t raw_write_buf[SWITCH_RECOMMENDED_BUFFER_SIZE];
uint8_t enc_write_buf[SWITCH_RECOMMENDED_BUFFER_SIZE];
switch_buffer_t *raw_read_buffer;
switch_frame_t raw_read_frame;
switch_frame_t enc_read_frame;
uint8_t raw_read_buf[SWITCH_RECOMMENDED_BUFFER_SIZE];
uint8_t enc_read_buf[SWITCH_RECOMMENDED_BUFFER_SIZE];
switch_audio_resampler_t *read_resampler;
switch_audio_resampler_t *write_resampler;
switch_mutex_t *mutex;
switch_thread_cond_t *cond;
switch_thread_rwlock_t *rwlock;
void *streams[SWITCH_MAX_STREAMS];
int stream_count;
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
void *private_info;
switch_queue_t *event_queue;
switch_queue_t *message_queue;
switch_queue_t *private_event_queue;
switch_thread_rwlock_t *bug_rwlock;
switch_media_bug_t *bugs;
};
struct switch_media_bug {
switch_buffer_t *raw_write_buffer;
switch_buffer_t *raw_read_buffer;
switch_frame_t *replace_frame_in;
switch_frame_t *replace_frame_out;
switch_media_bug_callback_t callback;
switch_mutex_t *read_mutex;
switch_mutex_t *write_mutex;
switch_core_session_t *session;
void *user_data;
uint32_t flags;
uint8_t ready;
struct switch_media_bug *next;
};
SWITCH_DECLARE(void) switch_core_sqldb_start(switch_memory_pool_t *pool);
SWITCH_DECLARE(void) switch_core_sqldb_stop(void);
SWITCH_DECLARE(void) switch_core_session_init(switch_memory_pool_t *pool);
SWITCH_DECLARE(switch_memory_pool_t *) switch_core_memory_init(void);
差异被折叠。
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
*
*
* switch_scheduler.h -- Scheduler Engine
*
*/
#ifndef SWITCH_SCHEDULER_H
#define SWITCH_SCHEDULER_H
#include <switch.h>
SWITCH_BEGIN_EXTERN_C
///\defgroup sched1 Scheduler
///\ingroup core1
///\{
struct switch_scheduler_task {
time_t created;
time_t runtime;
uint32_t cmd_id;
char *group;
void *cmd_arg;
uint32_t task_id;
};
/*!
\brief Schedule a task in the future
\param runtime the time in epoch seconds to execute the task.
\param func the callback function to execute when the task is executed.
\param desc an arbitrary description of the task.
\param group a group id tag to link multiple tasks to a single entity.
\param cmd_id an arbitrary index number be used in the callback.
\param cmd_arg user data to be passed to the callback.
\param flags flags to alter behaviour
\return the id of the task
*/
SWITCH_DECLARE(uint32_t) switch_scheduler_add_task(time_t task_runtime,
switch_scheduler_func_t func,
char *desc,
char *group,
uint32_t cmd_id, void *cmd_arg, switch_scheduler_flag_t flags);
/*!
\brief Delete a scheduled task
\param task_id the id of the task
\return SWITCH_STATUS_SUCCESS if the task was deleted.
*/
SWITCH_DECLARE(switch_status_t) switch_scheduler_del_task_id(uint32_t task_id);
/*!
\brief Delete a scheduled task based on the group name
\param group the group name
\return SWITCH_STATUS_SUCCESS if any tasks were deleted
*/
SWITCH_DECLARE(switch_status_t) switch_scheduler_del_task_group(char *group);
/*!
\brief Start the scheduler system
*/
SWITCH_DECLARE(void) switch_scheduler_task_thread_start(void);
/*!
\brief Stop the scheduler system
*/
SWITCH_DECLARE(void) switch_scheduler_task_thread_stop(void);
///\}
SWITCH_END_EXTERN_C
#endif
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
*/
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_asr.c -- Main Core Library (Speech Detection Interface)
*
*/
#include <switch.h>
#include "private/switch_core.h"
SWITCH_DECLARE(switch_status_t) switch_core_asr_open(switch_asr_handle_t *ah,
char *module_name,
char *codec,
int rate,
char *dest, switch_asr_flag_t *flags, switch_memory_pool_t *pool)
{
switch_status_t status;
assert(ah != NULL);
if ((ah->asr_interface = switch_loadable_module_get_asr_interface(module_name)) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "invalid asr module [%s]!\n", module_name);
return SWITCH_STATUS_GENERR;
}
ah->flags = *flags;
if (pool) {
ah->memory_pool = pool;
} else {
if ((status = switch_core_new_memory_pool(&ah->memory_pool)) != SWITCH_STATUS_SUCCESS) {
return status;
}
switch_set_flag(ah, SWITCH_ASR_FLAG_FREE_POOL);
}
ah->rate = rate;
ah->name = switch_core_strdup(ah->memory_pool, module_name);
return ah->asr_interface->asr_open(ah, codec, rate, dest, flags);
}
SWITCH_DECLARE(switch_status_t) switch_core_asr_load_grammar(switch_asr_handle_t *ah, char *grammar, char *path)
{
char *epath = NULL;
switch_status_t status;
assert(ah != NULL);
if (*path != '/') {
epath = switch_mprintf("%s%s%s", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, path);
path = epath;
}
status = ah->asr_interface->asr_load_grammar(ah, grammar, path);
switch_safe_free(epath);
return status;
}
SWITCH_DECLARE(switch_status_t) switch_core_asr_unload_grammar(switch_asr_handle_t *ah, char *grammar)
{
switch_status_t status;
assert(ah != NULL);
status = ah->asr_interface->asr_unload_grammar(ah, grammar);
return status;
}
SWITCH_DECLARE(switch_status_t) switch_core_asr_pause(switch_asr_handle_t *ah)
{
assert(ah != NULL);
return ah->asr_interface->asr_pause(ah);
}
SWITCH_DECLARE(switch_status_t) switch_core_asr_resume(switch_asr_handle_t *ah)
{
assert(ah != NULL);
return ah->asr_interface->asr_resume(ah);
}
SWITCH_DECLARE(switch_status_t) switch_core_asr_close(switch_asr_handle_t *ah, switch_asr_flag_t *flags)
{
assert(ah != NULL);
return ah->asr_interface->asr_close(ah, flags);
}
SWITCH_DECLARE(switch_status_t) switch_core_asr_feed(switch_asr_handle_t *ah, void *data, unsigned int len,
switch_asr_flag_t *flags)
{
assert(ah != NULL);
return ah->asr_interface->asr_feed(ah, data, len, flags);
}
SWITCH_DECLARE(switch_status_t) switch_core_asr_check_results(switch_asr_handle_t *ah, switch_asr_flag_t *flags)
{
assert(ah != NULL);
return ah->asr_interface->asr_check_results(ah, flags);
}
SWITCH_DECLARE(switch_status_t) switch_core_asr_get_results(switch_asr_handle_t *ah, char **xmlstr,
switch_asr_flag_t *flags)
{
assert(ah != NULL);
return ah->asr_interface->asr_get_results(ah, xmlstr, flags);
}
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_codec.c -- Main Core Library (codec functions)
*
*/
#include <switch.h>
#include "private/switch_core.h"
SWITCH_DECLARE(switch_status_t) switch_core_session_set_read_codec(switch_core_session_t *session,
switch_codec_t *codec)
{
switch_event_t *event;
assert(session != NULL);
if (switch_event_create(&event, SWITCH_EVENT_CODEC) == SWITCH_STATUS_SUCCESS) {
switch_channel_event_set_data(session->channel, event);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "channel-read-codec-name", "%s",
codec->implementation->iananame);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "channel-read-codec-rate", "%d",
codec->implementation->samples_per_second);
switch_event_fire(&event);
}
session->read_codec = codec;
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_codec_t *) switch_core_session_get_read_codec(switch_core_session_t *session)
{
return session->read_codec;
}
SWITCH_DECLARE(switch_status_t) switch_core_session_set_write_codec(switch_core_session_t *session,
switch_codec_t *codec)
{
switch_event_t *event;
assert(session != NULL);
if (switch_event_create(&event, SWITCH_EVENT_CODEC) == SWITCH_STATUS_SUCCESS) {
switch_channel_event_set_data(session->channel, event);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "channel-write-codec-name", "%s",
codec->implementation->iananame);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "channel-write-codec-rate", "%d",
codec->implementation->samples_per_second);
switch_event_fire(&event);
}
session->write_codec = codec;
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_codec_t *) switch_core_session_get_write_codec(switch_core_session_t *session)
{
return session->write_codec;
}
SWITCH_DECLARE(switch_status_t) switch_core_codec_init(switch_codec_t *codec, char *codec_name, char *fmtp,
uint32_t rate, int ms, int channels, uint32_t flags,
const switch_codec_settings_t *codec_settings,
switch_memory_pool_t *pool)
{
const switch_codec_interface_t *codec_interface;
const switch_codec_implementation_t *iptr, *implementation = NULL;
char *mode = fmtp;
assert(codec != NULL);
assert(codec_name != NULL);
memset(codec, 0, sizeof(*codec));
if ((codec_interface = switch_loadable_module_get_codec_interface(codec_name)) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "invalid codec %s!\n", codec_name);
return SWITCH_STATUS_GENERR;
}
if (!strcasecmp(codec_name, "ilbc") && mode && strncasecmp(mode, "mode=", 5)) {
int mms;
mode += 5;
if (mode) {
mms = atoi(mode);
if (mms > 0 && mms < 120) {
ms = mms;
}
}
}
/* If no specific codec interval is requested opt for 20ms above all else because lots of stuff assumes it */
if (!ms) {
for (iptr = codec_interface->implementations; iptr; iptr = iptr->next) {
if ((!rate || rate == iptr->samples_per_second) &&
(20 == (iptr->microseconds_per_frame / 1000)) && (!channels || channels == iptr->number_of_channels)) {
implementation = iptr;
goto found;
}
}
}
/* Either looking for a specific interval or there was no interval specified and there wasn't one @20ms available */
for (iptr = codec_interface->implementations; iptr; iptr = iptr->next) {
if ((!rate || rate == iptr->samples_per_second) &&
(!ms || ms == (iptr->microseconds_per_frame / 1000)) &&
(!channels || channels == iptr->number_of_channels)) {
implementation = iptr;
break;
}
}
found:
if (implementation) {
switch_status_t status;
codec->codec_interface = codec_interface;
codec->implementation = implementation;
codec->flags = flags;
if (pool) {
codec->memory_pool = pool;
} else {
if ((status = switch_core_new_memory_pool(&codec->memory_pool)) != SWITCH_STATUS_SUCCESS) {
return status;
}
switch_set_flag(codec, SWITCH_CODEC_FLAG_FREE_POOL);
}
if (fmtp) {
codec->fmtp_in = switch_core_strdup(codec->memory_pool, fmtp);
}
implementation->init(codec, flags, codec_settings);
return SWITCH_STATUS_SUCCESS;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
"Codec %s Exists but not at the desired implementation. %dhz %dms\n", codec_name, rate, ms);
}
return SWITCH_STATUS_NOTIMPL;
}
SWITCH_DECLARE(switch_status_t) switch_core_codec_encode(switch_codec_t *codec,
switch_codec_t *other_codec,
void *decoded_data,
uint32_t decoded_data_len,
uint32_t decoded_rate,
void *encoded_data,
uint32_t * encoded_data_len, uint32_t * encoded_rate,
unsigned int *flag)
{
assert(codec != NULL);
assert(encoded_data != NULL);
assert(decoded_data != NULL);
if (!codec->implementation) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
if (!switch_test_flag(codec, SWITCH_CODEC_FLAG_ENCODE)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec's encoder is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
return codec->implementation->encode(codec,
other_codec,
decoded_data,
decoded_data_len,
decoded_rate, encoded_data, encoded_data_len, encoded_rate, flag);
}
SWITCH_DECLARE(switch_status_t) switch_core_codec_decode(switch_codec_t *codec,
switch_codec_t *other_codec,
void *encoded_data,
uint32_t encoded_data_len,
uint32_t encoded_rate,
void *decoded_data,
uint32_t * decoded_data_len,
uint32_t * decoded_rate, unsigned int *flag)
{
assert(codec != NULL);
assert(encoded_data != NULL);
assert(decoded_data != NULL);
if (!codec->implementation) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
if (!switch_test_flag(codec, SWITCH_CODEC_FLAG_DECODE)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec's decoder is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
return codec->implementation->decode(codec,
other_codec,
encoded_data,
encoded_data_len,
encoded_rate, decoded_data, decoded_data_len, decoded_rate, flag);
}
SWITCH_DECLARE(switch_status_t) switch_core_codec_destroy(switch_codec_t *codec)
{
assert(codec != NULL);
if (!codec->implementation) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Codec is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
codec->implementation->destroy(codec);
if (switch_test_flag(codec, SWITCH_CODEC_FLAG_FREE_POOL)) {
switch_core_destroy_memory_pool(&codec->memory_pool);
}
return SWITCH_STATUS_SUCCESS;
}
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_directory.c -- Main Core Library (Directory Interface)
*
*/
#include <switch.h>
#include "private/switch_core.h"
SWITCH_DECLARE(switch_status_t) switch_core_directory_open(switch_directory_handle_t *dh,
char *module_name,
char *source,
char *dsn, char *passwd, switch_memory_pool_t *pool)
{
switch_status_t status;
if ((dh->directory_interface = switch_loadable_module_get_directory_interface(module_name)) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "invalid directory module [%s]!\n", module_name);
return SWITCH_STATUS_GENERR;
}
if (pool) {
dh->memory_pool = pool;
} else {
if ((status = switch_core_new_memory_pool(&dh->memory_pool)) != SWITCH_STATUS_SUCCESS) {
return status;
}
switch_set_flag(dh, SWITCH_DIRECTORY_FLAG_FREE_POOL);
}
return dh->directory_interface->directory_open(dh, source, dsn, passwd);
}
SWITCH_DECLARE(switch_status_t) switch_core_directory_query(switch_directory_handle_t *dh, char *base, char *query)
{
return dh->directory_interface->directory_query(dh, base, query);
}
SWITCH_DECLARE(switch_status_t) switch_core_directory_next(switch_directory_handle_t *dh)
{
return dh->directory_interface->directory_next(dh);
}
SWITCH_DECLARE(switch_status_t) switch_core_directory_next_pair(switch_directory_handle_t *dh, char **var, char **val)
{
return dh->directory_interface->directory_next_pair(dh, var, val);
}
SWITCH_DECLARE(switch_status_t) switch_core_directory_close(switch_directory_handle_t *dh)
{
return dh->directory_interface->directory_close(dh);
}
SWITCH_DECLARE(switch_status_t) switch_core_speech_open(switch_speech_handle_t *sh,
char *module_name,
char *voice_name,
unsigned int rate,
switch_speech_flag_t *flags, switch_memory_pool_t *pool)
{
switch_status_t status;
if ((sh->speech_interface = switch_loadable_module_get_speech_interface(module_name)) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "invalid speech module [%s]!\n", module_name);
return SWITCH_STATUS_GENERR;
}
switch_copy_string(sh->engine, module_name, sizeof(sh->engine));
sh->flags = *flags;
if (pool) {
sh->memory_pool = pool;
} else {
if ((status = switch_core_new_memory_pool(&sh->memory_pool)) != SWITCH_STATUS_SUCCESS) {
return status;
}
switch_set_flag(sh, SWITCH_SPEECH_FLAG_FREE_POOL);
}
sh->rate = rate;
sh->name = switch_core_strdup(pool, module_name);
return sh->speech_interface->speech_open(sh, voice_name, rate, flags);
}
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
*
* switch_core_event_hook.c Core Event Hooks
*
*/
#include "switch.h"
#include "private/switch_core.h"
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_outgoing(switch_core_session_t *session,
switch_outgoing_channel_hook_t outgoing_channel)
{
switch_io_event_hook_outgoing_channel_t *hook, *ptr;
assert(outgoing_channel != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->outgoing_channel = outgoing_channel;
if (!session->event_hooks.outgoing_channel) {
session->event_hooks.outgoing_channel = hook;
} else {
for (ptr = session->event_hooks.outgoing_channel; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_answer_channel(switch_core_session_t *session,
switch_answer_channel_hook_t answer_channel)
{
switch_io_event_hook_answer_channel_t *hook, *ptr;
assert(answer_channel != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->answer_channel = answer_channel;
if (!session->event_hooks.answer_channel) {
session->event_hooks.answer_channel = hook;
} else {
for (ptr = session->event_hooks.answer_channel; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_state_change(switch_core_session_t *session,
switch_answer_channel_hook_t state_change)
{
switch_io_event_hook_state_change_t *hook, *ptr;
assert(state_change != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->state_change = state_change;
if (!session->event_hooks.state_change) {
session->event_hooks.state_change = hook;
} else {
for (ptr = session->event_hooks.state_change; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_read_frame(switch_core_session_t *session,
switch_read_frame_hook_t read_frame)
{
switch_io_event_hook_read_frame_t *hook, *ptr;
assert(read_frame != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->read_frame = read_frame;
if (!session->event_hooks.read_frame) {
session->event_hooks.read_frame = hook;
} else {
for (ptr = session->event_hooks.read_frame; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_write_frame(switch_core_session_t *session,
switch_write_frame_hook_t write_frame)
{
switch_io_event_hook_write_frame_t *hook, *ptr;
assert(write_frame != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->write_frame = write_frame;
if (!session->event_hooks.write_frame) {
session->event_hooks.write_frame = hook;
} else {
for (ptr = session->event_hooks.write_frame; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_kill_channel(switch_core_session_t *session,
switch_kill_channel_hook_t kill_channel)
{
switch_io_event_hook_kill_channel_t *hook, *ptr;
assert(kill_channel != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->kill_channel = kill_channel;
if (!session->event_hooks.kill_channel) {
session->event_hooks.kill_channel = hook;
} else {
for (ptr = session->event_hooks.kill_channel; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_waitfor_read(switch_core_session_t *session,
switch_waitfor_read_hook_t waitfor_read)
{
switch_io_event_hook_waitfor_read_t *hook, *ptr;
assert(waitfor_read != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->waitfor_read = waitfor_read;
if (!session->event_hooks.waitfor_read) {
session->event_hooks.waitfor_read = hook;
} else {
for (ptr = session->event_hooks.waitfor_read; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_waitfor_write(switch_core_session_t *session,
switch_waitfor_write_hook_t waitfor_write)
{
switch_io_event_hook_waitfor_write_t *hook, *ptr;
assert(waitfor_write != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->waitfor_write = waitfor_write;
if (!session->event_hooks.waitfor_write) {
session->event_hooks.waitfor_write = hook;
} else {
for (ptr = session->event_hooks.waitfor_write; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_send_dtmf(switch_core_session_t *session,
switch_send_dtmf_hook_t send_dtmf)
{
switch_io_event_hook_send_dtmf_t *hook, *ptr;
assert(send_dtmf != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->send_dtmf = send_dtmf;
if (!session->event_hooks.send_dtmf) {
session->event_hooks.send_dtmf = hook;
} else {
for (ptr = session->event_hooks.send_dtmf; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_file.c -- Main Core Library (File I/O Functions)
*
*/
#include <switch.h>
#include "private/switch_core.h"
SWITCH_DECLARE(switch_status_t) switch_core_file_open(switch_file_handle_t *fh,
char *file_path,
uint8_t channels,
uint32_t rate, unsigned int flags, switch_memory_pool_t *pool)
{
char *ext;
switch_status_t status;
char stream_name[128] = "";
char *rhs = NULL;
if ((rhs = strstr(file_path, SWITCH_URL_SEPARATOR))) {
switch_copy_string(stream_name, file_path, (rhs + 1) - file_path);
ext = stream_name;
file_path = rhs + 3;
} else {
if ((ext = strrchr(file_path, '.')) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Format\n");
return SWITCH_STATUS_FALSE;
}
ext++;
}
if ((fh->file_interface = switch_loadable_module_get_file_interface(ext)) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "invalid file format [%s]!\n", ext);
return SWITCH_STATUS_GENERR;
}
fh->flags = flags;
if (pool) {
fh->memory_pool = pool;
} else {
if ((status = switch_core_new_memory_pool(&fh->memory_pool)) != SWITCH_STATUS_SUCCESS) {
return status;
}
switch_set_flag(fh, SWITCH_FILE_FLAG_FREE_POOL);
}
if (rhs) {
fh->handler = switch_core_strdup(fh->memory_pool, rhs);
}
if (rate) {
fh->samplerate = rate;
} else {
rate = 8000;
}
if (channels) {
fh->channels = channels;
} else {
fh->channels = 1;
}
if ((status = fh->file_interface->file_open(fh, file_path)) == SWITCH_STATUS_SUCCESS) {
switch_set_flag(fh, SWITCH_FILE_OPEN);
}
return status;
}
SWITCH_DECLARE(switch_status_t) switch_core_file_read(switch_file_handle_t *fh, void *data, switch_size_t *len)
{
assert(fh != NULL);
return fh->file_interface->file_read(fh, data, len);
}
SWITCH_DECLARE(switch_status_t) switch_core_file_write(switch_file_handle_t *fh, void *data, switch_size_t *len)
{
assert(fh != NULL);
return fh->file_interface->file_write(fh, data, len);
}
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);
switch_set_flag(fh, SWITCH_FILE_SEEK);
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)
{
switch_clear_flag(fh, SWITCH_FILE_OPEN);
return fh->file_interface->file_close(fh);
}
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_hash.c -- Main Core Library (hash functions)
*
*/
#include <switch.h>
#include "private/switch_core.h"
SWITCH_DECLARE(switch_status_t) switch_core_hash_init(switch_hash_t **hash, switch_memory_pool_t *pool)
{
assert(pool != NULL);
if ((*hash = apr_hash_make(pool)) != 0) {
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_GENERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_hash_destroy(switch_hash_t *hash)
{
assert(hash != NULL);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_dup(switch_hash_t *hash, const char *key, const void *data)
{
apr_hash_set(hash, switch_core_strdup(apr_hash_pool_get(hash), key), APR_HASH_KEY_STRING, data);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert(switch_hash_t *hash, const char *key, const void *data)
{
apr_hash_set(hash, key, APR_HASH_KEY_STRING, data);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_hash_delete(switch_hash_t *hash, const char *key)
{
apr_hash_set(hash, key, APR_HASH_KEY_STRING, NULL);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(void *) switch_core_hash_find(switch_hash_t *hash, const char *key)
{
return apr_hash_get(hash, key, APR_HASH_KEY_STRING);
}
差异被折叠。
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_media_bug.c -- Main Core Library (Media Bugs)
*
*/
#include "switch.h"
#include "private/switch_core.h"
static void switch_core_media_bug_destroy(switch_media_bug_t *bug)
{
switch_buffer_destroy(&bug->raw_read_buffer);
switch_buffer_destroy(&bug->raw_write_buffer);
}
SWITCH_DECLARE(switch_frame_t *) switch_core_media_bug_get_replace_frame(switch_media_bug_t *bug)
{
return bug->replace_frame_in;
}
SWITCH_DECLARE(void) switch_core_media_bug_set_replace_frame(switch_media_bug_t *bug, switch_frame_t *frame)
{
bug->replace_frame_out = frame;
}
SWITCH_DECLARE(void *) switch_core_media_bug_get_user_data(switch_media_bug_t *bug)
{
return bug->user_data;
}
SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *bug, switch_frame_t *frame)
{
uint32_t bytes = 0;
uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE] = { 0 };
uint32_t datalen = 0;
int16_t *dp, *fp;
uint32_t x;
size_t rlen = 0;
size_t wlen = 0;
uint32_t blen;
size_t rdlen = 0;
uint32_t maxlen;
switch_codec_t *read_codec = switch_core_session_get_read_codec(bug->session);
if (bug->raw_read_buffer) {
rlen = switch_buffer_inuse(bug->raw_read_buffer);
}
if (bug->raw_write_buffer) {
wlen = switch_buffer_inuse(bug->raw_write_buffer);
}
if ((bug->raw_read_buffer && bug->raw_write_buffer) && (!rlen && !wlen)) {
return SWITCH_STATUS_FALSE;
}
maxlen = sizeof(data) > frame->buflen ? frame->buflen : sizeof(data);
if ((rdlen = rlen > wlen ? wlen : rlen) > maxlen) {
rdlen = maxlen;
}
if (!rdlen) {
rdlen = maxlen;
}
frame->datalen = 0;
if (rlen) {
switch_mutex_lock(bug->read_mutex);
frame->datalen = (uint32_t) switch_buffer_read(bug->raw_read_buffer, frame->data, rdlen);
switch_mutex_unlock(bug->read_mutex);
}
if (wlen) {
switch_mutex_lock(bug->write_mutex);
datalen = (uint32_t) switch_buffer_read(bug->raw_write_buffer, data, rdlen);
switch_mutex_unlock(bug->write_mutex);
}
bytes = (datalen > frame->datalen) ? datalen : frame->datalen;
if (bytes) {
dp = (int16_t *) data;
fp = (int16_t *) frame->data;
rlen = frame->datalen / 2;
wlen = datalen / 2;
blen = bytes / 2;
for (x = 0; x < blen; x++) {
int32_t z = 0;
if (x < rlen) {
z += (int32_t) *(fp + x);
}
if (x < wlen) {
z += (int32_t) *(dp + x);
}
switch_normalize_to_16bit(z);
*(fp + x) = (int16_t) z;
}
frame->datalen = bytes;
frame->samples = bytes / sizeof(int16_t);
frame->rate = read_codec->implementation->samples_per_second;
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_FALSE;
}
#define MAX_BUG_BUFFER 1024 * 512
SWITCH_DECLARE(switch_status_t) switch_core_media_bug_add(switch_core_session_t *session,
switch_media_bug_callback_t callback,
void *user_data,
switch_media_bug_flag_t flags, switch_media_bug_t **new_bug)
{
switch_media_bug_t *bug, *bp;
switch_size_t bytes;
if (flags & SMBF_WRITE_REPLACE) {
switch_thread_rwlock_wrlock(session->bug_rwlock);
for (bp = session->bugs; bp; bp = bp->next) {
if (switch_test_flag(bp, SMBF_WRITE_REPLACE)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Only one bug of this type allowed!\n");
switch_thread_rwlock_unlock(session->bug_rwlock);
return SWITCH_STATUS_GENERR;
}
}
switch_thread_rwlock_unlock(session->bug_rwlock);
}
if (!(bug = switch_core_session_alloc(session, sizeof(*bug)))) {
return SWITCH_STATUS_MEMERR;
}
bug->callback = callback;
bug->user_data = user_data;
bug->session = session;
bug->flags = flags;
bug->ready = 1;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Attaching BUG to %s\n",
switch_channel_get_name(session->channel));
bytes = session->read_codec->implementation->bytes_per_frame;
if (!bug->flags) {
bug->flags = (SMBF_READ_STREAM | SMBF_WRITE_STREAM);
}
if (switch_test_flag(bug, SMBF_READ_STREAM)) {
switch_buffer_create_dynamic(&bug->raw_read_buffer, bytes * SWITCH_BUFFER_BLOCK_FRAMES,
bytes * SWITCH_BUFFER_START_FRAMES, MAX_BUG_BUFFER);
switch_mutex_init(&bug->read_mutex, SWITCH_MUTEX_NESTED, session->pool);
}
bytes = session->write_codec->implementation->bytes_per_frame;
if (switch_test_flag(bug, SMBF_WRITE_STREAM)) {
switch_buffer_create_dynamic(&bug->raw_write_buffer, bytes * SWITCH_BUFFER_BLOCK_FRAMES,
bytes * SWITCH_BUFFER_START_FRAMES, MAX_BUG_BUFFER);
switch_mutex_init(&bug->write_mutex, SWITCH_MUTEX_NESTED, session->pool);
}
switch_thread_rwlock_wrlock(session->bug_rwlock);
bug->next = session->bugs;
session->bugs = bug;
switch_thread_rwlock_unlock(session->bug_rwlock);
*new_bug = bug;
if (bug->callback) {
switch_bool_t result = bug->callback(bug, bug->user_data, SWITCH_ABC_TYPE_INIT);
if (result == SWITCH_FALSE) {
return switch_core_media_bug_remove(session, new_bug);
}
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove_all(switch_core_session_t *session)
{
switch_media_bug_t *bp;
if (session->bugs) {
switch_thread_rwlock_wrlock(session->bug_rwlock);
for (bp = session->bugs; bp; bp = bp->next) {
if (bp->callback) {
bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_CLOSE);
}
switch_core_media_bug_destroy(bp);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Removing BUG from %s\n",
switch_channel_get_name(session->channel));
}
switch_thread_rwlock_unlock(session->bug_rwlock);
session->bugs = NULL;
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_FALSE;
}
SWITCH_DECLARE(switch_status_t) switch_core_media_bug_close(switch_media_bug_t **bug)
{
switch_media_bug_t *bp = *bug;
if (bp) {
if (bp->callback) {
bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_CLOSE);
bp->ready = 0;
}
switch_core_media_bug_destroy(bp);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Removing BUG from %s\n",
switch_channel_get_name(bp->session->channel));
*bug = NULL;
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_FALSE;
}
SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove(switch_core_session_t *session, switch_media_bug_t **bug)
{
switch_media_bug_t *bp = NULL, *last = NULL;
switch_status_t status = SWITCH_STATUS_FALSE;
if (session->bugs) {
switch_thread_rwlock_wrlock(session->bug_rwlock);
for (bp = session->bugs; bp; bp = bp->next) {
if (!bp->ready) {
continue;
}
if (bp == *bug) {
if (last) {
last->next = bp->next;
} else {
session->bugs = bp->next;
}
break;
}
last = bp;
}
switch_thread_rwlock_unlock(session->bug_rwlock);
status = switch_core_media_bug_close(&bp);
}
return status;
}
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_memory.c -- Main Core Library (memory management)
*
*/
#include <switch.h>
#include "private/switch_core.h"
static struct {
switch_memory_pool_t *memory_pool;
} runtime;
SWITCH_DECLARE(switch_memory_pool_t *) switch_core_session_get_pool(switch_core_session_t *session)
{
return session->pool;
}
/* **ONLY** alloc things with this function that **WILL NOT** outlive
the session itself or expect an earth shattering KABOOM!*/
SWITCH_DECLARE(void *) switch_core_session_alloc(switch_core_session_t *session, switch_size_t memory)
{
void *ptr = NULL;
assert(session != NULL);
assert(session->pool != NULL);
#ifdef DEBUG_ALLOC
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate %d\n", memory);
#endif
if ((ptr = apr_palloc(session->pool, memory)) != 0) {
memset(ptr, 0, memory);
}
return ptr;
}
/* **ONLY** alloc things with these functions that **WILL NOT** need
to be freed *EVER* ie this is for *PERMANENT* memory allocation */
SWITCH_DECLARE(void *) switch_core_permanent_alloc(switch_size_t memory)
{
void *ptr = NULL;
assert(runtime.memory_pool != NULL);
#ifdef DEBUG_ALLOC
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Perm Allocate %d\n", memory);
#endif
if ((ptr = apr_palloc(runtime.memory_pool, memory)) != 0) {
memset(ptr, 0, memory);
}
return ptr;
}
SWITCH_DECLARE(char *) switch_core_permanent_strdup(const char *todup)
{
char *duped = NULL;
switch_size_t len;
assert(runtime.memory_pool != NULL);
if (!todup)
return NULL;
len = strlen(todup) + 1;
#ifdef DEBUG_ALLOC
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Perm Allocate %d\n", len);
#endif
if (todup && (duped = apr_palloc(runtime.memory_pool, len)) != 0) {
strncpy(duped, todup, len);
}
return duped;
}
SWITCH_DECLARE(char *) switch_core_session_sprintf(switch_core_session_t *session, const char *fmt, ...)
{
va_list ap;
char *result = NULL;
assert(session != NULL);
assert(session->pool != NULL);
va_start(ap, fmt);
result = apr_pvsprintf(session->pool, fmt, ap);
va_end(ap);
return result;
}
SWITCH_DECLARE(char *) switch_core_sprintf(switch_memory_pool_t *pool, const char *fmt, ...)
{
va_list ap;
char *result = NULL;
assert(pool != NULL);
va_start(ap, fmt);
result = apr_pvsprintf(pool, fmt, ap);
va_end(ap);
return result;
}
SWITCH_DECLARE(char *) switch_core_session_strdup(switch_core_session_t *session, const char *todup)
{
char *duped = NULL;
switch_size_t len;
assert(session != NULL);
assert(session->pool != NULL);
if (!todup) {
return NULL;
}
len = strlen(todup) + 1;
#ifdef DEBUG_ALLOC
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate %d\n", len);
#endif
if (todup && (duped = apr_palloc(session->pool, len)) != 0) {
strncpy(duped, todup, len);
}
return duped;
}
SWITCH_DECLARE(char *) switch_core_strdup(switch_memory_pool_t *pool, const char *todup)
{
char *duped = NULL;
switch_size_t len;
assert(pool != NULL);
if (!todup) {
return NULL;
}
len = strlen(todup) + 1;
#ifdef DEBUG_ALLOC
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate %d\n", len);
#endif
if (todup && (duped = apr_palloc(pool, len)) != 0) {
strncpy(duped, todup, len);
}
return duped;
}
SWITCH_DECLARE(switch_status_t) switch_core_new_memory_pool(switch_memory_pool_t **pool)
{
if ((apr_pool_create(pool, NULL)) != SWITCH_STATUS_SUCCESS) {
*pool = NULL;
return SWITCH_STATUS_MEMERR;
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_destroy_memory_pool(switch_memory_pool_t **pool)
{
apr_pool_destroy(*pool);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(void *) switch_core_alloc(switch_memory_pool_t *pool, switch_size_t memory)
{
void *ptr = NULL;
assert(pool != NULL);
#ifdef DEBUG_ALLOC
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate %d\n", memory);
/* assert(memory < 600000); */
#endif
if ((ptr = apr_palloc(pool, memory)) != 0) {
memset(ptr, 0, memory);
}
return ptr;
}
SWITCH_DECLARE(switch_memory_pool_t *) switch_core_memory_init(void)
{
memset(&runtime, 0, sizeof(runtime));
apr_pool_create(&runtime.memory_pool, NULL);
return runtime.memory_pool;
}
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_port_allocator.c -- Main Core Library (port allocator)
*
*/
#include <switch.h>
#include "private/switch_core.h"
struct switch_core_port_allocator {
switch_port_t start;
switch_port_t end;
switch_port_t next;
uint8_t inc;
switch_mutex_t *mutex;
switch_memory_pool_t *pool;
};
SWITCH_DECLARE(switch_status_t) switch_core_port_allocator_new(switch_port_t start,
switch_port_t end,
uint8_t inc,
switch_core_port_allocator_t **new_allocator)
{
switch_status_t status;
switch_memory_pool_t *pool;
switch_core_port_allocator_t *alloc;
if ((status = switch_core_new_memory_pool(&pool)) != SWITCH_STATUS_SUCCESS) {
return status;
}
if (!(alloc = switch_core_alloc(pool, sizeof(*alloc)))) {
switch_core_destroy_memory_pool(&pool);
return SWITCH_STATUS_MEMERR;
}
alloc->start = start;
alloc->next = start;
alloc->end = end;
if (!(alloc->inc = inc)) {
alloc->inc = 2;
}
switch_mutex_init(&alloc->mutex, SWITCH_MUTEX_NESTED, pool);
alloc->pool = pool;
*new_allocator = alloc;
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_management_exec(char *relative_oid,
switch_management_action_t action,
char *data, switch_size_t datalen)
{
const switch_management_interface_t *ptr;
switch_status_t status = SWITCH_STATUS_FALSE;
if ((ptr = switch_loadable_module_get_management_interface(relative_oid))) {
status = ptr->management_function(relative_oid, action, data, datalen);
}
return status;
}
SWITCH_DECLARE(switch_port_t) switch_core_port_allocator_request_port(switch_core_port_allocator_t *alloc)
{
switch_port_t port;
switch_mutex_lock(alloc->mutex);
port = alloc->next;
alloc->next = alloc->next + alloc->inc;
if (alloc->next > alloc->end) {
alloc->next = alloc->start;
}
switch_mutex_unlock(alloc->mutex);
return port;
}
SWITCH_DECLARE(void) switch_core_port_allocator_destroy(switch_core_port_allocator_t **alloc)
{
switch_memory_pool_t *pool = (*alloc)->pool;
switch_core_destroy_memory_pool(&pool);
*alloc = NULL;
}
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_rwlock.c -- Main Core Library (read / write locks)
*
*/
#include <switch.h>
#include "private/switch_core.h"
#ifdef SWITCH_DEBUG_RWLOCKS
SWITCH_DECLARE(switch_status_t) switch_core_session_perform_read_lock(switch_core_session_t *session,
const char *file, const char *func, int line)
#else
SWITCH_DECLARE(switch_status_t) switch_core_session_read_lock(switch_core_session_t *session)
#endif
{
switch_status_t status = SWITCH_STATUS_FALSE;
if (session->rwlock) {
if (switch_test_flag(session, SSF_DESTROYED)) {
status = SWITCH_STATUS_FALSE;
#ifdef SWITCH_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "%s Read lock FAIL\n",
switch_channel_get_name(session->channel));
#endif
} else {
status = (switch_status_t) switch_thread_rwlock_tryrdlock(session->rwlock);
#ifdef SWITCH_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "%s Read lock AQUIRED\n",
switch_channel_get_name(session->channel));
#endif
}
}
return status;
}
#ifdef SWITCH_DEBUG_RWLOCKS
SWITCH_DECLARE(void) switch_core_session_perform_write_lock(switch_core_session_t *session,
const char *file, const char *func, int line)
{
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "%s Write lock AQUIRED\n",
switch_channel_get_name(session->channel));
#else
SWITCH_DECLARE(void) switch_core_session_write_lock(switch_core_session_t *session)
{
#endif
switch_thread_rwlock_wrlock(session->rwlock);
}
#ifdef SWITCH_DEBUG_RWLOCKS
SWITCH_DECLARE(void) switch_core_session_perform_rwunlock(switch_core_session_t *session,
const char *file, const char *func, int line)
{
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "%s Read/Write lock CLEARED\n",
switch_channel_get_name(session->channel));
#else
SWITCH_DECLARE(void) switch_core_session_rwunlock(switch_core_session_t *session)
{
#endif
switch_thread_rwlock_unlock(session->rwlock);
}
差异被折叠。
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_speech.c -- Main Core Library (speech functions)
*
*/
#include <switch.h>
#include "private/switch_core.h"
SWITCH_DECLARE(switch_status_t) switch_core_speech_feed_tts(switch_speech_handle_t *sh, char *text,
switch_speech_flag_t *flags)
{
assert(sh != NULL);
return sh->speech_interface->speech_feed_tts(sh, text, flags);
}
SWITCH_DECLARE(void) switch_core_speech_flush_tts(switch_speech_handle_t *sh)
{
assert(sh != NULL);
if (sh->speech_interface->speech_flush_tts) {
sh->speech_interface->speech_flush_tts(sh);
}
}
SWITCH_DECLARE(void) switch_core_speech_text_param_tts(switch_speech_handle_t *sh, char *param, char *val)
{
assert(sh != NULL);
if (sh->speech_interface->speech_text_param_tts) {
sh->speech_interface->speech_text_param_tts(sh, param, val);
}
}
SWITCH_DECLARE(void) switch_core_speech_numeric_param_tts(switch_speech_handle_t *sh, char *param, int val)
{
assert(sh != NULL);
if (sh->speech_interface->speech_numeric_param_tts) {
sh->speech_interface->speech_numeric_param_tts(sh, param, val);
}
}
SWITCH_DECLARE(void) switch_core_speech_float_param_tts(switch_speech_handle_t *sh, char *param, double val)
{
assert(sh != NULL);
if (sh->speech_interface->speech_float_param_tts) {
sh->speech_interface->speech_float_param_tts(sh, param, val);
}
}
SWITCH_DECLARE(switch_status_t) switch_core_speech_read_tts(switch_speech_handle_t *sh,
void *data,
switch_size_t *datalen,
uint32_t * rate, switch_speech_flag_t *flags)
{
assert(sh != NULL);
return sh->speech_interface->speech_read_tts(sh, data, datalen, rate, flags);
}
SWITCH_DECLARE(switch_status_t) switch_core_speech_close(switch_speech_handle_t *sh, switch_speech_flag_t *flags)
{
return sh->speech_interface->speech_close(sh, flags);
}
差异被折叠。
差异被折叠。
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_timer.c -- Main Core Library (timer interface)
*
*/
#include <switch.h>
#include "private/switch_core.h"
SWITCH_DECLARE(switch_status_t) switch_core_timer_init(switch_timer_t *timer, char *timer_name, int interval,
int samples, switch_memory_pool_t *pool)
{
switch_timer_interface_t *timer_interface;
switch_status_t status;
memset(timer, 0, sizeof(*timer));
if ((timer_interface = switch_loadable_module_get_timer_interface(timer_name)) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "invalid timer %s!\n", timer_name);
return SWITCH_STATUS_GENERR;
}
timer->interval = interval;
timer->samples = samples;
timer->samplecount = 0;
timer->timer_interface = timer_interface;
if (pool) {
timer->memory_pool = pool;
} else {
if ((status = switch_core_new_memory_pool(&timer->memory_pool)) != SWITCH_STATUS_SUCCESS) {
return status;
}
switch_set_flag(timer, SWITCH_TIMER_FLAG_FREE_POOL);
}
timer->timer_interface->timer_init(timer);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_timer_next(switch_timer_t *timer)
{
if (!timer->timer_interface) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
if (timer->timer_interface->timer_next(timer) == SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_SUCCESS;
} else {
return SWITCH_STATUS_GENERR;
}
}
SWITCH_DECLARE(switch_status_t) switch_core_timer_step(switch_timer_t *timer)
{
if (!timer->timer_interface) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
return timer->timer_interface->timer_step(timer);
}
SWITCH_DECLARE(switch_status_t) switch_core_timer_check(switch_timer_t *timer)
{
if (!timer->timer_interface) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
return timer->timer_interface->timer_check(timer);
}
SWITCH_DECLARE(switch_status_t) switch_core_timer_destroy(switch_timer_t *timer)
{
if (!timer->timer_interface) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not initilized!\n");
return SWITCH_STATUS_GENERR;
}
timer->timer_interface->timer_destroy(timer);
if (switch_test_flag(timer, SWITCH_TIMER_FLAG_FREE_POOL)) {
switch_core_destroy_memory_pool(&timer->memory_pool);
}
return SWITCH_STATUS_SUCCESS;
}
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
#include <switch.h>
struct switch_scheduler_task_container {
switch_scheduler_task_t task;
time_t executed;
int in_thread;
int destroyed;
switch_scheduler_func_t func;
switch_memory_pool_t *pool;
uint32_t flags;
char *desc;
struct switch_scheduler_task_container *next;
};
typedef struct switch_scheduler_task_container switch_scheduler_task_container_t;
static struct {
switch_scheduler_task_container_t *task_list;
switch_mutex_t *task_mutex;
uint32_t task_id;
int task_thread_running;
switch_memory_pool_t *memory_pool;
} globals;
static void switch_scheduler_execute(switch_scheduler_task_container_t * tp)
{
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Executing task %u %s (%s)\n", tp->task.task_id, tp->desc, switch_str_nil(tp->task.group));
tp->func(&tp->task);
if (tp->task.runtime > tp->executed) {
tp->executed = 0;
} else {
tp->destroyed = 1;
}
}
static void *SWITCH_THREAD_FUNC task_own_thread(switch_thread_t *thread, void *obj)
{
switch_scheduler_task_container_t *tp = (switch_scheduler_task_container_t *) obj;
switch_memory_pool_t *pool;
pool = tp->pool;
tp->pool = NULL;
switch_scheduler_execute(tp);
switch_core_destroy_memory_pool(&pool);
tp->in_thread = 0;
return NULL;
}
static int task_thread_loop(int done)
{
switch_scheduler_task_container_t *tofree, *tp, *last = NULL;
switch_mutex_lock(globals.task_mutex);
for (tp = globals.task_list; tp; tp = tp->next) {
if (done) {
tp->destroyed = 1;
} else {
time_t now = time(NULL);
if (now >= tp->task.runtime && !tp->in_thread) {
tp->executed = now;
if (switch_test_flag(tp, SSHF_OWN_THREAD)) {
switch_thread_t *thread;
switch_threadattr_t *thd_attr;
switch_core_new_memory_pool(&tp->pool);
switch_threadattr_create(&thd_attr, tp->pool);
switch_threadattr_detach_set(thd_attr, 1);
tp->in_thread = 1;
switch_thread_create(&thread, thd_attr, task_own_thread, tp, tp->pool);
} else {
switch_scheduler_execute(tp);
}
}
}
}
switch_mutex_unlock(globals.task_mutex);
switch_mutex_lock(globals.task_mutex);
for (tp = globals.task_list; tp;) {
if (tp->destroyed && !tp->in_thread) {
tofree = tp;
tp = tp->next;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Deleting task %u %s (%s)\n",
tofree->task.task_id, tofree->desc, switch_str_nil(tofree->task.group));
if (last) {
last->next = tofree->next;
} else {
globals.task_list = tofree->next;
}
switch_safe_free(tofree->task.group);
if (tofree->task.cmd_arg && switch_test_flag(tofree, SSHF_FREE_ARG)) {
free(tofree->task.cmd_arg);
}
switch_safe_free(tofree->desc);
free(tofree);
} else {
last = tp;
tp = tp->next;
}
}
switch_mutex_unlock(globals.task_mutex);
return done;
}
static void *SWITCH_THREAD_FUNC switch_scheduler_task_thread(switch_thread_t *thread, void *obj)
{
globals.task_thread_running = 1;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Starting task thread\n");
while (globals.task_thread_running == 1) {
if (task_thread_loop(0)) {
break;
}
switch_yield(500000);
}
task_thread_loop(1);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Task thread ending\n");
globals.task_thread_running = 0;
return NULL;
}
SWITCH_DECLARE(uint32_t) switch_scheduler_add_task(time_t task_runtime,
switch_scheduler_func_t func,
char *desc,
char *group,
uint32_t cmd_id, void *cmd_arg, switch_scheduler_flag_t flags)
{
switch_scheduler_task_container_t *container, *tp;
switch_mutex_lock(globals.task_mutex);
switch_zmalloc(container, sizeof(*container));
assert(func);
container->func = func;
time(&container->task.created);
container->task.runtime = task_runtime;
container->task.group = strdup(group ? group : "none");
container->task.cmd_id = cmd_id;
container->task.cmd_arg = cmd_arg;
container->flags = flags;
container->desc = strdup(desc ? desc : "none");
for (tp = globals.task_list; tp && tp->next; tp = tp->next);
if (tp) {
tp->next = container;
} else {
globals.task_list = container;
}
for (container->task.task_id = 0; !container->task.task_id; container->task.task_id = ++globals.task_id);
switch_mutex_unlock(globals.task_mutex);
tp = container;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Added task %u %s (%s) to run at %" TIME_T_FMT "\n",
tp->task.task_id, tp->desc, switch_str_nil(tp->task.group), task_runtime);
return container->task.task_id;
}
SWITCH_DECLARE(switch_status_t) switch_scheduler_del_task_id(uint32_t task_id)
{
switch_scheduler_task_container_t *tp;
switch_status_t status = SWITCH_STATUS_FALSE;
switch_mutex_lock(globals.task_mutex);
for (tp = globals.task_list; tp; tp = tp->next) {
if (tp->task.task_id == task_id) {
tp->destroyed++;
status = SWITCH_STATUS_SUCCESS;
break;
}
}
switch_mutex_unlock(globals.task_mutex);
return status;
}
SWITCH_DECLARE(switch_status_t) switch_scheduler_del_task_group(char *group)
{
switch_scheduler_task_container_t *tp;
switch_status_t status = SWITCH_STATUS_FALSE;
switch_mutex_lock(globals.task_mutex);
for (tp = globals.task_list; tp; tp = tp->next) {
if (!strcmp(tp->task.group, group)) {
tp->destroyed++;
status = SWITCH_STATUS_SUCCESS;
}
}
switch_mutex_unlock(globals.task_mutex);
return status;
}
SWITCH_DECLARE(void) switch_scheduler_task_thread_start(void)
{
switch_thread_t *thread;
switch_threadattr_t *thd_attr;
switch_core_new_memory_pool(&globals.memory_pool);
switch_threadattr_create(&thd_attr, globals.memory_pool);
switch_mutex_init(&globals.task_mutex, SWITCH_MUTEX_NESTED, globals.memory_pool);
switch_threadattr_detach_set(thd_attr, 1);
switch_thread_create(&thread, thd_attr, switch_scheduler_task_thread, NULL, globals.memory_pool);
}
SWITCH_DECLARE(void) switch_scheduler_task_thread_stop(void)
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping Task Thread\n");
if (globals.task_thread_running == 1) {
int sanity = 0;
globals.task_thread_running = -1;
while (globals.task_thread_running) {
switch_yield(100000);
if (++sanity > 10) {
break;
}
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论