Main Page | Modules | Data Structures | File List | Globals | Related Pages

Advanced management of AnxRead callbacks
[Reading from Annodex media]

You retain control of the number of bytes read or input, and the callbacks you provide can instruct libannodex to immediately return control back to your application.

Callbacks

It is not required to implement all callbacks.

Return values

These mechanisms are illustrated in src/examples/print-lots.c:

#include <stdio.h> #include <string.h> #include <annodex/annodex.h> struct my_data { char * filename; long interesting_serialno; int interesting_raw_packets; int done; }; static int read_stream (ANNODEX * anx, double timebase, char * utc, void * user_data) { struct my_data * happy = (struct my_data *) user_data; printf ("Welcome to %s! The timebase is %f\n", happy->filename, timebase); return ANX_CONTINUE; } static int read_track (ANNODEX * anx, long serialno, char * id, char * content_type, anx_int64_t granule_rate_n, anx_int64_t granule_rate_d, int nr_header_packets, void * user_data) { struct my_data * happy = (struct my_data *) user_data; /* Ignore the annotations track, we don't find it interesting! */ if (!strncmp (content_type, "text/x-cmml", 12)) return ANX_CONTINUE; printf ("Our first track has content-type %s and granule rate %ld/%ld.\n", content_type, (long)granule_rate_n, (long)granule_rate_d); printf ("We will remember it by its serial number %ld " "and mark it with crosses.\n", serialno); happy->interesting_serialno = serialno; /* We don't care about any other tracks! */ anx_set_read_track_callback (anx, NULL, NULL); return ANX_CONTINUE; } static int read_raw (ANNODEX * annodex, unsigned char * buf, long n, long serialno, anx_int64_t granulepos, void * user_data) { struct my_data * happy = (struct my_data *) user_data; if (happy->done) { putchar ('!'); } else if (serialno == happy->interesting_serialno) { happy->interesting_raw_packets++; putchar ('+'); } else { putchar ('.'); } return ANX_CONTINUE; } static int read_clip3 (ANNODEX * anx, const AnxClip * clip, void * user_data) { struct my_data * happy = (struct my_data *) user_data; printf ("\nAnd the third clip links to %s\n", clip->anchor_href); happy->done = 1; printf ("This completes our whirlwind tour of the first three clips!\n"); return ANX_STOP_OK; } static int read_clip2 (ANNODEX * anx, const AnxClip * clip, void * user_data) { printf ("\nThe second clip links to %s\n", clip->anchor_href); anx_set_read_clip_callback (anx, read_clip3, user_data); return ANX_CONTINUE; } static int read_clip1 (ANNODEX * anx, const AnxClip * clip, void * user_data) { printf ("\nThe first clip links to %s\n", clip->anchor_href); anx_set_read_clip_callback (anx, read_clip2, user_data); return ANX_CONTINUE; } int main (int argc, char *argv[]) { ANNODEX * anx = NULL; struct my_data me; long n; if (argc != 2) { fprintf (stderr, "Usage: %s file.anx\n", argv[0]); exit (1); } me.filename = argv[1]; me.interesting_serialno = -1; me.interesting_raw_packets = 0; me.done = 0; anx = anx_open (me.filename, ANX_READ); anx_set_read_stream_callback (anx, read_stream, &me); anx_set_read_track_callback (anx, read_track, &me); anx_set_read_raw_callback (anx, read_raw, &me); anx_set_read_clip_callback (anx, read_clip1, &me); while (!me.done && (n = anx_read (anx, 1024)) > 0); printf ("%d packets from the first track (serialno %ld) were received\n", me.interesting_raw_packets, me.interesting_serialno); printf ("before the third clip.\n"); anx_close (anx); exit (0); }
00001 00033 #include <stdio.h> 00034 #include <string.h> 00035 #include <annodex/annodex.h> 00036 00037 struct my_data { 00038 char * filename; 00039 long interesting_serialno; 00040 int interesting_raw_packets; 00041 int done; 00042 }; 00043 00044 static int 00045 read_stream (ANNODEX * anx, double timebase, char * utc, void * user_data) 00046 { 00047 struct my_data * happy = (struct my_data *) user_data; 00048 00049 printf ("Welcome to %s! The timebase is %f\n", happy->filename, timebase); 00050 return ANX_CONTINUE; 00051 } 00052 00053 static int 00054 read_track (ANNODEX * anx, long serialno, char * id, char * content_type, 00055 anx_int64_t granule_rate_n, anx_int64_t granule_rate_d, 00056 int nr_header_packets, void * user_data) 00057 { 00058 struct my_data * happy = (struct my_data *) user_data; 00059 00060 /* Ignore the annotations track, we don't find it interesting! */ 00061 if (!strncmp (content_type, "text/x-cmml", 12)) return ANX_CONTINUE; 00062 00063 printf ("Our first track has content-type %s and granule rate %ld/%ld.\n", 00064 content_type, (long)granule_rate_n, (long)granule_rate_d); 00065 00066 printf ("We will remember it by its serial number %ld " 00067 "and mark it with crosses.\n", serialno); 00068 happy->interesting_serialno = serialno; 00069 00070 /* We don't care about any other tracks! */ 00071 anx_set_read_track_callback (anx, NULL, NULL); 00072 00073 return ANX_CONTINUE; 00074 } 00075 00076 static int 00077 read_raw (ANNODEX * annodex, unsigned char * buf, long n, 00078 long serialno, anx_int64_t granulepos, void * user_data) 00079 { 00080 struct my_data * happy = (struct my_data *) user_data; 00081 00082 if (happy->done) { 00083 putchar ('!'); 00084 } else if (serialno == happy->interesting_serialno) { 00085 happy->interesting_raw_packets++; 00086 putchar ('+'); 00087 } else { 00088 putchar ('.'); 00089 } 00090 00091 return ANX_CONTINUE; 00092 } 00093 00094 static int 00095 read_clip3 (ANNODEX * anx, const AnxClip * clip, void * user_data) 00096 { 00097 struct my_data * happy = (struct my_data *) user_data; 00098 00099 printf ("\nAnd the third clip links to %s\n", clip->anchor_href); 00100 00101 happy->done = 1; 00102 00103 printf ("This completes our whirlwind tour of the first three clips!\n"); 00104 return ANX_STOP_OK; 00105 } 00106 00107 static int 00108 read_clip2 (ANNODEX * anx, const AnxClip * clip, void * user_data) 00109 { 00110 printf ("\nThe second clip links to %s\n", clip->anchor_href); 00111 anx_set_read_clip_callback (anx, read_clip3, user_data); 00112 return ANX_CONTINUE; 00113 } 00114 00115 static int 00116 read_clip1 (ANNODEX * anx, const AnxClip * clip, void * user_data) 00117 { 00118 printf ("\nThe first clip links to %s\n", clip->anchor_href); 00119 anx_set_read_clip_callback (anx, read_clip2, user_data); 00120 return ANX_CONTINUE; 00121 } 00122 00123 int 00124 main (int argc, char *argv[]) 00125 { 00126 ANNODEX * anx = NULL; 00127 struct my_data me; 00128 long n; 00129 00130 if (argc != 2) { 00131 fprintf (stderr, "Usage: %s file.anx\n", argv[0]); 00132 exit (1); 00133 } 00134 00135 me.filename = argv[1]; 00136 me.interesting_serialno = -1; 00137 me.interesting_raw_packets = 0; 00138 me.done = 0; 00139 00140 anx = anx_open (me.filename, ANX_READ); 00141 00142 anx_set_read_stream_callback (anx, read_stream, &me); 00143 anx_set_read_track_callback (anx, read_track, &me); 00144 anx_set_read_raw_callback (anx, read_raw, &me); 00145 anx_set_read_clip_callback (anx, read_clip1, &me); 00146 00147 while (!me.done && (n = anx_read (anx, 1024)) > 0); 00148 00149 printf ("%d packets from the first track (serialno %ld) were received\n", 00150 me.interesting_raw_packets, me.interesting_serialno); 00151 printf ("before the third clip.\n"); 00152 00153 anx_close (anx); 00154 00155 exit (0); 00156 }

