FFmpegfs Fuse Multi Media Filesystem 2.16
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Buffer Class Reference

The Buffer class. More...

#include <buffer.h>

Inheritance diagram for Buffer:
Inheritance graph
[legend]
Collaboration diagram for Buffer:
Collaboration graph
[legend]

Classes

struct  _tagCACHEINFO
 Structure to hold current cache state. More...
 

Public Types

typedef struct Buffer::_tagCACHEINFO CACHEINFO
 Structure to hold current cache state.
 
typedef struct Buffer::_tagCACHEINFOLPCACHEINFO
 Pointer version of CACHEINFO.
 
typedef CACHEINFO const * LPCCACHEINFO
 Pointer to const version of CACHEINFO. More...
 

Public Member Functions

 Buffer ()
 Create Buffer object. More...
 
virtual ~Buffer ()
 Free Buffer object. More...
 
virtual VIRTUALTYPE type () const override
 Get type of this virtual file. More...
 
bool init (bool erase_cache)
 Initialise cache. More...
 
bool set_segment (uint32_t segment_no, size_t size)
 Set the current segment. More...
 
uint32_t segment_count ()
 Get segment count. More...
 
uint32_t current_segment_no ()
 Get the currently selected segment. More...
 
bool segment_exists (uint32_t segment_no)
 Check if segment exists. More...
 
bool release (int flags=CACHE_CLOSE_NOOPT)
 Release cache buffer. More...
 
virtual size_t bufsize () const override
 Size of this buffer. More...
 
virtual int openio (LPVIRTUALFILE virtualfile) override
 Open a virtual file. More...
 
virtual size_t readio (void *data, size_t size) override
 Not implemented. More...
 
size_t read_frame (std::vector< uint8_t > *data, uint32_t frame_no)
 Write image data for the frame number into the buffer. More...
 
virtual int error () const override
 Get last error. More...
 
virtual int64_t duration () const override
 Get the duration of the file, in AV_TIME_BASE fractional seconds. More...
 
virtual size_t size () const override
 Get the value of the internal buffer size pointer. More...
 
virtual size_t size (uint32_t segment_no) const
 Get the value of the internal buffer size pointer. More...
 
virtual size_t tell () const override
 Get the value of the internal read position pointer. More...
 
virtual size_t tell (uint32_t segment_no) const
 Get the value of the internal read position pointer. More...
 
virtual int seek (int64_t offset, int whence) override
 Seek to position in file. More...
 
virtual int seek (int64_t offset, int whence, uint32_t segment_no)
 Seek to position in file. More...
 
virtual bool eof () const override
 Check if at end of file. More...
 
virtual bool eof (uint32_t segment_no) const
 Check if at end of file. More...
 
virtual void closeio () override
 Close buffer. More...
 
size_t writeio (const uint8_t *data, size_t length)
 Write data to the current position in the buffer. The position pointer will be updated. More...
 
size_t write_frame (const uint8_t *data, size_t length, uint32_t frame_no)
 Write image data for the frame number into the buffer. More...
 
bool flush ()
 Flush buffer to disk. More...
 
bool clear ()
 Clear (delete) buffer. More...
 
bool reserve (size_t size)
 Reserve memory without changing size to reduce re-allocations. More...
 
size_t buffer_watermark (uint32_t segment_no=0) const
 Return the current watermark of the file while transcoding. More...
 
bool copy (std::vector< uint8_t > *out_data, size_t offset, uint32_t segment_no=0)
 Copy buffered data into output buffer. More...
 
bool copy (uint8_t *out_data, size_t offset, size_t bufsize, uint32_t segment_no=0)
 Copy buffered data into output buffer. More...
 
const std::string & cachefile (uint32_t segment_no) const
 Get cache filename. More...
 
bool have_frame (uint32_t frame_no)
 Check if we have the requested frame number. Works only when processing a frame set. More...
 
void finished_segment ()
 Complete the segment decoding. More...
 
bool is_segment_finished (uint32_t segment_no) const
 Return true if transcoding of the segment is finished. More...
 
bool open_file (uint32_t segment_no, uint32_t flags, size_t defaultsize=0)
 Open the cache file if not already open. More...
 
