Tremor documentation | Tremor version 1.0 - 20020403

# Callbacks and non-stdio I/O

Although stdio is convenient and nearly universally implemented as per ANSI C, it is not suited to all or even most potential uses of Vorbis. For additional flexibility, embedded applications may provide their own I/O functions for use with Tremor when stdio is unavailable or not suitable. One common example is decoding a Vorbis stream from a memory buffer.

Use custom I/O functions by populating an ov_callbacks structure and calling ov_open_callbacks() or ov_test_callbacks() rather than the typical ov_open() or ov_test(). Past the open call, use of libvorbisidec is identical to using it with stdio.

## Read function

The read-like function provided in the `read_func` field is used to fetch the requested amount of data. It expects the fetch operation to function similar to file-access, that is, a multiple read operations will retrieve contiguous sequential pieces of data, advancing a position cursor after each read.

The following behaviors are also expected:

- a return of '0' indicates end-of-data (if the by-thread errno is unset)
- short reads mean nothing special (short reads are not treated as error conditions)
- a return of zero with the by-thread errno set to nonzero indicates a read error

## Seek function

The seek-like function provided in the `seek_func` field is used to request non-sequential data access by libvorbisidec, moving the access cursor to the requested position.

libvorbisidec expects the following behavior:

- The seek function must always return -1 (failure) if the given data abstraction is not seekable. It may choose to always return -1 if the application desires libvorbisidec to treat the Vorbis data strictly as a stream (which makes for a less expensive open operation).

- If the seek function initially indicates seekability, it must always succeed upon being given a valid seek request.

- The seek function must implement all of SEEK_SET, SEEK_CUR and SEEK_END. The implementation of SEEK_END should set the access cursor one past the last byte of accessible data, as would stdio `fseek()`

## Close function

The close function should deallocate any access state used by the passed in instance of the data access abstraction and invalidate the instance handle. The close function is assumed to succeed.

One common use of callbacks and the close function is to change the behavior of libvorbisidec with respect to file closure for applications that must `fclose` data files themselves. By passing the normal stdio calls as callback functions, but passing a `close_func` that does nothing, an application may call ov_clear() and then `fclose()` the file originally passed to libvorbisidec.

## Tell function

The tell function is intended to mimic the behavior of `ftell()` and must return the byte position of the next data byte that would be read. If the data access cursor is at the end of the 'file' (pointing to one past the last byte of data, as it would be after calling `fseek(file,SEEK_END,0)`), the tell function must return the data position (and thus the total file size), not an error.

The tell function need not be provided if the data IO abstraction is not seekable.

copyright © 2002 Xiph.org | Ogg Vorbis
Tremor documentation | Tremor version 1.0 - 20020403