Share via


Create a Session and Make a Call

The following code example demonstrates how to make a call. First the session is created, then a participant is added to the session to initiate the call. 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

HRESULT           hr            = S_OK;
BSTR              bstrLocalURI  = NULL;
BSTR              bstrDestURI   = NULL;
RTC_SESSION_TYPE  SessionType;
IRTCSession       *pIRTCSession = NULL;
IRTCProfile       *pIRTCProfile = NULL;   

SessionType  =   // Specify session type 

// For phone-phone sessions, the local telephone URI is required.
if (SessionType == RTCST_PHONE_TO_PHONE)
{
    bstrLocalURI = SysAllocString(_T("tel:+14255550123"));

    // Unlike other session types, a phone-phone session must have a
    // valid profile. Developers should ensure that *pIRTCProfile 
    // points to a valid profile object.

    pIRTCProfile =  // Look at "Create and Enable a Profile" code example
}

// Get the URI to call; this can be a sip: or a tel: URI.
bstrDestURI = SysAllocString(_T("sip:someone@example.com")); 

// Create the session with the specified session type and local URI.
hr = pIRTCClient->CreateSession( SessionType, 
                                 bstrLocalURI, 
                                 pIRTCProfile,
                                 RTCCS_FORCE_PROFILE,
                                 &pIRTCSession );

// If (hr != S_OK), process the error here.

// The AddParticipant method sets the session into the active state
// and creates the IRTCParticipant interface. 
IRTCParticipant  *pIRTCParticipant;
hr = pIRTCSession->AddParticipant( bstrDestURI, 
                                   NULL, 
                                   &pIRTCParticipant );

// If (hr != S_OK), process the error here.

Visual Basic Code Example

'Set error handling routine here. 
' On Error GoTo MyErrorRoutine 

'Usually declared globally
Private g_objSession As IRTCSession
Private g_objProfile As IRTCProfile
Private g_objParticipant As IRTCParticipant

'Declare the SessionType variable.
SessionType As RTC_SESSION_TYPE

'Declare the URI variable for the called and local parties.
Dim strLocalURI As String
Dim strDestURI As String
        
'Create a session depending on the session type selected by the user.
If SessionType = RTCST_PC_TO_PC Then

    strDestURI = "sip:someone@example.com"
    Set g_objSession = g_objRTCClient.CreateSession(RTCST_PC_TO_PC, vbNullString, Nothing, 0)

ElseIf SessionType = RTCST_PC_TO_PHONE Then

    strDestURI = "tel:+14255550147"
    Set g_objSession = g_objRTCClient.CreateSession(RTCST_PC_TO_PHONE, vbNullString, Nothing, 0)

ElseIf SessionType = RTCST_PHONE_TO_PHONE Then

    strLocalURI = "tel:+14255550199"
    strDestURI = "tel:+14255550147"

    ' Unlike other session types, a phone-phone session must have a
    ' valid profile. Create a profile object, g_objProfile, and
    ' pass it to the CreateSession method.

    Set g_objProfile =  ' Look at "Create and Enable a Profile" code example

    Set g_objSession = g_objRTCClient.CreateSession(RTCST_PHONE_TO_PHONE, strLocalURI, g_objProfile, 0)

End If

'Add a participant to initiate the call.
Set g_objParticipant = g_objSession.AddParticipant(strDestURI, "Any Name")