[Freeswitch-svn] [commit] r1903 - freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip

Freeswitch SVN stkn at freeswitch.org
Mon Jul 17 10:24:15 EDT 2006


Author: stkn
Date: Mon Jul 17 10:24:14 2006
New Revision: 1903

Modified:
   freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip/mod_pjsip.c

Log:
Some mods, need to find the best way to handle state transitions.

Modified: freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip/mod_pjsip.c
==============================================================================
--- freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip/mod_pjsip.c	(original)
+++ freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip/mod_pjsip.c	Mon Jul 17 10:24:14 2006
@@ -208,10 +208,18 @@
 	{
 		pjsip_tx_data *txdata = NULL;
 		pjmedia_sdp_session *sdp = NULL;
-		char tmp[50];
+		char tmp[1024], *destination;
 		pj_str_t dst;
 
-		pj_ansi_sprintf( tmp, "<sip:%s:%d>", pvt->caller_profile->destination_number, 5060 );
+		switch_copy_string( tmp, pvt->caller_profile->destination_number, sizeof( tmp ) );
+		if( !(destination = strchr( tmp, '/' )) ) {
+			switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Malformed URL!\n" );
+			switch_core_session_destroy( &session );
+			return SWITCH_STATUS_GENERR;
+		}
+		*destination++ = '\0';
+
+		pj_ansi_sprintf( tmp, "<sip:%s:%d>", destination, 5060 );
 		pj_cstr( &dst, tmp );
 
 		pvt->sip_ua = pjsip_ua_instance();
@@ -275,8 +283,8 @@
 	} else {
 		pjsip_tx_data *txdata;
 
-		/* Send 183 Session Progress */
-		status = pjsip_inv_answer( pvt->sip_invite, 183, NULL, NULL, &txdata );
+		/* Send 180 Ring */
+		status = pjsip_inv_answer( pvt->sip_invite, 180, NULL, NULL, &txdata );
 		if( status != PJ_SUCCESS ) {
 			status = pjsip_inv_answer( pvt->sip_invite, PJSIP_SC_NOT_ACCEPTABLE, NULL, NULL, &txdata );
 
@@ -290,13 +298,10 @@
 		}
 		pjsip_inv_send_msg( pvt->sip_invite, txdata );
 	}
-
-	/* */
-	switch_set_flag( pvt, TFLAG_IO );
-
 	/* add call to hash */
 	switch_core_hash_insert( profile->call_hash, pvt->call_id, pvt );
 
+	switch_channel_set_state( channel, CS_RING );
 	return SWITCH_STATUS_SUCCESS;
 } 
 
@@ -422,6 +427,8 @@
 
 		assert( pvt->sip_invite != NULL );
 
+		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "* Sending 200 on INVITE to %s (%s)\n", pvt->call_id, switch_channel_get_name( channel ) );
+
 		/* create SIP 200 with SDP */
 		status = pjsip_inv_answer( pvt->sip_invite, 200, NULL, NULL, &txdata );
 		if( status != PJ_SUCCESS ) {
@@ -435,8 +442,8 @@
 			switch_core_session_destroy( &session );
 			return SWITCH_STATUS_GENERR;
 		}
-		switch_set_flag( pvt, TFLAG_ANSWERED );
 	}
+	switch_set_flag( pvt, TFLAG_ANSWERED );
 
 	return SWITCH_STATUS_SUCCESS;
 }
@@ -527,6 +534,10 @@
 
 	channel = switch_core_session_get_channel( *new_session );
 
+	/* create channel name, copy profile and parse destination number (?) */
+	snprintf( tmp, sizeof(tmp), "pjsip/%s-%04x", outbound_profile->destination_number, rand() & 0xffff );
+	switch_channel_set_name( channel, tmp );
+
 	pvt->session = *new_session;
 	pvt->profile = profile;
 
@@ -534,10 +545,6 @@
 	pvt->num_codecs = switch_loadable_module_get_codecs( switch_core_session_get_pool(pvt->session), pvt->codecs,
 								sizeof(pvt->codecs) / sizeof(pvt->codecs[0]) );
 
-	/* create channel name, copy profile and parse destination number (?) */
-	snprintf( tmp, sizeof(tmp), "pjsip/%s-%04x", outbound_profile->destination_number, rand() & 0xffff );
-	switch_channel_set_name( channel, tmp );
-
 	caller_profile = switch_caller_profile_clone( *new_session, outbound_profile );
 	switch_channel_set_caller_profile( channel, caller_profile );
 
@@ -702,7 +709,7 @@
 		}
 		/* give the new session a name */
 		pj_strdup_with_null( pjsip_pool, &tmp, &((pjsip_sip_uri *)rxdata->msg_info.from->uri)->host );
-		snprintf(name, sizeof(name), "pjsip/%s-%04x", tmp.ptr, rand() & 0xffff);
+		snprintf( name, sizeof(name), "pjsip/%s/%s-%04x", profile->name, tmp.ptr, rand() & 0xffff );
 		switch_channel_set_name( channel, name );
 
 		switch_channel_clear_flag( channel, CF_OUTBOUND );
