All Products
Search
Document Center

:RTMP players

Last Updated:May 12, 2023

This topic describes how to use a Real-Time Messaging Protocol (RTMP) player on an iOS app.

Process

  1. Create an RTMP player.

  2. Configure a rendering view and callbacks for the player.

  3. Configure other parameters for the player based on your business requirements, such as the decoding policy and display mode.

  4. Call the QueryLiveStreaming operation to obtain the RTMP playback URL of an IP camera from which live streams are pushed. Then, specify the URL for the player.

  5. Start a playback.

  6. Stop the playback.

  7. Delete the RTMP player.

Sample code

// Create a player instance.
self.player = [[LVLivePlayer alloc] init];
// Configure the required listener.
self.player.livePlayerDelegate = self;
...
    
#pragma mark LVLivePlayerDelegate
- (void) onLivePlayerError:(LVLivePlayer *_Nonnull)player error:(NSError *_Nonnull)error{
    [self appendInfoText:[NSString stringWithFormat:@"Playback failed: %@\n", error]];
}

- (void) onLivePlayerStateChange:(LVLivePlayer *_Nonnull)player playerState:(LVPlayerState)state{
    [self appendInfoText:[NSString stringWithFormat:@"Status changed: %d\n", state]];
}

- (void) onLivePlayerRenderedFirstFrame:(LVLivePlayer *_Nonnull)player elapsedTimeInMs:(NSInteger)elapsedTimeInMs{
    [self appendInfoText:[NSString stringWithFormat:@"Image generation time for the first frame: %ldms\n", (long)elapsedTimeInMs]];
    [self appendInfoText:[self.player getStatisticsInfo]];
}

- (void) onLivePlayerVideoSizeChanged:(LVLivePlayer *_Nonnull)player width:(NSInteger)width height:(NSInteger)height{
    [self appendInfoText:[NSString stringWithFormat:@"Image size changes: w=%ld h=%ld\n", (long)width, (long)height]];
}

- (void) onLivePlayerStandardSeiInfoUpdate:(LVLivePlayer *_Nonnull)player sei:(NSData*_Nonnull)data timeStamp:(NSInteger)timeStamp{
    [self appendInfoText:[NSString stringWithFormat:@"sei: %lu %ld\n", (unsigned long)data.length, timeStamp]];
}

- (void) onLivePlayerVideoJitterBufferEmpty:(LVLivePlayer *_Nonnull)player{
}
...
// Create an object named LVGlkView and add the object to another object named scrollview to enable the image scaling feature.
self.lvGlkView = [[LVGlkView alloc] initWithFrame:CGRectMake(0,0,self.scrollView.frame.size.width, self.scrollView.frame.size.height)];;
[self.scrollView addSubview:self.lvGlkView];
[self.lvGlkView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(@(0));
    make.centerY.equalTo(@(0));
    make.height.equalTo(self.scrollView.mas_height);
    make.width.equalTo(self.scrollView.mas_width);
}];
// Configure a rendering view for the player and disable image rotation.
[self.player setWindow:self.lvGlkView videoRotationMode:LV_MEDIA_VIDEO_ROTATE_0_CLOCKWISE];

...
// Specify an RTMP URL as a data source. The URL is obtained from IoT Platform.
[self.player setDataSource:@"rtmp://xx.xx.xx.xx/live/xxx"isEncrypted:(YES) decryptIvBase64:(@"xxxxxxx") decryptKeyBase64:@"xxxxxxx"];

// Specify hardware decoding as the highest-priority decoding policy.
[self.player setDecoderStrategy:LV_DECODER_STRATEGY_HARDWARE_FIRST];
// Specify an aspect ratio-based display mode.
[self.player setVideoScalingMode:LV_MEDIA_VIDEO_SCALING_MODE_FIT];
// Specify that the player displays the last frame when a playback stops.
[self.player setPlayerStoppedDrawingMode:LV_PLAYER_STOPPED_DRAWING_MODE_ALWAYS_KEEP_LAST_FRAME];
...
// Start a playback.
[self.player start];
...
// Stop the playback.
[self.player stop];