[Freeswitch-svn] [commit] r14044 - in freeswitch/trunk/contrib/mod/endpoints/mod_khomp: . commons include src
FreeSWITCH SVN
raulfragoso at freeswitch.org
Tue Jun 30 00:38:50 PDT 2009
Author: raulfragoso
Date: Tue Jun 30 02:38:50 2009
New Revision: 14044
Log:
Implemented some helper functions to start/stop the board mixer and audio callbacks;
The channel states are now working as they should !
Modified:
freeswitch/trunk/contrib/mod/endpoints/mod_khomp/commons/k3lapi.cpp
freeswitch/trunk/contrib/mod/endpoints/mod_khomp/include/khomp_pvt.h
freeswitch/trunk/contrib/mod/endpoints/mod_khomp/mod_khomp.cpp
freeswitch/trunk/contrib/mod/endpoints/mod_khomp/src/khomp_pvt.cpp
Modified: freeswitch/trunk/contrib/mod/endpoints/mod_khomp/commons/k3lapi.cpp
==============================================================================
--- freeswitch/trunk/contrib/mod/endpoints/mod_khomp/commons/k3lapi.cpp (original)
+++ freeswitch/trunk/contrib/mod/endpoints/mod_khomp/commons/k3lapi.cpp Tue Jun 30 02:38:50 2009
@@ -40,24 +40,24 @@
void K3LAPI::mixer(int32 dev, int32 obj, byte track, KMixerSource src, int32 index)
{
- KMixerCommand mix;
+ KMixerCommand mix;
mix.Track = track;
- mix.Source = src;
+ mix.Source = src;
mix.SourceIndex = index;
- command(dev, obj, CM_MIXER, (const char *) &mix);
+ command(dev, obj, CM_MIXER, (const char *) &mix);
}
void K3LAPI::mixerCTbus(int32 dev, int32 obj, byte track, KMixerSource src, int32 index)
{
- KMixerCommand mix;
+ KMixerCommand mix;
mix.Track = track;
- mix.Source = src;
+ mix.Source = src;
mix.SourceIndex = index;
- command(dev, obj, CM_MIXER_CTBUS, (const char *) &mix);
+ command(dev, obj, CM_MIXER_CTBUS, (const char *) &mix);
}
void K3LAPI::command(int32 dev, int32 obj, int32 code, std::string & str)
@@ -69,12 +69,12 @@
{
K3L_COMMAND cmd;
- cmd.Cmd = code;
+ cmd.Cmd = code;
cmd.Object = obj;
- cmd.Params = (byte *)parms;
+ cmd.Params = (byte *)parms;
int32 rc = k3lSendCommand(dev, &cmd);
-
+
if (rc != ksSuccess)
throw failed_command(code, dev, obj, rc);
}
Modified: freeswitch/trunk/contrib/mod/endpoints/mod_khomp/include/khomp_pvt.h
==============================================================================
--- freeswitch/trunk/contrib/mod/endpoints/mod_khomp/include/khomp_pvt.h (original)
+++ freeswitch/trunk/contrib/mod/endpoints/mod_khomp/include/khomp_pvt.h Tue Jun 30 02:38:50 2009
@@ -3,6 +3,8 @@
#include "globals.h"
+#define KHOMP_PACKET_SIZE 16
+
/*!
\brief This struct holds a static linked list representing all the Khomp channels
found in the host. It's also a place holder for session objects and some
@@ -14,15 +16,37 @@
typedef std::vector < KhompPvt * > PvtVectorType; /*!< Collection of pointers of KhompPvts */
typedef std::vector < PvtVectorType > PvtVector2Type; /*!< Collection of PvtVectorType */
typedef PvtVector2Type KhompPvtVector; /*!< A bidimensional array o KhompPvts, meaning [board][channel] */
-
+
+ K3LAPI::target _target; /*!< The card/device pair to bind this pvt to */
+ switch_core_session_t * _session; /*!< The session to which this pvt is associated with */
+
+ unsigned int flags; //TODO: Alterar o nome depois
+
+ switch_codec_t _read_codec;
+ switch_codec_t _write_codec;
+
+ switch_frame_t _read_frame;
+
+ switch_buffer_t * _audio_buffer; /*!< Audio buffer used to write data in the khomp callback and read by FS callback */
+
+ unsigned char _databuf[SWITCH_RECOMMENDED_BUFFER_SIZE];
+
+ switch_caller_profile_t *_caller_profile;
+
+ switch_mutex_t *_mutex;
+ switch_mutex_t *flag_mutex; //TODO: Alterar o nome depois
+
+ unsigned int _KDeviceId; /*!< Represent de board we are making the call from */
+ unsigned int _KChannelId; /*!< Represent the channel we are making the call from */
+
KhompPvt(K3LAPI::target & target)
- : _target(target), _session(NULL) {};
+ : _target(target), _session(NULL) {};
K3LAPI::target target()
{
return _target;
}
-
+
void session(switch_core_session_t * newSession)
{
_session = newSession;
@@ -33,6 +57,23 @@
return _session;
}
+ int32 get_audio_dsp();
+
+ bool obtain_both(void);
+ bool obtain_rx(bool with_delay);
+ bool obtain_tx(void);
+
+ bool start_stream(void);
+ bool stop_stream(void);
+
+ bool start_listen(bool conn_rx = true);
+ bool stop_listen(void);
+
+ /* static stuff */
+ static switch_mutex_t *_pvts_mutex;
+
+ static KhompPvtVector _pvts; /*!< Static structure that contains all the pvts. Will be initialized by KhompPvt::initialize */
+
static KhompPvt * khompPvt(int32 device, int32 object)
{
if (!Globals::_k3lapi.valid_channel(device, object))
@@ -90,33 +131,7 @@
}
}
}
-
- K3LAPI::target _target;
- switch_core_session_t * _session; /*!< The session to which this pvt is associated with */
-
- unsigned int flags; //TODO: Alterar o nome depois
-
- switch_codec_t _read_codec;
- switch_codec_t _write_codec;
-
- switch_frame_t _read_frame;
-
- switch_buffer_t * _audio_buffer;
- unsigned char _databuf[SWITCH_RECOMMENDED_BUFFER_SIZE];
-
- switch_caller_profile_t *_caller_profile;
-
- switch_mutex_t *_mutex;
- switch_mutex_t *flag_mutex; //TODO: Alterar o nome depois
-
- unsigned int _KDeviceId; /*!< Represent de board we are making the call from */
- unsigned int _KChannelId; /*!< Represent the channel we are making the call from */
-
- /* static stuff */
- static switch_mutex_t *_pvts_mutex;
-
- static KhompPvtVector _pvts; /*!< Static structure that contains all the pvts. Will be initialized by KhompPvt::initialize */
};
Modified: freeswitch/trunk/contrib/mod/endpoints/mod_khomp/mod_khomp.cpp
==============================================================================
--- freeswitch/trunk/contrib/mod/endpoints/mod_khomp/mod_khomp.cpp (original)
+++ freeswitch/trunk/contrib/mod/endpoints/mod_khomp/mod_khomp.cpp Tue Jun 30 02:38:50 2009
@@ -993,8 +993,12 @@
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not hangup channel: %u on board %u. Releasing board channel anyway. [EV_DISCONNECT]\n", obj, e->DeviceId);
try
{
- //Globals::_k3lapi.command(e->DeviceId, obj, CM_STOP_LISTEN, NULL);
+ /* Stop the audio callbacks */
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Stopping audio callbacks ...\n");
+ KhompPvt::khompPvt(e->DeviceId, obj)->stop_listen();
+ KhompPvt::khompPvt(e->DeviceId, obj)->stop_stream();
KhompPvt::khompPvt(e->DeviceId, obj)->session(NULL);
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Audio callbacks stopped successfully\n");
}
catch(K3LAPI::invalid_channel & err)
{
@@ -1012,10 +1016,10 @@
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Call will be answered on board %u, channel %u. [EV_CONNECT]\n", e->DeviceId, obj);
switch_channel_mark_answered(channel);
/* Start listening for audio */
- /*
- const size_t buffer_size = 16;
- Globals::_k3lapi.command(e->DeviceId, obj, CM_LISTEN, (const char *) &buffer_size);
- */
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Starting audio callbacks ...\n");
+ KhompPvt::khompPvt(e->DeviceId, obj)->start_stream();
+ KhompPvt::khompPvt(e->DeviceId, obj)->start_listen();
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Audio callbacks initialized successfully\n");
}
catch (K3LAPI::invalid_session & err)
{
Modified: freeswitch/trunk/contrib/mod/endpoints/mod_khomp/src/khomp_pvt.cpp
==============================================================================
--- freeswitch/trunk/contrib/mod/endpoints/mod_khomp/src/khomp_pvt.cpp (original)
+++ freeswitch/trunk/contrib/mod/endpoints/mod_khomp/src/khomp_pvt.cpp Tue Jun 30 02:38:50 2009
@@ -153,3 +153,161 @@
switch_mutex_unlock(_pvts_mutex);
return pvt;
}
+
+/* Helper functions - based on code from chan_khomp */
+
+int32 KhompPvt::get_audio_dsp()
+{
+ switch (Globals::_k3lapi.device_type(_KDeviceId))
+ {
+ case kdtFXO:
+ case kdtFXOVoIP:
+ case kdtGSM:
+ case kdtGSMSpx:
+ return 0;
+ default:
+ return 1;
+ }
+}
+
+bool KhompPvt::obtain_both(void)
+{
+ /* estes buffers *NAO PODEM SER ESTATICOS*! */
+ char cmd1[] = { 0x3f, 0x03, 0xff, 0x00, 0x05, 0xff };
+ char cmd2[] = { 0x3f, 0x03, 0xff, 0x01, 0x00, 0xff };
+
+ cmd1[2] = cmd1[5] = cmd2[2] = cmd2[5] = _KChannelId;
+
+ int32 val = get_audio_dsp();
+
+ try
+ {
+ Globals::_k3lapi.raw_command(_KDeviceId, val, cmd1, 6);
+ Globals::_k3lapi.raw_command(_KDeviceId, val, cmd2, 6);
+ }
+ catch(...)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+bool KhompPvt::obtain_rx(bool with_delay)
+{
+ /* estes buffers *NAO PODEM SER ESTATICOS*! */
+ char cmd1[] = { 0x3f, 0x03, 0xff, 0x00, (with_delay ? 0x05 : 0x0a), 0xff };
+ char cmd2[] = { 0x3f, 0x03, 0xff, 0x01, 0x09, 0x0f };
+
+ cmd1[2] = cmd1[5] = cmd2[2] = _KChannelId;
+
+ int32 val = get_audio_dsp();
+
+ try
+ {
+ Globals::_k3lapi.raw_command(_KDeviceId, val, cmd1, 6);
+ Globals::_k3lapi.raw_command(_KDeviceId, val, cmd2, 6);
+ }
+ catch(...)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+bool KhompPvt::obtain_tx(void)
+{
+ /* estes buffers *NAO PODEM SER ESTATICOS*! */
+ char cmd1[] = { 0x3f, 0x03, 0xff, 0x00, 0x00, 0xff };
+ char cmd2[] = { 0x3f, 0x03, 0xff, 0x01, 0x09, 0x0f };
+
+ cmd1[2] = cmd1[5] = cmd2[2] = _KChannelId;
+
+ int32 val = get_audio_dsp();
+
+ try
+ {
+ Globals::_k3lapi.raw_command(_KDeviceId, val, cmd1, 6);
+ Globals::_k3lapi.raw_command(_KDeviceId, val, cmd2, 6);
+ }
+ catch(...)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+bool KhompPvt::start_stream(void)
+{
+ try
+ {
+ Globals::_k3lapi.mixer(_KDeviceId, _KChannelId, 0, kmsPlay, _KChannelId);
+ Globals::_k3lapi.command(_KDeviceId, _KChannelId, CM_START_STREAM_BUFFER);
+ //flags.set(kflags::STREAM_UP);
+ }
+ catch(...)
+ {
+ return false;
+ }
+ return true;
+}
+
+bool KhompPvt::stop_stream(void)
+{
+ try
+ {
+ Globals::_k3lapi.mixer(_KDeviceId, _KChannelId, 0, kmsGenerator, kmtSilence);
+ Globals::_k3lapi.command(_KDeviceId, _KChannelId, CM_STOP_STREAM_BUFFER);
+ //flags.clear(kflags::STREAM_UP);
+ }
+ catch(...)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+bool KhompPvt::start_listen(bool conn_rx)
+{
+ const size_t buffer_size = KHOMP_PACKET_SIZE;
+
+ if (conn_rx)
+ {
+ if (!obtain_rx(false)) // no delay, by default..
+ return false;
+ }
+
+ try
+ {
+ Globals::_k3lapi.command(_KDeviceId, _KChannelId, CM_LISTEN, (const char *) &buffer_size);
+ }
+ catch(...)
+ {
+ return false;
+ }
+
+ /* always set this flag to avoid constant 'ksInvalidState' messages. */
+ //flags.set(kflags::LISTEN_UP);
+
+ return true;
+}
+
+bool KhompPvt::stop_listen(void)
+{
+ try
+ {
+ Globals::_k3lapi.command(_KDeviceId, _KChannelId, CM_STOP_LISTEN);
+ }
+ catch(...)
+ {
+ return false;
+ }
+
+ //flags.clear(kflags::LISTEN_UP);
+
+ return true;
+}
+
More information about the Freeswitch-svn
mailing list