Incoming P2P/PSTN Call

Applies to: Skype for Business 2015

Notes

Important

P2P calls are not supported in Google Chrome.

Listening for incoming call notifications

When a remote user starts a call we will receive an invitation to join the call. In order to see the notification we need to:

  1. Listen to the conversation collection for newly added conversations

    application.conversationsManager.conversations.added(function (conversation) {
        // ...
    });
    
  2. For every added conversation we need to observe the audioService.accept.enabled command.

    conversation.audioService.accept.enabled.when(true, function () {
        // ....
    })
    
  3. When the command becomes available we have received a notification. We can now prompt the user to accept ot decline the invitation. When the user accepts, we execute conversation.audioService.accept(). When they reject conversation.audioService.reject() is executed.

if (confirm('Accept incoming Audio invitation?')) {
    conversation.audioService.accept();
} else {
    conversation.audioService.reject();
}

Conversation State

We can subscribe to the conversation state to get information about the overall state of the conversation. If a conversation's state is Connected, it means that we are receiving live updates about state changes within the conversation, and will receive updates when the state of any active modality in the conversation changes, or when other participants connected to the conversation attempt to add or remove modalities. The conversation state being Connected does not mean that any particular modality is active.

conversation.state.changed(function (newValue, reason, oldValue) {
    //...
});

Possible Conversation States:

State Description
Created ...When conversation was created
Connecting ...When establishing a connection
Connected ...When the conversation was successfully connected
Disconnected ...When the conversation got disconnected

Participants in Conversation

In case the invitation is accepted we should subscribe to the participants collection on the conversation object to be notified when new participants enter the conversation.

conversation.participants.added(function (participant) {
    // ...
});

Ending an Audio Call

There are 2 ways to end an audio call: either stop the audio modality by calling conversation.audioService.stop() or leave the conversation entirely by calling conversation.leave(). If a modality other than audio, such as chat, is active in the conversation, calling conversation.leave() will disconnect that as well and cause the conversation.state() to become Disconnected. If you want to hang up audio in a call but remain connected to the conversation by chat, call conversation.audioService.stop().

conversation.leave().then(function () {
    // successfully left the conversation
}, function (error) {
    // error
});

// OR

conversation.audioService.stop().then(function () {
    // successfully stopped audio
}, function (error) {
    console.log("Failed to stop audio: " + error);
});

Complete Code Sample

Here is the code combined:

conversationsManager.conversations.added(function (conversation) {
    conversation.audioService.accept.enabled.when(true, function () {
        if (confirm('Accept incoming Audio invitation?')) {
            conversation.audioService.accept();
            conversation.participants.added(function (participant) {
                console.log('Participant:', participant.displayName(), 'has been added to the conversation');
            });
        } else {
            conversation.audioService.reject();
        }
    });

    conversation.state.changed(function (newValue, reason, oldValue) {
        console.log('Conversation state changed from', oldValue, 'to', newValue);
    });
});