Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

Example of streamer for iOS

This streamer can be used to publish WebRTC video stream on Web Call Server.

On the screenshot below the example is displayed when a stream is being published.
In the URL specified in the input field

  • 192.168.2.107 is the address of the WCS server
  • testStream is the stream name

Two videos are played

  • left - video from the camera
  • right - the published video stream is played from the server

Work with code of the example

To analyze the code, let's take Streamer example version with hash c313c93, which can be downloaded with build 2.2.4.

View class for the main view of the application: ViewController (header file ViewController.h; implementation file ViewController.m).

1. Import of API. ViewController.m, line 12

#import <FPWCSApi2/FPWCSApi2.h>


2. Connection to server.

ViewController method connect is called when Start button is tapped. ViewController.m, line 247

[self connect];


In the method,

- object with options for connection session is created (ViewController.m, line 36)

FPWCSApi2SessionOptions *options = [[FPWCSApi2SessionOptions alloc] init];
.....
options.urlServer = [NSString stringWithFormat:@"%@://%@:%@", url.scheme, url.host, url.port];
.....
options.appKey = @"defaultApp";


The options include URL of WCS server and appKey of internal server-side application.

- new session is created with method createSession, which returns FPWCSApi2Session object (ViewController.m, line 42)

session = [FPWCSApi2 createSession:options error:&error];


- callback functions for processing session statuses are added (ViewController.m, line 61)

[session on:kFPWCSSessionStatusEstablished callback:^(FPWCSApi2Session *rSession){
    [self changeConnectionStatus:[rSession getStatus]];
    [self onConnected:rSession];
}];

[session on:kFPWCSSessionStatusDisconnected callback:^(FPWCSApi2Session *rSession){
    [self changeConnectionStatus:[rSession getStatus]];
    [self onDisconnected];
}];

[session on:kFPWCSSessionStatusFailed callback:^(FPWCSApi2Session *rSession){
    [self changeConnectionStatus:[rSession getStatus]];
    [self onDisconnected];
}];


Depending on the session status, corresponding ViewController method will be called to make appropriate changes in controls of the interface

  • if connection is successfully established: onConnected
  • in case of disconnection, or connection failure: onDisconnected

- FPWCSApi2Session method connect is called to establish connection to server (ViewController.m, line 75)

[session connect];


3. Stream publishing.

When connection to the server is established, ViewController method publishStream is called to publish the stream. ViewController.m, line 199

[self publishStream];


In the method,

- object with stream publish options is created (ViewController.m, line 81)

FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init];
options.name = streamName;
options.display = _videoView.local;


The required options are stream name and view for displaying video.
Also, video constraints can be specified. E.g., in the example, constraints are added in case the iOS device is iPad:

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
    options.constraints = [[FPWCSApi2MediaConstraints alloc] initWithAudio:YES videoWidth:640 videoHeight:480 videoFps:15];
}


- new stream is created with FPWCSApi2Session method createStream, which returns FPWCSApi2Stream object (ViewController.m, line 88)

publishStream = [session createStream:options error:&error];


- callback functions for processing stream statuses are added (ViewController.m, line 106)

[publishStream on:kFPWCSStreamStatusPublishing callback:^(FPWCSApi2Stream *rStream){
    [self changeStreamStatus:rStream];
    [self onPublishing:rStream];
}];


Depending on the stream status, corresponding ViewController method will be called to make appropriate changes in controls of the interface

  • if stream is successfully published: onPublishing
  • in case of failure, or when stream is unpublished: onUnpublished

- FPWCSApi2Stream method publish is called to publish the stream (ViewController.m, line 120)

[publishStream publish:&error]


4. Disconnection. ViewController.m, line 239

FPWCSApi2Session method disconnect is called to close connection to the server when Stop button is tapped.

[session disconnect];
  • No labels