@@ -814,9 +821,6 @@
 	switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s: received new request\n", __FUNCTION__ );
 
 	switch( rxdata->msg_info.msg->line.req.method.id ) {
-		case PJSIP_ACK_METHOD:
-			return PJ_FALSE;
-			;;
 		case PJSIP_INVITE_METHOD:
 			on_incoming_call( rxdata );
 			return PJ_TRUE;
@@ -886,12 +890,12 @@
 	channel = switch_core_session_get_channel( session );
 	assert( channel != NULL );
 
-	switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "- Call %s state has changed (event type %s)\n", pvt->call_id, pjsip_event_str( evt->type ) );
-
 	switch( inv->state )
 	{
 	case PJSIP_INV_STATE_DISCONNECTED:
-		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "* Call %s has been disconnected\n", pvt->call_id );
+		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "* Call %s (%s) has been disconnected\n", pvt->call_id,
+									switch_channel_get_name( channel ) );
+
 		if( !switch_test_flag( pvt, TFLAG_BYE ) ) {
 			switch_call_cause_t cause;
 
@@ -904,18 +908,55 @@
 		}
 		break;
 	case PJSIP_INV_STATE_CONFIRMED:
+		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "* Call %s (%s) has been connected\n", pvt->call_id,
+									switch_channel_get_name( channel ) );
+
 		/* -> connected */
-		switch_channel_answer( channel );
-		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "* Call %s has been connected\n", pvt->call_id );
+		if( switch_channel_test_flag( channel, CF_OUTBOUND ) ) {
+			switch_channel_answer( channel );
+		}
+
 		break;
 	case PJSIP_INV_STATE_EARLY:
-		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "* Call %s, state %s\n", pvt->call_id, pjsip_inv_state_name( inv->state ) );
+		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "* Call %s (%s), state %s\n", pvt->call_id, 
+									switch_channel_get_name( channel ),
+									pjsip_inv_state_name( inv->state ) );
+
+#if 0
+		/* -> inbound ringing */
+		if( !switch_channel_test_flag( channel, CF_OUTBOUND ) &&
+		    !( switch_channel_get_state( channel ) >= CS_RING ) ) {
+
+			/* put inbound channel into RING state */
+			switch_channel_set_state( channel, CS_RING );
+		}
+#endif
 		break;
 
 	case PJSIP_INV_STATE_CONNECTING:
-		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "* Call %s, state %s\n", pvt->call_id, pjsip_inv_state_name( inv->state ) );
+		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "* Call %s (%s), state %s\n", pvt->call_id,
+									switch_channel_get_name( channel ),
+									pjsip_inv_state_name( inv->state ) );
+
+#if 0
+		/* -> outbound ringing */
+		if( switch_channel_test_flag( channel, CF_OUTBOUND ) &&
+		    !( switch_channel_get_state( channel ) >= CS_RING ) ) {
+
+			switch_channel_set_state( channel, CS_RING );
+		}
+#endif
 		break;
+	case PJSIP_INV_STATE_CALLING:
+	case PJSIP_INV_STATE_INCOMING:
+		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "* Call %s (%s), state %s\n", pvt->call_id,
+									switch_channel_get_name( channel ),
+									pjsip_inv_state_name( inv->state ) );
+		break;
 	default:
+		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "* Call %s (%s), UNHANDLED state %s\n", pvt->call_id,
+									switch_channel_get_name( channel ),
+									pjsip_inv_state_name( inv->state ) );
 		break;
 	}
 } 
@@ -1063,8 +1104,8 @@
 	/* start rtp */
 	start_rtp( pvt );
 
-	/* put channel into RING state */
-	switch_channel_set_state( channel, CS_RING );
+	/* activate io */
+	switch_set_flag( pvt, TFLAG_IO );
 }
 
 static switch_status_t pjsip_receive_message(switch_core_session_t *session, switch_core_session_message_t *msg)
@@ -1096,8 +1137,8 @@
 		{
 			pj_status_t status;
 			pjsip_tx_data *txdata;
-			/* Send 183 Session Progress */
 
+			/* Send 183 Session Progress */
 			status = pjsip_inv_answer( pvt->sip_invite, 183, NULL, NULL, &txdata );
 			if( status != PJ_SUCCESS ) {
 				status = pjsip_inv_answer( pvt->sip_invite, PJSIP_SC_NOT_ACCEPTABLE, NULL, NULL, &txdata );
@@ -1209,6 +1250,14 @@
 			media->desc.fmt_count++;
 		}
 	}
+	else {
+		/* houston, we've got a reference SDP (incoming invite) 
+		 * what to do next?
+		 * - scan the incoming SDP for the maximum supported samplerate
+		 * - build answer SDP based on the reference codec list + max samplerate
+		 */
+		;
+	}
 
 	/* Add sendrecv attribute */
 	attr = pj_pool_zalloc( pool, sizeof(pjmedia_sdp_attr) );
@@ -1366,6 +1415,8 @@
 		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Adding profile %s\n", profile->name );
 		switch_core_hash_insert( globals.profiles_hash, profile->name, profile );
 	}
+
+	switch_xml_free( xml );
 
 	return SWITCH_STATUS_SUCCESS;
 }



More information about the Freeswitch-svn mailing list