Suggestions

close search

Back to Basic Video Chat Overview

Basic Video Chat Step 6: Subscribing to another client's audio-video stream

  1. 1
    Basic Video Chat Step 1:
    Setting up the test web service
  2. 2
    Basic Video Chat Step 2:
    Configuring and testing the app
  3. 3
    Basic Video Chat Step 3:
    Initializing an OpenTok session
  4. 4
    Basic Video Chat Step 4:
    Connecting to the session
  5. 5
    Basic Video Chat Step 5:
    Publishing a stream to the session
  6. 6
    Basic Video Chat Step 6:
    Subscribing to audio-video streams
  7. 7
    Basic Video Chat Step 7:
    Customizing the UI

The [OTSessionDelegate session:streamCreated:] message is sent when a new stream is created in the session. The app implements this delegate method with the following:

- (void)session:(OTSession*)session
streamCreated:(OTStream *)stream
{
    NSLog(@"session streamCreated (%@)", stream.streamId);

    if (nil == _subscriber)
    {
        [self doSubscribe:stream];
    }
}

The method is passed an OTStream object (defined by the OpenTok iOS SDK), representing the stream that another client is publishing. Although this app assumes that only one other client is connecting to the session and publishing, the method checks to see if the app is already subscribing to a stream (if the _subscriber property is set). If not, the session calls [self doSubscribe:stream], passing in the OTStream object (for the new stream):

- (void)doSubscribe:(OTStream*)stream
{
    _subscriber = [[OTSubscriber alloc] initWithStream:stream
                                              delegate:self];
    OTError *error = nil;
    [_session subscribe:_subscriber error:&error];
    if (error)
    {
        NSLog(@"Unable to publish (%@)",
              error.localizedDescription);
    }
}

The method initializes an OTSubscriber object (_subscriber), used to subscribe to the stream, passing in the OTStream object to the initialization method. It also sets self to implement the OTSubscriberDelegate interface, which is sent messages related to the subscriber.

It then calls [OTSession subscribe:error:] to have the app to subscribe to the stream.

When the app starts receiving the subscribed stream, the [OTSubscriberDelegate subscriberDidConnectToStream:] message is sent. The implementation of the delegate method adds view of the subscriber stream (defined by the view property of the OTSubscriber object) as a subview of the _subscriberView UIView object, defined in the main storyboard:

- (void)subscriberDidConnectToStream:(OTSubscriberKit*)subscriber
{
    NSLog(@"subscriberDidConnectToStream (%@)",
          subscriber.stream.connection.connectionId);
    [_subscriber.view setFrame:CGRectMake(0, 0, _subscriberView.bounds.size.width,
                                          _subscriberView.bounds.size.height)];
    [_subscriberView addSubview:_subscriber.view];
    _subscriberAudioBtn.hidden = NO;

    _chatTextInputView.hidden = NO;
}

It also displays the input text field for the text chat. The app hides this field until you start viewing the other client's audio-video stream.

If the subscriber's stream is dropped from the session (perhaps the client chose to stop publishing or to the implementation of the [OTSession session:streamDestroyed] method is called:

- (void)session:(OTSession*)session
streamDestroyed:(OTStream *)stream
{
    NSLog(@"session streamDestroyed (%@)", stream.streamId);
    if ([_subscriber.stream.streamId isEqualToString:stream.streamId])
    {
        [self cleanupSubscriber];
    }
}

The [self cleanupSubscriber:] method removes the publisher's view (its video) from its superview:

- (void)cleanupPublisher {
    [_subscriber.view removeFromSuperview];
    _subscriber = nil;
}
  1. 1
    Basic Video Chat Step 1:
    Setting up the test web service
  2. 2
    Basic Video Chat Step 2:
    Configuring and testing the app
  3. 3
    Basic Video Chat Step 3:
    Initializing an OpenTok session
  4. 4
    Basic Video Chat Step 4:
    Connecting to the session
  5. 5
    Basic Video Chat Step 5:
    Publishing a stream to the session
  6. 6
    Basic Video Chat Step 6:
    Subscribing to audio-video streams
  7. 7
    Basic Video Chat Step 7:
    Customizing the UI