All Products
Search
Document Center

:RTMP player

Last Updated:May 12, 2023

This topic describes how to use a Real-Time Messaging Protocol (RTMP) player in a PC app.

Procedure

  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 to the player. Then, specify the URL for the player.

  5. Start the playback.

  6. Stop the playback.

  7. Delete the RTMP player.

Sample code

// Create and display a playback view.
QPoint playerViewPos = ui->playerviewplaceholder->pos();
QSize playerViewSize = ui->playerviewplaceholder->size();
pLVGLWidget_ = new LVGLWidget(0, this);
pLVGLWidget_->resize(playerViewSize);
pLVGLWidget_->move(playerViewPos);
pLVGLWidget_->show();

// Create and configure a player.
pLVLivePlayer_ = createLVLivePlayer();
pLVLivePlayer_->setWindow(pLVGLWidget_);
pLVLivePlayer_->setPlayerCallback(this, nullptr);

...
// Configure the callbacks for the player.
void LivePlayerWindow::onError(const LVPlayerError &error, void *pUserData) {
    emit appendLog(QString::asprintf("player error: code=%d subCode=%d message=%s", error.code, error.subCode,
                                     error.message.c_str()));
}

void LivePlayerWindow::onPlayerStateChange(LVPlayerState state, void *pUserData) {
    emit appendLog(QString::asprintf("player state changed to %d", state));
}

void LivePlayerWindow::onRenderedFirstFrame(int elapsedTimeInMs, void *pUserData) {
    emit appendLog(QString::asprintf("Time to first frame: %d ms", elapsedTimeInMs));
}

void LivePlayerWindow::onVideoSizeChanged(int width, int height, void *pUserData) {
    emit appendLog(QString::asprintf("Changes in the width or height of a video: w=%d h=%d", width, height));
}

void LivePlayerWindow::onStandardSeiInfoUpdate(const uint8_t *buffer, int length, long timeStamp, void *pUserData) {
   emit appendLog(QString::asprintf("Received SEI frame length%d", length));
}

...
// Specify an RTMP URL as the data source. The URL is obtained from IoT Platform.
pLVLivePlayer_->setDataSource("rtmp://xx.xx.xx.xx/live/xxx", true, decryptIvBase64String, decryptKeyBase64String);

// Specify hardware decoding as the highest-priority decoding policy.
pLVLivePlayer_->setDecoderStrategy(LV_DECODER_STRATEGY_HARDWARE_FIRST);
// Specify an aspect ratio-based display mode.
pLVLivePlayer_->setVideoScalingMode(LV_MEDIA_VIDEO_SCALING_MODE_FIT);
// Specify that the player displays the last frame when the playback stops.
pLVLivePlayer_->setPlayerStoppedDrawingMode(LV_PLAYER_STOPPED_DRAWING_MODE_ALWAYS_KEEP_LAST_FRAME);
...
// Start the playback.
LVPlayerCode code = pLVLivePlayer_->start();
if (code == LV_PLAYER_SUCCESS) {
    emit appendLog("The playback started successfully");
} else {
    emit appendLog("The playback failed to start");
}
...
// Stop the playback.
LVPlayerCode code = pLVLivePlayer_->stop();
if (code == LV_PLAYER_SUCCESS) {
    emit appendLog("The playback stopped successfully");
} else {
    emit appendLog("The playback failed to be stopped");
}
...
// Delete the player.
destroyLVLivePlayer(&pLVLivePlayer_);
delete pLVGLWidget_;