which produces output like:

Welcome to /tmp/alien_song.anx! The timebase is 20.000000
Our first track has mime-type video/x-theora and granule rate 1536/1.
We will remember it by its serial number 1810353996 and mark it with crosses.
+...+++++++++++++++++...................................++++++++++++++..........
......................++++++++++++..............................++++++++++++++++
++++..............................+++++++++++++.................................
...++++++++++++++++++++++................................++++++++++++++.........
........................++++++++++++++++....................................++++
++++++++++.......................................++++++++++++++.................
......+++++++++++++.......................+++++++++++++.........................
...+++++++++++++..................................+++++++++++++.................
......+++++++++++++...............................++++++++++++++................
..........+++++++++++..........................++++++++++++++...................
.....++++++++++++.......................++++++++++++........................++++
++++++++........................+++++++++++++........................+++++++++++
+++++.......................+++++++++++.......................+++++++++++++++++.
......................+++++++++++++........................++++++++++...........
.............+++++++++++.......................++++++++++++++++++...............
........++++++++........................+++++++++++++++++.......................
++++++++.......................+++++++++++++++++........................++++++++
++++++...........................+++++++++++........................++++++++++++
+++........................+++++++++++.......................++++++++++++++.....
.................++++++++++++++..............................++++++++++.........
.........................++++++++++++................................+++++++++++
+++...................................++++++
The first anchor links to http://www.mars.int/
+
The second anchor links to http://www.pluto.int/
+++......................+++++++++++.............................+++++++++++....
...........................+++++++++++..........................................
.......+++++++++++++...........................+++++++++++......................
.......+

And the third anchor links to http://www.venus.int/
This completes our whirlwind tour of the first three anchors!

639 packets from the first track (serialno 1810353996) were received
before the third anchor.

 


Generated on Wed Aug 18 05:27:54 2004 for libannodex by doxygen 1.3.7