NAP
Public Member Functions | Public Attributes | List of all members
VideoPlayer Class Reference

#include <videoplayer.h>

Public Member Functions

 VideoPlayer (VideoService &service)
 
const VideoFilegetFile () const
 
const VideogetVideo () const
 
VideogetVideo ()
 
int getIndex () const
 
int getCount () const
 
bool selectVideo (int index, utility::ErrorState &error)
 
void play (double startTime=0.0)
 
void stopPlayback ()
 
bool isPlaying () const
 
void loop (bool value)
 
bool isLooping () const
 
void setSpeed (float speed)
 
float getSpeed () const
 
void seek (double seconds)
 
double getCurrentTime () const
 
double getDuration () const
 
int getWidth () const
 
int getHeight () const
 
bool hasAudio () const
 
virtual bool start (utility::ErrorState &errorState) override
 
virtual void stop () override
 
bool hasSelection () const
 
Texture2DgetYTexture ()
 
Texture2DgetUTexture ()
 
Texture2DgetVTexture ()
 
- Public Member Functions inherited from Resource
 Resource ()
 
- Public Member Functions inherited from Object
 Object ()
 
virtual ~Object ()
 
virtual bool init (utility::ErrorState &errorState)
 
virtual void onDestroy ()
 
 Object (Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&)=delete
 
Objectoperator= (Object &&)=delete
 

Public Attributes

std::vector< nap::ResourcePtr< VideoFile > > mVideoFiles
 Property: 'Files' All video file links. More...
 
nap::uint mVideoIndex = 0
 Property: 'Index' Selected video index. More...
 
bool mLoop = false
 Property: 'Loop' if the selected video loops. More...
 
float mSpeed = 1.0f
 Property: 'Speed' video playback speed. More...
 
nap::Signal< nap::VideoPlayer & > VideoChanged
 
- Public Attributes inherited from Object
std::string mID
 Property: 'mID' unique name of the object. Used as an identifier by the system. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 

Description

Video playback device.

Decodes a video, from a list of videos, in a background thread and stores the result in a set of YUV textures. The textures are filled with the latest frame data on update(), which is a non-blocking operation. The nap::VideoService calls update() automatically each frame.

The main thread will consume the frames when they are present in the frame queue and their timestamp has 'passed'. Internally the textures are only updated when needed. YUV to RGB Conversion can be done in a shader.

Videos are cached internally. This means that all videos in the playlist are created on start(), including the associated video format and video codec contexts. This ensures that a video can be started and stopped fast, without having to re-open a video when switching. The IO and decode threads are spawned on play() and stopped on stopPlayback().

Note that the YUV textures are (re)-created when the video dimensions change from selection to selection. This is done to ensure there is always only 1 set of YUV textures allocated on the GPU per video player instance, instead of a set of textures per video. A valid set of textures is always available after a successful call to selectVideo() Listen to the VideoChanged signal to get notified about a video change.

Every video must contain a video stream, the audio stream is optional. Use a nap::VideoAudioComponent to decode and play back the audio of a video. Without a nap::VideoAudioComponent no audio is decoded and therefore played back.

Do not call start() and stop() at runtime, unless you want to completely restart the device and free all memory, start() and stop() are called automatically by the engine at the appropiate time. Use play() and stopPlayback() instead to start and stop playback of the currently selected video. You always have to call play() after selecting a different video:

utility::ErrorState error;
if (!mVideoPlayer->selectVideo(new_selection, error))
nap::Logger::error(error.toString());
else
mVideoPlayer->play();
Inheritance diagram for VideoPlayer:
[legend]
Collaboration diagram for VideoPlayer:
[legend]

Constructor & Destructor Documentation

◆ VideoPlayer()

VideoPlayer ( VideoService service)

Member Function Documentation

◆ getCount()

int getCount ( ) const
Returns
total number of available videos to choose from

◆ getCurrentTime()

double getCurrentTime ( ) const
Returns
The current playback position in seconds.

◆ getDuration()

double getDuration ( ) const
Returns
The duration of the video in seconds.

