\doxysection{Writing with Oggz\+Hungry callbacks}
\label{group__hungry}\index{Writing with OggzHungry callbacks@{Writing with OggzHungry callbacks}}


You can add packets to the Oggz packet queue only when it is "{}hungry"{} by providing an Oggz\+Hungry callback.  


You can add packets to the Oggz packet queue only when it is "{}hungry"{} by providing an Oggz\+Hungry callback. 

An Oggz\+Hungry callback will\+:
\begin{DoxyItemize}
\item Create an {\itshape ogg\+\_\+packet} structure
\item Add it to the packet queue with \doxyref{oggz\+\_\+write\+\_\+feed()}{p.}{group__write__api_ga6ccaceb107db1fd2eae047dbdbaa5889}
\end{DoxyItemize}

Once you have set such a callback with \doxyref{oggz\+\_\+write\+\_\+set\+\_\+hungry\+\_\+callback()}{p.}{group__write__api_gaf362c030bc7a7f57cb23f2b863a59389}, simply call \doxyref{oggz\+\_\+write()}{p.}{group__write__api_ga3c97d94ea425d64546adf9c368b71904} or \doxyref{oggz\+\_\+write\+\_\+output()}{p.}{group__write__api_ga5606dff01964caec4582eb172fde0c1c} repeatedly, and Oggz will call your callback to provide packets when it is hungry.

This process is illustrated in the following diagram\+:

 
\begin{DoxyImage}
\includegraphics[width=10cm]{hungry}
\doxyfigcaptionnolink{Using Oggz\+Hungry}
\end{DoxyImage}


The following example code generates a stream of ten packets, each containing a single byte (\textquotesingle{}A\textquotesingle{}, \textquotesingle{}B\textquotesingle{}, ... , \textquotesingle{}J\textquotesingle{})\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdlib.h>}\ \textcolor{comment}{/*\ exit\ */}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ "{}oggz/oggz.h"{}}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{/*}}
\DoxyCodeLine{\textcolor{comment}{**\ Inspiration\ :\ http://sourcefrog.net/weblog/software/languages/C/unused.html}}
\DoxyCodeLine{\textcolor{comment}{*/}}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef\ UNUSED}}
\DoxyCodeLine{\textcolor{preprocessor}{\#elif\ defined\ (\_\_GNUC\_\_)}}
\DoxyCodeLine{\textcolor{preprocessor}{\#\ \ \ \ \ \ \ define\ UNUSED(x)\ UNUSED\_\ \#\#\ x\ \_\_attribute\_\_\ ((unused))}}
\DoxyCodeLine{\textcolor{preprocessor}{\#elif\ defined\ (\_\_LCLINT\_\_)}}
\DoxyCodeLine{\textcolor{preprocessor}{\#\ \ \ \ \ \ \ define\ UNUSED(x)\ }\textcolor{comment}{/*@unused@*/}\textcolor{preprocessor}{\ x}}
\DoxyCodeLine{\textcolor{preprocessor}{\#else}}
\DoxyCodeLine{\textcolor{preprocessor}{\#\ \ \ \ \ \ \ define\ UNUSED(x)\ x}}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{long}\ serialno;}
\DoxyCodeLine{\textcolor{keyword}{static}\ ogg\_int64\_t\ granulepos\ =\ 0;}
\DoxyCodeLine{\textcolor{keyword}{static}\ ogg\_int64\_t\ packetno\ =\ 0;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{hungry\ (OGGZ\ *\ oggz,\ \textcolor{keywordtype}{int}\ UNUSED(empty),\ \textcolor{keywordtype}{void}\ *\ UNUSED(user\_data))}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ ogg\_packet\ op;}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{char}\ buf[1];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ buf[0]\ =\ \textcolor{charliteral}{'A'}\ +\ (int)packetno;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ op.packet\ =\ buf;}
\DoxyCodeLine{\ \ op.bytes\ =\ 1;}
\DoxyCodeLine{\ \ op.granulepos\ =\ granulepos;}
\DoxyCodeLine{\ \ op.packetno\ =\ packetno;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (packetno\ ==\ 0)\ op.b\_o\_s\ =\ 1;}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{else}\ op.b\_o\_s\ =\ 0;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (packetno\ ==\ 9)\ op.e\_o\_s\ =\ 1;}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{else}\ op.e\_o\_s\ =\ 0;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ oggz\_write\_feed\ (oggz,\ \&op,\ serialno,\ OGGZ\_FLUSH\_AFTER,\ NULL);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ granulepos\ +=\ 100;}
\DoxyCodeLine{\ \ packetno++;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main\ (\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}\ *\ argv[])}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{char}\ *\ progname,\ *\ filename\ =\ NULL;}
\DoxyCodeLine{\ \ OGGZ\ *\ oggz;}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{long}\ n;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ progname\ =\ argv[0];}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (argc\ >\ 1)\ filename\ =\ argv[1];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (filename)\ \{}
\DoxyCodeLine{\ \ \ \ oggz\ =\ oggz\_open\ (filename,\ OGGZ\_WRITE);}
\DoxyCodeLine{\ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{\ \ \ \ oggz\ =\ oggz\_open\_stdio\ (stdout,\ OGGZ\_WRITE);}
\DoxyCodeLine{\ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (oggz\ ==\ NULL)\ \{}
\DoxyCodeLine{\ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}\%s:\ Error\ creating\ oggz\(\backslash\)n"{}},\ progname);}
\DoxyCodeLine{\ \ \ \ exit\ (1);}
\DoxyCodeLine{\ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ serialno\ =\ oggz\_serialno\_new\ (oggz);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (oggz\_write\_set\_hungry\_callback\ (oggz,\ hungry,\ 1,\ NULL)\ ==\ -\/1)\ \{}
\DoxyCodeLine{\ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}\%s:\ Error\ setting\ OggzHungry\ callback\(\backslash\)n"{}},\ progname);}
\DoxyCodeLine{\ \ \ \ exit\ (1);}
\DoxyCodeLine{\ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{while}\ ((n\ =\ oggz\_write\ (oggz,\ 32))\ >\ 0);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ oggz\_close\ (oggz);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ exit\ (0);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 