bool close_file (uint32_t segment_no, uint32_t flags)
 If it hasn't already been done, close the cache file. More...
 
- Public Member Functions inherited from FileIO
 FileIO ()
 Create FileIO object. More...
 
virtual ~FileIO ()=default
 Free FileIO object.
 
virtual VIRTUALTYPE type () const =0
 Get type of the virtual file. More...
 
virtual size_t bufsize () const =0
 Get the ideal buffer size. More...
 
virtual int openio (LPVIRTUALFILE virtualfile)=0
 Open a virtual file. More...
 
virtual size_t readio (void *data, size_t size)=0
 Read data from a file. More...
 
virtual int error () const =0
 Get last error. More...
 
virtual int64_t duration () const =0
 Get the duration of the file, in AV_TIME_BASE fractional seconds. More...
 
virtual size_t size () const =0
 Get the file size. More...
 
virtual size_t tell () const =0
 Get current read position. More...
 
virtual int seek (int64_t offset, int whence)=0
 Seek to position in file. More...
 
virtual bool eof () const =0
 Check if at end of file. More...
 
virtual void closeio ()=0
 Close virtual file. More...
 
LPVIRTUALFILE virtualfile ()
 Get virtual file object. More...
 
const std::string & filename () const
 Get source filename. More...
 
const std::string & path () const
 Path to source file (without file name) More...
 

Static Public Member Functions

static const std::string & make_cachefile_name (std::string *cachefile, const std::string &filename, const std::string &fileext, bool is_idx)
 Make up a cache file name, including the full path. More...
 
static bool remove_file (const std::string &filename)
 Remove (unlink) the file. More...
 
- Static Public Member Functions inherited from FileIO
static std::shared_ptr< FileIOalloc (VIRTUALTYPE type)
 Allocate the correct object for type(). More...
 

Protected Member Functions

bool remove_cachefile (uint32_t segment_no=0) const
 Remove the cachefile. More...
 
bool is_open ()
 Check if the cache file is open. More...
 
- Protected Member Functions inherited from FileIO
void set_virtualfile (LPVIRTUALFILE virtualfile)
 Set the virtual file object. More...
 

Private Member Functions

uint8_t * write_prepare (size_t length)
 Prepare for the writing operation. More...
 
void increment_pos (size_t increment)
 Increment buffer position. More...
 
bool reallocate (size_t newsize)
 Reallocate the buffer to a new size. More...
 
bool map_file (const std::string &filename, volatile int *fd, uint8_t **p, size_t *filesize, bool *isdefaultsize, size_t defaultsize, bool truncate) const
 Map memory to a file. More...
 
bool unmap_file (const std::string &filename, volatile int *fd, uint8_t **p, size_t len, size_t *filesize) const
 Unmap memory from the file. More...
 
LPCACHEINFO cacheinfo (uint32_t segment_no)
 Get cache information. More...
 
LPCCACHEINFO const_cacheinfo (uint32_t segment_no) const
 Get cache information. More...
 

Private Attributes

std::recursive_mutex m_mutex
 Access mutex. More...
 
LPCACHEINFO m_cur_ci
 Convenience pointer to current write segment. More...
 
uint32_t m_cur_open
 Number of open files. More...
 
std::vector< CACHEINFOm_ci
 Cache info. More...
 

Static Private Attributes

static constexpr int PREALLOC_FACTOR = 5
 PREALLOC_FACTOR - Number of elements allocated on reallocate calls Number of elements allocated on reallocate calls. The buffer keeps track of the typical buffer write size. To prevent repeated reallocations, reallocate() attempts to set aside enough memory for PREALLOC FACTOR average elements whenever it is invoked. More...
 

Detailed Description

The Buffer class.

Definition at line 55 of file buffer.h.

Member Typedef Documentation

◆ LPCCACHEINFO

Pointer to const version of CACHEINFO.

Definition at line 121 of file buffer.h.

Constructor & Destructor Documentation

◆ Buffer()

Buffer::Buffer ( )
explicit

Create Buffer object.

Definition at line 42 of file buffer.cc.

◆ ~Buffer()

Buffer::~Buffer ( )
virtual

Free Buffer object.

Release memory and close files

Definition at line 49 of file buffer.cc.

References release().

Member Function Documentation

◆ buffer_watermark()

