MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::IO::VideoFileWriter Class Reference

Asynchronous video file encoder with a lock-free work queue. More...

#include <VideoFileWriter.hpp>

+ Collaboration diagram for MayaFlux::IO::VideoFileWriter:

Classes

struct  CloseCmd
 
struct  DownloadCmd
 
struct  RawFrame
 

Public Member Functions

std::shared_ptr< Core::Windowcapture_window () const
 
std::future< bool > close ()
 
bool is_open () const
 
bool is_recording () const
 
std::string last_error () const
 
bool open (const std::string &filepath, uint32_t width, uint32_t height, double frame_rate, AVPixelFormat src_pixel_format, AVCodecID explicit_codec=AV_CODEC_ID_NONE)
 
VideoFileWriteroperator= (const VideoFileWriter &)=delete
 
VideoFileWriteroperator= (VideoFileWriter &&)=delete
 
bool record (const std::shared_ptr< Core::Window > &window, const std::string &filepath, double frame_rate, AVCodecID codec_id=AV_CODEC_ID_NONE)
 
std::future< bool > stop_recording ()
 
 VideoFileWriter ()
 
 VideoFileWriter (const VideoFileWriter &)=delete
 
 VideoFileWriter (VideoFileWriter &&)=delete
 
void write (const std::shared_ptr< Buffers::TextureBuffer > &buffer)
 
void write (const std::shared_ptr< Kakshya::TextureContainer > &container, uint32_t layer=0)
 
void write (const std::shared_ptr< Kakshya::VideoStreamContainer > &container, uint64_t frame_index=0)
 
void write (const uint8_t *pixels, size_t size)
 
void write (std::span< const uint8_t > pixels)
 
 ~VideoFileWriter ()
 

Private Types

using WorkItem = std::variant< RawFrame, DownloadCmd, CloseCmd >
 

Private Member Functions

bool post (const WorkItem &item)
 
void set_error (std::string msg)
 
void worker_loop (const std::string &filepath, uint32_t width, uint32_t height, double frame_rate, AVPixelFormat src_fmt, AVCodecID codec_id)
 

Private Attributes

AVCodecID m_capture_codec_id { AV_CODEC_ID_NONE }
 
bool m_capture_did_enable { false }
 
std::string m_capture_filepath
 
double m_capture_frame_rate {}
 
std::atomic< bool > m_capture_opened { false }
 
std::shared_ptr< Core::Windowm_capture_window
 
std::promise< bool > m_close_promise
 
std::atomic< bool > m_closing { false }
 
std::mutex m_error_mutex
 
uint32_t m_height {}
 
std::string m_last_error
 
std::atomic< uint32_t > m_observer_id { 0 }
 
std::atomic< bool > m_open { false }
 
std::unique_ptr< Memory::LockFreeQueue< WorkItem, k_queue_capacity > > m_queue
 
AVPixelFormat m_src_fmt { AV_PIX_FMT_BGRA }
 
uint32_t m_width {}
 
std::thread m_worker
 

Static Private Attributes

static constexpr size_t k_queue_capacity = 512
 

Detailed Description

Asynchronous video file encoder with a lock-free work queue.

Accepts pixel data from multiple source types and encodes it to a file on a dedicated worker thread. The public API is fully non-blocking: every write call copies the pixel data, posts a work item to an SPSC queue, and returns immediately. The worker thread owns all FFmpeg state and is the only thread that touches it.

Supported input paths:

  • record() / stop_recording(): swapchain readback via DisplayService frame observer; encoder opened lazily on first frame.
  • Raw packed uint8_t pixels (span): caller supplies dimensions matching open().
  • TextureContainer: reads pixel_bytes(layer); dimensions from container.
  • VideoStreamContainer / CameraContainer: reads get_frame_pixels(); always RGBA.
  • TextureBuffer: CPU pixels via get_pixel_data() when present, otherwise GPU download via download_data_async() on the worker thread.

Source dimensions may differ from the encoder output dimensions; SwsContext rescales automatically. The format passed to open() must match the source pixel layout for all write() calls on that instance.

close() returns future<bool>; caller must .get() before process exit or the container trailer will not be written.

Definition at line 53 of file VideoFileWriter.hpp.


The documentation for this class was generated from the following files: