Versions Compared

Key

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

Example of iOS application with player and streamer

This streamer can be used to publish WebRTC video stream and play any of the following types of streams on Web Call Server

  • RTSP
  • WebRTC
  • RTMP
  • RTMFP

On the screenshot below the example is displayed when a stream is being published and another stream is being played.
Input fields

  • 'WCS URL', where 192.168.2.107 is the address of the WCS server
  • 'Publish Stream' - for the name of published stream
  • 'Play Stream' - for the name of played stream

Two videos are played

  • left - video from the camera
  • right - the played video stream

Image Added

Work with code of the example

To analyze the code, let's take TwoWayStreaming example version with hash 62b3aca, which can be downloaded with corresponding build 2.2.0.

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 11

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


2. Connection to server.

ViewController method connect is called when Connect button is tapped. ViewController.m, line 253

Code Block
languagejs
themeRDark
[self connect];


In the method,

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

Code Block
languagejs
themeRDark


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

- new session is created with method createSession (ViewController.m, line 33)

Code Block
languagejs
themeRDark
FPWCSApi2SessionOptions *options = [[FPWCSApi2SessionOptions alloc] init];
options.urlServer = _connectUrl.text;
options.appKey = @"defaultApp";


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

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 methods 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 66)

Code Block
languagejs
themeRDark
[session connect];


3. Stream publishing.

When connection to the server is established, stream publication can be started by tapping Publish button.
ViewController method publishStream is called when the button is tapped. ViewController.m, line 282

Code Block
languagejs
themeRDark
[self publishStream];


In the method,

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

Code Block
languagejs
themeRDark
FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init];
options.name = _localStreamName.text;
options.display = _localDisplay;


The required options are stream name and view for displaying video.
Also, video constraints can be specified. E.g., in the example, video 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 (ViewController.m, line 79)

Code Block
languagejs
themeRDark
FPWCSApi2Stream *stream = [session createStream:options error:&error];


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

Code Block
languagejs
themeRDark
[stream on:kFPWCSStreamStatusPublishing callback:^(FPWCSApi2Stream *rStream){
    [self changeStreamStatus:rStream];
    [self onPublishing:rStream];
}];

[stream on:kFPWCSStreamStatusUnpublished callback:^(FPWCSApi2Stream *rStream){
    [self changeStreamStatus:rStream];
    [self onUnpublished];
}];

[stream on:kFPWCSStreamStatusFailed callback:^(FPWCSApi2Stream *rStream){
    [self changeStreamStatus:rStream];
    [self onUnpublished];
}];


Depending on the stream status, corresponding ViewController methods 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 111)

Code Block
languagejs
themeRDark
[stream publish:&error]



4. Stop of streaming. ViewController.m, line 274

FPWCSApi2Stream method stop is called to stop streaming when Unpublish button is tapped.

Code Block
languagejs
themeRDark
[stream stop:&error];


5. Stream playback.

When connection to the server is established, playback of a stream can be started by tapping Play button.
ViewController method playStream is called when the button is tapped. ViewController.m, line 315

Code Block
languagejs
themeRDark
[self playStream];


In the method,

- object with stream playback options is created (ViewController.m, line 132)

Code Block
languagejs
themeRDark
FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init];
options.name = _remoteStreamName.text;
options.display = _remoteDisplay;


The options include stream name and view for displaying video.

- new stream is created with FPWCSApi2Session method createStream (ViewController.m, line 136)

Code Block
languagejs
themeRDark
FPWCSApi2Stream *stream = [session createStream:options error:nil];


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

Code Block
languagejs
themeRDark
[stream on:kFPWCSStreamStatusPlaying callback:^(FPWCSApi2Stream *rStream){
    [self changeStreamStatus:rStream];
    [self onPlaying:rStream];
}];
 
[stream on:kFPWCSStreamStatusStopped callback:^(FPWCSApi2Stream *rStream){
    [self changeStreamStatus:rStream];
    [self onStopped];
}];
[stream on:kFPWCSStreamStatusFailed callback:^(FPWCSApi2Stream *rStream){
    [self changeStreamStatus:rStream];
    [self onStopped];
}];


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

  • if playback is successfully started: onPlaying
  • in case of failure, or when playback is stopped: onStopped

- FPWCSApi2Stream method play is called to play the stream (ViewController.m, line 167)

Code Block
languagejs
themeRDark
[stream play:&error]


6. Stop of stream playback. ViewController.m, line 307

FPWCSApi2Stream method stop is called to stop stream playback when Stop button is tapped.

Code Block
languagejs
themeRDark
[stream stop:&error];


7. Disconnection. ViewController.m, line 245

FPWCSApi2Session method disconnect is called to close connection to the server.

Code Block
languagejs
themeRDark
[session disconnect];