size_t Buffer::buffer_watermark ( uint32_t  segment_no = 0) const

Return the current watermark of the file while transcoding.

While transcoding, this value reflects the current size of the transcoded file. This is the maximum byte offset until the file can be read so far.

Parameters
[in]segment_no- If > 0 returns watermark for specific segment. If 0, returns watermark for current write segment.
Returns
Returns the current watermark.

Definition at line 869 of file buffer.cc.

References const_cacheinfo().

Referenced by FFmpeg_Transcoder::encode_finish(), and write_frame().

◆ bufsize()

size_t Buffer::bufsize ( ) const
overridevirtual

Size of this buffer.

Returns
Not applicable, returns 0.

Implements FileIO.

Definition at line 59 of file buffer.cc.

Referenced by copy().

◆ cachefile()

const std::string & Buffer::cachefile ( uint32_t  segment_no) const

Get cache filename.

Parameters
[in]segment_no- [1..n] HLS segment file number or 0 for the current segment.
Returns
Returns cache filename.

Definition at line 961 of file buffer.cc.

References const_cacheinfo().

Referenced by make_cachefile_name().

◆ cacheinfo()

Buffer::LPCACHEINFO Buffer::cacheinfo ( uint32_t  segment_no)
private

Get cache information.

Parameters
[in]segment_no- HLS segment file number [1..n] or 0 for the current segment.
Returns
Pointer to CACHEINFO object, or nullptr on error.

Definition at line 1122 of file buffer.cc.

References m_ci, m_cur_ci, and segment_count().

Referenced by seek().

◆ clear()

bool Buffer::clear ( )

Clear (delete) buffer.

Returns
Returns true on success; false on error. Check errno for details.

Definition at line 584 of file buffer.cc.

References Logging::error(), m_ci, m_mutex, and remove_file().

◆ close_file()

bool Buffer::close_file ( uint32_t  segment_no,
uint32_t  flags 
)

If it hasn't already been done, close the cache file.

Parameters
[in]segment_no- [0..n-1] Index of segment file number.
[in]flags- CACHE_FLAG_* options
Returns
Returns true if the operation was successful or the file was already closed; false otherwise.

Definition at line 137 of file buffer.cc.

References Buffer::_tagCACHEINFO::m_buffer, Buffer::_tagCACHEINFO::m_buffer_pos, Buffer::_tagCACHEINFO::m_buffer_size, Buffer::_tagCACHEINFO::m_buffer_watermark, Buffer::_tagCACHEINFO::m_cachefile, m_ci, m_cur_open, Buffer::_tagCACHEINFO::m_fd, Buffer::_tagCACHEINFO::m_flags, m_mutex, Logging::trace(), and unmap_file().

Referenced by release(), and set_segment().

◆ closeio()

void Buffer::closeio ( )
overridevirtual

Close buffer.

Implements FileIO.

Definition at line 1058 of file buffer.cc.

References release().

◆ const_cacheinfo()

Buffer::LPCCACHEINFO Buffer::const_cacheinfo ( uint32_t  segment_no) const
private

Get cache information.

Parameters
[in]segment_no- HLS segment file number [1..n] or 0 for the current segment.
Returns
Pointer to CACHEINFO object, or nullptr on error.

Definition at line 1138 of file buffer.cc.

References m_ci, and m_cur_ci.

Referenced by buffer_watermark(), cachefile(), copy(), is_segment_finished(), size(), and tell().

◆ copy() [1/2]

bool Buffer::copy ( std::vector< uint8_t > *  out_data,
size_t  offset,
uint32_t  segment_no = 0 
)

Copy buffered data into output buffer.

Parameters
[in]out_data- Buffer to copy data to.
[in]offset- Offset in buffer to copy data from.
[in]segment_no- HLS segment file number [1..n] or 0 for current segment.
Returns
Returns true on success; false on error.

Definition at line 882 of file buffer.cc.

References copy().

Referenced by copy(), FFmpeg_Transcoder::create_fake_wav_header(), FFmpeg_Transcoder::read_aiff_chunk(), and read_frame().

◆ copy() [2/2]

bool Buffer::copy ( uint8_t *  out_data,
size_t  offset,
size_t  bufsize,
uint32_t  segment_no = 0 
)

