Create an Outgoing Session When the Client is Behind a NAT
The following code example demonstrates how to create an outgoing session when the client is behind a NAT. The operations in the Initialize RTC code example must be performed before using this example.
Note This example does not contain error checking or releases appropriate for real code.
C++ Code Example
//------------------------------- //Outgoing call from behind a NAT //------------------------------- HRESULT hr = S_OK; BSTR bstrLocalURI = NULL; BSTR bstrDestURI = NULL; RTC_SESSION_TYPE enSessionType; IRTCSession *pIRTCSession = NULL; IRTCSessionPortManagement *pSessionPortManagement; IRTCPortManager *pIRTCPortManager; IRTCProfile *pIRTCProfile = NULL; IRTCClient2 *pIRTCClient; enSessionType = // Specify session type // Get the URI to call; this can be a sip: or a tel: URI. // Machines behind a NAT cannot register with Server; // therefore, the profile will be NULL. bstrDestURI = ::SysAllocString(L"sip:someone@microsoft.com"); // CoCreate and initialize RTCClient. See the "Initialize // RTC" code example. // Cocreate and initialize the echo client. // QueryInterface for pIRTCPortManager. // Create the session with the specified session type // and local URI. hr = pIRTCClient->CreateSession( enSessionType, bstrLocalURI, pIRTCProfile, RTCCS_FORCE_PROFILE, &pIRTCSession ); // If (hr != S_OK), process the error here. // Query for the IRTCSessionPortManagement interface. hr = pIRTCSession->QueryInterface(IID_IRTCSessionPortManagement), (void **) &pSessionPortManagement); // If (hr != S_OK), process the error here. //Set the port manager on the session. hr = pSessionPortManagement->SetPortManager(pIRTCPortManager); // Release the pointer to IRTCSessionPortManagement // because we no longer need it. if(pSessionPortManagement) { pSessionPortManagement->Release(); pSessionPortManagement=NULL; } IRTCParticipant *pIRTCParticipant = NULL; hr = pIRTCSession->AddParticipant( bstrDestURI, NULL, &pIRTCParticipant ); // RTC calls into the IRTCPortManager::GetMapping() // method to get the SIP and media ports. // If (hr != S_OK), process the error here. ... // Free all the strings allocated with SysFreeString(). //Release all pointers. if(pIRTCSession) { pIRTCSession->Release(); pIRTCSession=NULL; } if(pIRTCPortManager) { pIRTCPortManager->Release(); pIRTCPortManager=NULL; } if(pIRTCClient) { pIRTCClient->Release(); pIRTCClient=NULL; } if(pIRTCParticipant) { pIRTCParticipant->Release(); pIRTCParticipant=NULL; }
Visual Basic Code Example
'------------------------------- 'Outgoing call from behind a NAT '------------------------------- Dim bstrLocalURI As String '(for example, someone@microsoft.com) Dim bstrDestURI As String '(for example, someone@microsoft.com) Dim enSessionType As RTC_SESSION_TYPE Dim objSession As IRTCSession Dim objSessionPortManagement As IRTCSessionPortManagement Dim objPortManager As IRTCPortManager Dim objProfile As IRTCProfile Dim objRTCClient As IRTCClient2 Dim objParticipant As IRTCParticipant bstrLocalURI = vbNullString bstrDestURI = vbNullString Set objSession = Nothing Set objSessionPortManagement = Nothing Set objProfile = Nothing 'enSessionType = 'Specify session type 'CoCreate and initialize RTCClient. 'Cocreate and initialize your implementation of the port manager. 'Create the session with the specified session type and local URI. Set objSession = objRTCClient.CreateSession(enSessionType, _ bstrLocalURI, _ objProfile, _ 0) 'If (Err.Number), process the error here. Set objSessionPortManagement = objSession 'Set the port manager for the session. Call objSessionPortManagement.SetPortManager(objPortManager) 'Add a participant to the session. Set objParticipant = objSession.AddParticipant(bstrDestURI, vbNullString) 'RTC calls into the Port Manager's GetMapping() implementation 'to obtain the SIP and media ports. ' Use VB error handling to catch failures. ...