Subscribing to Other Clients' Streams
Finally, we want clients to be able to subscribe to (or view) other clients' streams in the session:
- Add a
subscriberproperty to theViewControllerclass:
@interface ViewController() <OTSessionDelegate, OTPublisherDelegate>
@property (nonatomic) OTSession *session;
@property (nonatomic) OTPublisher *publisher;
@property (nonatomic) OTSubscriber *subscriber;
@end
The OTSubscriber class is defined in the iOS SDK. It uses the device's camera and microphone, to subscribe a stream Vonage Video session.
- Modify the implementation of the
[OTSessionDelegate session: streamCreated]method (one of theOTSessionDelegatecallbacks) to include code to subscribe to other clients' streams the session:
- (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(@"Unable to subscribe (%@)", error.localizedDescription);
return;
}
[_subscriber.view setFrame:[UIScreen mainScreen].bounds];
[self.view insertSubview:_subscriber.view atIndex:0];
}
When another client publishes a stream to a session, this method is called, and an OTStream object is passed in. The OTStream class is defined in the iOS SDK, and it represents an audio-video stream in the Vonage Video session. The code initializes an instance of the OTSubscriber class, defined in the iOS SDK. The OTSubscriber() constructor takes two parameters: The OTStream object (for the stream you want to view) and the object that implements the OTSubscriberDelegate protocol.
- Change the
ViewControllerinterface declaration to indicate that the class implements theOTSubscriberDelegateprotocol:
@interface ViewController() <OTSessionDelegate, OTPublisherDelegate,OTSubscriberDelegate>
Next we will implement methods of the OTSessionDelegate protocol. Add the following code to the end of the ViewController class (before the class's @end statement):
# pragma mark - OTSubscriber delegate callbacks
- (void)subscriberDidConnectToStream:(OTSubscriberKit *)subscriber {
NSLog(@"The subscirber: %@ did connect to the stream", subscriber);
}
- (void)subscriber:(OTSubscriberKit*)subscriber
didFailWithError:(OTError*)error
{
NSLog(@"subscriber %@ didFailWithError %@",
subscriber.stream.streamId,
error);
}
- If the client fails to connect to the Vonage Video session, an
OTErrorobject is passed into thesubscriber(_: didFailWithError:)method.
Basic video chat
Learn the basic concepts of the Vonage Video API platform, including how users can communicate through video, voice, and messaging. Explore a basic Vonage Video API flow.