<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>liboggz: Writing with OggzHungry callbacks</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.2-20100208 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> </ul> </div> </div> <div class="contents"> <h1>Writing with OggzHungry callbacks</h1> <p>You can add packets to the Oggz packet queue only when it is "hungry" by providing an OggzHungry callback. <a href="#_details">More...</a></p> <table border="0" cellpadding="0" cellspacing="0"> </table> <p>You can add packets to the Oggz packet queue only when it is "hungry" by providing an OggzHungry callback. </p> <p>An OggzHungry callback will:</p> <ul> <li>Create an <em>ogg_packet</em> structure</li> <li>Add it to the packet queue with <a class="el" href="group__write__api.html#ga6ccaceb107db1fd2eae047dbdbaa5889" title="Add a packet to oggz's packet queue.">oggz_write_feed()</a></li> </ul> <p>Once you have set such a callback with <a class="el" href="group__write__api.html#gaf362c030bc7a7f57cb23f2b863a59389" title="Set a callback for Oggz to call when oggz is hungry .">oggz_write_set_hungry_callback()</a>, simply call <a class="el" href="group__write__api.html#ga3c97d94ea425d64546adf9c368b71904" title="Write n bytes from an OGGZ handle.">oggz_write()</a> or <a class="el" href="group__write__api.html#ga5606dff01964caec4582eb172fde0c1c" title="Output data from an OGGZ handle.">oggz_write_output()</a> repeatedly, and Oggz will call your callback to provide packets when it is hungry.</p> <p>This process is illustrated in the following diagram:</p> <div align="center"> <img src="hungry.png" alt="hungry.png"/> </div> <p>The following example code generates a stream of ten packets, each containing a single byte ('A', 'B', ... , 'J'):</p> <div class="fragment"><pre class="fragment"> <span class="preprocessor">#include <stdlib.h></span> <span class="comment">/* exit */</span> <span class="preprocessor">#include "<a class="code" href="oggz_8h.html" title="The liboggz C API.">oggz/oggz.h</a>"</span> <span class="keyword">static</span> <span class="keywordtype">long</span> serialno; <span class="keyword">static</span> ogg_int64_t granulepos = 0; <span class="keyword">static</span> ogg_int64_t packetno = 0; <span class="keyword">static</span> <span class="keywordtype">int</span> hungry (<a class="code" href="oggz_8h.html#a672d218df13da45a4b41d5366211bfee" title="An opaque handle to an Ogg file.">OGGZ</a> * oggz, <span class="keywordtype">int</span> empty, <span class="keywordtype">void</span> * user_data) { ogg_packet op; <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> buf[1]; buf[0] = <span class="charliteral">'A'</span> + (int)packetno; op.packet = buf; op.bytes = 1; op.granulepos = granulepos; op.packetno = packetno; <span class="keywordflow">if</span> (packetno == 0) op.b_o_s = 1; <span class="keywordflow">else</span> op.b_o_s = 0; <span class="keywordflow">if</span> (packetno == 9) op.e_o_s = 1; <span class="keywordflow">else</span> op.e_o_s = 0; <a class="code" href="group__write__api.html#ga6ccaceb107db1fd2eae047dbdbaa5889" title="Add a packet to oggz&#39;s packet queue.">oggz_write_feed</a> (oggz, &op, serialno, <a class="code" href="oggz__constants_8h.html#a6a09e7685c864a9116473b236c847237a42efb730f40edcdb0dfdb8a6294619c4" title="Flush after this packet.">OGGZ_FLUSH_AFTER</a>, NULL); granulepos += 100; packetno++; <span class="keywordflow">return</span> 0; } <span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> * argv[]) { <span class="keywordtype">char</span> * progname, * filename = NULL; <a class="code" href="oggz_8h.html#a672d218df13da45a4b41d5366211bfee" title="An opaque handle to an Ogg file.">OGGZ</a> * oggz; <span class="keywordtype">long</span> n; progname = argv[0]; <span class="keywordflow">if</span> (argc > 1) filename = argv[1]; <span class="keywordflow">if</span> (filename) { oggz = <a class="code" href="oggz_8h.html#a65197cdd03f755f7ebfabf2fdff4c7db" title="Open an Ogg file, creating an OGGZ handle for it.">oggz_open</a> (filename, <a class="code" href="oggz__constants_8h.html#a12afc3c052f6e84eff5a99ac9f1ccdd3a8819c3d01c84191dbf846b5e0a98d757" title="Write only.">OGGZ_WRITE</a>); } <span class="keywordflow">else</span> { oggz = <a class="code" href="oggz_8h.html#ac49e9de0bc4ef1d91b43b13605f98b19" title="Create an OGGZ handle associated with a stdio stream.">oggz_open_stdio</a> (stdout, <a class="code" href="oggz__constants_8h.html#a12afc3c052f6e84eff5a99ac9f1ccdd3a8819c3d01c84191dbf846b5e0a98d757" title="Write only.">OGGZ_WRITE</a>); } <span class="keywordflow">if</span> (oggz == NULL) { fprintf (stderr, <span class="stringliteral">"%s: Error creating oggz\n"</span>, progname); exit (1); } serialno = <a class="code" href="oggz_8h.html#aaf89877e3e89408387d422f487bcf094" title="Request a new serialno, as required for a new stream, ensuring the serialno is not...">oggz_serialno_new</a> (oggz); <span class="keywordflow">if</span> (<a class="code" href="group__write__api.html#gaf362c030bc7a7f57cb23f2b863a59389" title="Set a callback for Oggz to call when oggz is hungry .">oggz_write_set_hungry_callback</a> (oggz, hungry, 1, NULL) == -1) { fprintf (stderr, <span class="stringliteral">"%s: Error setting OggzHungry callback\n"</span>, progname); exit (1); } <span class="keywordflow">while</span> ((n = <a class="code" href="group__write__api.html#ga3c97d94ea425d64546adf9c368b71904" title="Write n bytes from an OGGZ handle.">oggz_write</a> (oggz, 32)) > 0); <a class="code" href="oggz_8h.html#aadcfc04b2930660710bbcbc93140b783" title="Close an OGGZ handle.">oggz_close</a> (oggz); exit (0); } </pre></div> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address> </body> </html>