Copy buffered data into output buffer.

Parameters
[in]out_data- Buffer to copy data to.
[in]offset- Offset in buffer to copy data from.
[in]bufsize- Size of out_data buffer.
[in]segment_no- [1..n] HLS segment file number or 0 for the current segment.
Returns
Returns true on success; false on error.

Definition at line 887 of file buffer.cc.

References bufsize(), const_cacheinfo(), Logging::error(), and m_mutex.

◆ current_segment_no()

uint32_t Buffer::current_segment_no ( )

Get the currently selected segment.

Returns
current segment number [1..n], or 0 if none is chosen.

Definition at line 336 of file buffer.cc.

References m_ci, m_cur_ci, m_mutex, and segment_count().

Referenced by set_segment().

◆ duration()

int64_t Buffer::duration ( ) const
overridevirtual

Get the duration of the file, in AV_TIME_BASE fractional seconds.

Returns
Not applicable to buffer, always returns AV_NOPTS_VALUE.

Implements FileIO.

Definition at line 846 of file buffer.cc.

◆ eof() [1/2]

bool Buffer::eof ( ) const
overridevirtual

Check if at end of file.

Returns
Returns true if at end of buffer.

Implements FileIO.

Definition at line 1048 of file buffer.cc.

References eof().

Referenced by eof(), and FFmpeg_Transcoder::read_aiff_chunk().

◆ eof() [2/2]

bool Buffer::eof ( uint32_t  segment_no) const
virtual

Check if at end of file.

Parameters
[in]segment_no- [1..n] HLS segment file number or 0 for the current segment.
Returns
Returns true if at end of buffer.

Definition at line 1053 of file buffer.cc.

References size(), and tell().

◆ error()

int Buffer::error ( ) const
overridevirtual

Get last error.

Returns
errno value of last error.

Implements FileIO.

Definition at line 1043 of file buffer.cc.

◆ finished_segment()

void Buffer::finished_segment ( )

Complete the segment decoding.

Definition at line 1097 of file buffer.cc.

References flush(), m_cur_ci, and Buffer::_tagCACHEINFO::m_seg_finished.

Referenced by FFmpeg_Transcoder::encode_finish().

◆ flush()

bool Buffer::flush ( )

◆ have_frame()

bool Buffer::have_frame ( uint32_t  frame_no)

Check if we have the requested frame number. Works only when processing a frame set.

Parameters
[in]frame_no- 1...frames
Returns
Returns true if the frame is already in the cache, false if not.

Definition at line 1063 of file buffer.cc.

References Buffer::_tagCACHEINFO::m_buffer_idx, m_cur_ci, m_mutex, and FileIO::virtualfile().

Referenced by FFmpeg_Transcoder::seek_frame(), and FFmpeg_Transcoder::skip_decoded_frames().

◆ increment_pos()

void Buffer::increment_pos ( size_t  increment)
private

Increment buffer position.

Increment the location of the internal pointer. This cannot fail and so returns void. It does not ensure the position is valid memory. That is done by the write_prepare methods via reallocate.

Parameters
[in]increment- Increment size.

Definition at line 766 of file buffer.cc.

References Buffer::_tagCACHEINFO::m_buffer_pos, and m_cur_ci.

Referenced by writeio().

◆ init()

bool Buffer::init ( bool  erase_cache)

Initialise cache.

Parameters
[in]erase_cache- If true, delete the old file before opening.
Returns
Returns true on success; false on error.

Definition at line 179 of file buffer.cc.

References FFMPEGFS_PARAMS::current_format(), Logging::error(), FileIO::filename(), VIRTUALFILE::get_segment_count(), is_open(), m_ci, m_cur_ci, m_mutex, VIRTUALFILE::m_video_frame_count, make_cachefile_name(), make_filename(), map_file(), mktree(), new_strdup(), params, remove_cachefile(), FileIO::virtualfile(), VIRTUALFLAG_FRAME, and VIRTUALFLAG_HLS.

◆ is_open()

bool Buffer::is_open ( )
protected

Check if the cache file is open.

Returns
Returns true if the cache file is open; false if not.

Definition at line 1082 of file buffer.cc.

References m_ci, and m_mutex.

Referenced by init(), and release().

