How to initiate a Video call using lync sdk?

227 views Asked by At

My Aim: I want to initiate a video call from the start.

My Problem: It is getting initiated into audio call and then it's turning into video after end user answers the call.

void ConversationManager_ConversationAdded_Video(object sender, ConversationManagerEventArgs e)
{
    Console.WriteLine("Inside  conversation added for Video");

    if (e.Conversation.Modalities[ModalityTypes.AudioVideo].State != ModalityState.Notified)
    {

        if (e.Conversation.CanInvoke(ConversationAction.AddParticipant))
        {
            try
            {
                e.Conversation.ParticipantAdded += Conversation_ParticipantAdded_Video;
                e.Conversation.AddParticipant(client.ContactManager.GetContactByUri(receipient));

            }
            catch (ItemAlreadyExistException ex)
            {
            }

        }
    }
}

void Conversation_ParticipantAdded_Video(object source, ParticipantCollectionChangedEventArgs data)
{
    if (data.Participant.IsSelf != true)
    {
        if (((Conversation)source).Modalities[ModalityTypes.AudioVideo].CanInvoke(ModalityAction.Connect))
        {
            object[] asyncState = { ((Conversation)source).Modalities[ModalityTypes.AudioVideo], "CONNECT" };
            try
            {
                ((Conversation)source).Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += _AVModality_ModalityStateChanged_Video;
                Console.WriteLine("entered video Satheesh participant added");

               // ((Conversation)source).Modalities[ModalityTypes.AudioVideo].BeginConnect(ModalityCallback, asyncState);
                ((Conversation)source).Modalities[ModalityTypes.AudioVideo].BeginConnect(ModalityCallback, asyncState);
                //((Conversation)source).Modalities[ModalityTypes.AudioVideo].EndConnect(ModalityCallback, asyncState);
                Thread.Sleep(6000);
                Console.WriteLine(source);


                Console.WriteLine("entered video participant added");
            }
            catch (LyncClientException lce)
            {
                throw new Exception("Lync Platform Exception on BeginConnect: " + lce.Message);
            }
        }
    }
}



public void ModalityCallback(IAsyncResult ar)
{
    Object[] asyncState = (Object[])ar.AsyncState;
    try
    {
        if (ar.IsCompleted == true)
        {
            if (asyncState[1].ToString() == "RETRIEVE")
            {
                ((AVModality)asyncState[0]).EndRetrieve(ar);
            }
            if (asyncState[1].ToString() == "HOLD")
            {
                ((AVModality)asyncState[0]).EndHold(ar);
            }
            if (asyncState[1].ToString() == "CONNECT")
            {
                Console.WriteLine("inside connect method CONNECT");
                ((AVModality)asyncState[0]).EndConnect(ar);
            }
            if (asyncState[1].ToString() == "FORWARD")
            {
                ((AVModality)asyncState[0]).EndForward(ar);
            }
            if (asyncState[1].ToString() == "ACCEPT")
            {
                ((AVModality)asyncState[0]).Accept();
            }

        }
    }
    catch (LyncClientException)
    { }
}

public void _AVModality_ModalityStateChanged_Video(object sender, ModalityStateChangedEventArgs e)
{
    Console.WriteLine(sender);

    Console.WriteLine("entered video modality changed");
    switch (e.NewState)
    {

        case ModalityState.Connected:
            if (_VideoChannel == null)
            {

                _VideoChannel = ((AVModality)sender).VideoChannel;
                _VideoChannel.StateChanged += new EventHandler<ChannelStateChangedEventArgs>(_VideoChannel_StateChanged);
            }
            if (_VideoChannel.CanInvoke(ChannelAction.Start))
            {
                Console.WriteLine("entered video modality changed123");
                _VideoChannel.BeginStart(MediaChannelCallback, _VideoChannel);

            }
            break;

        case ModalityState.OnHold:
            break;
        case ModalityState.Connecting:
        case ModalityState.Forwarding:
            break;
        case ModalityState.Transferring:
            break;
    }
}

private void MediaChannelCallback(IAsyncResult ar)
{

    ((VideoChannel)ar.AsyncState).EndStart(ar);
}
1

There are 1 answers

0
Ahmed Ben Salem On

I Think it is because you call _AVModality_ModalityStateChanged_Video in ParticipantAdded event.

that means that you start your video when the person you call (the new participant) joins the conversation. thats why it is getting initiated into audio call and then it's turning into video after end user answers the call.

The solution is to fire a new event : ConversationStateChangedEvent

this is where I did it and works fine :

/// <summary>
/// Handles event raised when New meetNow window change it's state 
/// </summary>
private void _NewMeetNowConversation_StateChanged(object sender, ConversationStateChangedEventArgs e)
{
    Conversation conference = (Conversation)sender;
    switch(e.NewState)
    {
        case ConversationState.Active:
            conference.Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += AVConferenceModalityStateChanged;
            conference.ParticipantAdded += ConfParticipantAdded;
            break;
        case ConversationState.Terminated:      //conversation window completely closed
            break;
        case ConversationState.Inactive:   

            break;
        case ConversationState.Parked:

            break;
        case ConversationState.Invalid:

            break;
    }
}