Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

Image Added

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

Code Block
languagejs
themeRDark
#import <FPWCSApi2/FPWCSApi2.h>


2. Connection to server.

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

Code Block
languagejs
themeRDark
[self connect];


In the method,

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

Code Block
languagejs
themeRDark
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)

Code Block
languagejs
themeRDark
session = [FPWCSApi2 createSession:options error:&error];


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

Code Block
languagejs
themeRDark
[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)

Code Block
languagejs
themeRDark
[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

Code Block
languagejs
themeRDark
[self publishStream];


In the method,

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

Code Block
languagejs
themeRDark
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:

Code Block
languagejs
themeRDark
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)

Code Block
languagejs
themeRDark
publishStream = [session createStream:options error:&error];


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

Code Block
languagejs
themeRDark
[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)

Code Block
languagejs
themeRDark
[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.

Code Block
languagejs
themeRDark
[session disconnect];