◆ is_segment_finished()

bool Buffer::is_segment_finished ( uint32_t  segment_no) const

Return true if transcoding of the segment is finished.

Parameters
[in]segment_no- [1..n] HLS segment file number or 0 for the current segment.
Returns
Returns true if finished, false if not.

Definition at line 1109 of file buffer.cc.

References const_cacheinfo().

◆ make_cachefile_name()

const std::string & Buffer::make_cachefile_name ( std::string *  cachefile,
const std::string &  filename,
const std::string &  fileext,
bool  is_idx 
)
static

Make up a cache file name, including the full path.

Parameters
[out]cachefile- Name of cache file.
[in]filename- Source file name.
[in]fileext- File extension (MP4, WEBM etc.).
[in]is_idx- If true, create an index file; otherwise, create a cache.
Returns
Returns the name of the cache/index file.

Definition at line 975 of file buffer.cc.

References cachefile(), FileIO::filename(), FFMPEGFS_PARAMS::m_mountpath, params, and transcoder_cache_path().

Referenced by init(), load_path(), and make_hls_fileset().

◆ map_file()

bool Buffer::map_file ( const std::string &  filename,
volatile int *  fd,
uint8_t **  p,
size_t *  filesize,
bool *  isdefaultsize,
size_t  defaultsize,
bool  truncate 
) const
private

Map memory to a file.

Parameters
[in]filename- Name of the cache file to open.
[out]fd- The file descriptor of the open cache file.
[out]p- Memory pointer to the cache file.
[out]filesize- Actual size of the cache file after this call.
[in,out]isdefaultsize-
In: If false, the file size will be the size of the existing file, returning the size in filesize. If the file does not exist, it will be sized to defaultsize. If true, the defaultsize will be used in any case, resizing an existing file if necessary.
Out: true if the file size was set to default.
[out]defaultsize- The default size of the file if it does not exist. This parameter can be zero, in which case the size will be set to the system's page size.
[out]truncate- If true, when the file is opened, it is truncated.
Returns
Returns true if successful and fd, p, filesize, and isdefaultsize are filled in, or false on error.

Definition at line 358 of file buffer.cc.

References Logging::error(), FileIO::filename(), format_size(), and Logging::trace().

Referenced by init(), and open_file().

◆ open_file()

bool Buffer::open_file ( uint32_t  segment_no,
uint32_t  flags,
size_t  defaultsize = 0 
)

Open the cache file if not already open.

Parameters
[in]segment_no- [0..n-1] Index of segment file number.
[in]flags- CACHE_FLAG_* options.
[in]defaultsize- If nonzero, after opening, the file will be resized to this value. Works on writeable files only.
Returns
Returns true if the operation was successful or the file was already open; false otherwise.

Definition at line 77 of file buffer.cc.

References CACHE_FLAG_RW, Logging::debug(), Buffer::_tagCACHEINFO::m_buffer, Buffer::_tagCACHEINFO::m_buffer_pos, Buffer::_tagCACHEINFO::m_buffer_size, Buffer::_tagCACHEINFO::m_buffer_watermark, Buffer::_tagCACHEINFO::m_buffer_write_size, Buffer::_tagCACHEINFO::m_buffer_writes, Buffer::_tagCACHEINFO::m_cachefile, m_ci, m_cur_open, Buffer::_tagCACHEINFO::m_fd, Buffer::_tagCACHEINFO::m_flags, m_mutex, map_file(), reserve(), and Logging::trace().

Referenced by FFmpeg_Transcoder::open_output(), FFmpeg_Transcoder::open_output_frame_set(), and set_segment().

◆ openio()

int Buffer::openio ( LPVIRTUALFILE  virtualfile)
overridevirtual

Open a virtual file.

Parameters
[in]virtualfile- LPCVIRTUALFILE of file to open
Returns
Upon successful completion, openio() returns 0. On error, an nonzero value is returned and errno is set to indicate the error.

Implements FileIO.

Definition at line 64 of file buffer.cc.

References FileIO::set_virtualfile(), and FileIO::virtualfile().

◆ read_frame()

size_t Buffer::read_frame ( std::vector< uint8_t > *  data,
uint32_t  frame_no 
)

Write image data for the frame number into the buffer.

