Here is your VideoPlayer_C documentation as a single continuous text without any emojis/icons and with all code enclosed in triple backticks (```
):
VideoPlayer Module
Introduction
The VideoPlayer_C
module enables video playback functionality using Qt's QMediaPlayer
, with support for rendering via a QAbstractVideoSurface
. This class provide functionality to integrate video player in qml.
Supported Features:
- Video playback:
.mp4
,.avi
, etc. - Play / Pause / Stop control
- Duration and position tracking
- Surface rendering via
QAbstractVideoSurface
- Metadata extraction
- QML integration with
Q_PROPERTY
andQ_INVOKABLE
- Dynamic creation and destruction of media player
Initialization
VideoPlayer_C* m_videoPlayer = std::make_shared<AudioVideoPlayer_CPlayer_C>(this);
m_engine->rootContext()->setContextProperty("VideoPlayer_Q", m_videoPlayer.get());
Internally, the constructor sets the media to a sample file like /rw_data/sample_video.mp4
.
Public API Functions
void play()
Starts or resumes video playback.
m_videoPlayer->play();
void pause()
Pauses the video.
m_videoPlayer->pause();
void stop()
Stops playback and resets internal state.
m_videoPlayer->stop();
void setMediaPosition(int newPosition)
Seeks to a specified position in milliseconds.
m_videoPlayer->setMediaPosition(5000); // 5 seconds
void initMediaPlayer()
Initializes the media player instance if it was previously destroyed. Also reattaches the video surface and restores last playback state.
void destroyMediaPlayer()
Destroys the player and frees resources. Useful for switching contexts or releasing video hardware.
void setFormat(int width, int height, int format)
Sets the video format for the rendering surface. (Currently not fully used in source code)
void getMetaData()
Fetches and logs all available metadata from the media file.
m_videoPlayer->getMetaData();
Properties (Q_PROPERTY)
Property | Type | Description |
---|---|---|
videoSurface |
QAbstractVideoSurface* |
Surface used for rendering frames. |
position |
int |
Current playback position (ms). |
duration |
int |
Total media duration (ms). |
state |
QMediaPlayer::State |
Current playback state. |
Slots
void setVideoSurface(QAbstractVideoSurface *surface)
Attaches a new video rendering surface to the media player.
void onPositionChanged(qint64 value)
Updates the current playback position.
void onDurationChangeed(qint64 value)
Updates the media duration.
void onStateChanged(QMediaPlayer::State value)
Updates the player's internal state.
void onNewVideoContentReceived(const QVideoFrame &frame)
Receives raw frames and pushes them to the video surface.
// Called when a new frame is received
m_surface->present(frame);
void onMediaStatusChanged(QMediaPlayer::MediaStatus status)
Handles changes in media status like buffering, loaded, etc.
void setPosition(int position)
Sets the current playback position and emits positionChanged
.
void setDuration(int duration)
Sets media duration and emits durationChanged
.
void setState(QMediaPlayer::State state)
Sets player state and emits stateChanged
.
Signals
Signal | Description |
---|---|
void frameFinished() |
Emitted when a video frame is completed. |
void positionChanged(int) |
Emitted when playback position updates. |
void durationChanged(int) |
Emitted when total video duration changes. |
void stateChanged(State) |
Emitted on play/pause/stop state change. |
Usage Example
Basic Video Playback
m_videoPlayer->play();
Pause / Stop
m_videoPlayer->pause();
m_videoPlayer->stop();
Seek to Position
m_videoPlayer->setMediaPosition(120000); // Jump to 2 minutes
Dynamic Initialization / Destruction
m_videoPlayer->destroyMediaPlayer();
m_videoPlayer->initMediaPlayer();
Metadata Logging
m_videoPlayer->getMetaData(); // Logs all metadata keys & values
Connecting to Signals
QObject::connect(m_videoPlayer, &VideoPlayer_C::positionChanged, [](int pos){
qDebug() << "Position: " << pos;
});
Default Video Path Resolution
The constructor attempts to automatically resolve a path like:
/rw_data/sample_video.mp4
or
$HOME/sample_video.mp4
It uses getcwd()
and conditional compilation with OS_EMBEDDED
to resolve it appropriately.
Usage of VideoPlayer_Q with VideoOutput
In this code, you're using the VideoOutput element to display video content from a backend media player object named VideoPlayer_Q.
VideoOutput { id: videoOut source: VideoPlayer_Q anchors.fill: parent }
🔹 What’s Happening:
VideoOutput is a built-in QML type that renders video frames.
source: VideoPlayer_Q connects it to a video player object.
When you call VideoPlayer_Q.play(), the video will appear inside this VideoOutput.