diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 8f668fbdd2..063a3e9a89 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -496,7 +496,8 @@ typedef enum {
     SWITCH_EVENT_CUSTOM				- A custom event
-    SWITCH_EVENT_CHANNEL_CREATE		- A channel has changed state
+    SWITCH_EVENT_CHANNEL_CREATE		- A channel has been created
+    SWITCH_EVENT_CHANNEL_DESTROY	- A channel has been destroyed
     SWITCH_EVENT_CHANNEL_STATE		- A channel has changed state
     SWITCH_EVENT_CHANNEL_ANSWER		- A channel has been answered
     SWITCH_EVENT_CHANNEL_HANGUP		- A channel has been hungup
@@ -520,6 +521,7 @@ typedef enum {
 typedef enum {
 	SWITCH_EVENT_CUSTOM,
 	SWITCH_EVENT_CHANNEL_CREATE,
+	SWITCH_EVENT_CHANNEL_DESTROY,
 	SWITCH_EVENT_CHANNEL_STATE,
 	SWITCH_EVENT_CHANNEL_ANSWER,
 	SWITCH_EVENT_CHANNEL_HANGUP,
diff --git a/src/switch_channel.c b/src/switch_channel.c
index e9389e3f9f..1bc0113eb0 100644
--- a/src/switch_channel.c
+++ b/src/switch_channel.c
@@ -483,6 +483,8 @@ SWITCH_DECLARE(switch_channel_state) switch_channel_set_state(switch_channel *ch
 
 
 	if (ok) {
+		switch_event *event;
+		
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s State Change %s -> %s\n", channel->name,
 							  state_names[last_state], state_names[state]);
 		channel->state = state;
@@ -491,6 +493,11 @@ SWITCH_DECLARE(switch_channel_state) switch_channel_set_state(switch_channel *ch
 			channel->hangup_cause = SWITCH_CAUSE_NORMAL_CLEARING;
 		}
 
+		if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_STATE) == SWITCH_STATUS_SUCCESS) {
+			switch_channel_event_set_data(channel, event);
+			switch_event_fire(&event);
+		}
+		
 		if (state < CS_DONE) {
 			switch_core_session_signal_state_change(channel->session);
 		}
@@ -690,8 +697,15 @@ SWITCH_DECLARE(switch_channel_state) switch_channel_hangup(switch_channel *chann
 	}
 
 	if (channel->state < CS_HANGUP) {
+		switch_event *event;
+
 		channel->state = CS_HANGUP;
 		channel->hangup_cause = hangup_cause;
+		if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_STATE) == SWITCH_STATUS_SUCCESS) {
+			switch_channel_event_set_data(channel, event);
+			switch_event_fire(&event);
+		}
+
 		switch_core_session_kill_channel(channel->session, SWITCH_SIG_KILL);
 		switch_core_session_signal_state_change(channel->session);
 	}
diff --git a/src/switch_core.c b/src/switch_core.c
index b02b31531b..2c6b62d260 100644
--- a/src/switch_core.c
+++ b/src/switch_core.c
@@ -1837,17 +1837,11 @@ SWITCH_DECLARE(void) switch_core_session_run(switch_core_session *session)
 	switch_mutex_lock(session->mutex);
 
 	while ((state = switch_channel_get_state(session->channel)) != CS_DONE) {
-		switch_event *event;
-
 		if (state != laststate) {
 			int index = 0;
 			int proceed = 1;
 			midstate = state;
 
-			if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_STATE) == SWITCH_STATUS_SUCCESS) {
-				switch_channel_event_set_data(session->channel, event);
-				switch_event_fire(&event);
-			}
 
 			switch (state) {
 			case CS_NEW:		/* Just created, Waiting for first instructions */
@@ -2095,6 +2089,12 @@ SWITCH_DECLARE(void) switch_core_session_run(switch_core_session *session)
 SWITCH_DECLARE(void) switch_core_session_destroy(switch_core_session **session)
 {
 	switch_memory_pool *pool;
+	switch_event *event;
+
+	if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_DESTROY) == SWITCH_STATUS_SUCCESS) {
+		switch_channel_event_set_data(session->channel, event);
+		switch_event_fire(&event);
+	}
 
 	pool = (*session)->pool;
 	*session = NULL;
@@ -2336,6 +2336,11 @@ static void core_event_handler(switch_event *event)
 	}
 
 	switch (event->event_id) {
+
+	case SWITCH_EVENT_CHANNEL_DESTROY:
+		snprintf(buf, sizeof(buf), "delete from channels where uuid='%s'", switch_event_get_header(event, "unique-id"));
+		sql = buf;
+		break;
 	case SWITCH_EVENT_CHANNEL_CREATE:
 		snprintf(buf, sizeof(buf), "insert into channels (uuid,created,name,state) values('%s','%s','%s','%s')",
 				 switch_event_get_header(event, "unique-id"),
@@ -2360,8 +2365,7 @@ static void core_event_handler(switch_event *event)
 
 			switch(state_i) {
 			case CS_HANGUP:
-				snprintf(buf, sizeof(buf), "delete from channels where uuid='%s'", switch_event_get_header(event, "unique-id"));
-				sql = buf;
+			case CS_DONE:
 				break;
 			case CS_RING:
 				snprintf(buf, sizeof(buf), "update channels set state='%s',cid_name='%s',cid_num='%s',ip_addr='%s',dest='%s'"
@@ -2440,6 +2444,7 @@ static void core_event_handler(switch_event *event)
 				break;
 			}
 		}
+		//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL [%s]\n", sql);
 	}
 }
 
diff --git a/src/switch_event.c b/src/switch_event.c
index 2d52f7b174..6bd49af7c0 100644
--- a/src/switch_event.c
+++ b/src/switch_event.c
@@ -85,6 +85,7 @@ also never put any new ones before EVENT_ALL
 static char *EVENT_NAMES[] = {
 	"CUSTOM",
 	"CHANNEL_CREATE",
+	"CHANNEL_DESTROY",
 	"CHANNEL_STATE",
 	"CHANNEL_ANSWER",
 	"CHANNEL_HANGUP",