Parameters
[out]data- Buffer to read data in.
[in]frame_no- Number of the frame to write.
Returns
Upon successful completion, readio() returns the number of bytes read.
This may be less than size.
On error, the value 0 is returned and errno is set to indicate the error.
If at the end of the file, 0 may be returned by errno not set. error() will return 0 if at EOF.
If the image frame is not yet read, the function also returns 0 and errno is EAGAIN.

Definition at line 1016 of file buffer.cc.

References copy(), Buffer::_tagCACHEINFO::m_buffer_idx, m_cur_ci, m_mutex, and FileIO::virtualfile().

◆ readio()

size_t Buffer::readio ( void *  data,
size_t  size 
)
overridevirtual

Not implemented.

Parameters
[out]data- unused
[in]size- unused
Returns
Always returns 0 and errno is EPERM.

Implements FileIO.

Definition at line 1009 of file buffer.cc.

◆ reallocate()

bool Buffer::reallocate ( size_t  newsize)
private

Reallocate the buffer to a new size.

Ensure the allocation has at least the size of bytes available. If not, reallocate memory to make more available. Fill the newly allocated memory with zeroes.

Parameters
[in]newsize- New buffer size.
Returns
Returns true on success; false on error.

Definition at line 933 of file buffer.cc.

References FileIO::filename(), Buffer::_tagCACHEINFO::m_buffer_write_size, Buffer::_tagCACHEINFO::m_buffer_writes, m_cur_ci, PREALLOC_FACTOR, reserve(), size(), and Logging::trace().

Referenced by write_prepare().

◆ release()

bool Buffer::release ( int  flags = CACHE_CLOSE_NOOPT)

Release cache buffer.

Parameters
[in]flags- One of the CACHE_CLOSE_* flags.
Returns
Returns true on success; false on error.

Definition at line 491 of file buffer.cc.

References CACHE_CHECK_BIT, CACHE_CLOSE_DELETE, CACHE_FLAG_RO, CACHE_FLAG_RW, close_file(), flush(), is_open(), m_ci, m_mutex, remove_cachefile(), segment_count(), and unmap_file().

Referenced by closeio(), and ~Buffer().

◆ remove_cachefile()

bool Buffer::remove_cachefile ( uint32_t  segment_no = 0) const
protected

Remove the cachefile.

Parameters
[in]segment_no- [1..n] HLS segment file number or 0 for the current segment.
Returns
Returns true on success; false on error.

Definition at line 541 of file buffer.cc.

References Buffer::_tagCACHEINFO::m_cachefile, Buffer::_tagCACHEINFO::m_cachefile_idx, m_ci, m_cur_ci, and remove_file().

Referenced by init(), and release().

◆ remove_file()

bool Buffer::remove_file ( const std::string &  filename)
static

Remove (unlink) the file.

Parameters
[in]filename- Name of the file to remove.
Returns
Returns true on success; false on error.

Definition at line 995 of file buffer.cc.

References FileIO::filename(), and Logging::warning().

Referenced by clear(), and remove_cachefile().

◆ reserve()

bool Buffer::reserve ( size_t  size)

Reserve memory without changing size to reduce re-allocations.

Parameters
[in]size- Size of buffer to reserve.
Returns
Returns true on success; false on error.

Definition at line 624 of file buffer.cc.

References Logging::error(), Buffer::_tagCACHEINFO::m_buffer, Buffer::_tagCACHEINFO::m_buffer_size, Buffer::_tagCACHEINFO::m_cachefile, m_cur_ci, Buffer::_tagCACHEINFO::m_fd, m_mutex, and size().

Referenced by open_file(), reallocate(), and set_segment().

◆ seek() [1/2]

int Buffer::seek ( int64_t  offset,
int  whence 
)
overridevirtual

Seek to position in file.

Repositions the offset of the open file to the argument offset according to the directive whence. May block for a long time if the buffer has not been filled to the requested offset.

Parameters
[in]offset- offset in bytes
[in]whence- how to seek:
SEEK_SET: The offset is set to offset bytes.
SEEK_CUR: The offset is set to its current location plus offset bytes.
SEEK_END: The offset is set to the size of the file plus offset bytes.
Returns
Upon successful completion, seek() returns the resulting offset location as measured in bytes from the beginning of the file.

