| 1 |
/* |
| 2 |
This shows the steps necessary to encode a Kate stream. |
| 3 |
For clarity, error checking is omitted. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
#include <ogg/ogg.h> |
| 9 |
#include "common.h" |
| 10 |
|
| 11 |
/* All the libkate API is available from the main kate header file: */ |
| 12 |
|
| 13 |
#include <kate/oggkate.h> |
| 14 |
|
| 15 |
/* |
| 16 |
We want to control when Ogg pages are output, as Kate is a discontinuous |
| 17 |
codec, so we don't know when the next event will happen, hence the need |
| 18 |
to create a page for every event. |
| 19 |
*/ |
| 20 |
|
| 21 |
static void flush_page(ogg_stream_state *os) |
| 22 |
{ |
| 23 |
ogg_page og; |
| 24 |
while (1) { |
| 25 |
int ret=ogg_stream_flush(os,&og); |
| 26 |
if (ret==0) break; |
| 27 |
fwrite(og.header,1,og.header_len,stdout); |
| 28 |
fwrite(og.body,1,og.body_len,stdout); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
int main() |
| 33 |
{ |
| 34 |
/* We need an Ogg stream to write to */ |
| 35 |
|
| 36 |
ogg_stream_state os; |
| 37 |
ogg_packet op; |
| 38 |
|
| 39 |
/* |
| 40 |
First, a kate_info structure needs to be created and setup for the stream to create. |
| 41 |
A kate_comment structure also has to be created. |
| 42 |
Information from both of these will get encoded into the stream headers. |
| 43 |
Last, we also need a kate_state structure, which will be initialized later. |
| 44 |
*/ |
| 45 |
|
| 46 |
kate_info ki; |
| 47 |
kate_comment kc; |
| 48 |
kate_state k; |
| 49 |
|
| 50 |
kate_info_init(&ki); |
| 51 |
kate_comment_init(&kc); |
| 52 |
|
| 53 |
/* |
| 54 |
The most important part of the kate_info structure on encoding is the granule |
| 55 |
encoding information, which describes how granules and time are mapped. |
| 56 |
Here, we map one granule to one millisecond. |
| 57 |
*/ |
| 58 |
|
| 59 |
ki.granule_shift=32; |
| 60 |
ki.gps_numerator=1000; |
| 61 |
ki.gps_denominator=1; |
| 62 |
|
| 63 |
/* With that done, we can initialize libkate for encoding, and initialize libogg as well: */ |
| 64 |
|
| 65 |
kate_encode_init(&k,&ki); |
| 66 |
ogg_stream_init(&os,0x12345678); |
| 67 |
|
| 68 |
/* for the benefit of windows, which mangles data otherwise */ |
| 69 |
set_binary_file(stdout); |
| 70 |
|
| 71 |
/* |
| 72 |
Before you can create events, headers need to be sent. Here, we'll just send |
| 73 |
the headers directly, but you will usually want to add regions, styles, etc to |
| 74 |
the headers before doing so: |
| 75 |
*/ |
| 76 |
|
| 77 |
while (kate_ogg_encode_headers(&k,&kc,&op)==0) { |
| 78 |
ogg_stream_packetin(&os,&op); |
| 79 |
ogg_packet_clear(&op); |
| 80 |
} |
| 81 |
flush_page(&os); |
| 82 |
|
| 83 |
/* |
| 84 |
Events can now be created, and we'll just create and send a single one here, |
| 85 |
starting at time 10 seconds, and stopping at time 15 seconds. |
| 86 |
*/ |
| 87 |
|
| 88 |
#define text "Hello, world!" |
| 89 |
kate_ogg_encode_text(&k,10.0,15.0,text,strlen(text)+1,&op); |
| 90 |
ogg_stream_packetin(&os,&op); |
| 91 |
ogg_packet_clear(&op); |
| 92 |
flush_page(&os); |
| 93 |
|
| 94 |
/* |
| 95 |
When we're done, we can tell libkate so an "end of stream" packet will be generated, |
| 96 |
and clear the resources we've been using: |
| 97 |
*/ |
| 98 |
|
| 99 |
kate_ogg_encode_finish(&k,-1,&op); |
| 100 |
ogg_stream_packetin(&os,&op); |
| 101 |
ogg_packet_clear(&op); |
| 102 |
flush_page(&os); |
| 103 |
ogg_stream_clear(&os); |
| 104 |
kate_clear(&k); |
| 105 |
kate_info_clear(&ki); |
| 106 |
kate_comment_clear(&kc); |
| 107 |
|
| 108 |
/* |
| 109 |
That's it, we now have created a full kate stream. You may now want to decode it, |
| 110 |
or multiplex it with a Theora video, etc. |
| 111 |
*/ |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
|