1 #include <iostream>
2 #include <cstring>
3 
4 #include "kateExtractor.h"
5 #include "definition.h"
6 #include "oggTypes.h"
7 #include "oggHeader.h"
8 #include "kateHeader.h"
9 #include "kateStreamParameter.h"
10 #include "katePosInterpreter.h"
11 #include "log.h"
12 
KateExtractor()13 KateExtractor::KateExtractor()
14 {
15 }
16 
~KateExtractor()17 KateExtractor::~KateExtractor()
18 {
19 }
20 
_extract(uint8 * data,ExtractorInformation & info)21 bool KateExtractor::_extract(uint8* data, ExtractorInformation& info)
22 {
23 
24   StreamType*   streaminfo   = (StreamType*) (data);
25   KateHeader* kateHeader     = (KateHeader*) (data + sizeof(StreamType));
26 
27   /* if this is not a kate header, return with an error */
28   if ((streaminfo->headerType != 0x80) ||
29       (memcmp(streaminfo->typeName, "kate\0\0\0", 7) != 0)) {
30     // TODO: no size of the passed data, the above could overflow (on read)
31     logger.error() << "KatePosInterpreter::initialize: This page is not a kate bos\n";
32     return(false);
33   }
34 
35   // first extract the parameters
36   std::shared_ptr<KateStreamParameter> param(new KateStreamParameter);
37 
38   param->granulerateNum   = (kateHeader->granulerateNumerator);
39   param->granulerateDenom = (kateHeader->granulerateDenominator);
40   param->granuleShift     = kateHeader->granuleShift;
41 
42   param->language = std::string(kateHeader->language, 16);
43   param->category = std::string(kateHeader->category, 16);
44 
45   /* are there any old info stored, then delete them */
46   info.parameter = param;
47 
48   /* set the ogg type and the number of header packets */
49   info.type = OggType::kate;
50   info.numOfHeaderPackets = kateHeader->numHeaders;
51 
52   return(true);
53 }
54 
extract(OggPage & oggPage,ExtractorInformation & information)55 bool KateExtractor::extract(OggPage& oggPage, ExtractorInformation& information)
56 {
57   /* if this is not a Begin Of Stream page, return immediately */
58   if (!oggPage->isBOS()) {
59     logger.error() << "KatePosInterpreter::extract: This page is not a BOS (Begin Of Stream) page\n";
60     return(false);
61   }
62 
63   uint8_t* dataPtr = &(oggPage->data())[0];
64 
65   /* get the information starting points within the raw data */
66   OggHeader* oggHeader  = (OggHeader*) dataPtr;
67   uint8*     data       = dataPtr + sizeof(OggHeader) + oggHeader->tableSegments;
68 
69   if (_extract(data, information) == false)
70     return(false);
71 
72   information.serialNo = oggHeader->serial;
73 
74   return(true);
75 }
76 
extract(OggPacket & packet,ExtractorInformation & information)77 bool KateExtractor::extract(OggPacket& packet, ExtractorInformation& information)
78 {
79   // if this is not a Begin Of Stream page, return immediately
80   if (!packet->isBOS()) {
81     logger.error() << "TheoraPosInterpreter::extract: This packet is not a BOS (Begin Of Stream) page\n";
82     return(false);
83   }
84 
85   if (_extract(packet->data(), information) == false)
86     return(false);
87 
88   return(true);
89 }
90