Implements FileIO.

Definition at line 771 of file buffer.cc.

References seek().

Referenced by FFmpeg_Transcoder::create_fake_aiff_header(), FFmpeg_Transcoder::create_fake_wav_header(), seek(), and write_frame().

◆ seek() [2/2]

int Buffer::seek ( int64_t  offset,
int  whence,
uint32_t  segment_no 
)
virtual

Seek to position in file.

Repositions the offset of the open file to the argument offset according to the directive whence. May block for a long time if the buffer has not been filled to the requested offset.

Parameters
[in]offset- offset in bytes
[in]whence- how to seek:
SEEK_SET: The offset is set to offset bytes.
SEEK_CUR: The offset is set to its current location plus offset bytes.
SEEK_END: The offset is set to the size of the file plus offset bytes.
[in]segment_no- [1..n] HLS segment file number or 0 for the current segment.
Returns
Upon successful completion, seek() returns the resulting offset location as measured in bytes from the beginning of the file.

Definition at line 776 of file buffer.cc.

References cacheinfo(), Buffer::_tagCACHEINFO::m_buffer, Buffer::_tagCACHEINFO::m_buffer_pos, size(), and tell().

◆ segment_count()

uint32_t Buffer::segment_count ( )

Get segment count.

Returns
Number of segments.

Definition at line 329 of file buffer.cc.

References m_ci, and m_mutex.

Referenced by cacheinfo(), current_segment_no(), flush(), release(), segment_exists(), and set_segment().

◆ segment_exists()

bool Buffer::segment_exists ( uint32_t  segment_no)

Check if segment exists.

Parameters
[in]segment_no- [1..n] HLS segment file number.
Returns
Returns true if it exists, or false if it still has to be decoded.

Definition at line 347 of file buffer.cc.

References file_exists(), m_ci, m_cur_ci, m_mutex, and segment_count().

Referenced by FFmpeg_Transcoder::start_new_segment().

◆ set_segment()

bool Buffer::set_segment ( uint32_t  segment_no,
size_t  size 
)

Set the current segment.

Parameters
[in]segment_no- [1..n] HLS segment file number.
[in]size- Estimated size of segment. Should be large enough to hold the resulting size.
Returns
Returns true on success; if segment_no is 0 or greater, then segment_count() returns false and sets errno to EINVALID.

Definition at line 303 of file buffer.cc.

References CACHE_FLAG_RW, close_file(), current_segment_no(), m_ci, m_cur_ci, m_mutex, open_file(), reserve(), segment_count(), and size().

Referenced by FFmpeg_Transcoder::start_new_segment().

◆ size() [1/2]

size_t Buffer::size ( ) const
overridevirtual

Get the value of the internal buffer size pointer.

Returns
Returns the value of the internal buffer size pointer.

Implements FileIO.

Definition at line 851 of file buffer.cc.

References size().

Referenced by eof(), reallocate(), reserve(), seek(), set_segment(), and size().

◆ size() [2/2]

size_t Buffer::size ( uint32_t  segment_no) const
virtual

Get the value of the internal buffer size pointer.

Parameters
[in]segment_no- [1..n] HLS segment file number or 0 for the current segment.
Returns
Returns the value of the internal buffer size pointer.

Definition at line 856 of file buffer.cc.

References const_cacheinfo().

◆ tell() [1/2]

size_t Buffer::tell ( ) const
overridevirtual

Get the value of the internal read position pointer.

Returns
Returns the value of the internal read position pointer.

Implements FileIO.

Definition at line 828 of file buffer.cc.

References tell().

Referenced by FFmpeg_Transcoder::create_fake_aiff_header(), FFmpeg_Transcoder::create_fake_wav_header(), eof(), seek(), FFmpeg_Transcoder::start_new_segment(), and tell().

◆ tell() [2/2]

size_t Buffer::tell ( uint32_t  segment_no) const
virtual

Get the value of the internal read position pointer.

Parameters
[in]segment_no- [1..n] HLS segment file number or 0 for the current segment.
Returns
Returns the value of the internal read position pointer.

Definition at line 833 of file buffer.cc.

References const_cacheinfo().

◆ type()

