Versions Compared

Key

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

Example of player for iOS

This player can be used to play any type of stream on Web Call Server

...

  • 192.168.2.107 in the URL is the address of the WCS server
  • stream name is entered in to the 'Play Stream' field (RTSP URL in this case)

Work with code of the example

To analyze the code, let's take Player example version with hash 9f4f5c8, which can be downloaded with corresponding build build 2.5.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

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

2. Connection to server.

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

Code Block
languagejs
themeRDark
[self connect];

In the method,

- object with options for connection session is created (ViewController.m, line 30Session creation.

FPWCSApi2 createSession code

The options include:

  • URL of WCS server
  • appKey of internal server-side application (defaultApp)
Code Block
languagejscpp
themeRDark
FPWCSApi2SessionOptions *options = [[FPWCSApi2SessionOptions alloc] init];
options.urlServer = _connectUrl.text;
options.appKey = @"defaultApp";

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

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

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

- callback functions for processing session statuses are added (ViewController.m, line 53)3. Connection to the server

FPWCSApi2Session connect code

Code Block
languagejscpp
themeRDark
[session on:kFPWCSSessionStatusEstablished callback:^connect];

4. Receiving the event confirming successful connection.

ViewController onConnected code

On this event, ViewController playStream method is called to play stream

Code Block
languagecpp
themeRDark
- (void)onConnected:(FPWCSApi2Session *rSession)session {
    [self changeConnectionStatuschangeViewState:[rSession getStatus]_remoteStreamName enabled:NO];
    [self onConnected:rSessionplayStream];
}];

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

5. Stream playback.

FPWCSApi2Session createStream, FPWCSApi2Stream play code

Object with next stream options is passed to createStream method:

  • stream name
  • view to display video
Code Block
languagecpp
themeRDark
- (FPWCSApi2Stream *)playStream {
    FPWCSApi2Session *session = [FPWCSApi2 getSessions][0];
    [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 67)

Code Block
languagejs
themeRDark
[session connect];

3. Stream playback.

When connection to the server is established, ViewController method playStream is called to start stream playback. ViewController.m, line 130

Code Block
languagejs
themeRDark
[self playStream];

In the method,

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

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 77)

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

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

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 108)

Code Block
languagejs
themeRDark
[stream play:&error]

4. Disconnection. ViewController.m, line 158

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

Code Block
languagejs
themeRDark
[session disconnect];FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init];
    options.name = _remoteStreamName.text;
    options.display = _remoteDisplay;
    NSError *error;
    FPWCSApi2Stream *stream = [session createStream:options error:nil];
    ...
    if(![stream play:&error]) {
        UIAlertController * alert = [UIAlertController
                                     alertControllerWithTitle:@"Failed to play"
                                     message:error.localizedDescription
                                     preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* okButton = [UIAlertAction
                                   actionWithTitle:@"Ok"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action) {
                                       
                                   }];
        
        [alert addAction:okButton];
        [self presentViewController:alert animated:YES completion:nil];
    }
    return stream;
}

6. Disconnection.

FPWCSApi2Session disconnect code

Code Block
languagecpp
themeRDark
- (void)startButton:(UIButton *)button {
    [self changeViewState:button enabled:NO];
    if ([button.titleLabel.text isEqualToString:@"STOP"]) {
        if ([FPWCSApi2 getSessions].count) {
            FPWCSApi2Session *session = [FPWCSApi2 getSessions][0];
            NSLog(@"Disconnect session with server %@", [session getServerUrl]);
            [session disconnect];
        } else {
            NSLog(@"Nothing to disconnect");
            [self onDisconnected];
        }
    } else {
        [self changeViewState:_connectUrl enabled:NO];
        [self connect];
    }
}

7. Receiving the event confirming successful disconnection.

ViewController onDisconnected code

Code Block
languagecpp
themeRDark
- (void)onDisconnected {
    [self changeViewState:_connectUrl enabled:YES];
    [self onStopped];
}