AV module

MediaCapture

Unified capture and decode source for files and live media devices.

MediaCapture

#include <icy/av/mediacapture.h>

Inherits: ICapture, Runnable Subclassed by: AudioCapture, VideoCapture

Unified capture and decode source for files and live media devices.

Public Attributes

ReturnNameDescription
NullSignalClosingSignals that the capture thread is closing. This signal is emitted from the capture thread context.

Closing

NullSignal Closing

Signals that the capture thread is closing. This signal is emitted from the capture thread context.

Public Methods

ReturnNameDescription
MediaCapture
MediaCaptureDeleted constructor.
MediaCaptureDeleted constructor.
voidopenFile virtualOpen a media file for decoding. Automatically detects video and audio streams.
voidclose virtualStop the capture thread and close the media stream and all decoders.
voidstart virtualStart the background capture and decode thread. Throws std::runtime_error if no media streams have been opened.
voidstop virtualSignal the capture thread to stop and join it before returning.
voidrun virtualEntry point for the background capture thread. Reads and decodes packets from the format context until EOF or stop() is called.
voidgetEncoderFormat virtualFill format with the combined encoder-ready video and audio codec parameters.
voidgetEncoderAudioCodec virtualFill params with the decoder's output audio codec parameters. Throws std::runtime_error if audio parameters have not been initialised.
voidgetEncoderVideoCodec virtualFill params with the decoder's output video codec parameters. Throws std::runtime_error if video parameters have not been initialised.
voidsetLoopInputContinuously loop the input file when set.
voidsetLimitFramerateLimit playback to video FPS.
voidsetRealtimePTSSet to use realtime PTS calculation. This is preferred when using live captures as FFmpeg-provided values are not always reliable.
AVFormatContext *formatCtx const#### Returns
VideoDecoder *video const#### Returns
AudioDecoder *audio const#### Returns
boolstopping const#### Returns
std::stringerror const#### Returns

MediaCapture

MediaCapture()

MediaCapture

MediaCapture(const MediaCapture &) = delete

Deleted constructor.


MediaCapture

MediaCapture(MediaCapture &&) = delete

Deleted constructor.


openFile

virtual

virtual void openFile(const std::string & file)

Open a media file for decoding. Automatically detects video and audio streams.

Parameters

  • file Path to the media file.

close

virtual

virtual void close()

Stop the capture thread and close the media stream and all decoders.


start

virtual

virtual void start()

Start the background capture and decode thread. Throws std::runtime_error if no media streams have been opened.


stop

virtual

virtual void stop()

Signal the capture thread to stop and join it before returning.


run

virtual

virtual void run()

Entry point for the background capture thread. Reads and decodes packets from the format context until EOF or stop() is called.


getEncoderFormat

virtual

virtual void getEncoderFormat(Format & format)

Fill format with the combined encoder-ready video and audio codec parameters.

Parameters

  • format Output Format struct to populate.

getEncoderAudioCodec

virtual

virtual void getEncoderAudioCodec(AudioCodec & params)

Fill params with the decoder's output audio codec parameters. Throws std::runtime_error if audio parameters have not been initialised.

Parameters


getEncoderVideoCodec

virtual

virtual void getEncoderVideoCodec(VideoCodec & params)

Fill params with the decoder's output video codec parameters. Throws std::runtime_error if video parameters have not been initialised.

Parameters


setLoopInput

void setLoopInput(bool flag)

Continuously loop the input file when set.


setLimitFramerate

void setLimitFramerate(bool flag)

Limit playback to video FPS.


setRealtimePTS

void setRealtimePTS(bool flag)

Set to use realtime PTS calculation. This is preferred when using live captures as FFmpeg-provided values are not always reliable.


formatCtx

const

AVFormatContext * formatCtx() const

Returns

The underlying AVFormatContext (thread-safe, mutex-protected).


video

const

VideoDecoder * video() const

Returns

The active VideoDecoder, or nullptr if no video stream was opened.


audio

const

AudioDecoder * audio() const

Returns

The active AudioDecoder, or nullptr if no audio stream was opened.


stopping

const

bool stopping() const

Returns

True if the capture thread has been asked to stop.


error

const

std::string error() const

Returns

The last error message, or an empty string if no error has occurred.

Protected Attributes

ReturnNameDescription
std::mutex_mutex
Thread_thread
AVFormatContext *_formatCtx
std::unique_ptr< VideoDecoder >_video
std::unique_ptr< AudioDecoder >_audio
std::string_error
std::atomic< bool >_stopping
std::atomic< bool >_looping
std::atomic< bool >_realtime
std::atomic< bool >_ratelimit

_mutex

std::mutex _mutex

_thread

Thread _thread

_formatCtx

AVFormatContext * _formatCtx

_video

std::unique_ptr< VideoDecoder > _video

_audio

std::unique_ptr< AudioDecoder > _audio

_error

std::string _error

_stopping

std::atomic< bool > _stopping

_looping

std::atomic< bool > _looping

_realtime

std::atomic< bool > _realtime

_ratelimit

std::atomic< bool > _ratelimit

Protected Methods

ReturnNameDescription
voidopenStream virtualOpen the underlying media stream.
voidemit virtualEmit an existing packet directly onto the outgoing signal.

openStream

virtual

virtual void openStream(const std::string & filename, const AVInputFormat * inputFormat, AVDictionary ** formatParams)

Open the underlying media stream.

Parameters

  • filename The file path or device name to open.

  • inputFormat The forced input format, or nullptr for auto-detect.

  • formatParams Optional format parameters; may be updated by FFmpeg on return.


emit

virtual

virtual void emit(IPacket & packet)

Emit an existing packet directly onto the outgoing signal.

Parameters

  • packet The packet to forward; must remain valid for the duration of the call.

Public Types

NameDescription
Ptr

Ptr

std::shared_ptr< MediaCapture > Ptr()