VIRTUALTYPE Buffer::type ( ) const
overridevirtual

Get type of this virtual file.

Returns
Returns VIRTUALTYPE::BUFFER.

Implements FileIO.

Definition at line 54 of file buffer.cc.

References BUFFER.

◆ unmap_file()

bool Buffer::unmap_file ( const std::string &  filename,
volatile int *  fd,
uint8_t **  p,
size_t  len,
size_t *  filesize 
) const
private

Unmap memory from the file.

Parameters
[in]filename- Name of cache file to unmap.
[in]fd- The file descriptor of the open cache file.
[in]p- Memory pointer to the cache file.
[in]len- Length of the allocated block.
[in]filesize- Actual size of the cache file.
Returns
Returns true on success; false on error.

Definition at line 441 of file buffer.cc.

References Logging::error(), FileIO::filename(), and Logging::trace().

Referenced by close_file(), and release().

◆ write_frame()

size_t Buffer::write_frame ( const uint8_t *  data,
size_t  length,
uint32_t  frame_no 
)

Write image data for the frame number into the buffer.

Parameters
[in]data- Buffer with data to write.
[in]length- Length of buffer to write.
[in]frame_no- Number of the frame to write.
Returns
Returns the bytes written to the buffer. If less than length, this indicates an error. Consult errno for details.

Definition at line 695 of file buffer.cc.

References buffer_watermark(), IMAGE_FRAME_TAG, Buffer::_tagCACHEINFO::m_buffer_idx, m_cur_ci, IMAGE_FRAME::m_frame_no, m_mutex, IMAGE_FRAME::m_offset, IMAGE_FRAME::m_size, IMAGE_FRAME::m_tag, seek(), FileIO::virtualfile(), and writeio().

Referenced by FFmpeg_Transcoder::encode_image_frame().

◆ write_prepare()

uint8_t * Buffer::write_prepare ( size_t  length)
private

Prepare for the writing operation.

Ensure the buffer has sufficient space for the quantity of data and return a pointer to where the data may be written. The position pointer should be updated afterward with increment_pos().

Parameters
[in]length- Buffer length to prepare.
Returns
Returns a pointer to the memory to write to.

Definition at line 749 of file buffer.cc.

References Buffer::_tagCACHEINFO::m_buffer, Buffer::_tagCACHEINFO::m_buffer_pos, Buffer::_tagCACHEINFO::m_buffer_watermark, m_cur_ci, and reallocate().

Referenced by writeio().

◆ writeio()

size_t Buffer::writeio ( const uint8_t *  data,
size_t  length 
)

Write data to the current position in the buffer. The position pointer will be updated.

Parameters
[in]data- Buffer with data to write.
[in]length- Length of buffer to write.
Returns
Returns the bytes written to the buffer. If less than length, this indicates an error. Consult errno for details.

Definition at line 668 of file buffer.cc.

References increment_pos(), Buffer::_tagCACHEINFO::m_buffer, Buffer::_tagCACHEINFO::m_buffer_write_size, Buffer::_tagCACHEINFO::m_buffer_writes, m_cur_ci, m_mutex, and write_prepare().

Referenced by FFmpeg_Transcoder::create_fake_aiff_header(), FFmpeg_Transcoder::create_fake_wav_header(), FFmpeg_Transcoder::output_write(), and write_frame().

Member Data Documentation

◆ m_ci

std::vector<CACHEINFO> Buffer::m_ci
private

◆ m_cur_ci

LPCACHEINFO Buffer::m_cur_ci
private

◆ m_cur_open

uint32_t Buffer::m_cur_open
private

Number of open files.

Definition at line 475 of file buffer.h.

Referenced by close_file(), and open_file().

◆ m_mutex

std::recursive_mutex Buffer::m_mutex
private

◆ PREALLOC_FACTOR

constexpr int Buffer::PREALLOC_FACTOR = 5
staticconstexprprivate

PREALLOC_FACTOR - Number of elements allocated on reallocate calls Number of elements allocated on reallocate calls. The buffer keeps track of the typical buffer write size. To prevent repeated reallocations, reallocate() attempts to set aside enough memory for PREALLOC FACTOR average elements whenever it is invoked.

Definition at line 64 of file buffer.h.

Referenced by reallocate().


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