Suggestions

close search

Subscribing to streams — iOS

Once have connected to a session, you can subscribe to streams in the session. When you subscribe to a stream, you can put its video view in the app.

This topic includes the following sections:

Automatic reconnection

If a client drops a connection to a subscribed stream (for example, due to a drop in network connectivity in either client), it will attempt to automatically reconnect to the stream. When the stream is dropped and the client tries to reconnect, the [OTSubscriberKitDelegate subscriberDidDisconnectFromStream:] message is sent to the OTSubscriberKit object's delegate. When the connection is restored, the [OTSubscriberKitDelegate subscriberDidReconnectToStream:] message is sent. If the client cannot restore the connection, the [OTSessionDelegate session:streamDestroyed:] message is sent.

In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states.

// OTSubscriber delegate callbacks:
- (void)subscriberDidDisconnectFromStream:(OTStream*)stream
{
  // Display a user interface notification.
}

- (void)subscriberDidDisconnectFromStream:(OTStream*)stream
{
  // Adjust user interface.
}

// OTSession delegate callback:
- (void)session:(OTSession*)session session:streamDestroyed:(OTStream*)stream 
{
  // Adjust user interface.
}

Detecting streams in a session

The [OTSessionDelegate session:streamCreated:] message is sent when a new stream is created in a session. (A stream is created when a client publishes a stream to the session.) The OTStream object has properties that define the stream. Compare the connection property of the OTStream object with the connection property of the OTSession object to determine whether the stream is one that your client published:

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

    // See the declaration of subscribeToSelf above.
    if ([stream.connection.connectionId isEqualToString: session.connection.connectionId]) {
        // This is my own stream
    } else {
        // This is a stream from another client.
    }
}

You can subscribe to a stream to display it in your app. See the next section.

Subscribing to a stream

To subscribe to a stream, call the [OTSubscriber initWithStream:] method, passing in an OTStream object and a delegate object to receive subscriber-related messages. Then call the[OTSession subscribe:error] method to start subscribing to the stream:

- (void)session:(OTSession*)session streamCreated:(OTStream*)stream
{
    subscriber = [[OTSubscriber alloc] initWithStream:stream delegate:self];
    OTError* error = nil;
    [session subscribe:subscriber error:&error]
    if (error) {
      NSLog(@"subscribe failed with error: (%@)", error);
    }
}

The [OTSubscriberDelegate subscriberDidConnectToStream:] message is sent when the app starts receiving the subscriber's stream. At this point, you can add the subscriber's view (represented by the OTSubscriber.view property) to the app:

    - (void)subscriberDidConnectToStream:(OTSubscriber*)subscriber
    {
        [subscriber.view setFrame:CGRectMake(0, 300, 400, 300)];
        [self.view addSubview:subscriber.view];
    }

Unsubscribing from a stream

To stop playing a stream you are subscribed to, call the [OTSession unsubscribe:error:] method:

    OTError* error = nil;
    [session unsubscribe:_subscriber error:&error]
    if (error) {
      NSLog(@"unsubscribe failed with error: (%@)", error);
    }

The Subscriber is disconnected. Next, remove its view from its superview:

    [subscriber.view removeFromSuperview:];

Detecting when streams leave a session

When streams leave a session, the [OTSession session: streamDestroyed:] message is sent. When a stream is dropped, the view for any OTSubscriber object for the stream is removed from its superview. Check if the stream is not published by your own client, and remove its view from its superview.

- (void)session:(OTSession*)session streamDestroyed:(OTStream *)stream
{
    NSLog(@"session streamDestroyed (%@)", stream.streamId);
    
    if ([subscriber.stream.streamId isEqualToString:stream.streamId])
    {
        [_subscriber.view removeFromSuperview];
        _subscriber = nil;
    }
}

Detecting when a subscriber's video is disabled

The subscriber's delegate sends the [OTSubscriberDelegate subscriberVideoDisabled:reason:] message when the subscriber's video is disabled:

- (void)subscriberVideoDisabled:(OTSubscriber *)subscriber
                         reason:(OTSubscriberVideoEventReason)reason
{
    NSLog(@"subscriber video disabled.");
}

The reason parameter can be set to one of the following constants defined in the OTSubscriberVideoEventReason enum:

If the video stream resumes, the [OTSubscriberKit subscriberVideoEnabled:reason:] message is sent.

When you publish a stream, you can prevent it from having its video disabled due to stream quality. Before calling the [OTSession publish:error:] method, set the audioFallbackEnabled property of the Publisher object (or PublisherKit object) to NO.

Getting information about a stream

The OTStream object has the following properties that define the stream:

You can set a OTSubscriberKitNetworkStatsDelegate object for the OTSubscriberKit object to monitor the following statistics for a subscriber's stream:

See the SubscriberKit.networkStatsDelegate property.

To get statistics for a stream published by the local client, you must use a session that uses the OpenTok Media Router (sessions with the media mode set to routed).