]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
revert
authorAnthony Minessale <anthm@freeswitch.org>
Thu, 16 Aug 2012 17:09:19 +0000 (12:09 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Thu, 16 Aug 2012 17:09:19 +0000 (12:09 -0500)
15 files changed:
src/include/private/switch_core_pvt.h
src/mod/endpoints/mod_opal/Makefile
src/mod/endpoints/mod_opal/mod_opal.cpp
src/mod/endpoints/mod_opal/mod_opal.h
src/mod/endpoints/mod_sofia/mod_sofia.c
src/mod/endpoints/mod_sofia/mod_sofia.h
src/mod/endpoints/mod_sofia/sofia.c
src/mod/xml_int/mod_xml_radius/00_dialplan_auth.xml
src/mod/xml_int/mod_xml_radius/mod_xml_radius.c
src/mod/xml_int/mod_xml_radius/xml_radius.conf.xml
src/switch_channel.c
src/switch_core_memory.c
src/switch_core_session.c
src/switch_core_state_machine.c
src/switch_ivr_bridge.c

index 6d36bae00838484da0cbde21cb21654012dfcf53..2df37b0e053bfce3054799b47f12dd92b6b996b9 100644 (file)
@@ -98,7 +98,8 @@ typedef enum {
        SSF_READ_TRANSCODE = (1 << 5),
        SSF_WRITE_TRANSCODE = (1 << 6),
        SSF_READ_CODEC_RESET = (1 << 7),
-       SSF_WRITE_CODEC_RESET = (1 << 8)
+       SSF_WRITE_CODEC_RESET = (1 << 8),
+       SSF_DESTROYABLE = (1 << 9)
 } switch_session_flag_t;
 
 
index fc0fc4873dfd20bad1fea359d84068ba10a0272a..d80ba64e5794601710cccca93589985bba277120 100644 (file)
@@ -1,7 +1,19 @@
 BASE=../../../..
-LOCAL_INSERT_CFLAGS= pkg-config opal --cflags 
-LOCAL_CFLAGS+=-g -ggdb -I.
-LOCAL_INSERT_LDFLAGS= pkg-config opal --libs
+
+PKG_DIR:=/usr/local/lib/pkgconfig
+ifeq ($(PKG_CONFIG_PATH),)
+  export PKG_CONFIG_PATH:=$(PKG_DIR)
+else
+  ifeq ($(findstring $(PKG_DIR),$(PKG_CONFIG_PATH)),)
+    export PKG_CONFIG_PATH:=$(PKG_CONFIG_PATH):$(PKG_DIR)
+  endif
+endif
+
+#DEBUG_SUFFIX:=--define-variable=suffix=_d
+
+LOCAL_INSERT_CFLAGS= pkg-config opal $(DEBUG_SUFFIX) --cflags 
+LOCAL_CFLAGS+=-g -ggdb
+LOCAL_INSERT_LDFLAGS= pkg-config opal $(DEBUG_SUFFIX) --libs
 
 include $(BASE)/build/modmake.rules
 
index 0760588019b674449d826ab62d50dcf075b08239..cf160ba527fd3a969a7c8596b14b94f9abda0f2f 100644 (file)
-/* Opal endpoint interface for Freeswitch Modular Media Switching Software Library /
- * Soft-Switch Application
- *
- * Version: MPL 1.1
- *
- * Copyright (c) 2007 Tuyan Ozipek (tuyanozipek@gmail.com)
- *
- * 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.
- *
- * Contributor(s):
- * Tuyan Ozipek   (tuyanozipek@gmail.com)
- * Lukasz Zwierko (lzwierko@gmail.com)
- * Robert Jongbloed (robertj@voxlucida.com.au)
- *
- */
-
-#include "mod_opal.h"
-#include <opal/patch.h>
-#include <rtp/rtp.h>
-#include <h323/h323pdu.h>
-#include <h323/gkclient.h>
-
-SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_codec_string, mod_opal_globals.codec_string);
-SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_context, mod_opal_globals.context);
-SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, mod_opal_globals.dialplan);
-
-
-#define CF_NEED_FLUSH (1 << 1)
-
-struct mod_opal_globals mod_opal_globals = { 0 };
-
-
-static switch_call_cause_t create_outgoing_channel(switch_core_session_t *session, switch_event_t *var_event,
-                                                   switch_caller_profile_t *outbound_profile, switch_core_session_t **new_session,
-                                                   switch_memory_pool_t **pool, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause);
-
-
-static FSProcess *opal_process = NULL;
-
-
-static const char ModuleName[] = "opal";
-
-
-static switch_status_t on_hangup(switch_core_session_t *session);
-static switch_status_t on_destroy(switch_core_session_t *session);
-
-
-static switch_io_routines_t opalfs_io_routines = {
-    /*.outgoing_channel */ create_outgoing_channel,
-    /*.read_frame */ FSConnection::read_audio_frame,
-    /*.write_frame */ FSConnection::write_audio_frame,
-    /*.kill_channel */ FSConnection::kill_channel,
-    /*.send_dtmf */ FSConnection::send_dtmf,
-    /*.receive_message */ FSConnection::receive_message,
-    /*.receive_event */ FSConnection::receive_event,
-    /*.state_change */ FSConnection::state_change,
-    /*.read_video_frame */ FSConnection::read_video_frame,
-    /*.write_video_frame */ FSConnection::write_video_frame
-};
-
-static switch_state_handler_table_t opalfs_event_handlers = {
-    /*.on_init */ FSConnection::on_init,
-    /*.on_routing */ FSConnection::on_routing,
-    /*.on_execute */ FSConnection::on_execute,
-    /*.on_hangup */ on_hangup,
-    /*.on_exchange_media */ FSConnection::on_exchange_media,
-    /*.on_soft_execute */ FSConnection::on_soft_execute,
-    /*.on_consume_media*/ NULL,
-    /*.on_hibernate*/ NULL,
-    /*.on_reset*/ NULL,
-    /*.on_park*/ NULL,
-    /*.on_reporting*/ NULL,
-    /*.on_destroy*/ on_destroy
-};
-
-
-SWITCH_BEGIN_EXTERN_C
-/*******************************************************************************/
-
-SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load);
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_opal_shutdown);
-SWITCH_MODULE_DEFINITION(mod_opal, mod_opal_load, mod_opal_shutdown, NULL);
-
-SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load) {
-    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Starting loading mod_opal\n");
-
-    /* Prevent the loading of OPAL codecs via "plug ins", this is a directory
-       full of DLLs that will be loaded automatically. */
-    putenv((char *)"PTLIBPLUGINDIR=/no/thanks");
-
-
-    *module_interface = switch_loadable_module_create_module_interface(pool, modname);
-    if (!*module_interface) {
-        return SWITCH_STATUS_MEMERR;
-    }
-
-    opal_process = new FSProcess();
-    if (opal_process == NULL) {
-        return SWITCH_STATUS_MEMERR;
-    }
-
-    if (opal_process->Initialise(*module_interface)) {
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Opal manager initialized and running\n");
-        //unloading causes a seg in linux
-        return SWITCH_STATUS_NOUNLOAD;
-        //return SWITCH_STATUS_SUCCESS;
-    }
-
-    delete opal_process;
-    opal_process = NULL;
-    return SWITCH_STATUS_FALSE;
-}
-
-
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_opal_shutdown) {
-    
-    switch_safe_free(mod_opal_globals.context);
-    switch_safe_free(mod_opal_globals.dialplan);
-    switch_safe_free(mod_opal_globals.codec_string);
-    delete opal_process;
-    opal_process = NULL;
-    return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_END_EXTERN_C
-/*******************************************************************************/
-
-
-
-static switch_call_cause_t create_outgoing_channel(switch_core_session_t *session,
-                                                   switch_event_t *var_event,
-                                                   switch_caller_profile_t *outbound_profile,
-                                                   switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause)
-{
-    if (opal_process == NULL) {
-        return SWITCH_CAUSE_CRASH;
-    }
-
-    PString token;
-
-    FSManager & manager = opal_process->GetManager();
-    if (!manager.SetUpCall("local:", outbound_profile->destination_number, token, outbound_profile)) {
-        return SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
-    }
-
-    PSafePtr < OpalCall > call = manager.FindCallWithLock(token);
-
-    if (call == NULL) {
-        return SWITCH_CAUSE_PROTOCOL_ERROR;
-    }
-
-    PSafePtr < FSConnection > connection = call->GetConnectionAs < FSConnection > (0);
-
-    if (connection == NULL) {
-        return SWITCH_CAUSE_PROTOCOL_ERROR;
-    }
-
-    *new_session = connection->GetSession();
-
-    return SWITCH_CAUSE_SUCCESS;
-}
-
-
-///////////////////////////////////////////////////////////////////////
-
-#if PTRACING
-
-class FSTrace : public ostream {
- public:
- FSTrace()
-     : ostream(&buffer)
-        {
-        }
-
- private:
- class Buffer : public streambuf {
-        char buffer[250];
-
-    public:
-        Buffer()
-            {
-                setg(buffer, buffer, &buffer[sizeof(buffer)-2]);
-                setp(buffer, &buffer[sizeof(buffer)-2]);
-            }
-
-        virtual int sync()
-        {
-            return overflow(EOF);
-        }
-
-        virtual int underflow()
-        {
-            return EOF;
-        }
-
-        virtual int overflow(int c)
-        {
-            const char *fmt = "%s";
-            char *func = NULL;
-
-            int bufSize = pptr() - pbase();
-
-            if (c != EOF) {
-                *pptr() = (char)c;
-                bufSize++;
-            }
-            
-            if (bufSize != 0) {
-                char *bufPtr = pbase();
-                char *bufEndPtr = NULL;
-                setp(bufPtr, epptr());
-                bufPtr[bufSize] = '\0';
-                int line = 0;
-                char *p;
-                
-                char *file = NULL;
-                switch_log_level_t level;
-
-                
-                switch (strtoul(bufPtr, &file, 10)) {
-                case 1 :
-                    level = SWITCH_LOG_INFO;
-                    break;
-                default :
-                    level = SWITCH_LOG_DEBUG;
-                    break;
-                }
-
-                if (file) {
-                    while (isspace(*file)) file++;
-                    
-                    if (file && (bufPtr = strchr(file, '(')) && (bufEndPtr = strchr(bufPtr, ')'))) {
-                        char *e;
-
-                        for(p = bufPtr; p && *p; p++) {
-                            if (*p == '\t') {
-                                *p = ' ';
-                            }
-                        }
-
-                        *bufPtr++ = '\0';
-                        line = atoi(bufPtr);
-                        while (bufEndPtr && isspace(*(++bufEndPtr)));
-                        bufPtr = bufEndPtr;
-                        if (bufPtr && ((e = strchr(bufPtr, ' ')) || (e = strchr(bufPtr, '\t')))) {
-                            func = bufPtr;
-                            bufPtr = e;
-                            *bufPtr++ = '\0';
-                        }
-                    }
-                }
-                
-                switch_text_channel_t tchannel = SWITCH_CHANNEL_ID_LOG;
-
-                if (!bufPtr) {
-                    bufPtr = pbase();
-                    level = SWITCH_LOG_DEBUG;
-                }
-
-                if (bufPtr) {
-                    if (end_of(bufPtr) != '\n') {
-                        fmt = "%s\n";
-                    }
-                    if (!(file && func && line)) tchannel = SWITCH_CHANNEL_ID_LOG_CLEAN;
-
-                    switch_log_printf(tchannel, file, func, line, NULL, level, fmt, bufPtr);
-                }
-                
-            }
-
-            return 0;
-        }
-    } buffer;
-};
-
-#endif
-
-
-///////////////////////////////////////////////////////////////////////
-
-FSProcess::FSProcess()
-  : PLibraryProcess("Vox Lucida Pty. Ltd.", "mod_opal", 1, 0, AlphaCode, 1)
-  , m_manager(NULL)
-{
-}
-
-
-FSProcess::~FSProcess()
-{
-  delete m_manager;
-}
-
-
-bool FSProcess::Initialise(switch_loadable_module_interface_t *iface)
-{
-  m_manager = new FSManager();
-  return m_manager != NULL && m_manager->Initialise(iface);
-}
-
-
-///////////////////////////////////////////////////////////////////////
-
-FSManager::FSManager()
-{
-    // These are deleted by the OpalManager class, no need to have destructor
-    m_h323ep = new H323EndPoint(*this);
-    m_iaxep = new IAX2EndPoint(*this);
-    m_fsep = new FSEndPoint(*this);
-}
-
-
-bool FSManager::Initialise(switch_loadable_module_interface_t *iface)
-{
-    ReadConfig(false);
-
-#if PTRACING
-    PTrace::SetLevel(mod_opal_globals.trace_level);        //just for fun and eyecandy ;)
-    PTrace::SetOptions(PTrace::TraceLevel);
-    PTrace::SetStream(new FSTrace);
-#endif
-
-    m_FreeSwitch = (switch_endpoint_interface_t *) switch_loadable_module_create_interface(iface, SWITCH_ENDPOINT_INTERFACE);
-    m_FreeSwitch->interface_name = ModuleName;
-    m_FreeSwitch->io_routines = &opalfs_io_routines;
-    m_FreeSwitch->state_handler = &opalfs_event_handlers;
-
-    silenceDetectParams.m_mode = OpalSilenceDetector::NoSilenceDetection;
-
-    if (m_listeners.empty()) {
-        m_h323ep->StartListener("");
-    } else {
-        for (std::list < FSListener >::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it) {
-            if (!m_h323ep->StartListener(it->listenAddress)) {
-                PTRACE(3, "mod_opal\tCannot start listener for " << it->name);
-            }
-        }
-    }
-
-    AddRouteEntry("h323:.* = local:<da>");  // config option for direct routing
-    AddRouteEntry("iax2:.* = local:<da>");  // config option for direct routing
-    AddRouteEntry("local:.* = h323:<da>");  // config option for direct routing
-
-    // Make sure all known codecs are instantiated,
-    // these are ones we know how to translate into H.323 capabilities
-    GetOpalG728();
-    GetOpalG729();
-    GetOpalG729A();
-    GetOpalG729B();
-    GetOpalG729AB();
-    GetOpalG7231_6k3();
-    GetOpalG7231_5k3();
-    GetOpalG7231A_6k3();
-    GetOpalG7231A_5k3();
-    GetOpalGSM0610();
-    GetOpalGSMAMR();
-    GetOpaliLBC();
-
-    /* For compatibility with the algorithm in FSConnection::SetCodecs() we need
-       to set all audio media formats to be 1 frame per packet */
-    OpalMediaFormatList allCodecs = OpalMediaFormat::GetAllRegisteredMediaFormats();
-    for (OpalMediaFormatList::iterator it = allCodecs.begin(); it != allCodecs.end(); ++it) {
-      if (it->GetMediaType() == OpalMediaType::Audio()) {
-        it->SetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption(), 1);
-        it->SetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption(), 1);
-      }
-    }
-
-    if (!m_gkAddress.IsEmpty()) {
-      if (m_h323ep->UseGatekeeper(m_gkAddress, m_gkIdentifer, m_gkInterface))
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Started gatekeeper: %s\n",
-                          (const char *)m_h323ep->GetGatekeeper()->GetName());
-      else
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
-                          "Could not start gatekeeper: addr=\"%s\", id=\"%s\", if=\"%s\"\n",
-                          (const char *)m_gkAddress,
-                          (const char *)m_gkIdentifer,
-                          (const char *)m_gkInterface);
-    }
-
-    return TRUE;
-}
-
-
-switch_status_t FSManager::ReadConfig(int reload)
-{
-    const char *cf = "opal.conf";
-    switch_status_t status = SWITCH_STATUS_SUCCESS;
-
-    switch_memory_pool_t *pool = NULL;
-    if ((status = switch_core_new_memory_pool(&pool)) != SWITCH_STATUS_SUCCESS) {
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
-        return status;
-    }
-
-    set_global_context("default");
-    set_global_dialplan("XML");
-
-    switch_event_t *params = NULL;
-    switch_event_create(&params, SWITCH_EVENT_REQUEST_PARAMS);
-    switch_assert(params);
-    switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "profile", switch_str_nil(""));
-    switch_xml_t cfg;
-    switch_xml_t xml = switch_xml_open_cfg(cf, &cfg, params);
-    if (xml == NULL) {
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
-        return SWITCH_STATUS_FALSE;
-    }
-
-    switch_xml_t xmlSettings = switch_xml_child(cfg, "settings");
-    if (xmlSettings) {
-        for (switch_xml_t xmlParam = switch_xml_child(xmlSettings, "param"); xmlParam != NULL; xmlParam = xmlParam->next) {
-            const char *var = switch_xml_attr_soft(xmlParam, "name");
-            const char *val = switch_xml_attr_soft(xmlParam, "value");
-
-            if (!strcasecmp(var, "trace-level")) {
-                int level = atoi(val);
-                if (level > 0) {
-                    mod_opal_globals.trace_level = level;
-                }
-            } else if (!strcasecmp(var, "context")) {
-                set_global_context(val);
-            } else if (!strcasecmp(var, "dialplan")) {
-                set_global_dialplan(val);
-            } else if (!strcasecmp(var, "codec-prefs")) {
-                set_global_codec_string(val);
-            } else if (!strcasecmp(var, "jitter-size")) {
-                char * next;
-                unsigned minJitter = strtoul(val, &next, 10);
-                if (minJitter >= 10) {
-                    unsigned maxJitter = minJitter;
-                    if (*next == ',')
-                      maxJitter = atoi(next+1);
-                    SetAudioJitterDelay(minJitter, maxJitter); // In milliseconds
-                }
-            } else if (!strcasecmp(var, "gk-address")) {
-                m_gkAddress = val;
-            } else if (!strcasecmp(var, "gk-identifer")) {
-                m_gkIdentifer = val;
-            } else if (!strcasecmp(var, "gk-interface")) {
-                m_gkInterface = val;
-            }
-        }
-    }
-
-    switch_xml_t xmlListeners = switch_xml_child(cfg, "listeners");
-    if (xmlListeners != NULL) {
-        for (switch_xml_t xmlListener = switch_xml_child(xmlListeners, "listener"); xmlListener != NULL; xmlListener = xmlListener->next) {
-
-            m_listeners.push_back(FSListener());
-            FSListener & listener = m_listeners.back();
-
-            listener.name = switch_xml_attr_soft(xmlListener, "name");
-            if (listener.name.IsEmpty())
-                listener.name = "unnamed";
-
-            PIPSocket::Address ip;
-            WORD port = 1720;
-
-            for (switch_xml_t xmlParam = switch_xml_child(xmlListener, "param"); xmlParam != NULL; xmlParam = xmlParam->next) {
-                const char *var = switch_xml_attr_soft(xmlParam, "name");
-                const char *val = switch_xml_attr_soft(xmlParam, "value");
-                //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Var - '%s' and Val - '%s' \n", var, val);
-                if (!strcasecmp(var, "h323-ip"))
-                    ip = val;
-                else if (!strcasecmp(var, "h323-port"))
-                    port = (WORD) atoi(val);
-            }
-
-            listener.listenAddress = OpalTransportAddress(ip, port);
-            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created Listener '%s'\n", (const char *) listener.name);
-        }
-    }
-
-    switch_event_destroy(&params);
-
-    if (xml)
-        switch_xml_free(xml);
-
-    return status;
-}
-
-
-OpalCall * FSManager::CreateCall(void * /*userData*/)
-{
-  return new FSCall(*this);
-}
-
-
-///////////////////////////////////////////////////////////////////////
-
-FSEndPoint::FSEndPoint(FSManager & manager)
-:   OpalLocalEndPoint(manager)
-{
-    PTRACE(3, "mod_opal\t FSEndPoint Created!");
-}
-
-
-bool FSEndPoint::OnIncomingCall(OpalLocalConnection & connection)
-{
-    return ((FSConnection &) connection).OnIncoming();
-}
-
-
-OpalLocalConnection *FSEndPoint::CreateConnection(OpalCall & call, void *userData, unsigned options, OpalConnection::StringOptions* stringOptions)
-{
-    FSManager & mgr = (FSManager &) GetManager();
-    switch_core_session_t *fsSession = switch_core_session_request(mgr.GetSwitchInterface(), 
-                                       (switch_caller_profile_t *)userData ? SWITCH_CALL_DIRECTION_OUTBOUND : SWITCH_CALL_DIRECTION_INBOUND, SOF_NONE, NULL);
-    if (fsSession == NULL)
-        return NULL;
-
-    switch_channel_t *fsChannel = switch_core_session_get_channel(fsSession);
-
-    if (fsChannel == NULL) {
-        switch_core_session_destroy(&fsSession);
-        return NULL;
-    }
-
-    return new FSConnection(call, *this, userData, options, stringOptions, (switch_caller_profile_t *)userData, fsSession, fsChannel);
-}
-
-
-///////////////////////////////////////////////////////////////////////
-
-FSCall::FSCall(OpalManager & manager)
-  : OpalCall(manager)
-{
-}
-
-
-PBoolean FSCall::OnSetUp(OpalConnection & connection)
-{
-  // Transfer FS caller_id_number & caller_id_name from the FSConnection
-  // to the protocol connectionm (e.g. H.323) so gets sent correctly
-  // in outgoing packets
-  PSafePtr<FSConnection> local = GetConnectionAs<FSConnection>();
-  if (local != NULL) {
-    PSafePtr<OpalConnection> proto = local->GetOtherPartyConnection();
-    if (proto != NULL) {
-      proto->SetLocalPartyName(local->GetLocalPartyName());
-      proto->SetDisplayName(local->GetDisplayName());
-    }
-  }
-
-  return OpalCall::OnSetUp(connection);
-}
-
-
-///////////////////////////////////////////////////////////////////////
-
-
-FSConnection::FSConnection(OpalCall & call, FSEndPoint & endpoint, void* userData, unsigned options, OpalConnection::StringOptions* stringOptions, switch_caller_profile_t *outbound_profile, switch_core_session_t *fsSession, switch_channel_t *fsChannel)
-  : OpalLocalConnection(call, endpoint, userData, options, stringOptions)
-  , m_endpoint(endpoint)
-  , m_fsSession(fsSession)
-  , m_fsChannel(fsChannel)
-{
-    opal_private_t *tech_pvt;
-
-    tech_pvt = (opal_private_t *) switch_core_session_alloc(m_fsSession, sizeof(*tech_pvt));
-    tech_pvt->me = this;
-    switch_core_session_set_private(m_fsSession, tech_pvt);
-
-    if (outbound_profile != NULL) {
-        SetLocalPartyName(outbound_profile->caller_id_number);
-        SetDisplayName(outbound_profile->caller_id_name);
-
-        switch_caller_profile_t *caller_profile = switch_caller_profile_clone(m_fsSession, outbound_profile);
-        switch_channel_set_caller_profile(m_fsChannel, caller_profile);
-
-        PString name = "opal/";
-        name += outbound_profile->destination_number;
-        switch_channel_set_name(m_fsChannel, name);
-
-        switch_channel_set_state(m_fsChannel, CS_INIT);
-    }
-}
-
-
-bool FSConnection::OnIncoming()
-{
-    if (m_fsSession == NULL) {
-        PTRACE(1, "mod_opal\tSession request failed.");
-        return false;
-    }
-
-    switch_core_session_add_stream(m_fsSession, NULL);
-
-    switch_channel_t *channel = switch_core_session_get_channel(m_fsSession);
-    if (channel == NULL) {
-        PTRACE(1, "mod_opal\tSession does not have a channel");
-        return false;
-    }
-
-    PURL url = GetRemotePartyURL();
-    switch_caller_profile_t *caller_profile = switch_caller_profile_new(switch_core_session_get_pool(m_fsSession),
-                                                                        url.GetUserName(),
-                                                                        /** username */
-                                                                        mod_opal_globals.dialplan,
-                                                                        /** dial plan */
-                                                                        GetRemotePartyName(),
-                                                                        /** caller_id_name */
-                                                                        GetRemotePartyNumber(),
-                                                                        /** caller_id_number */
-                                                                        url.GetHostName(),
-                                                                        /** network addr */
-                                                                        NULL,
-                                                                        /** ANI */
-                                                                        NULL,
-                                                                        /** ANI II */
-                                                                        NULL,
-                                                                        /** RDNIS */
-                                                                        ModuleName,
-                                                                        /** source */
-                                                                        mod_opal_globals.context,
-                                                                        /** set context  */
-                                                                        GetCalledPartyNumber()
-                                                                        /** destination_number */
-                                                                        );
-    if (caller_profile == NULL) {
-        PTRACE(1, "mod_opal\tCould not create caller profile");
-        return false;
-    }
-
-    PTRACE(4, "mod_opal\tCreated switch caller profile:\n"
-           "  username       = " << caller_profile->username << "\n"
-           "  dialplan       = " << caller_profile->dialplan << "\n"
-           "  caller_id_name     = " << caller_profile->caller_id_name << "\n"
-           "  caller_id_number   = " << caller_profile->caller_id_number << "\n"
-           "  network_addr   = " << caller_profile->network_addr << "\n"
-           "  source         = " << caller_profile->source << "\n"
-           "  context        = " << caller_profile->context << "\n" "  destination_number= " << caller_profile->destination_number);
-    switch_channel_set_caller_profile(channel, caller_profile);
-
-    char name[256] = "opal/in:";
-    switch_copy_string(name + 8, caller_profile->destination_number, sizeof(name)-8);
-    switch_channel_set_name(channel, name);
-    switch_channel_set_state(channel, CS_INIT);
-
-    if (switch_core_session_thread_launch(m_fsSession) != SWITCH_STATUS_SUCCESS) {
-        PTRACE(1, "mod_opal\tCould not launch session thread");
-        return false;
-    }
-
-    return true;
-}
-
-
-void FSConnection::OnReleased()
-{
-    opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(m_fsSession);
-    
-    /* so FS on_hangup will not try to deref a landmine */
-    tech_pvt->me = NULL;
-    
-    m_rxAudioOpened.Signal();   // Just in case
-    m_txAudioOpened.Signal();
-    H225_ReleaseCompleteReason dummy;
-    switch_channel_hangup(switch_core_session_get_channel(m_fsSession),
-                          (switch_call_cause_t)H323TranslateFromCallEndReason(GetCallEndReason(), dummy));    
-    OpalLocalConnection::OnReleased();
-}
-
-
-void FSConnection::OnAlerting()
-{
-    switch_channel_mark_ring_ready(m_fsChannel);
-    return OpalLocalConnection::OnAlerting();
-}
-
-PBoolean FSConnection::SetAlerting(const PString & calleeName, PBoolean withMedia)
-{
-    return OpalLocalConnection::SetAlerting(calleeName, withMedia);
-}
-
-
-void FSConnection::OnEstablished()
-{
-    OpalLocalConnection::OnEstablished();
-}
-
-
-PBoolean FSConnection::SendUserInputTone(char tone, unsigned duration)
-{
-    switch_dtmf_t dtmf = { tone, duration };
-    return switch_channel_queue_dtmf(m_fsChannel, &dtmf) == SWITCH_STATUS_SUCCESS;
-}
-
-
-PBoolean FSConnection::SendUserInputString(const PString & value)
-{
-  return OpalConnection::SendUserInputString(value);
-}
-
-
-OpalMediaFormatList FSConnection::GetMediaFormats() const
-{
-    if (m_switchMediaFormats.IsEmpty()) {
-        const_cast<FSConnection *>(this)->SetCodecs();
-    }
-    
-    return m_switchMediaFormats;
-}
-
-
-void FSConnection::SetCodecs()
-{
-    int numCodecs = 0;
-    const switch_codec_implementation_t *codecs[SWITCH_MAX_CODECS];    
-    const char *codec_string = NULL, *abs, *ocodec;
-    char *tmp_codec_string = NULL;
-    char *codec_order[SWITCH_MAX_CODECS];
-    int codec_order_last;
-
-
-    if ((abs = switch_channel_get_variable(m_fsChannel, "absolute_codec_string"))) {
-        codec_string = abs;
-    } else {
-        if ((abs = switch_channel_get_variable(m_fsChannel, "codec_string"))) {
-            codec_string = abs;
-        }
-
-        if ((ocodec = switch_channel_get_variable(m_fsChannel, SWITCH_ORIGINATOR_CODEC_VARIABLE))) {
-            codec_string = switch_core_session_sprintf(m_fsSession, "%s,%s", ocodec, codec_string);
-        }
-    }
-    
-    if (!codec_string) {
-        codec_string = mod_opal_globals.codec_string;
-    }
-
-    if (codec_string) {
-        if ((tmp_codec_string = strdup(codec_string))) {
-            codec_order_last = switch_separate_string(tmp_codec_string, ',', codec_order, SWITCH_MAX_CODECS);
-            numCodecs = switch_loadable_module_get_codecs_sorted(codecs, SWITCH_MAX_CODECS, codec_order, codec_order_last);
-            
-        }
-    } else {
-        numCodecs = switch_loadable_module_get_codecs(codecs, sizeof(codecs) / sizeof(codecs[0]));
-    }
-    
-    for (int i = 0; i < numCodecs; i++) {
-        const switch_codec_implementation_t *codec = codecs[i];
-
-        // See if we have a match by PayloadType/rate/name
-        OpalMediaFormat switchFormat((RTP_DataFrame::PayloadTypes)codec->ianacode,
-                                     codec->samples_per_second,
-                                     codec->iananame);
-        if (!switchFormat.IsValid()) {
-            // See if we have a match by name alone
-            switchFormat = codec->iananame;
-            if (!switchFormat.IsValid()) {
-              PTRACE(2, "mod_opal\tCould not match FS codec " << codec->iananame << " to OPAL media format.");
-              continue;
-            }
-        }
-        
-
-        // Did we match or create a new media format?
-        if (switchFormat.IsValid() && codec->codec_type == SWITCH_CODEC_TYPE_AUDIO) {
-            PTRACE(2, "mod_opal\tMatched FS codec " << codec->iananame << " to OPAL media format " << switchFormat);
-
-            // Calculate frames per packet, do not use codec->codec_frames_per_packet as that field
-            // has slightly different semantics when used in streamed codecs such as G.711
-            int fpp = codec->samples_per_packet/switchFormat.GetFrameTime();
-
-            /* Set the frames/packet to maximum of what is in the FS table. The OPAL negotiations will
-               drop the value from there. This might fail if there are "holes" in the FS table, e.g.
-               if for some reason G.723.1 has 30ms and 90ms but not 60ms, then the OPAL negotiations
-               could end up with 60ms and the codec cannot be created. The "holes" are unlikely in
-               all but streamed codecs such as G.711, where it is theoretically possible for OPAL to
-               come up with 32ms and there is only 30ms and 40ms in the FS table. We deem these
-               scenarios succifiently rare that we can safely ignore them ... for now. */
-
-            if (fpp > switchFormat.GetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption())) {
-                switchFormat.SetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption(), fpp);
-            }
-
-            if (fpp > switchFormat.GetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption())) {
-                switchFormat.SetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption(), fpp);
-            }
-        }
-
-        m_switchMediaFormats += switchFormat;
-    }
-    
-    switch_safe_free(tmp_codec_string);
-}
-
-
-OpalMediaStream *FSConnection::CreateMediaStream(const OpalMediaFormat & mediaFormat, unsigned sessionID, PBoolean isSource)
-{
-    return new FSMediaStream(*this, mediaFormat, sessionID, isSource);
-}
-
-
-PBoolean FSConnection::OnOpenMediaStream(OpalMediaStream & stream)
-{
-    if (!OpalConnection::OnOpenMediaStream(stream)) {
-        return false;
-    }
-
-    if (stream.GetMediaFormat().GetMediaType() != OpalMediaType::Audio()) {
-        return true;
-    }
-
-    if (stream.IsSource()) {
-        m_rxAudioOpened.Signal();
-    } else {
-        m_txAudioOpened.Signal();
-    }
-
-    if (GetMediaStream(stream.GetSessionID(), stream.IsSink()) != NULL) {
-        // Have open media in both directions.
-        if (GetPhase() == AlertingPhase) {
-            switch_channel_mark_pre_answered(m_fsChannel);
-        } else if (GetPhase() < ReleasingPhase) {
-            switch_channel_mark_answered(m_fsChannel);
-        }
-    }
-
-    return true;
-}
-
-
-switch_status_t FSConnection::on_init()
-{
-    switch_channel_t *channel = switch_core_session_get_channel(m_fsSession);
-    if (channel == NULL) {
-        return SWITCH_STATUS_FALSE;
-    }
-
-    PTRACE(3, "mod_opal\tStarted routing for connection " << *this);
-    switch_channel_set_state(channel, CS_ROUTING);
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::on_routing()
-{
-    PTRACE(3, "mod_opal\tRouting connection " << *this);
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::on_execute()
-{
-    PTRACE(3, "mod_opal\tExecuting connection " << *this);
-    return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t on_destroy(switch_core_session_t *session)
-{
-    //switch_channel_t *channel = switch_core_session_get_channel(session);
-    opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session);
-    
-    if (tech_pvt) {
-    if (tech_pvt->read_codec.implementation) {
-        switch_core_codec_destroy(&tech_pvt->read_codec);
-    }
-
-    if (tech_pvt->write_codec.implementation) {
-        switch_core_codec_destroy(&tech_pvt->write_codec);
-    }
-
-    if (tech_pvt->vid_read_codec.implementation) {
-        switch_core_codec_destroy(&tech_pvt->vid_read_codec);
-    }
-
-    if (tech_pvt->vid_write_codec.implementation) {
-        switch_core_codec_destroy(&tech_pvt->vid_write_codec);
-    }
-
-    if (tech_pvt->read_timer.timer_interface) {
-        switch_core_timer_destroy(&tech_pvt->read_timer);
-    }
-
-    if (tech_pvt->vid_read_timer.timer_interface) {
-        switch_core_timer_destroy(&tech_pvt->vid_read_timer);
-    }
-    }
-
-    return SWITCH_STATUS_SUCCESS;
-}
-
-/* this function has to be called with the original session beause the FSConnection might already be destroyed and we 
-   will can't have it be a method of a dead object
- */
-static switch_status_t on_hangup(switch_core_session_t *session)
-{
-    switch_channel_t *channel = switch_core_session_get_channel(session);
-    opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session);
-    
-    /* if this is still here it was our idea to hangup not opal's */
-    if (tech_pvt->me) {
-        Q931::CauseValues cause = (Q931::CauseValues)switch_channel_get_cause_q850(channel);
-        tech_pvt->me->SetQ931Cause(cause);
-        tech_pvt->me->ClearCallSynchronous(NULL, H323TranslateToCallEndReason(cause, UINT_MAX));
-        tech_pvt->me = NULL;
-    }
-
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::on_exchange_media()
-{
-    PTRACE(3, "mod_opal\tLoopback on connection " << *this);
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::on_soft_execute()
-{
-    PTRACE(3, "mod_opal\tTransmit on connection " << *this);
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::kill_channel(int sig)
-{
-    PTRACE(3, "mod_opal\tKill " << sig << " on connection " << *this);
-
-    switch (sig) {
-    case SWITCH_SIG_BREAK:
-        break;
-    case SWITCH_SIG_KILL:
-        m_rxAudioOpened.Signal();
-        m_txAudioOpened.Signal();
-        break;
-    default:
-        break;
-    }
-
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::send_dtmf(const switch_dtmf_t *dtmf)
-{
-    OnUserInputTone(dtmf->digit, dtmf->duration);
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::receive_message(switch_core_session_message_t *msg)
-{
-    switch_channel_t *channel = switch_core_session_get_channel(m_fsSession);
-
-
-    /*
-      SWITCH_MESSAGE_INDICATE_PROGRESS:  establish early media now and return SWITCH_STATUS_FALSE if you can't
-      SWITCH_MESSAGE_INDICATE_ANSWER:  answer and set up media now if it's not already and return SWITCH_STATUS_FALSE if you can't
-
-      Neither message means anything on an outbound call....
-
-      It would only happen if someone called switch_channel_answer() instead of switch_channel_mark_answered() on an outbound call.
-      it should not do anything if someone does it by accident somewhere hense this in both cases:
-
-      if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
-      return SWITCH_STATUS_FALSE;
-      }
-
-
-      When we get these messages the core will trust that you have triggered FSMediaStream::Open and are ready for media if we do not
-      have media we MUST return SWITCH_STATUS_FALSE or it will cause a CRASH.
-
-
-
-    */
-    switch (msg->message_id) {
-    case SWITCH_MESSAGE_INDICATE_BRIDGE:
-    case SWITCH_MESSAGE_INDICATE_UNBRIDGE:
-    case SWITCH_MESSAGE_INDICATE_AUDIO_SYNC:
-        switch_channel_set_private_flag(channel, CF_NEED_FLUSH);
-        break;
-
-    case SWITCH_MESSAGE_INDICATE_RINGING:
-    case SWITCH_MESSAGE_INDICATE_PROGRESS:
-    case SWITCH_MESSAGE_INDICATE_ANSWER:
-        {
-            switch_caller_profile_t * profile = switch_channel_get_caller_profile(channel);
-            if (profile != NULL && profile->caller_extension != NULL)
-            {
-                PSafePtr<OpalConnection> other = GetOtherPartyConnection();
-                if (other != NULL) {
-                    other->SetLocalPartyName(profile->caller_extension->extension_number);
-                    other->SetDisplayName(profile->caller_extension->extension_name);
-                }
-                SetLocalPartyName(profile->caller_extension->extension_number);
-                SetDisplayName(profile->caller_extension->extension_name);
-            }
-        }
-        break;
-
-    default:
-        break;
-    }
-
-    switch (msg->message_id) {
-    case SWITCH_MESSAGE_INDICATE_RINGING:
-        SetPhase(OpalConnection::AlertingPhase);
-        OnAlerting();
-        break;
-
-    case SWITCH_MESSAGE_INDICATE_DEFLECT:
-    {
-        PSafePtr<OpalConnection> other = GetOtherPartyConnection();
-        if (other != NULL)
-          other->TransferConnection(msg->string_arg);
-        break;
-    }
-
-    case SWITCH_MESSAGE_INDICATE_PROGRESS:
-    case SWITCH_MESSAGE_INDICATE_ANSWER:
-        {
-            int fixed = 0;
-            
-            if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
-                return SWITCH_STATUS_FALSE;
-            }
-
-            if (msg->message_id == SWITCH_MESSAGE_INDICATE_PROGRESS) {
-                if (fixed) {
-                    /* this should send alerting + media and wait for it to be established and return SUCCESS or FAIL
-                       depending on if media was able to be established.  Need code to tell the other side we want early media here.
-                    */
-                    GetCall().OpenSourceMediaStreams(*this, OpalMediaType::Audio());
-                    SetPhase(OpalConnection::AlertingPhase);
-                    /* how do i say please establish early media ? */
-                    OnAlerting();
-                } else {
-                    /* hack to avoid getting stuck, pre_answer will imply answer */
-                    OnConnectedInternal();
-                }
-            } else {
-                OnConnectedInternal();
-            }
-
-            // Wait for media
-            PTRACE(2, "mod_opal\tAwaiting media start on connection " << *this);
-            m_rxAudioOpened.Wait();
-            m_txAudioOpened.Wait();
-            
-            if (GetPhase() >= ReleasingPhase) {
-                // Call got aborted
-                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(m_fsSession), SWITCH_LOG_ERROR, "Call abandoned!\n");
-                return SWITCH_STATUS_FALSE;
-            }
-
-            PTRACE(4, "mod_opal\tMedia started on connection " << *this);
-
-            if (msg->message_id == SWITCH_MESSAGE_INDICATE_PROGRESS) {
-                if (!switch_channel_test_flag(m_fsChannel, CF_EARLY_MEDIA)) {
-                    switch_channel_mark_pre_answered(m_fsChannel);
-                }
-            } else {
-                if (!switch_channel_test_flag(m_fsChannel, CF_EARLY_MEDIA)) {
-                    switch_channel_mark_answered(m_fsChannel);
-                }
-            }
-
-        }
-        break;
-
-    default:
-        PTRACE(3, "mod_opal\tReceived message " << msg->message_id << " on connection " << *this);
-    }
-
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::receive_event(switch_event_t *event)
-{
-    PTRACE(3, "mod_opal\tReceived event " << event->event_id << " on connection " << *this);
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::state_change()
-{
-    PTRACE(3, "mod_opal\tState changed on connection " << *this);
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSConnection::read_audio_frame(switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
-{
-    return read_frame(OpalMediaType::Audio(), frame, flags);
-}
-
-
-switch_status_t FSConnection::write_audio_frame(switch_frame_t *frame, switch_io_flag_t flags, int stream_id)
-{
-    return write_frame(OpalMediaType::Audio(), frame, flags);
-}
-
-
-switch_status_t FSConnection::read_video_frame(switch_frame_t **frame, switch_io_flag_t flag, int stream_id)
-{
-    return read_frame(OpalMediaType::Video(), frame, flag);
-}
-
-
-switch_status_t FSConnection::write_video_frame(switch_frame_t *frame, switch_io_flag_t flag, int stream_id)
-{
-    return write_frame(OpalMediaType::Video(), frame, flag);
-}
-
-
-switch_status_t FSConnection::read_frame(const OpalMediaType & mediaType, switch_frame_t **frame, switch_io_flag_t flags)
-{
-    PSafePtr < FSMediaStream > stream = PSafePtrCast < OpalMediaStream, FSMediaStream > (GetMediaStream(mediaType, false));
-    return stream != NULL ? stream->read_frame(frame, flags) : SWITCH_STATUS_FALSE;
-}
-
-
-switch_status_t FSConnection::write_frame(const OpalMediaType & mediaType, const switch_frame_t *frame, switch_io_flag_t flags)
-{
-    PSafePtr < FSMediaStream > stream = PSafePtrCast < OpalMediaStream, FSMediaStream > (GetMediaStream(mediaType, true));
-    return stream != NULL ? stream->write_frame(frame, flags) : SWITCH_STATUS_FALSE;
-}
-
-
-///////////////////////////////////////////////////////////////////////
-
-FSMediaStream::FSMediaStream(FSConnection & conn, const OpalMediaFormat & mediaFormat, unsigned sessionID, bool isSource)
-    : OpalMediaStream(conn, mediaFormat, sessionID, isSource)
-    , m_fsSession(conn.GetSession())
-    , m_readRTP(0, 512)
-    , m_callOnStart(true)
-{
-    memset(&m_readFrame, 0, sizeof(m_readFrame));
-    m_readFrame.codec = m_switchCodec;
-    m_readFrame.flags = SFF_RAW_RTP;
-}
-
-
-PBoolean FSMediaStream::Open()
-{
-    opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(m_fsSession);
-
-    if (IsOpen()) {
-        return true;
-    }
-
-    bool isAudio;
-    if (mediaFormat.GetMediaType() == OpalMediaType::Audio()) {
-        isAudio = true;
-    } else if (mediaFormat.GetMediaType() == OpalMediaType::Video()) {
-        isAudio = false;
-    } else {
-        return OpalMediaStream::Open();
-    }
-
-    m_fsChannel = switch_core_session_get_channel(m_fsSession);
-    
-    int ptime = mediaFormat.GetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption()) * mediaFormat.GetFrameTime() / mediaFormat.GetTimeUnits();
-
-
-    if (IsSink()) {
-        m_switchCodec = isAudio ? &tech_pvt->read_codec : &tech_pvt->vid_read_codec;
-        m_switchTimer = isAudio ? &tech_pvt->read_timer : &tech_pvt->vid_read_timer;
-    } else {
-        m_switchCodec = isAudio ? &tech_pvt->write_codec : &tech_pvt->vid_write_codec;
-    }
-
-    // The following is performed on two different instances of this object.
-    if (switch_core_codec_init(m_switchCodec, mediaFormat.GetEncodingName(), NULL, // FMTP
-                               mediaFormat.GetClockRate(), ptime, 1,  // Channels
-                               SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,   // Settings
-                               switch_core_session_get_pool(m_fsSession)) != SWITCH_STATUS_SUCCESS) {
-        // Could not select a codecs using negotiated frames/packet, so try using default.
-        if (switch_core_codec_init(m_switchCodec, mediaFormat.GetEncodingName(), NULL, // FMTP
-                                   mediaFormat.GetClockRate(), 0, 1,  // Channels
-                                   SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,   // Settings
-                                   switch_core_session_get_pool(m_fsSession)) != SWITCH_STATUS_SUCCESS) {
-            PTRACE(1, "mod_opal  " << switch_channel_get_name(m_fsChannel)<< " Cannot initialise " << (IsSink()? "read" : "write") << ' '
-                   << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this);
-            switch_channel_hangup(m_fsChannel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);
-            return false;
-        }
-        PTRACE(2, "mod_opal " << switch_channel_get_name(m_fsChannel)<< " Unsupported ptime of " << ptime << " on " << (IsSink()? "read" : "write") << ' '
-               << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this);
-    }
-
-    PTRACE(1, "mod_opal " << switch_channel_get_name(m_fsChannel)<< " initialise " << 
-           switch_channel_get_name(m_fsChannel) << (IsSink()? "read" : "write") << ' '
-           << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this);
-
-    if (IsSink()) {
-        m_readFrame.rate = mediaFormat.GetClockRate();
-
-        if (isAudio) {
-            switch_core_session_set_read_codec(m_fsSession, m_switchCodec);
-            if (switch_core_timer_init(m_switchTimer,
-                                       "soft",
-                                       m_switchCodec->implementation->microseconds_per_packet / 1000,
-                                       m_switchCodec->implementation->samples_per_packet,
-                                       switch_core_session_get_pool(m_fsSession)) != SWITCH_STATUS_SUCCESS) {
-                switch_core_codec_destroy(m_switchCodec);
-                m_switchCodec = NULL;
-                return false;
-            }
-        } else {
-            switch_core_session_set_video_read_codec(m_fsSession, m_switchCodec);
-            switch_channel_set_flag(m_fsChannel, CF_VIDEO);
-        }
-    } else {
-        if (isAudio) {
-            switch_core_session_set_write_codec(m_fsSession, m_switchCodec);
-        } else {
-            switch_core_session_set_video_write_codec(m_fsSession, m_switchCodec);
-            switch_channel_set_flag(m_fsChannel, CF_VIDEO);
-        }
-    }
-
-    PTRACE(3, "mod_opal\tSet " << (IsSink()? "read" : "write") << ' '
-           << mediaFormat.GetMediaType() << " codec to << " << mediaFormat << " for connection " << *this);
-
-    return OpalMediaStream::Open();
-}
-
-
-PBoolean FSMediaStream::Close()
-{
-    if (!IsOpen())
-        return false;
-
-    /* forget these FS will properly destroy them for us */
-
-    m_switchTimer = NULL;
-    m_switchCodec = NULL;
-
-    return OpalMediaStream::Close();
-}
-
-
-PBoolean FSMediaStream::IsSynchronous() const
-{
-    return true;
-}
-
-
-PBoolean FSMediaStream::RequiresPatchThread(OpalMediaStream *) const
-{
-    return false;
-}
-
-bool FSMediaStream::CheckPatchAndLock()
-{
-    if (GetConnection().GetPhase() >= GetConnection().ReleasingPhase || !IsOpen())
-        return false;
-
-    if (LockReadWrite()) {
-        if (!GetPatch() || !IsOpen()) {
-            UnlockReadWrite();
-            return false;
-        }
-        return true;
-    } else {
-        return false;
-    }
-}
-
-switch_status_t FSMediaStream::read_frame(switch_frame_t **frame, switch_io_flag_t flags)
-{
-
-    if (!m_switchCodec) {
-        return SWITCH_STATUS_FALSE;
-    }
-
-    if (m_callOnStart) {
-        /*
-          There is a race here... sometimes we make it here and GetPatch() is NULL
-          if we wait it shows up in 1ms, maybe there is a better way to wait.
-          
-        */
-        while(!GetPatch()) {
-            if (!m_fsChannel || !switch_channel_up(m_fsChannel)) {
-                return SWITCH_STATUS_FALSE;
-            }
-            switch_cond_next();
-        }
-        if (CheckPatchAndLock()) {
-            GetPatch()->OnStartMediaPatch();
-            m_callOnStart = false;
-            UnlockReadWrite();
-        } else {
-            return SWITCH_STATUS_FALSE; 
-        }
-    }
-
-    m_readFrame.flags = 0;
-
-    /*
-    while (switch_channel_ready(m_fsChannel)) {
-        if (CheckPatchAndLock()) {
-            if (!GetPatch()->GetSource().ReadPacket(m_readRTP)) {
-                UnlockReadWrite();
-                return SWITCH_STATUS_FALSE;
-            }            
-            UnlockReadWrite();
-        } else {
-            return SWITCH_STATUS_FALSE; 
-        }
-        
-        if ((m_readFrame.datalen = m_readRTP.GetPayloadSize()) || switch_core_timer_check(&m_switchTimer, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
-            if (m_readFrame.datalen) {
-            } else {
-                m_readFrame.flags = SFF_CNG;
-            }
-            break;
-        }
-
-        switch_yield(1000);
-    }
-    */
-
-    if (switch_channel_test_private_flag(m_fsChannel, CF_NEED_FLUSH)) {
-        switch_channel_clear_private_flag(m_fsChannel, CF_NEED_FLUSH);
-        for(;;) {
-            if (CheckPatchAndLock()) {
-                if (!GetPatch()->GetSource().ReadPacket(m_readRTP)) {
-                    UnlockReadWrite();
-                    return SWITCH_STATUS_FALSE;
-                }            
-                UnlockReadWrite();
-            } else {
-                return SWITCH_STATUS_FALSE; 
-            }
-
-            if (!m_readRTP.GetPayloadSize()) {
-                m_readFrame.flags = SFF_CNG;
-                break;
-            }
-        }
-    } else {
-
-        if (CheckPatchAndLock()) {
-            if (!m_switchTimer || !GetPatch()->GetSource().ReadPacket(m_readRTP)) {
-                UnlockReadWrite();
-                return SWITCH_STATUS_FALSE;
-            }            
-            UnlockReadWrite();
-        } else {
-            return SWITCH_STATUS_FALSE; 
-        }
-    
-        switch_core_timer_next(m_switchTimer);
-    
-        if (!(m_readFrame.datalen = m_readRTP.GetPayloadSize())) {
-            m_readFrame.flags = SFF_CNG;
-        }
-    }
-
-    if (!switch_channel_ready(m_fsChannel)) {
-        return SWITCH_STATUS_FALSE;
-    }
-
-    if (!switch_core_codec_ready(m_switchCodec)) {
-        return SWITCH_STATUS_FALSE;
-    }
-
-    //switch_core_timer_step(&m_switchTimer);
-
-    if (m_readFrame.payload == RTP_DataFrame::CN || m_readFrame.payload == RTP_DataFrame::Cisco_CN) {
-        m_readFrame.flags = SFF_CNG;
-    }
-
-    if (m_readFrame.flags & SFF_CNG) {
-        m_readFrame.buflen = sizeof(m_buf);
-        m_readFrame.data = m_buf;
-        m_readFrame.packet = NULL;
-        m_readFrame.packetlen = 0;
-        m_readFrame.timestamp = 0;
-        m_readFrame.m = SWITCH_FALSE;
-        m_readFrame.seq = 0;
-        m_readFrame.ssrc = 0;
-        m_readFrame.codec = m_switchCodec;
-    } else {
-        m_readFrame.buflen = m_readRTP.GetSize();
-        m_readFrame.data = m_readRTP.GetPayloadPtr();
-        m_readFrame.packet = m_readRTP.GetPointer();
-        m_readFrame.packetlen = m_readRTP.GetHeaderSize() + m_readFrame.datalen;
-        m_readFrame.payload = (switch_payload_t) m_readRTP.GetPayloadType();
-        m_readFrame.timestamp = m_readRTP.GetTimestamp();
-        m_readFrame.m = (switch_bool_t) m_readRTP.GetMarker();
-        m_readFrame.seq = m_readRTP.GetSequenceNumber();
-        m_readFrame.ssrc = m_readRTP.GetSyncSource();
-        m_readFrame.codec = m_switchCodec;
-    }
-
-    *frame = &m_readFrame;
-
-    return SWITCH_STATUS_SUCCESS;
-}
-
-
-switch_status_t FSMediaStream::write_frame(const switch_frame_t *frame, switch_io_flag_t flags)
-{
-    if (!switch_channel_ready(m_fsChannel)) {
-        return SWITCH_STATUS_FALSE;
-    }
-
-    if (m_callOnStart) {
-        if (CheckPatchAndLock()) {
-            GetPatch()->OnStartMediaPatch();
-            m_callOnStart = false;
-            UnlockReadWrite();
-        } else {
-            return SWITCH_STATUS_FALSE; 
-        }
-    }
-
-    if ((frame->flags & SFF_CNG)) {
-        return SWITCH_STATUS_SUCCESS;
-    }
-
-    if ((frame->flags & SFF_RAW_RTP) != 0) {
-        RTP_DataFrame rtp((const BYTE *) frame->packet, frame->packetlen, false);
-
-        if (CheckPatchAndLock()) {
-            if (GetPatch()->PushFrame(rtp)) {
-                UnlockReadWrite();
-                return SWITCH_STATUS_SUCCESS;
-            }
-            UnlockReadWrite();
-        } else {
-            return SWITCH_STATUS_FALSE; 
-        }
-    } 
-    
-    /* If we reach this code it means a call to an ivr or something else that does not generate timestamps
-       Its possible that frame->timestamp is set but not guarenteed and is best ignored for the time being.
-       We are probably relying on the rtp stack to generate the timestamp and ssrc for us at this point.
-       As a quick hack I am going to keep a sample counter and increment it by frame->samples but it would be 
-       better if we could engage whatever it is in opal that makes it generate the timestamp.
-     */
-
-    RTP_DataFrame rtp(frame->datalen);
-    rtp.SetPayloadType(mediaFormat.GetPayloadType());
-
-    m_timeStamp += frame->samples;
-    rtp.SetTimestamp(m_timeStamp);
-    
-    //rtp.SetTimestamp(frame->timestamp);
-    //rtp.SetSyncSource(frame->ssrc);
-    //rtp.SetMarker(frame->m);
-
-    memcpy(rtp.GetPayloadPtr(), frame->data, frame->datalen);
-
-    if (CheckPatchAndLock()) {
-        if (GetPatch()->PushFrame(rtp)) {
-            UnlockReadWrite();
-            return SWITCH_STATUS_SUCCESS;
-        }
-        UnlockReadWrite();
-    } else {
-        return SWITCH_STATUS_FALSE; 
-    }
-
-
-    return SWITCH_STATUS_FALSE;
-}
-
-
-/* For Emacs:
- * Local Variables:
- * mode:c
- * indent-tabs-mode:nil
- * tab-width:4
- * c-basic-offset:4
- * End:
- * For VIM:
- * vim:set softtabstop=4 shiftwidth=4 tabstop=4:s:
- */
+/* Opal endpoint interface for Freeswitch Modular Media Switching Software Library /\r
+ * Soft-Switch Application\r
+ *\r
+ * Version: MPL 1.1\r
+ *\r
+ * Copyright (c) 2007 Tuyan Ozipek (tuyanozipek@gmail.com)\r
+ * Copyright (c) 2008-2012 Vox Lucida Pty. Ltd. (robertj@voxlucida.com.au)\r
+ *\r
+ * The contents of this file are subject to the Mozilla Public License Version\r
+ * 1.1 (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ * http://www.mozilla.org/MPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS IS" basis,\r
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\r
+ * for the specific language governing rights and limitations under the\r
+ * License.\r
+ *\r
+ * Contributor(s):\r
+ * Tuyan Ozipek   (tuyanozipek@gmail.com)\r
+ * Lukasz Zwierko (lzwierko@gmail.com)\r
+ * Robert Jongbloed (robertj@voxlucida.com.au)\r
+ *\r
+ */\r
+\r
+#include "mod_opal.h"\r
+#include <opal/patch.h>\r
+#include <rtp/rtp.h>\r
+#include <h323/h323pdu.h>\r
+#include <h323/gkclient.h>\r
+\r
+\r
+/* FreeSWITCH does not correctly handle an H.323 subtely, that is that a\r
+   MAXIMUM audio frames per packet is nototiated, and there is no\r
+   requirement for the remote to actually send that many. So, in say GSM, we\r
+   negotiate up to 3 frames or 60ms of data and the remote actually sends one\r
+   (20ms) frame per packet. Perfectly legal but blows up the media handling\r
+   in FS.\r
+\r
+   Eventually we will get around to bundling the packets, but not yet. This\r
+   compile flag will just force one frame/packet for all audio codecs.\r
+ */\r
+#define IMPLEMENT_MULTI_FAME_AUDIO 0\r
+\r
+\r
+static switch_call_cause_t create_outgoing_channel(switch_core_session_t   *session,\r
+                                                   switch_event_t          *var_event,\r
+                                                   switch_caller_profile_t *outbound_profile,\r
+                                                   switch_core_session_t  **new_session,\r
+                                                   switch_memory_pool_t   **pool,\r
+                                                   switch_originate_flag_t  flags,\r
+                                                   switch_call_cause_t     *cancel_cause);\r
+\r
+\r
+static FSProcess *opal_process = NULL;\r
+\r
+\r
+static PConstString const ModuleName("opal");\r
+static char const ConfigFile[] = "opal.conf";\r
+\r
+\r
+static switch_io_routines_t opalfs_io_routines = {\r
+    /*.outgoing_channel */ create_outgoing_channel,\r
+    /*.read_frame */ FSConnection::read_audio_frame,\r
+    /*.write_frame */ FSConnection::write_audio_frame,\r
+    /*.kill_channel */ FSConnection::kill_channel,\r
+    /*.send_dtmf */ FSConnection::send_dtmf,\r
+    /*.receive_message */ FSConnection::receive_message,\r
+    /*.receive_event */ FSConnection::receive_event,\r
+    /*.state_change */ FSConnection::state_change,\r
+    /*.read_video_frame */ FSConnection::read_video_frame,\r
+    /*.write_video_frame */ FSConnection::write_video_frame\r
+};\r
+\r
+static switch_state_handler_table_t opalfs_event_handlers = {\r
+    /*.on_init */ FSConnection::on_init,\r
+    /*.on_routing */ FSConnection::on_routing,\r
+    /*.on_execute */ FSConnection::on_execute,\r
+    /*.on_hangup */ FSConnection::on_hangup,\r
+    /*.on_exchange_media */ FSConnection::on_exchange_media,\r
+    /*.on_soft_execute */ FSConnection::on_soft_execute,\r
+    /*.on_consume_media*/ NULL,\r
+    /*.on_hibernate*/ NULL,\r
+    /*.on_reset*/ NULL,\r
+    /*.on_park*/ NULL,\r
+    /*.on_reporting*/ NULL,\r
+    /*.on_destroy*/ FSConnection::on_destroy\r
+};\r
+\r
+\r
+SWITCH_BEGIN_EXTERN_C\r
+/*******************************************************************************/\r
+\r
+SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load);\r
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_opal_shutdown);\r
+SWITCH_MODULE_DEFINITION(mod_opal, mod_opal_load, mod_opal_shutdown, NULL);\r
+\r
+SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load)\r
+{\r
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Starting loading mod_opal\n");\r
+\r
+    /* Prevent the loading of OPAL codecs via "plug ins", this is a directory\r
+       full of DLLs that will be loaded automatically. */\r
+    putenv((char *)"PTLIBPLUGINDIR=/no/thanks");\r
+\r
+\r
+    *module_interface = switch_loadable_module_create_module_interface(pool, modname);\r
+    if (!*module_interface) {\r
+        return SWITCH_STATUS_MEMERR;\r
+    }\r
+\r
+    opal_process = new FSProcess();\r
+    if (opal_process == NULL) {\r
+        return SWITCH_STATUS_MEMERR;\r
+    }\r
+\r
+    if (opal_process->Initialise(*module_interface)) {\r
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Opal manager initialized and running\n");\r
+        //unloading causes a seg in linux\r
+        //return SWITCH_STATUS_UNLOAD;\r
+        return SWITCH_STATUS_SUCCESS;\r
+    }\r
+\r
+    delete opal_process;\r
+    opal_process = NULL;\r
+    return SWITCH_STATUS_FALSE;\r
+}\r
+\r
+\r
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_opal_shutdown)\r
+{\r
+    delete opal_process;\r
+    opal_process = NULL;\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+SWITCH_END_EXTERN_C\r
+/*******************************************************************************/\r
+\r
+///////////////////////////////////////////////////////////////////////\r
+\r
+#if PTRACING\r
+\r
+class FSTrace : public std::ostream\r
+{\r
+private:\r
+  class Buffer : public std::stringbuf\r
+  {\r
+    virtual int sync()\r
+    {\r
+      std::string s = str();\r
+      if (s.empty())\r
+        return 0;\r
+\r
+      //Due to explicit setting of flags we know exactly what we are getting\r
+  #define THREAD_ID_INDEX 2\r
+  #define FILE_NAME_INDEX 3\r
+  #define FILE_LINE_INDEX 4\r
+#if PTLIB_CHECK_VERSION(2,11,1)\r
+  #define CONTEXT_ID_REGEX "([0-9]+|- - - - - - -)\t"\r
+  #define LOG_PRINTF_FORMAT "{%s,%s} %s"\r
+  #define FULL_TEXT_INDEX 6\r
+#else\r
+  #define CONTEXT_ID_REGEX\r
+  #define LOG_PRINTF_FORMAT "{%s} %s"\r
+  #define FULL_TEXT_INDEX 5\r
+#endif\r
+      PStringArray fields(7);\r
+      static PRegularExpression logRE("^([0-9]+)\t *(.+)\t *([^(]+)\\(([0-9]+)\\)\t"CONTEXT_ID_REGEX"(.*)",\r
+                                      PRegularExpression::Extended);\r
+      if (!logRE.Execute(s.c_str(), fields)) {\r
+        fields[1] = "4";\r
+        fields[THREAD_ID_INDEX] = "unknown";\r
+        fields[FILE_NAME_INDEX] = __FILE__;\r
+        fields[FILE_LINE_INDEX] = __LINE__;\r
+        fields[FULL_TEXT_INDEX] = s;\r
+      }\r
+\r
+      switch_log_level_t level;\r
+      switch (fields[1].AsUnsigned()) {\r
+        case 0 :\r
+          level = SWITCH_LOG_ALERT;\r
+          break;\r
+        case 1 :\r
+          level = SWITCH_LOG_ERROR;\r
+          break;\r
+        case 2 :\r
+          level = SWITCH_LOG_WARNING;\r
+          break;\r
+        case 3 :\r
+          level = SWITCH_LOG_INFO;\r
+          break;\r
+        default :\r
+          level = SWITCH_LOG_DEBUG;\r
+          break;\r
+      }\r
+\r
+      fields[4].Replace("\t", " ", true);\r
+#if PTLIB_CHECK_VERSION(2,11,1)\r
+      fields[5].Replace("- - - - - - -", "-"),\r
+#endif\r
+      switch_log_printf(SWITCH_CHANNEL_ID_LOG,\r
+                        fields[FILE_NAME_INDEX],\r
+                        "PTLib-OPAL",\r
+                        fields[FILE_LINE_INDEX].AsUnsigned(),\r
+                        NULL,\r
+                        level,\r
+                        LOG_PRINTF_FORMAT,\r
+                        fields[THREAD_ID_INDEX].GetPointer(),\r
+#if PTLIB_CHECK_VERSION(2,11,1)\r
+                        fields[5].GetPointer(),\r
+#endif\r
+                        fields[FULL_TEXT_INDEX].GetPointer());\r
+\r
+      // Reset string\r
+      str(std::string());\r
+      return 0;\r
+    }\r
+  } buffer;\r
+\r
+public:\r
+  FSTrace()\r
+    : ostream(&buffer)\r
+  {\r
+  }\r
+};\r
+\r
+#endif // PTRACING\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////\r
+\r
+FSProcess::FSProcess()\r
+  : PLibraryProcess("Vox Lucida Pty. Ltd.", MODNAME, 1, 1, BetaCode, 1)\r
+  , m_manager(NULL)\r
+{\r
+}\r
+\r
+\r
+FSProcess::~FSProcess()\r
+{\r
+  delete m_manager;\r
+#if PTRACING\r
+    PTrace::SetStream(NULL); // This will delete the FSTrace object\r
+#endif\r
+}\r
+\r
+\r
+bool FSProcess::Initialise(switch_loadable_module_interface_t *iface)\r
+{\r
+    m_manager = new FSManager();\r
+    return m_manager != NULL && m_manager->Initialise(iface);\r
+}\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////\r
+\r
+FSManager::FSManager()\r
+  : m_context("default")\r
+  , m_dialplan("XML")\r
+{\r
+    // These are deleted by the OpalManager class, no need to have destructor\r
+    m_h323ep = new H323EndPoint(*this);\r
+    m_iaxep = new IAX2EndPoint(*this);\r
+    m_fsep = new FSEndPoint(*this);\r
+}\r
+\r
+\r
+bool FSManager::Initialise(switch_loadable_module_interface_t *iface)\r
+{\r
+    ReadConfig(false);\r
+\r
+    m_FreeSwitch = (switch_endpoint_interface_t *) switch_loadable_module_create_interface(iface, SWITCH_ENDPOINT_INTERFACE);\r
+    m_FreeSwitch->interface_name = ModuleName;\r
+    m_FreeSwitch->io_routines = &opalfs_io_routines;\r
+    m_FreeSwitch->state_handler = &opalfs_event_handlers;\r
+\r
+    silenceDetectParams.m_mode = OpalSilenceDetector::NoSilenceDetection;\r
+\r
+    if (m_listeners.empty()) {\r
+        m_h323ep->StartListener("");\r
+    } else {\r
+        for (std::list < FSListener >::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it) {\r
+            if (!m_h323ep->StartListener(OpalTransportAddress(it->m_address, it->m_port))) {\r
+                PTRACE(2, "mod_opal\tCannot start listener for " << it->m_name);\r
+            }\r
+        }\r
+    }\r
+\r
+    AddRouteEntry("h323:.* = local:<da>");  // config option for direct routing\r
+    AddRouteEntry("iax2:.* = local:<da>");  // config option for direct routing\r
+    AddRouteEntry("local:.* = h323:<da>");  // config option for direct routing\r
+\r
+    // Make sure all known codecs are instantiated,\r
+    // these are ones we know how to translate into H.323 capabilities\r
+    GetOpalG728();\r
+    GetOpalG729();\r
+    GetOpalG729A();\r
+    GetOpalG729B();\r
+    GetOpalG729AB();\r
+    GetOpalG7231_6k3();\r
+    GetOpalG7231_5k3();\r
+    GetOpalG7231A_6k3();\r
+    GetOpalG7231A_5k3();\r
+    GetOpalGSM0610();\r
+    GetOpalGSMAMR();\r
+    GetOpaliLBC();\r
+\r
+#if !IMPLEMENT_MULTI_FAME_AUDIO\r
+    OpalMediaFormatList allCodecs = OpalMediaFormat::GetAllRegisteredMediaFormats();\r
+    for (OpalMediaFormatList::iterator it = allCodecs.begin(); it != allCodecs.end(); ++it) {\r
+      if (it->GetMediaType() == OpalMediaType::Audio()) {\r
+        it->SetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption(), 1);\r
+        it->SetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption(), 1);\r
+        OpalMediaFormat::SetRegisteredMediaFormat(*it);\r
+      }\r
+    }\r
+#endif // IMPLEMENT_MULTI_FAME_AUDIO\r
+\r
+    if (!m_gkAddress.IsEmpty()) {\r
+      if (m_h323ep->UseGatekeeper(m_gkAddress, m_gkIdentifer, m_gkInterface))\r
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Started gatekeeper: %s\n",\r
+                          (const char *)m_h323ep->GetGatekeeper()->GetName());\r
+      else\r
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,\r
+                          "Could not start gatekeeper: addr=\"%s\", id=\"%s\", if=\"%s\"\n",\r
+                          (const char *)m_gkAddress,\r
+                          (const char *)m_gkIdentifer,\r
+                          (const char *)m_gkInterface);\r
+    }\r
+\r
+    return TRUE;\r
+}\r
+\r
+\r
+switch_status_t FSManager::ReadConfig(int reload)\r
+{\r
+    switch_event_t *request_params = NULL;\r
+    switch_event_create(&request_params, SWITCH_EVENT_REQUEST_PARAMS);\r
+    switch_assert(request_params);\r
+    switch_event_add_header_string(request_params, SWITCH_STACK_BOTTOM, "profile", switch_str_nil(""));\r
+\r
+    switch_xml_t cfg;\r
+    switch_xml_t xml = switch_xml_open_cfg(ConfigFile, &cfg, request_params);\r
+    if (xml == NULL) {\r
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", ConfigFile);\r
+        return SWITCH_STATUS_FALSE;\r
+    }\r
+\r
+    switch_xml_t xmlSettings = switch_xml_child(cfg, "settings");\r
+    if (xmlSettings) {\r
+        for (switch_xml_t xmlParam = switch_xml_child(xmlSettings, "param"); xmlParam != NULL; xmlParam = xmlParam->next) {\r
+            PConstCaselessString const var(switch_xml_attr_soft(xmlParam, "name"));\r
+            PConstString const val(switch_xml_attr_soft(xmlParam, "value"));\r
+\r
+            if (var == "context") {\r
+                m_context = val;\r
+            } else if (var == "dialplan") {\r
+                m_dialplan = val;\r
+            } else if (var == "codec-prefs") {\r
+                m_codecPrefs = val;\r
+            } else if (var == "disable-transcoding") {\r
+                m_disableTranscoding = switch_true(val);\r
+            } else if (var == "jitter-size") {\r
+                SetAudioJitterDelay(val.AsUnsigned(), val.Mid(val.Find(',')+1).AsUnsigned()); // In milliseconds\r
+            } else if (var == "gk-address") {\r
+                m_gkAddress = val;\r
+            } else if (var == "gk-identifer") {\r
+                m_gkIdentifer = val;\r
+            } else if (var == "gk-interface") {\r
+                m_gkInterface = val;\r
+#if PTRACING\r
+            } else if (var == "trace-level") {\r
+                unsigned level = val.AsUnsigned();\r
+                if (level > 0) {\r
+                    PTrace::SetLevel(level);\r
+                    PTrace::ClearOptions(0xffffffff); // Everything off\r
+                    PTrace::SetOptions(               // Except these\r
+                      PTrace::TraceLevel|PTrace::FileAndLine|PTrace::Thread\r
+#if PTLIB_CHECK_VERSION(2,11,1)\r
+                      |PTrace::ContextIdentifier\r
+#endif\r
+                    );\r
+                    PTrace::SetStream(new FSTrace);\r
+                }\r
+#endif\r
+            }\r
+        }\r
+    }\r
+\r
+    switch_xml_t xmlListeners = switch_xml_child(cfg, "listeners");\r
+    if (xmlListeners != NULL) {\r
+        for (switch_xml_t xmlListener = switch_xml_child(xmlListeners, "listener"); xmlListener != NULL; xmlListener = xmlListener->next) {\r
+\r
+            m_listeners.push_back(FSListener());\r
+            FSListener & listener = m_listeners.back();\r
+\r
+            listener.m_name = switch_xml_attr_soft(xmlListener, "name");\r
+            if (listener.m_name.IsEmpty())\r
+                listener.m_name = "unnamed";\r
+\r
+            for (switch_xml_t xmlParam = switch_xml_child(xmlListener, "param"); xmlParam != NULL; xmlParam = xmlParam->next) {\r
+                PConstCaselessString const var(switch_xml_attr_soft(xmlParam, "name"));\r
+                PConstString const val(switch_xml_attr_soft(xmlParam, "value"));\r
+                if (var == "h323-ip")\r
+                    listener.m_address = val;\r
+                else if (var == "h323-port")\r
+                    listener.m_port = (uint16_t)val.AsUnsigned();\r
+            }\r
+\r
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created Listener '%s'\n", (const char *) listener.m_name);\r
+        }\r
+    }\r
+\r
+    switch_event_destroy(&request_params);\r
+\r
+    if (xml)\r
+        switch_xml_free(xml);\r
+\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+static switch_call_cause_t create_outgoing_channel(switch_core_session_t   *session,\r
+                                                   switch_event_t          *var_event,\r
+                                                   switch_caller_profile_t *outbound_profile,\r
+                                                   switch_core_session_t  **new_session,\r
+                                                   switch_memory_pool_t   **pool,\r
+                                                   switch_originate_flag_t  flags,\r
+                                                   switch_call_cause_t     *cancel_cause)\r
+{\r
+    if (opal_process == NULL)\r
+      return SWITCH_CAUSE_CRASH;\r
+\r
+    FSConnection::outgoing_params params;\r
+    params.var_event        = var_event;\r
+    params.outbound_profile = outbound_profile;\r
+    params.new_session      = new_session;\r
+    params.pool             = pool;\r
+    params.flags            = flags;\r
+    params.cancel_cause     = cancel_cause;\r
+    params.fail_cause       = SWITCH_CAUSE_INVALID_NUMBER_FORMAT;\r
+\r
+    if (opal_process->GetManager().SetUpCall("local:", outbound_profile->destination_number, &params) != NULL)\r
+        return SWITCH_CAUSE_SUCCESS;\r
+\r
+    if (*new_session != NULL)\r
+        switch_core_session_destroy(new_session);\r
+    return params.fail_cause;\r
+}\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////\r
+\r
+FSEndPoint::FSEndPoint(FSManager & manager)\r
+  : OpalLocalEndPoint(manager)\r
+  , m_manager(manager)\r
+{\r
+    PTRACE(4, "mod_opal\tFSEndPoint created.");\r
+}\r
+\r
+\r
+OpalLocalConnection *FSEndPoint::CreateConnection(OpalCall & call, void *userData, unsigned options, OpalConnection::StringOptions* stringOptions)\r
+{\r
+    return new FSConnection(call, *this, options, stringOptions, (FSConnection::outgoing_params *)userData);\r
+}\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////\r
+\r
+FSConnection::FSConnection(OpalCall & call,\r
+                           FSEndPoint & endpoint,\r
+                           unsigned options,\r
+                           OpalConnection::StringOptions* stringOptions,\r
+                           outgoing_params * params)\r
+  : OpalLocalConnection(call, endpoint, NULL, options, stringOptions)\r
+  , m_endpoint(endpoint)\r
+  , m_fsSession(NULL)\r
+  , m_fsChannel(NULL)\r
+  , m_flushAudio(false)\r
+{\r
+    memset(&m_read_timer, 0, sizeof(m_read_timer));\r
+    memset(&m_read_codec, 0, sizeof(m_read_codec));\r
+    memset(&m_write_codec, 0, sizeof(m_write_codec));\r
+    memset(&m_vid_read_timer, 0, sizeof(m_vid_read_timer));\r
+    memset(&m_vid_read_codec, 0, sizeof(m_vid_read_codec));\r
+    memset(&m_vid_write_codec, 0, sizeof(m_vid_write_codec));\r
+\r
+    if (params != NULL) {\r
+        // If we fail, this is the cause\r
+        params->fail_cause = SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;\r
+\r
+        if ((m_fsSession = switch_core_session_request(endpoint.GetManager().GetSwitchInterface(),\r
+                  SWITCH_CALL_DIRECTION_INBOUND, params->flags, params->pool)) == NULL) {\r
+            PTRACE(1, "mod_opal\tCannot create session for outgoing call.");\r
+            return;\r
+        }\r
+    }\r
+    else {\r
+      if ((m_fsSession = switch_core_session_request(endpoint.GetManager().GetSwitchInterface(),\r
+                                                     SWITCH_CALL_DIRECTION_INBOUND, SOF_NONE, NULL)) == NULL) {\r
+            PTRACE(1, "mod_opal\tCannot create session for incoming call.");\r
+            return;\r
+      }\r
+    }\r
+\r
+    if ((m_fsChannel = switch_core_session_get_channel(m_fsSession)) == NULL) {\r
+        switch_core_session_destroy(&m_fsSession);\r
+        return;\r
+    }\r
+\r
+    switch_core_session_set_private(m_fsSession, this);\r
+    SafeReference(); // Make sure cannot be deleted until on_destroy()\r
+\r
+    if (params != NULL) {\r
+        switch_caller_profile_t *caller_profile = switch_caller_profile_clone(m_fsSession, params->outbound_profile);\r
+        switch_channel_set_caller_profile(m_fsChannel, caller_profile);\r
+        SetLocalPartyName(caller_profile->caller_id_number);\r
+        SetDisplayName(caller_profile->caller_id_name);\r
+\r
+        *params->new_session = m_fsSession;\r
+    }\r
+\r
+    switch_channel_set_state(m_fsChannel, CS_INIT);\r
+}\r
+\r
+\r
+bool FSConnection::OnOutgoingSetUp()\r
+{\r
+    if (m_fsSession == NULL || m_fsChannel == NULL) {\r
+        PTRACE(1, "mod_opal\tSession request failed.");\r
+        return false;\r
+    }\r
+\r
+    // Transfer FS caller_id_number & caller_id_name from the FSConnection\r
+    // to the protocol connection (e.g. H.323) so gets sent correctly\r
+    // in outgoing packets\r
+    PSafePtr<OpalConnection> proto = GetOtherPartyConnection();\r
+    if (proto == NULL) {\r
+        PTRACE(1, "mod_opal\tNo protocol connection in call.");\r
+        return false;\r
+    }\r
+\r
+    proto->SetLocalPartyName(GetLocalPartyName());\r
+    proto->SetDisplayName(GetDisplayName());\r
+\r
+    switch_channel_set_name(m_fsChannel, ModuleName + '/' + GetRemotePartyURL());\r
+    return true;\r
+}\r
+\r
+\r
+bool FSConnection::OnIncoming()\r
+{\r
+    if (m_fsSession == NULL || m_fsChannel == NULL) {\r
+        PTRACE(1, "mod_opal\tSession request failed.");\r
+        return false;\r
+    }\r
+\r
+    switch_core_session_add_stream(m_fsSession, NULL);\r
+\r
+    PURL url = GetRemotePartyURL();\r
+    switch_caller_profile_t *caller_profile = switch_caller_profile_new(\r
+          switch_core_session_get_pool(m_fsSession),\r
+          url.GetUserName(),      /** username */\r
+          m_endpoint.GetManager().GetDialPlan(), /** dial plan */\r
+          GetRemotePartyName(),   /** caller_id_name */\r
+          GetRemotePartyNumber(), /** caller_id_number */\r
+          url.GetHostName(),      /** network addr */\r
+          NULL,                   /** ANI */\r
+          NULL,                   /** ANI II */\r
+          NULL,                   /** RDNIS */\r
+          ModuleName,             /** source */\r
+          m_endpoint.GetManager().GetContext(), /** set context  */\r
+          GetCalledPartyNumber()  /** destination_number */\r
+    );\r
+    if (caller_profile == NULL) {\r
+        PTRACE(1, "mod_opal\tCould not create caller profile");\r
+        return false;\r
+    }\r
+\r
+    PTRACE(4, "mod_opal\tCreated switch caller profile:\n"\r
+              "  username          = " << caller_profile->username << "\n"\r
+              "  dialplan          = " << caller_profile->dialplan << "\n"\r
+              "  caller_id_name    = " << caller_profile->caller_id_name << "\n"\r
+              "  caller_id_number  = " << caller_profile->caller_id_number << "\n"\r
+              "  network_addr      = " << caller_profile->network_addr << "\n"\r
+              "  source            = " << caller_profile->source << "\n"\r
+              "  context           = " << caller_profile->context << "\n"\r
+              "  destination_number= " << caller_profile->destination_number);\r
+    switch_channel_set_caller_profile(m_fsChannel, caller_profile);\r
+\r
+    switch_channel_set_name(m_fsChannel, ModuleName + '/' + url.GetScheme() + ':' + caller_profile->destination_number);\r
+\r
+    if (switch_core_session_thread_launch(m_fsSession) != SWITCH_STATUS_SUCCESS) {\r
+        PTRACE(1, "mod_opal\tCould not launch session thread");\r
+        switch_core_session_destroy(&m_fsSession);\r
+        m_fsChannel = NULL;\r
+        return false;\r
+    }\r
+\r
+    return true;\r
+}\r
+\r
+\r
+void FSConnection::OnReleased()\r
+{\r
+    m_rxAudioOpened.Signal();   // Just in case\r
+    m_txAudioOpened.Signal();\r
+\r
+    if (m_fsChannel == NULL) {\r
+        PTRACE(3, "mod_opal\tHanging up FS side");\r
+        switch_channel_hangup(m_fsChannel, (switch_call_cause_t)callEndReason.q931);\r
+    }\r
+\r
+    OpalLocalConnection::OnReleased();\r
+}\r
+\r
+\r
+PBoolean FSConnection::SetAlerting(const PString & calleeName, PBoolean withMedia)\r
+{\r
+    if (PAssertNULL(m_fsChannel) == NULL)\r
+        return false;\r
+\r
+    switch_channel_mark_ring_ready(m_fsChannel);\r
+    return OpalLocalConnection::SetAlerting(calleeName, withMedia);\r
+}\r
+\r
+\r
+PBoolean FSConnection::SendUserInputTone(char tone, unsigned duration)\r
+{\r
+    if (PAssertNULL(m_fsChannel) == NULL)\r
+        return false;\r
+\r
+    switch_dtmf_t dtmf = { tone, duration };\r
+    return switch_channel_queue_dtmf(m_fsChannel, &dtmf) == SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+OpalMediaFormatList FSConnection::GetMediaFormats() const\r
+{\r
+    if (m_switchMediaFormats.IsEmpty()) {\r
+        const_cast<FSConnection *>(this)->SetCodecs();\r
+    }\r
+\r
+    return m_switchMediaFormats;\r
+}\r
+\r
+\r
+void FSConnection::SetCodecs()\r
+{\r
+    int numCodecs = 0;\r
+    const switch_codec_implementation_t *codecs[SWITCH_MAX_CODECS];\r
+\r
+    PString codec_string = switch_channel_get_variable(m_fsChannel, "absolute_codec_string");\r
+    if (codec_string.IsEmpty()) {\r
+        codec_string = switch_channel_get_variable(m_fsChannel, "codec_string");\r
+        if (codec_string.IsEmpty()) {\r
+            codec_string = m_endpoint.GetManager().GetCodecPrefs();\r
+            if (codec_string.IsEmpty()) {\r
+                numCodecs = switch_loadable_module_get_codecs(codecs, sizeof(codecs) / sizeof(codecs[0]));\r
+                for (int i = 0; i < numCodecs; i++) {\r
+                  if (i > 0)\r
+                    codec_string += ',';\r
+                  codec_string += codecs[i]->iananame;\r
+                }\r
+                PTRACE(4, "mod_opal\tDefault to all loaded codecs=" << codec_string);\r
+            }\r
+            else {\r
+                PTRACE(4, "mod_opal\tSettings codec-prefs=" << codec_string);\r
+            }\r
+        }\r
+        else {\r
+            PTRACE(4, "mod_opal\tChannel codec_string=" << codec_string);\r
+        }\r
+\r
+        PString orig_codec = switch_channel_get_variable(m_fsChannel, SWITCH_ORIGINATOR_CODEC_VARIABLE);\r
+        if (!orig_codec.IsEmpty()) {\r
+          if (m_endpoint.GetManager().GetDisableTranscoding()) {\r
+              codec_string = orig_codec;\r
+              PTRACE(4, "mod_opal\tNo transcoding, forced to originator codec=" << orig_codec);\r
+          }\r
+          else {\r
+              codec_string.Splice(orig_codec+',', 0);\r
+              PTRACE(4, "mod_opal\tSetting preference to originator codec=" << orig_codec);\r
+          }\r
+        }\r
+    }\r
+    else {\r
+        PTRACE(4, "mod_opal\tChannel absolute_codec_string=" << codec_string);\r
+    }\r
+\r
+    {\r
+        char *codec_order[SWITCH_MAX_CODECS];\r
+        int codec_order_last = switch_separate_string((char *)codec_string.GetPointer(), ',', codec_order, SWITCH_MAX_CODECS);\r
+        numCodecs = switch_loadable_module_get_codecs_sorted(codecs, SWITCH_MAX_CODECS, codec_order, codec_order_last);\r
+    }\r
+\r
+    for (int i = 0; i < numCodecs; i++) {\r
+        const switch_codec_implementation_t *codec = codecs[i];\r
+\r
+        // See if we have a match by PayloadType/rate/name\r
+        OpalMediaFormat switchFormat((RTP_DataFrame::PayloadTypes)codec->ianacode,\r
+                                     codec->samples_per_second,\r
+                                     codec->iananame);\r
+        if (!switchFormat.IsValid()) {\r
+            // See if we have a match by name alone\r
+            switchFormat = codec->iananame;\r
+            if (!switchFormat.IsValid()) {\r
+              PTRACE(2, "mod_opal\tCould not match FS codec "\r
+                     << codec->iananame << '@' << codec->samples_per_second\r
+                     << " (pt=" << (unsigned)codec->ianacode << ")"\r
+                        " to an OPAL media format.");\r
+              continue;\r
+            }\r
+        }\r
+\r
+        PTRACE(4, "mod_opal\tMatched FS codec " << codec->iananame << " to OPAL media format " << switchFormat);\r
+\r
+#if IMPLEMENT_MULTI_FAME_AUDIO\r
+        // Did we match or create a new media format?\r
+        if (switchFormat.IsValid() && codec->codec_type == SWITCH_CODEC_TYPE_AUDIO) {\r
+            // Calculate frames per packet, do not use codec->codec_frames_per_packet as that field\r
+            // has slightly different semantics when used in streamed codecs such as G.711\r
+            int fpp = codec->samples_per_packet/switchFormat.GetFrameTime();\r
+\r
+            /* Set the frames/packet to maximum of what is in the FS table. The OPAL negotiations will\r
+               drop the value from there. This might fail if there are "holes" in the FS table, e.g.\r
+               if for some reason G.723.1 has 30ms and 90ms but not 60ms, then the OPAL negotiations\r
+               could end up with 60ms and the codec cannot be created. The "holes" are unlikely in\r
+               all but streamed codecs such as G.711, where it is theoretically possible for OPAL to\r
+               come up with 32ms and there is only 30ms and 40ms in the FS table. We deem these\r
+               scenarios sufficiently rare that we can safely ignore them ... for now. */\r
+\r
+            if (fpp > switchFormat.GetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption())) {\r
+                switchFormat.SetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption(), fpp);\r
+            }\r
+\r
+            if (fpp > switchFormat.GetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption())) {\r
+                switchFormat.SetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption(), fpp);\r
+            }\r
+        }\r
+#endif // IMPLEMENT_MULTI_FAME_AUDIO\r
+\r
+        m_switchMediaFormats += switchFormat;\r
+    }\r
+}\r
+\r
+\r
+OpalMediaStream *FSConnection::CreateMediaStream(const OpalMediaFormat & mediaFormat, unsigned sessionID, PBoolean isSource)\r
+{\r
+    return new FSMediaStream(*this, mediaFormat, sessionID, isSource);\r
+}\r
+\r
+\r
+void FSConnection::OnPatchMediaStream(PBoolean isSource, OpalMediaPatch & patch)\r
+{\r
+    OpalConnection::OnPatchMediaStream(isSource, patch);\r
+\r
+    if (PAssertNULL(m_fsChannel) == NULL)\r
+        return;\r
+\r
+    if (patch.GetSource().GetMediaFormat().GetMediaType() != OpalMediaType::Audio())\r
+        return;\r
+\r
+    if (switch_channel_direction(m_fsChannel) == SWITCH_CALL_DIRECTION_INBOUND) {\r
+      if (isSource)\r
+          m_rxAudioOpened.Signal();\r
+      else\r
+          m_txAudioOpened.Signal();\r
+    }\r
+    else if (GetMediaStream(OpalMediaType::Audio(), !isSource) != NULL) {\r
+        // Have open media in both directions.\r
+        if (IsEstablished())\r
+            switch_channel_mark_answered(m_fsChannel);\r
+        else if (!IsReleased())\r
+            switch_channel_mark_pre_answered(m_fsChannel);\r
+    }\r
+}\r
+\r
+\r
+switch_status_t FSConnection::on_init()\r
+{\r
+    if (PAssertNULL(m_fsChannel) == NULL)\r
+        return SWITCH_STATUS_FALSE;\r
+\r
+    PTRACE(4, "mod_opal\tStarted routing for connection " << *this);\r
+    switch_channel_set_state(m_fsChannel, CS_ROUTING);\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::on_routing()\r
+{\r
+    if (PAssertNULL(m_fsChannel) == NULL)\r
+        return SWITCH_STATUS_FALSE;\r
+\r
+    PTRACE(4, "mod_opal\tRouting connection " << *this);\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::on_execute()\r
+{\r
+    if (PAssertNULL(m_fsChannel) == NULL)\r
+        return SWITCH_STATUS_FALSE;\r
+\r
+    PTRACE(4, "mod_opal\tExecuting connection " << *this);\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::on_destroy()\r
+{\r
+    PTRACE(3, "mod_opal\tFS on_destroy for connection " << *this);\r
+\r
+    m_fsChannel = NULL; // Will be destoyed by FS, so don't use it any more.\r
+\r
+    switch_core_codec_destroy(&m_read_codec);\r
+    switch_core_codec_destroy(&m_write_codec);\r
+    switch_core_codec_destroy(&m_vid_read_codec);\r
+    switch_core_codec_destroy(&m_vid_write_codec);\r
+    switch_core_timer_destroy(&m_read_timer);\r
+    switch_core_timer_destroy(&m_vid_read_timer);\r
+\r
+    switch_core_session_set_private(m_fsSession, NULL);\r
+    SafeDereference();\r
+\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::on_hangup()\r
+{\r
+    if (PAssertNULL(m_fsChannel) == NULL)\r
+        return SWITCH_STATUS_FALSE;\r
+\r
+    /* if this is still here it was our idea to hangup not opal's */\r
+    ClearCallSynchronous(NULL, H323TranslateToCallEndReason(\r
+              (Q931::CauseValues)switch_channel_get_cause_q850(m_fsChannel), UINT_MAX));\r
+\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::on_exchange_media()\r
+{\r
+    PTRACE(4, "mod_opal\tExchanging media on connection " << *this);\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::on_soft_execute()\r
+{\r
+    PTRACE(4, "mod_opal\tTransmit on connection " << *this);\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::kill_channel(int sig)\r
+{\r
+    switch (sig) {\r
+    case SWITCH_SIG_KILL:\r
+        m_rxAudioOpened.Signal();\r
+        m_txAudioOpened.Signal();\r
+        PTRACE(4, "mod_opal\tSignal channel KILL on connection " << *this);\r
+        break;\r
+    case SWITCH_SIG_XFER:\r
+    case SWITCH_SIG_BREAK:\r
+    default:\r
+        PTRACE(4, "mod_opal\tSignal channel " << sig << " on connection " << *this);\r
+        break;\r
+    }\r
+\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::send_dtmf(const switch_dtmf_t *dtmf)\r
+{\r
+    OnUserInputTone(dtmf->digit, dtmf->duration);\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::receive_message(switch_core_session_message_t *msg)\r
+{\r
+    if (PAssertNULL(m_fsChannel) == NULL)\r
+        return SWITCH_STATUS_FALSE;\r
+\r
+    switch (msg->message_id) {\r
+    case SWITCH_MESSAGE_INDICATE_RINGING:\r
+    case SWITCH_MESSAGE_INDICATE_PROGRESS:\r
+    case SWITCH_MESSAGE_INDICATE_ANSWER:\r
+    case SWITCH_MESSAGE_INDICATE_DEFLECT:\r
+        if (switch_channel_direction(m_fsChannel) == SWITCH_CALL_DIRECTION_INBOUND) {\r
+            switch_caller_profile_t * profile = switch_channel_get_caller_profile(m_fsChannel);\r
+            if (profile != NULL && profile->caller_extension != NULL)\r
+            {\r
+                PSafePtr<OpalConnection> other = GetOtherPartyConnection();\r
+                if (other != NULL) {\r
+                    other->SetLocalPartyName(profile->caller_extension->extension_number);\r
+                    other->SetDisplayName(profile->caller_extension->extension_name);\r
+                }\r
+                SetLocalPartyName(profile->caller_extension->extension_number);\r
+                SetDisplayName(profile->caller_extension->extension_name);\r
+            }\r
+        }\r
+        else {\r
+            return SWITCH_STATUS_FALSE;\r
+        }\r
+        break;\r
+\r
+    default:\r
+        break;\r
+    }\r
+\r
+    switch (msg->message_id) {\r
+    case SWITCH_MESSAGE_INDICATE_BRIDGE:\r
+    case SWITCH_MESSAGE_INDICATE_UNBRIDGE:\r
+    case SWITCH_MESSAGE_INDICATE_AUDIO_SYNC:\r
+        m_flushAudio = true;\r
+        break;\r
+\r
+    case SWITCH_MESSAGE_INDICATE_RINGING:\r
+        AlertingIncoming();\r
+        break;\r
+\r
+    case SWITCH_MESSAGE_INDICATE_PROGRESS:\r
+        AutoStartMediaStreams();\r
+        AlertingIncoming();\r
+\r
+        if (!WaitForMedia())\r
+            return SWITCH_STATUS_FALSE;\r
+\r
+        if (!switch_channel_test_flag(m_fsChannel, CF_EARLY_MEDIA)) {\r
+            switch_channel_mark_pre_answered(m_fsChannel);\r
+        }\r
+        break;\r
+\r
+    case SWITCH_MESSAGE_INDICATE_ANSWER:\r
+        AcceptIncoming();\r
+\r
+        if (!WaitForMedia())\r
+            return SWITCH_STATUS_FALSE;\r
+\r
+        if (!switch_channel_test_flag(m_fsChannel, CF_ANSWERED)) {\r
+            switch_channel_mark_answered(m_fsChannel);\r
+        }\r
+        break;\r
+\r
+    case SWITCH_MESSAGE_INDICATE_DEFLECT:\r
+        ownerCall.Transfer(msg->string_arg, GetOtherPartyConnection());\r
+        break;\r
+\r
+    default:\r
+        PTRACE(3, "mod_opal\tReceived unhandled message " << msg->message_id << " on connection " << *this);\r
+    }\r
+\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+bool FSConnection::WaitForMedia()\r
+{\r
+    PTRACE(4, "mod_opal\tAwaiting media start on connection " << *this);\r
+    m_rxAudioOpened.Wait();\r
+    m_txAudioOpened.Wait();\r
+\r
+    if (IsReleased()) {\r
+        // Call got aborted\r
+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(m_fsSession), SWITCH_LOG_ERROR, "Call abandoned!\n");\r
+        return false;\r
+    }\r
+\r
+    PTRACE(3, "mod_opal\tMedia started on connection " << *this);\r
+    return true;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::receive_event(switch_event_t *event)\r
+{\r
+    PTRACE(4, "mod_opal\tReceived event " << event->event_id << " on connection " << *this);\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::state_change()\r
+{\r
+    PTRACE(4, "mod_opal\tState changed on connection " << *this);\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::read_audio_frame(switch_frame_t **frame, switch_io_flag_t flags, int stream_id)\r
+{\r
+    return read_frame(OpalMediaType::Audio(), frame, flags);\r
+}\r
+\r
+\r
+switch_status_t FSConnection::write_audio_frame(switch_frame_t *frame, switch_io_flag_t flags, int stream_id)\r
+{\r
+    return write_frame(OpalMediaType::Audio(), frame, flags);\r
+}\r
+\r
+\r
+switch_status_t FSConnection::read_video_frame(switch_frame_t **frame, switch_io_flag_t flag, int stream_id)\r
+{\r
+    return read_frame(OpalMediaType::Video(), frame, flag);\r
+}\r
+\r
+\r
+switch_status_t FSConnection::write_video_frame(switch_frame_t *frame, switch_io_flag_t flag, int stream_id)\r
+{\r
+    return write_frame(OpalMediaType::Video(), frame, flag);\r
+}\r
+\r
+\r
+switch_status_t FSConnection::read_frame(const OpalMediaType & mediaType, switch_frame_t **frame, switch_io_flag_t flags)\r
+{\r
+    PSafePtr <FSMediaStream> stream = PSafePtrCast <OpalMediaStream, FSMediaStream>(GetMediaStream(mediaType, false));\r
+    return stream != NULL ? stream->read_frame(frame, flags) : SWITCH_STATUS_FALSE;\r
+}\r
+\r
+\r
+switch_status_t FSConnection::write_frame(const OpalMediaType & mediaType, const switch_frame_t *frame, switch_io_flag_t flags)\r
+{\r
+    PSafePtr <FSMediaStream> stream = PSafePtrCast<OpalMediaStream, FSMediaStream>(GetMediaStream(mediaType, true));\r
+    return stream != NULL ? stream->write_frame(frame, flags) : SWITCH_STATUS_FALSE;\r
+}\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////\r
+\r
+FSMediaStream::FSMediaStream(FSConnection & conn, const OpalMediaFormat & mediaFormat, unsigned sessionID, bool isSource)\r
+    : OpalMediaStream(conn, mediaFormat, sessionID, isSource)\r
+    , m_connection(conn)\r
+    , m_readRTP(0, SWITCH_RECOMMENDED_BUFFER_SIZE)\r
+{\r
+    memset(&m_readFrame, 0, sizeof(m_readFrame));\r
+}\r
+\r
+\r
+PBoolean FSMediaStream::Open()\r
+{\r
+    if (IsOpen()) {\r
+        return true;\r
+    }\r
+\r
+    switch_core_session_t *fsSession = m_connection.GetSession();\r
+    switch_channel_t *fsChannel = m_connection.GetChannel();\r
+    if (PAssertNULL(fsSession) == NULL || PAssertNULL(fsChannel) == NULL)\r
+        return false;\r
+\r
+    bool isAudio;\r
+    if (mediaFormat.GetMediaType() == OpalMediaType::Audio()) {\r
+        isAudio = true;\r
+    } else if (mediaFormat.GetMediaType() == OpalMediaType::Video()) {\r
+        isAudio = false;\r
+    } else {\r
+        return false;\r
+    }\r
+\r
+    int ptime = mediaFormat.GetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption()) * mediaFormat.GetFrameTime() / mediaFormat.GetTimeUnits();\r
+\r
+    if (IsSink()) {\r
+        m_switchCodec = isAudio ? &m_connection.m_read_codec : &m_connection.m_vid_read_codec;\r
+        m_switchTimer = isAudio ? &m_connection.m_read_timer : &m_connection.m_vid_read_timer;\r
+        m_readFrame.codec = m_switchCodec;\r
+        m_readFrame.rate = mediaFormat.GetClockRate();\r
+    } else {\r
+        m_switchCodec = isAudio ? &m_connection.m_write_codec : &m_connection.m_vid_write_codec;\r
+    }\r
+\r
+    // The following is performed on two different instances of this object.\r
+    if (switch_core_codec_init(m_switchCodec, mediaFormat.GetEncodingName(), NULL, // FMTP\r
+                               mediaFormat.GetClockRate(), ptime, 1,  // Channels\r
+                               SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,   // Settings\r
+                               switch_core_session_get_pool(fsSession)) != SWITCH_STATUS_SUCCESS) {\r
+        // Could not select a codecs using negotiated frames/packet, so try using default.\r
+        if (switch_core_codec_init(m_switchCodec, mediaFormat.GetEncodingName(), NULL, // FMTP\r
+                                   mediaFormat.GetClockRate(), 0, 1,  // Channels\r
+                                   SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,   // Settings\r
+                                   switch_core_session_get_pool(fsSession)) != SWITCH_STATUS_SUCCESS) {\r
+            PTRACE(1, "mod_opal\t" << switch_channel_get_name(fsChannel)\r
+                   << " cannot initialise " << (IsSink()? "read" : "write") << ' '\r
+                   << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this);\r
+            switch_channel_hangup(fsChannel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);\r
+            return false;\r
+        }\r
+        PTRACE(2, "mod_opal\t" << switch_channel_get_name(fsChannel)\r
+               << " unsupported ptime of " << ptime << " on " << (IsSink()? "read" : "write") << ' '\r
+               << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this);\r
+    }\r
+\r
+    if (IsSink()) {\r
+        if (isAudio) {\r
+            switch_core_session_set_read_codec(fsSession, m_switchCodec);\r
+            if (switch_core_timer_init(m_switchTimer,\r
+                                       "soft",\r
+                                       m_switchCodec->implementation->microseconds_per_packet / 1000,\r
+                                       m_switchCodec->implementation->samples_per_packet,\r
+                                       switch_core_session_get_pool(fsSession)) != SWITCH_STATUS_SUCCESS) {\r
+                PTRACE(1, "mod_opal\t" << switch_channel_get_name(fsChannel)\r
+                       << " timer init failed on " << (IsSink()? "read" : "write") << ' '\r
+                       << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this);\r
+                switch_core_codec_destroy(m_switchCodec);\r
+                m_switchCodec = NULL;\r
+                return false;\r
+            }\r
+        } else {\r
+            switch_core_session_set_video_read_codec(fsSession, m_switchCodec);\r
+            switch_channel_set_flag(fsChannel, CF_VIDEO);\r
+        }\r
+    } else {\r
+        if (isAudio) {\r
+            switch_core_session_set_write_codec(fsSession, m_switchCodec);\r
+        } else {\r
+            switch_core_session_set_video_write_codec(fsSession, m_switchCodec);\r
+            switch_channel_set_flag(fsChannel, CF_VIDEO);\r
+        }\r
+    }\r
+\r
+    PTRACE(3, "mod_opal\t" << switch_channel_get_name(fsChannel)\r
+           << " initialised " << (IsSink()? "read" : "write") << ' '\r
+           << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this);\r
+\r
+    return OpalMediaStream::Open();\r
+}\r
+\r
+\r
+void FSMediaStream::InternalClose()\r
+{\r
+}\r
+\r
+\r
+PBoolean FSMediaStream::IsSynchronous() const\r
+{\r
+    return true;\r
+}\r
+\r
+\r
+PBoolean FSMediaStream::RequiresPatchThread(OpalMediaStream *) const\r
+{\r
+    return false;\r
+}\r
+\r
+\r
+int FSMediaStream::StartReadWrite(PatchPtr & mediaPatch) const\r
+{\r
+    if (!IsOpen()) {\r
+        PTRACE(2, "mod_opal\tNot open!");\r
+        return -1;\r
+    }\r
+\r
+    if (!m_switchCodec) {\r
+        PTRACE(2, "mod_opal\tNo codec!");\r
+        return -1;\r
+    }\r
+\r
+    if (!m_connection.IsChannelReady()) {\r
+        PTRACE(2, "mod_opal\tChannel not ready!");\r
+        return -1;\r
+    }\r
+\r
+    // We make referenced copy of pointer so can't be deleted out from under us\r
+    mediaPatch = m_mediaPatch;\r
+    if (mediaPatch == NULL) {\r
+        /*There is a race here... sometimes we make it here and m_mediaPatch is NULL\r
+          if we wait it shows up in 1ms, maybe there is a better way to wait. */\r
+        PTRACE(3, "mod_opal\tPatch not ready!");\r
+        return 1;\r
+    }\r
+\r
+    return 0;\r
+}\r
+\r
+\r
+switch_status_t FSMediaStream::read_frame(switch_frame_t **frame, switch_io_flag_t flags)\r
+{\r
+    PatchPtr mediaPatch;\r
+    switch (StartReadWrite(mediaPatch)) {\r
+      case -1 :\r
+        return SWITCH_STATUS_FALSE;\r
+      case 1 :\r
+        return SWITCH_STATUS_SUCCESS;\r
+    }\r
+\r
+    if (m_connection.NeedFlushAudio()) {\r
+        mediaPatch->GetSource().EnableJitterBuffer(); // This flushes data and resets jitter buffer\r
+        m_readRTP.SetPayloadSize(0);\r
+    } else {\r
+        m_readRTP.SetTimestamp(m_readFrame.timestamp + m_switchCodec->implementation->samples_per_packet);\r
+\r
+        if (!mediaPatch->GetSource().ReadPacket(m_readRTP)) {\r
+            return SWITCH_STATUS_FALSE;\r
+        }\r
+    }\r
+\r
+    if (m_switchTimer != NULL) {\r
+        switch_core_timer_next(m_switchTimer);\r
+    }\r
+\r
+    if (m_switchCodec != NULL) {\r
+        if (!switch_core_codec_ready(m_switchCodec)) {\r
+            PTRACE(2, "mod_opal\tread_frame: codec not ready!");\r
+            return SWITCH_STATUS_FALSE;\r
+        }\r
+    }\r
+\r
+    m_readFrame.packet    = m_readRTP.GetPointer();\r
+    m_readFrame.packetlen = m_readRTP.GetHeaderSize() + m_readFrame.datalen;\r
+\r
+#if IMPLEMENT_MULTI_FAME_AUDIO\r
+    // Repackage frames in incoming packet to agree with what FS expects.\r
+    // Not implmented yet!!!!!!!!!\r
+    // Cheating and only supporting one frame per packet\r
+#endif\r
+\r
+    m_readFrame.buflen    = m_readRTP.GetSize();\r
+    m_readFrame.data      = m_readRTP.GetPayloadPtr();\r
+    m_readFrame.datalen   = m_readRTP.GetPayloadSize();\r
+    m_readFrame.timestamp = m_readRTP.GetTimestamp();\r
+    m_readFrame.seq       = m_readRTP.GetSequenceNumber();\r
+    m_readFrame.ssrc      = m_readRTP.GetSyncSource();\r
+    m_readFrame.m         = m_readRTP.GetMarker() ? SWITCH_TRUE : SWITCH_FALSE;\r
+    m_readFrame.payload   = (switch_payload_t)m_readRTP.GetPayloadType();\r
+    m_readFrame.flags     = m_readFrame.datalen == 0 ||\r
+                            m_readFrame.payload == RTP_DataFrame::CN ||\r
+                            m_readFrame.payload == RTP_DataFrame::Cisco_CN ? SFF_CNG : 0;\r
+\r
+    *frame = &m_readFrame;\r
+\r
+    return SWITCH_STATUS_SUCCESS;\r
+}\r
+\r
+\r
+switch_status_t FSMediaStream::write_frame(const switch_frame_t *frame, switch_io_flag_t flags)\r
+{\r
+    PatchPtr mediaPatch;\r
+    switch (StartReadWrite(mediaPatch)) {\r
+      case -1 :\r
+        return SWITCH_STATUS_FALSE;\r
+      case 1 :\r
+        return SWITCH_STATUS_SUCCESS;\r
+    }\r
+\r
+    if ((frame->flags & SFF_RAW_RTP) != 0) {\r
+        RTP_DataFrame rtp((const BYTE *)frame->packet, frame->packetlen, false);\r
+        return mediaPatch->PushFrame(rtp) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;\r
+    }\r
+\r
+    RTP_DataFrame rtp(frame->datalen);\r
+    memcpy(rtp.GetPayloadPtr(), frame->data, frame->datalen);\r
+\r
+    rtp.SetPayloadType(mediaFormat.GetPayloadType());\r
+\r
+    /* Not sure what FS is going to give us!\r
+       Suspect it depends on the mod on the other side sending it. */\r
+    if (frame->timestamp != 0)\r
+      timestamp = frame->timestamp;\r
+    else if (frame->samples != 0)\r
+      timestamp += frame->samples;\r
+    else\r
+      timestamp += m_switchCodec->implementation->samples_per_packet;\r
+    rtp.SetTimestamp(timestamp);\r
+\r
+    return mediaPatch->PushFrame(rtp) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;\r
+}\r
+\r
+\r
+/* For Emacs:\r
+ * Local Variables:\r
+ * mode:c\r
+ * indent-tabs-mode:nil\r
+ * tab-width:4\r
+ * c-basic-offset:4\r
+ * End:\r
+ * For VIM:\r
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:s:\r
+ */\r
index 1915d54b90da7b821a4865085d4deef6f285f998..310ceecb57aacfe97692a562a7d55101476e82e9 100644 (file)
-/* Opal endpoint interface for Freeswitch Modular Media Switching Software Library /
- * Soft-Switch Application
- *
- * Version: MPL 1.1
- *
- * Copyright (c) 2007 Tuyan Ozipek (tuyanozipek@gmail.com)
- *
- * 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.
- *
- * Contributor(s):
- * Tuyan Ozipek   (tuyanozipek@gmail.com)
- * Lukasz Zwierko (lzwierko@gmail.com)
- * Robert Jongbloed (robertj@voxlucida.com.au)
- *
- */
-
-
-#ifndef __FREESWITCH_MOD_OPAL__
-#define __FREESWITCH_MOD_OPAL__
-
-#if defined(__GNUC__) && defined(HAVE_VISIBILITY)
-#pragma GCC visibility push(default)
-#endif
-
-#include <ptlib.h>
-#include <opal/manager.h>
-#include <opal/localep.h>
-#include <h323/h323ep.h>
-#include <iax2/iax2ep.h>
-
-#if defined(__GNUC__) && defined(HAVE_VISIBILITY)
-#pragma GCC visibility pop
-#endif
-
-#undef strcasecmp
-#undef strncasecmp
-
-#define HAVE_APR
-#include <switch.h>
-#include <switch_version.h>
-#define MODNAME "mod_opal"
-
-
-class FSEndPoint;
-class FSManager;
-
-
-struct mod_opal_globals {
-       int trace_level;
-       char *codec_string;
-       char *context;
-       char *dialplan;
-};
-
-extern struct mod_opal_globals mod_opal_globals;
-
-
-class FSProcess:public PLibraryProcess {
-       PCLASSINFO(FSProcess, PLibraryProcess);
-
-  public:
-       FSProcess();
-       ~FSProcess();
-
-       bool Initialise(switch_loadable_module_interface_t *iface);
-
-            FSManager & GetManager() const {
-               return *m_manager;
-  } protected:
-             FSManager * m_manager;
-};
-
-
-struct FSListener {
-       FSListener() {
-       } PString name;
-       OpalTransportAddress listenAddress;
-       PString localUserName;
-       PString gatekeeper;
-};
-
-
-class FSCall:public OpalCall {
-       PCLASSINFO(FSCall, OpalCall);
-  public:
-       FSCall(OpalManager & manager);
-       virtual PBoolean OnSetUp(OpalConnection & connection);
-};
-
-
-class FSManager:public OpalManager {
-       PCLASSINFO(FSManager, OpalManager);
-
-  public:
-       FSManager();
-
-       bool Initialise(switch_loadable_module_interface_t *iface);
-
-       switch_status_t ReadConfig(int reload);
-
-       switch_endpoint_interface_t *GetSwitchInterface() const {
-               return m_FreeSwitch;
-       } virtual OpalCall *CreateCall(void *userData);
-
-  private:
-       switch_endpoint_interface_t *m_FreeSwitch;
-
-       H323EndPoint *m_h323ep;
-       IAX2EndPoint *m_iaxep;
-       FSEndPoint *m_fsep;
-
-       PString m_gkAddress;
-       PString m_gkIdentifer;
-       PString m_gkInterface;
-
-               list < FSListener > m_listeners;
-};
-
-
-class FSConnection;
-typedef struct {
-       switch_timer_t read_timer;
-       switch_codec_t read_codec;
-       switch_codec_t write_codec;
-
-       switch_timer_t vid_read_timer;
-       switch_codec_t vid_read_codec;
-       switch_codec_t vid_write_codec;
-       FSConnection *me;
-} opal_private_t;
-
-
-class FSEndPoint:public OpalLocalEndPoint {
-       PCLASSINFO(FSEndPoint, OpalLocalEndPoint);
-  public:
-       FSEndPoint(FSManager & manager);
-
-       virtual bool OnIncomingCall(OpalLocalConnection &);
-       virtual OpalLocalConnection *CreateConnection(OpalCall & call, void *userData, unsigned options, OpalConnection::StringOptions * stringOptions);
-};
-
-
-#define DECLARE_CALLBACK0(name)                           \
-    static switch_status_t name(switch_core_session_t *session) {       \
-        opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session); \
-        return tech_pvt && tech_pvt->me != NULL ? tech_pvt->me->name() : SWITCH_STATUS_FALSE; } \
-switch_status_t name()
-
-#define DECLARE_CALLBACK1(name, type1, name1)                           \
-    static switch_status_t name(switch_core_session_t *session, type1 name1) { \
-        opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session); \
-        return tech_pvt && tech_pvt->me != NULL ? tech_pvt->me->name(name1) : SWITCH_STATUS_FALSE; } \
-switch_status_t name(type1 name1)
-
-#define DECLARE_CALLBACK3(name, type1, name1, type2, name2, type3, name3) \
-    static switch_status_t name(switch_core_session_t *session, type1 name1, type2 name2, type3 name3) { \
-        opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session); \
-        return tech_pvt && tech_pvt->me != NULL ? tech_pvt->me->name(name1, name2, name3) : SWITCH_STATUS_FALSE; } \
-switch_status_t name(type1 name1, type2 name2, type3 name3)
-
-
-
-
-class FSConnection:public OpalLocalConnection {
-       PCLASSINFO(FSConnection, OpalLocalConnection)
-
-  public:
-       FSConnection(OpalCall & call,
-                                FSEndPoint & endpoint,
-                                void *userData,
-                                unsigned options,
-                                OpalConnection::StringOptions * stringOptions,
-                                switch_caller_profile_t *outbound_profile, switch_core_session_t *fsSession, switch_channel_t *fsChannel);
-
-       virtual bool OnIncoming();
-       virtual void OnReleased();
-       virtual PBoolean SetAlerting(const PString & calleeName, PBoolean withMedia);
-       virtual void OnAlerting();
-       virtual void OnEstablished();
-       virtual OpalMediaStream *CreateMediaStream(const OpalMediaFormat &, unsigned, PBoolean);
-       virtual PBoolean OnOpenMediaStream(OpalMediaStream & stream);
-       virtual OpalMediaFormatList GetMediaFormats() const;
-       virtual PBoolean SendUserInputTone(char tone, unsigned duration);
-       virtual PBoolean SendUserInputString(const PString & value);
-
-       void SetCodecs();
-
-            DECLARE_CALLBACK0(on_init);
-            DECLARE_CALLBACK0(on_routing);
-            DECLARE_CALLBACK0(on_execute);
-
-            DECLARE_CALLBACK0(on_exchange_media);
-            DECLARE_CALLBACK0(on_soft_execute);
-
-            DECLARE_CALLBACK1(kill_channel, int, sig);
-           DECLARE_CALLBACK1(send_dtmf, const switch_dtmf_t *, dtmf);
-                     DECLARE_CALLBACK1(receive_message, switch_core_session_message_t *, msg);
-                                     DECLARE_CALLBACK1(receive_event, switch_event_t *, event);
-                      DECLARE_CALLBACK0(state_change);
-                      DECLARE_CALLBACK3(read_audio_frame, switch_frame_t **, frame, switch_io_flag_t, flags, int, stream_id);
-           DECLARE_CALLBACK3(write_audio_frame, switch_frame_t *, frame, switch_io_flag_t, flags, int, stream_id);
-           DECLARE_CALLBACK3(read_video_frame, switch_frame_t **, frame, switch_io_flag_t, flag, int, stream_id);
-           DECLARE_CALLBACK3(write_video_frame, switch_frame_t *, frame, switch_io_flag_t, flag, int, stream_id);
-
-       switch_status_t read_frame(const OpalMediaType & mediaType, switch_frame_t **frame, switch_io_flag_t flags);
-       switch_status_t write_frame(const OpalMediaType & mediaType, const switch_frame_t *frame, switch_io_flag_t flags);
-
-       switch_core_session_t *GetSession() const {
-               return m_fsSession;
-  } private:
-             FSEndPoint & m_endpoint;
-       switch_core_session_t *m_fsSession;
-       switch_channel_t *m_fsChannel;
-       PSyncPoint m_rxAudioOpened;
-       PSyncPoint m_txAudioOpened;
-       OpalMediaFormatList m_switchMediaFormats;
-};
-
-
-class FSMediaStream:public OpalMediaStream {
-       PCLASSINFO(FSMediaStream, OpalMediaStream);
-  public:
-       FSMediaStream(FSConnection & conn, const OpalMediaFormat & mediaFormat, ///<  Media format for stream
-                                 unsigned sessionID,   ///<  Session number for stream
-                                 bool isSource ///<  Is a source stream
-               );
-
-       virtual PBoolean Open();
-       virtual PBoolean Close();
-       virtual PBoolean IsSynchronous() const;
-       virtual PBoolean RequiresPatchThread(OpalMediaStream *) const;
-
-       switch_status_t read_frame(switch_frame_t **frame, switch_io_flag_t flags);
-       switch_status_t write_frame(const switch_frame_t *frame, switch_io_flag_t flags);
-
-  private:
-       switch_core_session_t *m_fsSession;
-       switch_channel_t *m_fsChannel;
-       switch_timer_t *m_switchTimer;
-       switch_codec_t *m_switchCodec;
-       switch_frame_t m_readFrame;
-       unsigned char m_buf[SWITCH_RECOMMENDED_BUFFER_SIZE];
-       RTP_DataFrame m_readRTP;
-       bool m_callOnStart;
-       uint32_t m_timeStamp;
-
-       bool CheckPatchAndLock();
-};
-
-
-#endif /* __FREESWITCH_MOD_OPAL__ */
-
-/* For Emacs:
- * Local Variables:
- * mode:c
- * indent-tabs-mode:nil
- * tab-width:4
- * c-basic-offset:4
- * End:
- * For VIM:
- * vim:set softtabstop=4 shiftwidth=4 tabstop=4:s:
- */
+/* Opal endpoint interface for Freeswitch Modular Media Switching Software Library /\r
+ * Soft-Switch Application\r
+ *\r
+ * Version: MPL 1.1\r
+ *\r
+ * Copyright (c) 2007 Tuyan Ozipek (tuyanozipek@gmail.com)\r
+ * Copyright (c) 2008-2012 Vox Lucida Pty. Ltd. (robertj@voxlucida.com.au)\r
+ *\r
+ * The contents of this file are subject to the Mozilla Public License Version\r
+ * 1.1 (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ * http://www.mozilla.org/MPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS IS" basis,\r
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\r
+ * for the specific language governing rights and limitations under the\r
+ * License.\r
+ *\r
+ * Contributor(s):\r
+ * Tuyan Ozipek   (tuyanozipek@gmail.com)\r
+ * Lukasz Zwierko (lzwierko@gmail.com)\r
+ * Robert Jongbloed (robertj@voxlucida.com.au)\r
+ *\r
+ */\r
+\r
+\r
+#ifndef __FREESWITCH_MOD_OPAL__\r
+#define __FREESWITCH_MOD_OPAL__\r
+\r
+#if defined(__GNUC__) && defined(HAVE_VISIBILITY)\r
+#pragma GCC visibility push(default)\r
+#endif\r
+\r
+#include <ptlib.h>\r
+#include <opal/manager.h>\r
+#include <opal/localep.h>\r
+#include <h323/h323ep.h>\r
+#include <iax2/iax2ep.h>\r
+\r
+#if defined(__GNUC__) && defined(HAVE_VISIBILITY)\r
+#pragma GCC visibility pop\r
+#endif\r
+\r
+#undef strcasecmp\r
+#undef strncasecmp\r
+\r
+\r
+#if _MSC_VER < 1600\r
+/*The following insanity is because libteletone_generate.h defines int8_t in\r
+  a slightly different manner to most other cases (SDL, PCAP, Java V8, stdint.h\r
+  etc) and does not provide a mechanism to prevent it's inclusion. Then, to\r
+  cap it off, VS2008 barfs on the difference. VS2010 seems OK with it.\r
+\r
+  Sigh.\r
+ */\r
+#pragma include_alias(<libteletone.h>,          <../../libs/libteletone/src/libteletone.h>)\r
+#pragma include_alias(<libteletone_generate.h>, <../../libs/libteletone/src/libteletone_generate.h>)\r
+#pragma include_alias(<libteletone_detect.h>,   <../../libs/libteletone/src/libteletone_detect.h>)\r
+#define int8_t signed int8_t\r
+#include <libteletone_generate.h>\r
+#undef int8_t\r
+#endif // End of insanity\r
+\r
+\r
+#define HAVE_APR\r
+#define uint32_t uint32_t // Avoid conflict in stdint definitions\r
+#include <switch.h>\r
+#undef uint32_t\r
+\r
+#include <switch_version.h>\r
+\r
+\r
+#define MODNAME "mod_opal"\r
+\r
+\r
+class FSEndPoint;\r
+class FSManager;\r
+\r
+\r
+class FSProcess : public PLibraryProcess\r
+{\r
+    PCLASSINFO(FSProcess, PLibraryProcess);\r
+  public:\r
+    FSProcess();\r
+    ~FSProcess();\r
+\r
+    bool Initialise(switch_loadable_module_interface_t *iface);\r
+\r
+    FSManager & GetManager() const\r
+    {\r
+        return *m_manager;\r
+    }\r
+\r
+  protected:\r
+    FSManager * m_manager;\r
+};\r
+\r
+\r
+struct FSListener\r
+{\r
+    FSListener() : m_port(H323EndPoint::DefaultTcpSignalPort) { }\r
+\r
+    PString            m_name;\r
+    PIPSocket::Address m_address;\r
+    uint16_t           m_port;\r
+};\r
+\r
+\r
+class FSManager : public OpalManager\r
+{\r
+    PCLASSINFO(FSManager, OpalManager);\r
+\r
+  public:\r
+    FSManager();\r
+\r
+    bool Initialise(switch_loadable_module_interface_t *iface);\r
+\r
+    switch_status_t ReadConfig(int reload);\r
+\r
+    switch_endpoint_interface_t *GetSwitchInterface() const { return m_FreeSwitch; }\r
+    const PString & GetContext() const { return m_context; }\r
+    const PString & GetDialPlan() const { return m_dialplan; }\r
+    const PString & GetCodecPrefs() const { return m_codecPrefs; }\r
+    bool GetDisableTranscoding() const { return m_disableTranscoding; }\r
+\r
+  private:\r
+    switch_endpoint_interface_t *m_FreeSwitch;\r
+\r
+    H323EndPoint *m_h323ep;\r
+    IAX2EndPoint *m_iaxep;\r
+    FSEndPoint   *m_fsep;\r
+\r
+    PString m_context;\r
+    PString m_dialplan;\r
+    PString m_codecPrefs;\r
+    bool    m_disableTranscoding;\r
+    PString m_gkAddress;\r
+    PString m_gkIdentifer;\r
+    PString m_gkInterface;\r
+\r
+    list <FSListener> m_listeners;\r
+};\r
+\r
+\r
+class FSEndPoint : public OpalLocalEndPoint\r
+{\r
+    PCLASSINFO(FSEndPoint, OpalLocalEndPoint);\r
+  public:\r
+    FSEndPoint(FSManager & manager);\r
+\r
+    virtual OpalLocalConnection *CreateConnection(OpalCall & call, void *userData, unsigned options, OpalConnection::StringOptions * stringOptions);\r
+\r
+    FSManager & GetManager() const { return m_manager; }\r
+\r
+  protected:\r
+    FSManager & m_manager;\r
+};\r
+\r
+\r
+class FSConnection;\r
+\r
+\r
+class FSMediaStream : public OpalMediaStream\r
+{\r
+    PCLASSINFO(FSMediaStream, OpalMediaStream);\r
+  public:\r
+    FSMediaStream(\r
+      FSConnection & conn,\r
+      const OpalMediaFormat & mediaFormat,    ///<  Media format for stream\r
+      unsigned sessionID,    ///<  Session number for stream\r
+      bool isSource    ///<  Is a source stream\r
+    );\r
+\r
+    virtual PBoolean Open();\r
+    virtual PBoolean IsSynchronous() const;\r
+    virtual PBoolean RequiresPatchThread(OpalMediaStream *) const;\r
+\r
+    switch_status_t read_frame(switch_frame_t **frame, switch_io_flag_t flags);\r
+    switch_status_t write_frame(const switch_frame_t *frame, switch_io_flag_t flags);\r
+\r
+  protected:\r
+    virtual void InternalClose();\r
+    int StartReadWrite(PatchPtr & mediaPatch) const;\r
+\r
+  private:\r
+    bool CheckPatchAndLock();\r
+\r
+    FSConnection          &m_connection;\r
+    switch_timer_t        *m_switchTimer;\r
+    switch_codec_t        *m_switchCodec;\r
+    switch_frame_t         m_readFrame;\r
+    RTP_DataFrame          m_readRTP;\r
+};\r
+\r
+\r
+#define DECLARE_CALLBACK0(name)                           \\r
+    static switch_status_t name(switch_core_session_t *session) {       \\r
+        FSConnection *tech_pvt = (FSConnection *) switch_core_session_get_private(session); \\r
+        return tech_pvt != NULL ? tech_pvt->name() : SWITCH_STATUS_FALSE; } \\r
+    switch_status_t name()\r
+\r
+#define DECLARE_CALLBACK1(name, type1, name1)                           \\r
+    static switch_status_t name(switch_core_session_t *session, type1 name1) { \\r
+        FSConnection *tech_pvt = (FSConnection *) switch_core_session_get_private(session); \\r
+        return tech_pvt != NULL ? tech_pvt->name(name1) : SWITCH_STATUS_FALSE; } \\r
+    switch_status_t name(type1 name1)\r
+\r
+#define DECLARE_CALLBACK3(name, type1, name1, type2, name2, type3, name3) \\r
+    static switch_status_t name(switch_core_session_t *session, type1 name1, type2 name2, type3 name3) { \\r
+        FSConnection *tech_pvt = (FSConnection *) switch_core_session_get_private(session); \\r
+        return tech_pvt != NULL ? tech_pvt->name(name1, name2, name3) : SWITCH_STATUS_FALSE; } \\r
+    switch_status_t name(type1 name1, type2 name2, type3 name3)\r
+\r
+\r
+\r
+\r
+class FSConnection : public OpalLocalConnection\r
+{\r
+    PCLASSINFO(FSConnection, OpalLocalConnection)\r
+\r
+  public:\r
+    struct outgoing_params {\r
+      switch_event_t          *var_event;\r
+      switch_caller_profile_t *outbound_profile;\r
+      switch_core_session_t  **new_session;\r
+      switch_memory_pool_t   **pool;\r
+      switch_originate_flag_t  flags;\r
+      switch_call_cause_t     *cancel_cause;\r
+      switch_call_cause_t      fail_cause;\r
+    };\r
+\r
+    FSConnection(OpalCall & call,\r
+                 FSEndPoint & endpoint,\r
+                 unsigned options,\r
+                 OpalConnection::StringOptions * stringOptions,\r
+                 outgoing_params * params);\r
+\r
+    virtual bool OnOutgoingSetUp();\r
+    virtual bool OnIncoming();\r
+    virtual void OnReleased();\r
+    virtual PBoolean SetAlerting(const PString & calleeName, PBoolean withMedia);\r
+    virtual OpalMediaStream *CreateMediaStream(const OpalMediaFormat &, unsigned, PBoolean);\r
+    virtual void OnPatchMediaStream(PBoolean isSource, OpalMediaPatch & patch);\r
+    virtual OpalMediaFormatList GetMediaFormats() const;\r
+    virtual PBoolean SendUserInputTone(char tone, unsigned duration);\r
+\r
+    DECLARE_CALLBACK0(on_init);\r
+    DECLARE_CALLBACK0(on_destroy);\r
+    DECLARE_CALLBACK0(on_routing);\r
+    DECLARE_CALLBACK0(on_execute);\r
+    DECLARE_CALLBACK0(on_hangup);\r
+\r
+    DECLARE_CALLBACK0(on_exchange_media);\r
+    DECLARE_CALLBACK0(on_soft_execute);\r
+\r
+    DECLARE_CALLBACK1(kill_channel, int, sig);\r
+    DECLARE_CALLBACK1(send_dtmf, const switch_dtmf_t *, dtmf);\r
+    DECLARE_CALLBACK1(receive_message, switch_core_session_message_t *, msg);\r
+    DECLARE_CALLBACK1(receive_event, switch_event_t *, event);\r
+    DECLARE_CALLBACK0(state_change);\r
+    DECLARE_CALLBACK3(read_audio_frame, switch_frame_t **, frame, switch_io_flag_t, flags, int, stream_id);\r
+    DECLARE_CALLBACK3(write_audio_frame, switch_frame_t *, frame, switch_io_flag_t, flags, int, stream_id);\r
+    DECLARE_CALLBACK3(read_video_frame, switch_frame_t **, frame, switch_io_flag_t, flag, int, stream_id);\r
+    DECLARE_CALLBACK3(write_video_frame, switch_frame_t *, frame, switch_io_flag_t, flag, int, stream_id);\r
+\r
+    __inline switch_core_session_t *GetSession() const\r
+    {\r
+        return m_fsSession;\r
+    }\r
+\r
+    __inline switch_channel_t *GetChannel() const\r
+    {\r
+        return m_fsChannel;\r
+    }\r
+\r
+    bool IsChannelReady() const\r
+    {\r
+        return m_fsChannel != NULL && switch_channel_ready(m_fsChannel);\r
+    }\r
+\r
+    bool NeedFlushAudio()\r
+    {\r
+        if (!m_flushAudio)\r
+          return false;\r
+        m_flushAudio = false;\r
+        return true;\r
+    }\r
+\r
+  protected:\r
+    void SetCodecs();\r
+    bool WaitForMedia();\r
+\r
+    switch_status_t read_frame(const OpalMediaType & mediaType, switch_frame_t **frame, switch_io_flag_t flags);\r
+    switch_status_t write_frame(const OpalMediaType & mediaType, const switch_frame_t *frame, switch_io_flag_t flags);\r
+\r
+  private:\r
+    FSEndPoint            &m_endpoint;\r
+    switch_core_session_t *m_fsSession;\r
+    switch_channel_t      *m_fsChannel;\r
+    PSyncPoint             m_rxAudioOpened;\r
+    PSyncPoint             m_txAudioOpened;\r
+    OpalMediaFormatList    m_switchMediaFormats;\r
+\r
+    // If FS ever supports more than one audio and one video, this needs to change\r
+    switch_timer_t m_read_timer;\r
+    switch_codec_t m_read_codec;\r
+    switch_codec_t m_write_codec;\r
+\r
+    switch_timer_t m_vid_read_timer;\r
+    switch_codec_t m_vid_read_codec;\r
+    switch_codec_t m_vid_write_codec;\r
+\r
+    bool m_flushAudio;\r
+\r
+    friend PBoolean FSMediaStream::Open();\r
+};\r
+\r
+\r
+#endif /* __FREESWITCH_MOD_OPAL__ */\r
+\r
+/* For Emacs:\r
+ * Local Variables:\r
+ * mode:c\r
+ * indent-tabs-mode:nil\r
+ * tab-width:4\r
+ * c-basic-offset:4\r
+ * End:\r
+ * For VIM:\r
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:s:\r
+ */\r
index 650b89839954faf3972e53b4355b2d69a805f46d..5cb910859774c092443d21e744af695510cec8ea 100644 (file)
@@ -429,6 +429,10 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session)
                gateway_ptr = sofia_reg_find_gateway(gateway_name);
        }
 
+       if (!tech_pvt) {
+               return SWITCH_STATUS_SUCCESS;
+       }
+
        switch_mutex_lock(tech_pvt->sofia_mutex);
 
        rec = sofia_test_flag(tech_pvt, TFLAG_RECOVERING);
index 6d01de8a384a6ae0b9c2196cd4686284b4233b54..472b327ea8fdc9be0b624e05c4c32d793a03c7b5 100644 (file)
@@ -154,6 +154,7 @@ typedef struct sofia_dispatch_event_s {
        int save;
        switch_core_session_t *session;
        switch_memory_pool_t *pool;
+       struct sofia_dispatch_event_s *next;
 } sofia_dispatch_event_t;
 
 struct sofia_private {
@@ -166,6 +167,7 @@ struct sofia_private {
        int is_call;
        int is_static;
        sofia_dispatch_event_t *de;
+       sofia_dispatch_event_t *deq;
 };
 
 #define set_param(ptr,val) if (ptr) {free(ptr) ; ptr = NULL;} if (val) {ptr = strdup(val);}
index 58a658e9eabff921520a66782a9642e58bda19e8..bd1c0a88a832295ad2d68c77c5f7272c5a03174e 100644 (file)
@@ -969,18 +969,25 @@ static void our_sofia_event_callback(nua_event_t event,
        int locked = 0;
        int check_destroy = 1;
 
-       if (sofia_private && sofia_private->is_call && sofia_private->de) {
-               sofia_dispatch_event_t *qde = sofia_private->de;
-               sofia_private->de = NULL;
+       if (sofia_private && sofia_private->is_call) {
+               sofia_dispatch_event_t *qde = NULL;
 
-               if (event == nua_i_cancel) {
-                       nua_destroy_event(qde->event);
-                       su_free(nh->nh_home, qde);      
-               } else {
+               switch_mutex_lock(profile->flag_mutex);
+               if (sofia_private->de) {
+                       qde = sofia_private->de;
+                       sofia_private->de = NULL;
+               }
+               switch_mutex_unlock(profile->flag_mutex);
+
+               if (qde) {
                        sofia_process_dispatch_event(&qde);
                }
        }
 
+       if (sofia_private && (sofia_private->destroy_me == 12)) {
+               return;
+       }
+
        profile->last_sip_event = switch_time_now();
 
        /* sofia_private will be == &mod_sofia_globals.keep_private whenever a request is done with a new handle that has to be 
@@ -1525,22 +1532,51 @@ void sofia_process_dispatch_event_in_thread(sofia_dispatch_event_t **dep)
 
 void sofia_process_dispatch_event(sofia_dispatch_event_t **dep)
 {
-       sofia_dispatch_event_t *de = *dep;
+       sofia_dispatch_event_t *de = *dep, *deq = NULL;
        nua_handle_t *nh = de->nh;
        nua_t *nua = de->nua;
        sofia_profile_t *profile = de->profile;
-
+       sofia_private_t *sofia_private = nua_handle_magic(de->nh);
        *dep = NULL;
 
        our_sofia_event_callback(de->data->e_event, de->data->e_status, de->data->e_phrase, de->nua, de->profile, 
-                                                        de->nh, nua_handle_magic(de->nh), de->sip, de, (tagi_t *) de->data->e_tags);
+                                                        de->nh, sofia_private, de->sip, de, (tagi_t *) de->data->e_tags);
 
        nua_destroy_event(de->event);   
        su_free(nh->nh_home, de);
 
        switch_mutex_lock(profile->flag_mutex);
        profile->queued_events--;
+       if (sofia_private && sofia_private->is_call && sofia_private->deq) {
+               deq = sofia_private->deq;
+               sofia_private->deq = NULL;
+       }
        switch_mutex_unlock(profile->flag_mutex);
+
+       if (deq) {
+               for (;;) {
+                       switch_mutex_lock(profile->flag_mutex);
+                       if ((de = deq)) {
+                               deq = deq->next;
+                               de->next = NULL;
+                       }
+                       switch_mutex_unlock(profile->flag_mutex);
+
+                       if (!de) {
+                               break;
+                       }
+
+                       our_sofia_event_callback(de->data->e_event, de->data->e_status, de->data->e_phrase, de->nua, de->profile, 
+                                                                        de->nh, sofia_private, de->sip, de, (tagi_t *) de->data->e_tags);
+                       
+                       nua_destroy_event(de->event);   
+                       su_free(nh->nh_home, de);
+                       nua_handle_unref(nh);
+                       nua_stack_unref(nua);
+                       
+               }
+       }
+
        
        nua_handle_unref(nh);
        nua_stack_unref(nua);
@@ -1684,8 +1720,6 @@ void sofia_event_callback(nua_event_t event,
                return;
        }
 
-       
-
        switch_mutex_lock(profile->flag_mutex);
        profile->queued_events++;
        switch_mutex_unlock(profile->flag_mutex);
@@ -1707,7 +1741,9 @@ void sofia_event_callback(nua_event_t event,
                memset(sofia_private, 0, sizeof(*sofia_private));
                sofia_private->is_call++;
                sofia_private->is_static++;
+               switch_mutex_lock(profile->flag_mutex);
                sofia_private->de = de;
+               switch_mutex_unlock(profile->flag_mutex);
                nua_handle_bind(nh, sofia_private);
                return;
        }
@@ -1715,7 +1751,23 @@ void sofia_event_callback(nua_event_t event,
        if (sofia_private && sofia_private != &mod_sofia_globals.destroy_private && sofia_private != &mod_sofia_globals.keep_private) {
                switch_core_session_t *session;
 
-               if (!zstr(sofia_private->uuid)) {
+               if (zstr(sofia_private->uuid)) {
+                       if (sofia_private->is_call && !sofia_private->de) {
+                               sofia_dispatch_event_t *dep;
+
+                               switch_mutex_lock(profile->flag_mutex);
+
+                               if (!sofia_private->deq) {
+                                       sofia_private->deq = de;
+                               } else {
+                                       for (dep = sofia_private->deq; dep && dep->next; dep = dep->next);
+                                       dep->next = de;
+                               }
+
+                               switch_mutex_unlock(profile->flag_mutex);
+                               return;
+                       }
+               } else {
                        if ((session = switch_core_session_locate(sofia_private->uuid))) {
                                if (switch_core_session_running(session)) {
                                        switch_core_session_queue_signal_data(session, de);
@@ -1732,7 +1784,7 @@ void sofia_event_callback(nua_event_t event,
                        }
                }
        }
-
+       
        sofia_queue_message(de);
        switch_os_yield();
 }
@@ -6040,9 +6092,9 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
                        }
                }
 
-               if (sofia_private) {
-                       sofia_private->destroy_me = 1;
-               }
+               //              if (sofia_private) {
+                       //sofia_private->destroy_me = 1;
+               //}
        }
 
        if (session) {
@@ -8013,6 +8065,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
 
        profile->ib_calls++;
 
+
        if (sess_count >= sess_max || !sofia_test_pflag(profile, PFLAG_RUNNING)) {
                nua_respond(nh, 503, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END());
                goto fail;
@@ -8217,14 +8270,8 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
                nua_respond(nh, 503, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END());
                goto fail;
        }
-
-       if (!(tech_pvt = (private_object_t *) switch_core_session_alloc(session, sizeof(private_object_t)))) {
-               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Hey where is my memory pool?\n");
-               nua_respond(nh, SIP_503_SERVICE_UNAVAILABLE, TAG_END());
-               switch_core_session_destroy(&session);
-               goto fail;
-       }
-
+       
+       tech_pvt = (private_object_t *) switch_core_session_alloc(session, sizeof(private_object_t));
 
        switch_mutex_init(&tech_pvt->flag_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
        switch_mutex_init(&tech_pvt->sofia_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
@@ -9031,6 +9078,10 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
        }
        switch_copy_string(tech_pvt->sofia_private->uuid, switch_core_session_get_uuid(session), sizeof(tech_pvt->sofia_private->uuid));
 
+       if (switch_core_session_running(session) || switch_core_session_started(session)) {
+               return;
+       }
+
        if (sip && switch_core_session_thread_launch(session) == SWITCH_STATUS_SUCCESS) {
                const char *dialog_from_user = "", *dialog_from_host = "", *to_user = "", *to_host = "", *contact_user = "", *contact_host = "";
                const char *user_agent = "", *call_id = "";
@@ -9119,6 +9170,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Setting NAT mode based on %s\n", is_nat);
                        switch_channel_set_variable(channel, "sip_nat_detected", "true");
                }
+
                return;
        }
 
@@ -9140,10 +9192,12 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
                switch_mutex_unlock(tech_pvt->profile->flag_mutex);
        }
 
-       nua_handle_bind(nh, NULL);
-       sofia_private_free(sofia_private);
-       switch_core_session_destroy(&session);
-       nua_respond(nh, 503, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END());
+       if (!switch_core_session_running(session)) {
+               nua_handle_bind(nh, NULL);
+               sofia_private_free(sofia_private);
+               switch_core_session_destroy(&session);
+               nua_respond(nh, 503, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END());
+       }
        return;
 
   fail:
index 4085a68de07fb13f927dfca00b68b4c86050dc35..8b8ceae9cd6c782d69da363238b6b9260318a4de 100644 (file)
@@ -1,7 +1,7 @@
 <include>
 
   <extension name="auth" continue="true">
-    <condition>
+    <condition field="${radius_auth_result}" expression="^$">
       <action application="radius_auth" inline="true"/>
     </condition>
   </extension>
     </condition>
   </extension>
 
+  <extension name="originate_leg" continue="true">
+    <condition>
+      <action application="export" data="nolocal:h323-call-origin=originate"/>
+    </condition>
+  </extension>
 </include>
 
index 9a5176bcee5497a9425d3a60c7415c78f8a377a9..2a74f9cff9ea9b6f7bb9ac4923ffd71d5f0361c6 100644 (file)
@@ -34,6 +34,7 @@
 static struct {
        switch_memory_pool_t *pool;
        switch_xml_t auth_invite_configs;
+       switch_xml_t auth_reg_configs;
        switch_xml_t auth_app_configs;
        switch_xml_t acct_start_configs;
        switch_xml_t acct_end_configs;
@@ -149,7 +150,42 @@ switch_status_t do_config()
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Could not find 'auth_invite' section in config file.\n");               
        }
        
-       if ((tmp = switch_xml_dup(switch_xml_child(cfg, "auth_app"))) == NULL ) {
+       serv = timeout = deadtime = retries = dict = seq = 0;
+       if ((tmp = switch_xml_dup(switch_xml_child(cfg, "auth_reg"))) != NULL ) {
+               if ( (server = switch_xml_child(tmp, "connection")) != NULL) {
+                               for (param = switch_xml_child(server, "param"); param; param = param->next) {
+                                       char *var = (char *) switch_xml_attr_soft(param, "name");
+                                       if ( strncmp(var, "authserver", 10) == 0 ) {
+                                               serv = 1;
+                                       } else if ( strncmp(var, "radius_timeout", 14) == 0 ) {
+                                               timeout = 1;
+                                       } else if ( strncmp(var, "radius_deadtime", 15) == 0 ) {
+                                               deadtime = 1;
+                                       } else if ( strncmp(var, "radius_retries", 14) == 0 ) {
+                                               retries = 1;
+                                       } else if ( strncmp(var, "dictionary", 10) == 0 ) {
+                                               dict = 1;
+                                       } else if ( strncmp(var, "seqfile", 7) == 0 ) {
+                                               seq = 1;
+                                       }
+                               }
+                               
+                               if ( serv && timeout && deadtime && retries && dict && seq ) {
+                                       globals.auth_reg_configs = tmp;
+                               } else {
+                                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing a require section for radius connections\n");
+                                       goto err;
+                               }
+               } else {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not find 'connection' section for auth_invite\n");
+                       goto err;
+               }               
+       } else {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Could not find 'auth_invite' section in config file.\n");               
+       }
+       
+       serv = timeout = deadtime = retries = dict = seq = 0;
+       if ((tmp = switch_xml_dup(switch_xml_child(cfg, "auth_app"))) != NULL ) {
                if ( (server = switch_xml_child(tmp, "connection")) != NULL) {
                                for (param = switch_xml_child(server, "param"); param; param = param->next) {
                                        char *var = (char *) switch_xml_attr_soft(param, "name");
@@ -182,7 +218,8 @@ switch_status_t do_config()
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Could not find 'auth_app' section in config file.\n");          
        }
        
-       if (( tmp = switch_xml_dup(switch_xml_child(cfg, "acct_start"))) == NULL ) {
+       serv = timeout = deadtime = retries = dict = seq = 0;
+       if (( tmp = switch_xml_dup(switch_xml_child(cfg, "acct_start"))) != NULL ) {
                if ( (server = switch_xml_child(tmp, "connection")) != NULL) {
                                for (param = switch_xml_child(server, "param"); param; param = param->next) {
                                        char *var = (char *) switch_xml_attr_soft(param, "name");
@@ -215,7 +252,8 @@ switch_status_t do_config()
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Could not find 'acct_start' section in config file.\n");                
        }
        
-       if (( tmp = switch_xml_dup(switch_xml_child(cfg, "acct_end"))) == NULL ) {
+       serv = timeout = deadtime = retries = dict = seq = 0;
+       if (( tmp = switch_xml_dup(switch_xml_child(cfg, "acct_end"))) != NULL ) {
                if ( (server = switch_xml_child(tmp, "connection")) != NULL) {
                                for (param = switch_xml_child(server, "param"); param; param = param->next) {
                                        char *var = (char *) switch_xml_attr_soft(param, "name");
@@ -281,8 +319,11 @@ switch_status_t mod_xml_radius_add_params(switch_core_session_t *session, switch
                char *var = (char *) switch_xml_attr(param, "name");
                char *vend = (char *) switch_xml_attr(param, "vendor");
                char *variable = (char *) switch_xml_attr(param, "variable");
+               char *variable_secondary = (char *) switch_xml_attr(param, "variable_secondary");
+               char *val_default = (char *) switch_xml_attr(param, "default");
                char *format = (char *) switch_xml_attr(param, "format");
-               
+               char *other_leg = (char *) switch_xml_attr(param, "other_leg");
+
                attribute = rc_dict_findattr(handle, var);
                
                if ( attribute == NULL ) {
@@ -375,8 +416,36 @@ switch_status_t mod_xml_radius_add_params(switch_core_session_t *session, switch
                                        }                       
                                        
                                } else {
+                                       if ( format == NULL ) {
+                                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing format attribute for %s variable\n", variable);
+                                               goto err;
+                                       }
+
                                        if ( attribute->type == 0 ) {
-                                               av_value = switch_mprintf(format, switch_channel_get_variable(channel, variable));
+                                               const char *val = NULL;
+                                               
+                                               if ( other_leg ) {
+                                                       val = switch_channel_get_variable_partner(channel, variable);
+                                                       if ( val == NULL && variable_secondary != NULL) {
+                                                               val = switch_channel_get_variable_partner(channel, variable_secondary);
+                                                       }
+                                               } else {
+                                                       val = switch_channel_get_variable(channel, variable);
+                                                       if ( val == NULL && variable_secondary != NULL) {
+                                                               val = switch_channel_get_variable(channel, variable_secondary);
+                                                       }
+                                               }
+                                               
+                                               if ( val == NULL && val_default != NULL) {
+                                                       av_value = switch_mprintf(format, val_default);                                                 
+                                               } else {
+                                                       av_value = switch_mprintf(format, val);
+                                               }
+                                               
+                                               if ( GLOBAL_DEBUG ) {
+                                                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: value: %s\n", (char *) av_value);
+                                               }
+                               
                                                if (rc_avpair_add(handle, send, attr_num, av_value, -1, vend_num) == NULL) {
                                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add option with val '%s' to handle\n", (char *) av_value);
                                                        goto err;
@@ -487,7 +556,10 @@ switch_xml_t mod_xml_radius_auth_invite(switch_event_t *params) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: starting invite authentication\n");
        }
        
-       mod_xml_radius_new_handle(&new_handle, globals.auth_invite_configs);
+       if ( mod_xml_radius_new_handle(&new_handle, globals.auth_invite_configs) != SWITCH_STATUS_SUCCESS ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to load radius handle\n");
+               goto err;               
+       }
 
        if ( new_handle == NULL ) {
                goto err;
@@ -565,6 +637,100 @@ switch_xml_t mod_xml_radius_auth_invite(switch_event_t *params) {
        return NULL;
 }
 
+switch_xml_t mod_xml_radius_auth_reg(switch_event_t *params) {
+       int result = 0, param_idx = 0;
+       VALUE_PAIR *send = NULL, *recv = NULL, *service_vp = NULL;
+       char msg[512 * 10 + 1] = {0};
+       uint32_t service = PW_AUTHENTICATE_ONLY;
+       rc_handle *new_handle = NULL;
+       switch_xml_t fields, xml, dir, dom, usr, vars, var;
+       char name[512], value[512], *strtmp;
+
+       if (GLOBAL_DEBUG ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: starting registration authentication\n");
+       }
+       
+       if ( mod_xml_radius_new_handle(&new_handle, globals.auth_invite_configs) != SWITCH_STATUS_SUCCESS ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to load radius handle\n");
+               goto err;               
+       }
+
+       if ( new_handle == NULL ) {
+               goto err;
+       }
+       
+       if ((fields = switch_xml_child(globals.auth_reg_configs, "fields")) == NULL ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not find 'fields' section in config file.\n");           
+               goto err;
+       }
+       
+       if ( mod_xml_radius_add_params(NULL, params, new_handle, &send, fields) != SWITCH_STATUS_SUCCESS ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to add params to rc_handle\n");         
+               goto err;
+       }
+       
+       if (rc_avpair_add(new_handle, &send, PW_SERVICE_TYPE, &service, -1, 0) == NULL) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add option to handle\n");
+               goto err;
+       }
+       
+       result = rc_auth(new_handle, 0, send, &recv, msg);
+       
+       if ( GLOBAL_DEBUG ){
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: result(RC=%d) %s \n", result, msg);
+       }
+       
+       if ( result != 0 ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: Failed to authenticate\n");
+               goto err;
+       }
+
+       xml = switch_xml_new("document");
+       switch_xml_set_attr_d(xml, "type", "freeswitch/xml");
+       dir = switch_xml_add_child_d(xml, "section", 0);
+       switch_xml_set_attr_d(dir, "name", "directory");
+       dom = switch_xml_add_child_d(dir, "domain", 0);
+       switch_xml_set_attr_d(dom, "name", switch_event_get_header(params, "domain"));
+       usr = switch_xml_add_child_d(dom, "user", 0);
+       vars = switch_xml_add_child_d(usr, "variables", 0);
+       
+       switch_xml_set_attr_d(usr, "id", switch_event_get_header(params, "user"));
+               
+       service_vp = recv;
+       while (service_vp != NULL) {
+               rc_avpair_tostr(new_handle, service_vp, name, 512, value, 512);
+               if ( GLOBAL_DEBUG )
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "\tattribute (%s)[%s] found in radius packet\n", name, value);
+               var = switch_xml_add_child_d(vars, "variable", param_idx++);
+               strtmp = strdup(name);
+               switch_xml_set_attr_d(var, "name", strtmp);
+               free(strtmp);
+               strtmp = strdup(value);
+               switch_xml_set_attr_d(var, "value", strtmp);
+               free(strtmp);
+               service_vp = service_vp->next;
+       }
+
+       if ( GLOBAL_DEBUG ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XML: %s \n", switch_xml_toxml(xml, 1));
+       }
+       
+       rc_avpair_free(recv);
+       rc_destroy(new_handle);
+       return xml;
+ err:
+       if ( recv ) {
+               rc_avpair_free(recv);
+               recv = NULL;
+       }
+       if ( new_handle ) {
+               rc_destroy(new_handle);
+               new_handle = NULL;
+       }
+       
+       return NULL;
+}
+
 static switch_xml_t mod_xml_radius_directory_search(const char *section, const char *tag_name, const char *key_name, const char *key_value, 
                                                                                                        switch_event_t *params, void *user_data)
 {
@@ -584,9 +750,11 @@ static switch_xml_t mod_xml_radius_directory_search(const char *section, const c
        if ( auth_method == NULL) {
                return NULL;
        }
-
+       
        if ( strncmp( "INVITE", auth_method, 6) == 0) {
                xml = mod_xml_radius_auth_invite(params);
+       } else if ( strncmp( "REGISTER", auth_method, 8) == 0) {
+               xml = mod_xml_radius_auth_reg(params);
        } else {
                xml = NULL;
        }
@@ -622,6 +790,7 @@ switch_status_t mod_xml_radius_check_conditions(switch_channel_t *channel, switc
                        }
                        
                        if ( switch_regex_match( switch_channel_get_variable(channel, channel_var), regex) != SWITCH_STATUS_SUCCESS) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Didn't match: %s == %s \n", switch_channel_get_variable(channel, channel_var), regex);
                                all_matched = 0;
                        }
                }
@@ -643,6 +812,7 @@ switch_status_t mod_xml_radius_accounting_start(switch_core_session_t *session){
 
        if (GLOBAL_DEBUG ) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: starting accounting start\n");
+               switch_core_session_execute_application(session, "info", NULL);
        }
 
        /* If there are conditions defined, and none of them pass, then skip this accounting */
@@ -651,7 +821,10 @@ switch_status_t mod_xml_radius_accounting_start(switch_core_session_t *session){
                goto end;
        }
        
-       mod_xml_radius_new_handle(&new_handle, globals.acct_start_configs);
+       if ( mod_xml_radius_new_handle(&new_handle, globals.acct_start_configs) != SWITCH_STATUS_SUCCESS ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to load radius handle\n");
+               goto end;               
+       }
 
        if ((fields = switch_xml_child(globals.acct_start_configs, "fields")) == NULL ) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not find 'fields' section in config file.\n");           
@@ -700,7 +873,10 @@ switch_status_t mod_xml_radius_accounting_end(switch_core_session_t *session){
                goto end;
        }
        
-       mod_xml_radius_new_handle(&new_handle, globals.acct_end_configs);
+       if ( mod_xml_radius_new_handle(&new_handle, globals.acct_end_configs) != SWITCH_STATUS_SUCCESS ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to load radius handle\n");
+               goto end;               
+       }
 
        if ((fields = switch_xml_child(globals.acct_end_configs, "fields")) == NULL ) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not find 'fields' section in config file.\n");           
@@ -724,8 +900,10 @@ switch_status_t mod_xml_radius_accounting_end(switch_core_session_t *session){
        }
 
  end:
-       rc_destroy(new_handle);
-
+       if ( new_handle) {
+               rc_destroy(new_handle);
+       }
+       
        return SWITCH_STATUS_SUCCESS;
 }
 
index cb0ec3a01a67415b62bc4cf34fc461aee84d9460..eb3070c929e47531a7436fe1550665d2f22b4e30 100644 (file)
       <param vendor="Cisco" name="Cisco-AVPair" variable="ip" format="src-gw-ip=%s"/>
       <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_user" format="src-gw-name=%s"/>
       <param vendor="Cisco" name="h323-conf-id" variable="Core-UUID" format="%s"/>
+      <param vendor="Cisco" name="Cisco-AVPair" variable="ip" format="number"/>
       <param name="Called-Station-Id" variable="sip_to_user" format="%s"/>
       <param name="Calling-Station-Id" variable="sip_from_user" format="%s"/>
+      <param name="User-Name" variable="sip_from_user" format="%s"/>
+      <param name="Digest-Response" variable="sip_auth_response" format="%s"/>
+      <param name="Digest-Realm" variable="sip_auth_realm" format="%s"/>
+      <param name="Digest-Nonce" variable="sip_auth_nonce" format="%s"/>
+      <param name="Digest-Username" variable="sip_auth_username" format="%s"/> 
+      <param name="Digest-URI" variable="sip_auth_uri" format="%s"/>
+      <param name="Digest-Method" variable="sip_auth_method" format="%s"/>
+      <param name="Digest-Algorithm" variable="sip_auth_method" format="MD5"/>
+      <param name="Digest-Qop" variable="sip_auth_qop" format="%s"/>
+      <param name="Digest-CNonce" variable="sip_auth_cnonce" format="%s"/>
+      <param name="Digest-Nonce-Count" variable="sip_auth_nc" format="%s"/> 
     </fields>
   </auth_invite>
+  <auth_reg>
+    <connection name="testing">
+      <param name="authserver" value="127.0.0.1:1812:testing123"/>
+      <param name="radius_timeout" value="10"/>
+      <param name="radius_retries" value="2"/>
+      <param name="radius_deadtime" value="0"/>
+      <param name="dictionary" value="/usr/local/src/freeswitch/src/mod/xml_int/mod_xml_radius/dictionaries/dictionary"/>
+      <param name="seqfile" value="/var/run/radius.seq"/>
+    </connection>
+    <fields>
+      <param vendor="Cisco" name="Cisco-AVPair" variable="ip" format="user"/>
+      <param vendor="Cisco" name="Cisco-AVPair" variable="ip" format="src-gw-ip=%s"/>
+      <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_user" format="src-gw-name=%s"/>
+      <param name="User-Name" variable="sip_from_user" format="%s"/>
+      <param name="Digest-Response" variable="sip_auth_response" format="%s"/>
+      <param name="Digest-Realm" variable="sip_auth_realm" format="%s"/>
+      <param name="Digest-Nonce" variable="sip_auth_nonce" format="%s"/>
+      <param name="Digest-Username" variable="sip_auth_username" format="%s"/>
+      <param name="Digest-URI" variable="sip_auth_uri" format="%s"/>
+      <param name="Digest-Method" variable="sip_auth_method" format="%s"/>
+      <param name="Digest-Algorithm" variable="sip_auth_method" format="MD5"/>
+      <param name="Digest-Qop" variable="sip_auth_qop" format="%s"/>
+      <param name="Digest-CNonce" variable="sip_auth_cnonce" format="%s"/>
+      <param name="Digest-Nonce-Count" variable="sip_auth_nc" format="%s"/>
+    </fields>
+  </auth_reg>
   <auth_app>
     <connection name="testing">
       <param name="authserver" value="127.0.0.1:1812:testing123"/>
@@ -36,7 +74,7 @@
       <param name="seqfile" value="/var/run/radius.seq"/>
     </connection>
     <fields>
-      <param vendor="Cisco" name="Cisco-AVPair" variable="sip_network_ip" format="src-gw-ip=%s"/>
+      <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_host" format="src-gw-ip=%s"/>
       <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_user" format="src-gw-name=%s"/>
       <param vendor="Cisco" name="h323-conf-id" variable="Core-UUID" format="%s"/>
       <param name="Called-Station-Id" variable="sip_to_user" format="%s"/>
     <fields>
       <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_host" format="src-gw-ip=%s"/>
       <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_user" format="src-gw-name=%s"/>
-      <param vendor="Cisco" name="Cisco-AVPair" variable="sip_to_host" format="dst-gw-ip=%s"/>
-      <param vendor="Cisco" name="h323-conf-id" variable="uuid" format="%s"/>
+      <param vendor="Cisco" name="Cisco-AVPair" variable="sip_to_host" format="dst-gw-ip=%s"/>  
+      <param vendor="Cisco" name="h323-conf-id" variable_secondary="uuid" variable="originating_leg_uuid" format="%s"/>
       <param vendor="Cisco" name="h323-setup-time"/>
       <param vendor="Cisco" name="h323-connect-time"/>
+      <param vendor="Cisco" name="h323-call-origin" variable="h323-call-origin" default="answer" format="%s"/>
       <param name="Called-Station-Id" variable="sip_to_user" format="%s"/>
       <param name="Calling-Station-Id" variable="sip_from_user" format="%s"/>
     </fields>
-    <conditions>
+<!--    <conditions>
       <condition>
-       <param var="direction" regex="inbound"/>
-      </condition>
-    </conditions>
+       <param var="direction" regex="^outbound$"/>
+      </condition> 
+    </conditions> -->
   </acct_start>
   <acct_end>
     <connection name="testing">
     <fields>
       <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_host" format="src-gw-ip=%s"/>
       <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_user" format="src-gw-name=%s"/>
-      <param vendor="Cisco" name="Cisco-AVPair" variable="sip_to_host" format="dst-gw-ip=%s"/>
-      <param vendor="Cisco" name="h323-conf-id" variable="uuid" format="%s"/>
+      <param vendor="Cisco" name="Cisco-AVPair" variable="sip_to_host" format="dst-gw-ip=%s"/> 
+      <param vendor="Cisco" name="h323-conf-id" variable_secondary="uuid" variable="originating_leg_uuid" format="%s"/>
       <param vendor="Cisco" name="h323-setup-time"/>
       <param vendor="Cisco" name="h323-connect-time"/>
       <param vendor="Cisco" name="h323-disconnect-time"/>
       <param vendor="Cisco" name="h323-disconnect-cause"/>
+      <param vendor="Cisco" name="h323-call-origin" variable="h323-call-origin" format="%s" default="answer"/>
       <param name="Called-Station-Id" variable="sip_to_user" format="%s"/>
       <param name="Acct-Session-Time" variable="billsec" format="%s"/>
       <param name="Calling-Station-Id" variable="sip_from_user" format="%s"/>
       <param vendor="Cisco" name="Cisco-AVPair" variable="sip_from_user" format="src-number-out=%s"/>
       <param vendor="Cisco" name="Cisco-AVPair" variable="sip_to_user" format="dst-number-out=%s"/>
     </fields>
-    <conditions>
+<!--    <conditions>
       <condition>
-       <param var="direction" regex="inbound"/>
-      </condition>
-    </conditions>
+       <param var="direction" regex="^outbound$"/>
+      </condition> 
+    </conditions> -->
   </acct_end>
 </configuration>
index a8f05f19283aa05d571c3b0cfe1c3b95d8490a33..2a3a7ad04fc9471e322bab39c2483a0c8f565ffa 100644 (file)
@@ -576,10 +576,15 @@ SWITCH_DECLARE(void) switch_channel_uninit(switch_channel_t *channel)
        while (switch_queue_trypop(channel->dtmf_log_queue, &pop) == SWITCH_STATUS_SUCCESS) {
                switch_safe_free(pop);
        }
-       switch_core_hash_destroy(&channel->private_hash);
+
+       if (channel->private_hash) {
+               switch_core_hash_destroy(&channel->private_hash);
+       }
+
        if (channel->app_flag_hash) {
                switch_core_hash_destroy(&channel->app_flag_hash);
        }
+
        switch_mutex_lock(channel->profile_mutex);
        switch_event_destroy(&channel->variables);
        switch_event_destroy(&channel->api_list);
index 38a066b020f44c43109cc7a8c41906d8470268a4..aedceaf435026b92de9bf79ff0e38151f7345e8a 100644 (file)
@@ -80,8 +80,8 @@ SWITCH_DECLARE(void *) switch_core_perform_session_alloc(switch_core_session_t *
 
 #ifdef DEBUG_ALLOC
        if (memory > 500)
-               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Session Allocate %s %d\n", 
-                                                 apr_pool_tag(session->pool, NULL), (int) memory);
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p %p Session Allocate %s %d\n", 
+                                                 (void *) session->pool, (void *) session, apr_pool_tag(session->pool, NULL), (int) memory);
 #endif
 
        ptr = apr_palloc(session->pool, memory);
@@ -113,8 +113,8 @@ SWITCH_DECLARE(void *) switch_core_perform_permanent_alloc(switch_size_t memory,
 #endif
 
 #ifdef DEBUG_ALLOC
-       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Perm Allocate %s %d\n", 
-                                         apr_pool_tag(memory_manager.memory_pool, NULL), (int) memory);
+       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Perm Allocate %s %d\n", 
+                                         (void *)memory_manager.memory_pool, apr_pool_tag(memory_manager.memory_pool, NULL), (int) memory);
 #endif
 
        ptr = apr_palloc(memory_manager.memory_pool, memory);
@@ -155,8 +155,8 @@ SWITCH_DECLARE(char *) switch_core_perform_permanent_strdup(const char *todup, c
        switch_assert(duped != NULL);
 
 #ifdef DEBUG_ALLOC
-       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Perm Allocate %s %d\n", 
-                                         apr_pool_tag(memory_manager.memory_pool, NULL), (int) len);
+       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Perm Allocate %s %d\n", 
+                                         (void *) memory_manager.memory_pool, apr_pool_tag(memory_manager.memory_pool, NULL), (int) len);
 #endif
 
 #ifdef LOCK_MORE
@@ -248,8 +248,8 @@ SWITCH_DECLARE(char *) switch_core_perform_session_strdup(switch_core_session_t
 #ifdef DEBUG_ALLOC
        len = strlen(todup);
        if (len > 500)
-               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Sess Strdup Allocate %s %ld\n", 
-                                                 apr_pool_tag(session->pool, NULL), strlen(todup));
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p %p Sess Strdup Allocate %s %ld\n", 
+                                                 (void *) session->pool, (void *)session, apr_pool_tag(session->pool, NULL), strlen(todup));
 #endif
 
        duped = apr_pstrdup(session->pool, todup);
@@ -287,8 +287,8 @@ SWITCH_DECLARE(char *) switch_core_perform_strdup(switch_memory_pool_t *pool, co
 
 #ifdef DEBUG_ALLOC
        if (len > 500)
-               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Core Strdup Allocate %s %d\n", 
-                                                 apr_pool_tag(pool, NULL), (int)len);
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Core Strdup Allocate %s %d\n", 
+                                                 (void *) pool, apr_pool_tag(pool, NULL), (int)len);
 #endif
 
        duped = apr_pstrmemdup(pool, todup, len);
@@ -400,7 +400,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_new_memory_pool(switch_memor
        apr_pool_tag(*pool, tmp);
 
 #ifdef DEBUG_ALLOC2
-       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "New Pool %s\n", apr_pool_tag(*pool, NULL));
+       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p New Pool %s\n", (void *) *pool, apr_pool_tag(*pool, NULL));
 #endif
 
 
@@ -416,7 +416,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_destroy_memory_pool(switch_m
        switch_assert(pool != NULL);
 
 #ifdef DEBUG_ALLOC2
-       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Free Pool %s\n", apr_pool_tag(*pool, NULL));
+       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Free Pool %s\n", (void *) *pool, apr_pool_tag(*pool, NULL));
 #endif
 
 #ifdef INSTANTLY_DESTROY_POOLS
@@ -458,8 +458,8 @@ SWITCH_DECLARE(void *) switch_core_perform_alloc(switch_memory_pool_t *pool, swi
 
 #ifdef DEBUG_ALLOC
        if (memory > 500)
-               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Core Allocate %s %d\n", 
-                                                 apr_pool_tag(pool, NULL), (int) memory);
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Core Allocate %s %d\n", 
+                                                 (void *) pool, apr_pool_tag(pool, NULL), (int) memory);
        /*switch_assert(memory < 20000); */
 #endif
 
@@ -525,16 +525,26 @@ static void *SWITCH_THREAD_FUNC pool_thread(switch_thread_t *thread, void *obj)
 #ifdef USE_MEM_LOCK
                                switch_mutex_lock(memory_manager.mem_lock);
 #endif
+
+#ifdef DEBUG_ALLOC
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);   
+#endif
                                apr_pool_destroy(pop);
 #ifdef USE_MEM_LOCK
                                switch_mutex_unlock(memory_manager.mem_lock);
 #endif
 #else
                                apr_pool_mutex_set(pop, NULL);
+#ifdef DEBUG_ALLOC
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);   
+#endif
                                apr_pool_clear(pop);
                                if (switch_queue_trypush(memory_manager.pool_recycle_queue, pop) != SWITCH_STATUS_SUCCESS) {
 #ifdef USE_MEM_LOCK
                                        switch_mutex_lock(memory_manager.mem_lock);
+#endif
+#ifdef DEBUG_ALLOC
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%p DESTROY POOL\n", (void *) pop);   
 #endif
                                        apr_pool_destroy(pop);
 #ifdef USE_MEM_LOCK
index e3262899a6b6539f2ae029f0926ca9487ada79b6..4113afd1e016e336c9b51e4fbd22f8ac064781a9 100644 (file)
@@ -1260,6 +1260,11 @@ SWITCH_DECLARE(void) switch_core_session_perform_destroy(switch_core_session_t *
        switch_endpoint_interface_t *endpoint_interface = (*session)->endpoint_interface;
        int i;
 
+       if (switch_core_session_running(*session) && !switch_test_flag((*session), SSF_DESTROYABLE)) {
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(*session), SWITCH_LOG_ERROR,
+                                                 "Cowardly ignoring an attempt to call destroy on a running session.\n");
+       }
+
        switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(*session), SWITCH_LOG_NOTICE, "Close Channel %s [%s]\n",
                                          switch_channel_get_name((*session)->channel), switch_channel_state_name(switch_channel_get_state((*session)->channel)));
 
@@ -1440,6 +1445,8 @@ static void *SWITCH_THREAD_FUNC switch_core_session_thread(switch_thread_t *thre
 
        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Session %" SWITCH_SIZE_T_FMT " (%s) Ended\n",
                                          session->id, switch_channel_get_name(session->channel));
+
+       switch_set_flag(session, SSF_DESTROYABLE);
        switch_core_session_destroy(&session);
        return NULL;
 }
@@ -1454,6 +1461,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_thread_launch(switch_core_se
        switch_threadattr_detach_set(thd_attr, 1);
 
        if (switch_test_flag(session, SSF_THREAD_RUNNING) || switch_test_flag(session, SSF_THREAD_STARTED)) {
+               status = SWITCH_STATUS_INUSE;
                goto end;
        }
 
index e567daa9fb3647373700bd2cdbe80cd65fa66600..46d49e688301a04df031f2f7d9f5288e91573dd6 100644 (file)
@@ -336,7 +336,7 @@ SWITCH_DECLARE(void) switch_core_session_run(switch_core_session_t *session)
        const switch_state_handler_table_t *driver_state_handler = NULL;
        const switch_state_handler_table_t *application_state_handler = NULL;
        int silly = 0;
-       uint32_t new_loops = 60000;
+       uint32_t new_loops = 5000;
 
        /*
           Life of the channel. you have channel and pool in your session
index f2aec42fdd91570124921542cc6190cd7f931806..3ca4dd271533a9d646e01f31f1766d5c5e8f79a7 100644 (file)
@@ -761,7 +761,7 @@ static switch_status_t uuid_bridge_on_hibernate(switch_core_session_t *session)
 static switch_status_t uuid_bridge_on_soft_execute(switch_core_session_t *session)
 {
        switch_channel_t *channel = switch_core_session_get_channel(session);
-       switch_core_session_t *other_session = NULL;
+       switch_core_session_t *other_session;
        const char *other_uuid = NULL;
 
        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s CUSTOM SOFT_EXECUTE\n", switch_channel_get_name(channel));
@@ -818,6 +818,7 @@ static switch_status_t uuid_bridge_on_soft_execute(switch_core_session_t *sessio
                switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
 
                if (switch_ivr_wait_for_answer(session, other_session) != SWITCH_STATUS_SUCCESS) {
+                       switch_core_session_rwunlock(other_session);
                        if (switch_true(switch_channel_get_variable(channel, "uuid_bridge_continue_on_cancel"))) {
                                switch_channel_set_state(channel, CS_EXECUTE);
                        } else if (!switch_channel_test_flag(channel, CF_TRANSFER)) {
@@ -842,6 +843,7 @@ static switch_status_t uuid_bridge_on_soft_execute(switch_core_session_t *sessio
                                        switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
                                }
                        }
+                       switch_core_session_rwunlock(other_session);
                        goto done;
                }
 
@@ -867,17 +869,13 @@ static switch_status_t uuid_bridge_on_soft_execute(switch_core_session_t *sessio
                        !switch_channel_test_flag(channel, CF_REDIRECT) && state < CS_HANGUP && state != CS_ROUTING && state != CS_PARK) {
                        switch_channel_set_state(channel, CS_EXECUTE);
                }
+               switch_core_session_rwunlock(other_session);
        } else {
                switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
        }
 
  done:
 
-       if (other_session) {
-               switch_core_session_rwunlock(other_session);
-               other_session = NULL;
-       }
-
        switch_channel_clear_flag_recursive(channel, CF_BRIDGE_ORIGINATOR);
 
        return SWITCH_STATUS_FALSE;