1<html> 2 3<head> 4<title>Tremor - Callbacks and non-stdio I/O</title> 5<link rel=stylesheet href="style.css" type="text/css"> 6</head> 7 8<body bgcolor=white text=black link="#5555ff" alink="#5555ff" vlink="#5555ff"> 9<table border=0 width=100%> 10<tr> 11<td><p class=tiny>Tremor documentation</p></td> 12<td align=right><p class=tiny>Tremor version 1.0 - 20020403</p></td> 13</tr> 14</table> 15 16<h1>Callbacks and non-stdio I/O</h1> 17 18Although stdio is convenient and nearly universally implemented as per 19ANSI C, it is not suited to all or even most potential uses of Vorbis. 20For additional flexibility, embedded applications may provide their 21own I/O functions for use with Tremor when stdio is unavailable or not 22suitable. One common example is decoding a Vorbis stream from a 23memory buffer.<p> 24 25Use custom I/O functions by populating an <a 26href="ov_callbacks.html">ov_callbacks</a> structure and calling <a 27href="ov_open_callbacks.html">ov_open_callbacks()</a> or <a 28href="ov_test_callbacks.html">ov_test_callbacks()</a> rather than the 29typical <a href="ov_open.html">ov_open()</a> or <a 30href="ov_test.html">ov_test()</a>. Past the open call, use of 31libvorbisidec is identical to using it with stdio. 32 33<h2>Read function</h2> 34 35The read-like function provided in the <tt>read_func</tt> field is 36used to fetch the requested amount of data. It expects the fetch 37operation to function similar to file-access, that is, a multiple read 38operations will retrieve contiguous sequential pieces of data, 39advancing a position cursor after each read.<p> 40 41The following behaviors are also expected:<p> 42<ul> 43<li>a return of '0' indicates end-of-data (if the by-thread errno is unset) 44<li>short reads mean nothing special (short reads are not treated as error conditions) 45<li>a return of zero with the by-thread errno set to nonzero indicates a read error 46</ul> 47<p> 48 49<h2>Seek function</h2> 50 51The seek-like function provided in the <tt>seek_func</tt> field is 52used to request non-sequential data access by libvorbisidec, moving 53the access cursor to the requested position.<p> 54 55libvorbisidec expects the following behavior: 56<ul> 57<li>The seek function must always return -1 (failure) if the given 58data abstraction is not seekable. It may choose to always return -1 59if the application desires libvorbisidec to treat the Vorbis data 60strictly as a stream (which makes for a less expensive open 61operation).<p> 62 63<li>If the seek function initially indicates seekability, it must 64always succeed upon being given a valid seek request.<p> 65 66<li>The seek function must implement all of SEEK_SET, SEEK_CUR and 67SEEK_END. The implementation of SEEK_END should set the access cursor 68one past the last byte of accessible data, as would stdio 69<tt>fseek()</tt><p> 70</ul> 71 72<h2>Close function</h2> 73 74The close function should deallocate any access state used by the 75passed in instance of the data access abstraction and invalidate the 76instance handle. The close function is assumed to succeed.<p> 77 78One common use of callbacks and the close function is to change the 79behavior of libvorbisidec with respect to file closure for applications 80that <em>must</em> <tt>fclose</tt> data files themselves. By passing 81the normal stdio calls as callback functions, but passing a 82<tt>close_func</tt> that does nothing, an application may call <a 83href="ov_clear.html">ov_clear()</a> and then <tt>fclose()</tt> the 84file originally passed to libvorbisidec. 85 86<h2>Tell function</h2> 87 88The tell function is intended to mimic the 89behavior of <tt>ftell()</tt> and must return the byte position of the 90next data byte that would be read. If the data access cursor is at 91the end of the 'file' (pointing to one past the last byte of data, as 92it would be after calling <tt>fseek(file,SEEK_END,0)</tt>), the tell 93function must return the data position (and thus the total file size), 94not an error.<p> 95 96The tell function need not be provided if the data IO abstraction is 97not seekable.<p. 98 99<br><br> 100<hr noshade> 101<table border=0 width=100%> 102<tr valign=top> 103<td><p class=tiny>copyright © 2002 Xiph.org</p></td> 104<td align=right><p class=tiny><a href="http://www.xiph.org/ogg/vorbis/">Ogg Vorbis</a></p></td> 105</tr><tr> 106<td><p class=tiny>Tremor documentation</p></td> 107<td align=right><p class=tiny>Tremor version 1.0 - 20020403</p></td> 108</tr> 109</table> 110 111</body> 112 113</html> 114