◆ getFile()

const VideoFile& getFile ( ) const
Returns
currently selected video file.

◆ getHeight()

int getHeight ( ) const
Returns
Height of the video, in pixels.

◆ getIndex()

int getIndex ( ) const
Returns
selected video index

◆ getSpeed()

float getSpeed ( ) const
Returns
current video playback speed

◆ getUTexture()

Texture2D& getUTexture ( )

Returns the decoded video U texture. The texture is not initialized to zero, but to the 'black' equivalent in YUV space. The size of the U texture is HALF the width * height.

Returns
The video Y texture.

◆ getVideo() [1/2]

Video& getVideo ( )
Returns
selected video context

◆ getVideo() [2/2]

const Video& getVideo ( ) const
Returns
selected video context

◆ getVTexture()

Texture2D& getVTexture ( )

Returns the decoded video V texture. The texture is not initialized to zero, but to the 'black' equivalent in YUV space. The size of the V texture is HALF the width * height.

Returns
The video V texture.

◆ getWidth()

int getWidth ( ) const
Returns
Width of the video, in pixels.

◆ getYTexture()

Texture2D& getYTexture ( )

Returns the decoded video Y texture. The texture is not initialized to zero, but to the 'black' equivalent in YUV space. The size of the Y texture is width * height.

Returns
The video Y texture.

◆ hasAudio()

bool hasAudio ( ) const
Returns
Whether this video has an audio stream.

◆ hasSelection()

bool hasSelection ( ) const
Returns
if there is a video selected.

◆ isLooping()

bool isLooping ( ) const
Returns
if the current video is looping

◆ isPlaying()

bool isPlaying ( ) const

Check if the currently loaded video is playing.

Returns
If the video is currently playing.

◆ loop()

void loop ( bool  value)

If the video re-starts after completion.

Parameters
valueif the video re-starts after completion.

◆ play()

void play ( double  startTime = 0.0)

Starts playback of the current video at the given offset in seconds.

Parameters
startTimeThe offset in seconds to start the video at.

◆ seek()

void seek ( double  seconds)

Seeks within the video to the time provided. This can be called while playing.

Parameters
secondsthe time offset in seconds in the video.

◆ selectVideo()

bool selectVideo ( int  index,
utility::ErrorState error 
)

Loads a new video to play, returns false if selection fails. The 'VideoChanged' signal is emitted on success. Call play() afterwards to start playback of the video.

utility::ErrorState error;
if (!mVideoPlayer->selectVideo(new_selection, error))
nap::Logger::error(error.toString());
else
mVideoPlayer->play();

A new set of YUV textures is generated IF the new video has different dimensions or is the first to be selected. The old set of textures will be destroyed immediately and will be invalid after this call.

Parameters
indexindex of the video to load.
errorcontains the error if changing the video fails.
Returns
if video selection succeeded or failed

◆ setSpeed()

void setSpeed ( float  speed)

Changes the playback speed of the player.

Parameters
speednew playback speed, 1.0f = default speed.

◆ start()

virtual bool start ( utility::ErrorState errorState)
overridevirtual

Starts the device.

Parameters
errorStatecontains the error if the device can't be started
Returns
if the device started

Reimplemented from Device.

◆ stop()

virtual void stop ( )
overridevirtual

Stops the device

Reimplemented from Device.

◆ stopPlayback()

void stopPlayback ( )

Stops playback of the current video.

Member Data Documentation

◆ mLoop

bool mLoop = false

Property: 'Loop' if the selected video loops.

◆ mSpeed

float mSpeed = 1.0f

Property: 'Speed' video playback speed.

◆ mVideoFiles

std::vector<nap::ResourcePtr<VideoFile> > mVideoFiles

Property: 'Files' All video file links.

◆ mVideoIndex

nap::uint mVideoIndex = 0

Property: 'Index' Selected video index.

◆ VideoChanged

nap::Signal<nap::VideoPlayer&> VideoChanged

Emitted after a successful video switch.

nap::icon::error
constexpr const char * error
Definition: imguiservice.h:55