MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
dtsc.h
Go to the documentation of this file.
1 
4 #pragma once
5 #include <vector>
6 #include <iostream>
7 #include <stdint.h> //for uint64_t
8 #include <string>
9 #include <deque>
10 #include <set>
11 #include <stdio.h> //for FILE
12 #include "json.h"
13 #include "socket.h"
14 #include "timing.h"
15 
16 #define DTSC_INT 0x01
17 #define DTSC_STR 0x02
18 #define DTSC_OBJ 0xE0
19 #define DTSC_ARR 0x0A
20 #define DTSC_CON 0xFF
21 
22 namespace DTSC {
23 
25  enum datatype {
26  AUDIO,
27  VIDEO,
28  META,
29  PAUSEMARK,
31  INVALID
32  };
33 
34  extern char Magic_Header[];
35  extern char Magic_Packet[];
36  extern char Magic_Packet2[];
37 
39  struct seekPos {
43  bool operator < (const seekPos & rhs) const {
44  if (seekTime < rhs.seekTime) {
45  return true;
46  } else {
47  if (seekTime == rhs.seekTime) {
48  if (trackID < rhs.trackID) {
49  return true;
50  }
51  }
52  }
53  return false;
54  }
55  long long unsigned int seekTime;
56  long long unsigned int bytePos;
57  unsigned int trackID;
58  };
59 
60  enum packType {
62  DTSC_HEAD,
63  DTSC_V1,
64  DTSC_V2
65  };
66 
69  class Scan {
70  public:
71  Scan();
72  Scan(char * pointer, size_t len);
73  operator bool() const;
74  std::string toPrettyString(unsigned int indent = 0);
75  bool hasMember(std::string indice);
76  bool hasMember(const char * indice, const unsigned int ind_len);
77  Scan getMember(std::string indice);
78  Scan getMember(const char * indice);
79  Scan getMember(const char * indice, const unsigned int ind_len);
80  Scan getIndice(unsigned int num);
81  std::string getIndiceName(unsigned int num);
82  unsigned int getSize();
83 
84  char getType();
85  bool asBool();
86  long long asInt();
87  std::string asString();
88  void getString(char *& result, unsigned int & len);
89  JSON::Value asJSON();
90  private:
91  char * p;
92  size_t len;
93  };
94 
100  class Packet {
101  public:
102  Packet();
103  Packet(const Packet & rhs);
104  Packet(const char * data_, unsigned int len, bool noCopy = false);
105  ~Packet();
106  void null();
107  void operator = (const Packet & rhs);
108  operator bool() const;
109  packType getVersion() const;
110  void reInit(const char * data_, unsigned int len, bool noCopy = false);
111  void genericFill(long long packTime, long long packOffset, long long packTrack, const char * packData, long long packDataSize, long long packBytePos, bool isKeyframe);
112  void getString(const char * identifier, char *& result, unsigned int & len) const;
113  void getString(const char * identifier, std::string & result) const;
114  void getInt(const char * identifier, int & result) const;
115  int getInt(const char * identifier) const;
116  void getFlag(const char * identifier, bool & result) const;
117  bool getFlag(const char * identifier) const;
118  bool hasMember(const char * identifier) const;
119  long long unsigned int getTime() const;
120  long int getTrackId() const;
121  char * getData() const;
122  int getDataLen() const;
123  int getPayloadLen() const;
124  JSON::Value toJSON() const;
125  Scan getScan() const;
126  protected:
127  bool master;
129  void resize(unsigned int size);
130  char * data;
131  unsigned int bufferLen;
132  unsigned int dataLen;
133  };
134 
136  struct livePos {
138  seekTime = 0;
139  trackID = 0;
140  }
141  livePos(const livePos & rhs) {
142  seekTime = rhs.seekTime;
143  trackID = rhs.trackID;
144  }
145  void operator = (const livePos & rhs) {
146  seekTime = rhs.seekTime;
147  trackID = rhs.trackID;
148  }
149  bool operator == (const livePos & rhs) {
150  return seekTime == rhs.seekTime && trackID == rhs.trackID;
151  }
152  bool operator != (const livePos & rhs) {
153  return seekTime != rhs.seekTime || trackID != rhs.trackID;
154  }
155  bool operator < (const livePos & rhs) const {
156  if (seekTime < rhs.seekTime) {
157  return true;
158  } else {
159  if (seekTime > rhs.seekTime) {
160  return false;
161  }
162  }
163  return (trackID < rhs.trackID);
164  }
165  long long unsigned int seekTime;
166  unsigned int trackID;
167  };
168 
169  /*LTS-START*/
173  class Ivec {
174  public:
175  Ivec();
176  Ivec(long long int iVec);
177  void setIvec(long long int iVec);
178  void setIvec(std::string iVec);
179  void setIvec(const char * iVec, int len);
180  long long int asInt();
181  char * getData();
182  private:
186  char data[8];
187  };
188  /*LTS-END*/
189 
191  class Part {
192  public:
193  long getSize();
194  void setSize(long newSize);
195  short getDuration();
196  void setDuration(short newDuration);
197  long getOffset();
198  void setOffset(long newOffset);
199  char * getData();
200  void toPrettyString(std::ostream & str, int indent = 0);
201  private:
207  char data[9];
208  };
209 
213  class Key {
214  public:
215  unsigned long long getBpos();
216  void setBpos(unsigned long long newBpos);
217  unsigned long getLength();
218  void setLength(unsigned long newLength);
219  unsigned long getNumber();
220  void setNumber(unsigned long newNumber);
221  unsigned short getParts();
222  void setParts(unsigned short newParts);
223  unsigned long long getTime();
224  void setTime(unsigned long long newTime);
225  char * getData();
226  void toPrettyString(std::ostream & str, int indent = 0);
227  private:
228 #ifdef BIGMETA
229 #define PACKED_KEY_SIZE 25
230 #else
238 #define PACKED_KEY_SIZE 16
239 #endif
247  char data[PACKED_KEY_SIZE];
248  };
249 
251  class Fragment {
252  public:
253  unsigned long getDuration();
254  void setDuration(unsigned long newDuration);
255  char getLength();
256  void setLength(char newLength);
257  unsigned long getNumber();
258  void setNumber(unsigned long newNumber);
259  unsigned long getSize();
260  void setSize(unsigned long newSize);
261  char * getData();
262  void toPrettyString(std::ostream & str, int indent = 0);
263  private:
264 #ifdef BIGMETA
265 #define PACKED_FRAGMENT_SIZE 13
266 #else
273 #define PACKED_FRAGMENT_SIZE 11
274 #endif
281  char data[PACKED_FRAGMENT_SIZE];
282  };
283 
285  class Track {
286  public:
287  Track();
288  Track(JSON::Value & trackRef);
289  Track(Scan & trackRef);
290 
291  inline operator bool() const {
292  return (parts.size() && keySizes.size() && (keySizes.size() == keys.size()));
293  }
294  /*
295  void update(long long packTime, long long packOffset, long long packDataSize, long long packBytePos, bool isKeyframe, long long packSendSize, unsigned long segment_size = 5000);
296  */
297  void update(long long packTime, long long packOffset, long long packDataSize, long long packBytePos, bool isKeyframe, long long packSendSize, unsigned long segment_size = 5000, const char * iVec = 0);
298  int getSendLen();
299  void send(Socket::Connection & conn);
300  void writeTo(char *& p);
301  JSON::Value toJSON(bool skipBinary = false);
302  std::deque<Fragment> fragments;
303  std::deque<Key> keys;
304  std::deque<unsigned long> keySizes;
305  std::deque<Part> parts;
306  std::deque<Ivec> ivecs; /*LTS*/
307  Key & getKey(unsigned int keyNum);
308  unsigned int timeToKeynum(unsigned int timestamp);
309  unsigned int timeToFragnum(unsigned int timestamp);
310  void reset();
311  void toPrettyString(std::ostream & str, int indent = 0, int verbosity = 0);
312  void finalize();
313 
314  std::string getIdentifier();
315  std::string getWritableIdentifier();
316  unsigned int trackID;
317  unsigned long long firstms;
318  unsigned long long lastms;
319  int bps;
320  int missedFrags;
321  std::string init;
322  std::string codec;
323  std::string type;
324  //audio only
325  int rate;
326  int size;
327  int channels;
328  //video only
329  int width;
330  int height;
331  int fpks;
332  private:
333  std::string cachedIdent;
334  };
335 
337  class Meta{
339  public:
340  Meta();
341  Meta(const DTSC::Packet & source);
342  Meta(JSON::Value & meta);
343 
344  inline operator bool() const { //returns if the object contains valid meta data BY LOOKING AT vod/live FLAGS
345  return vod || live;
346  }
347  void reinit(const DTSC::Packet & source);
348  void update(DTSC::Packet & pack, unsigned long segment_size = 5000);
349  void updatePosOverride(DTSC::Packet & pack, unsigned long bpos);
350  void update(JSON::Value & pack, unsigned long segment_size = 5000);
351  /*LTS
352  void update(long long packTime, long long packOffset, long long packTrack, long long packDataSize, long long packBytePos, bool isKeyframe, long long packSendSize = 0, unsigned long segment_size = 5000);
353  LTS*/
354  void update(long long packTime, long long packOffset, long long packTrack, long long packDataSize, long long packBytePos, bool isKeyframe, long long packSendSize = 0, unsigned long segment_size = 5000, const char * iVec = 0);
355  unsigned int getSendLen();
356  void send(Socket::Connection & conn);
357  void writeTo(char * p);
358  JSON::Value toJSON();
359  void reset();
360  void toPrettyString(std::ostream & str, int indent = 0, int verbosity = 0);
361  //members:
362  std::map<unsigned int, Track> tracks;
363  bool vod;
364  bool live;
365  bool merged;
366  long long int moreheader;
367  long long int bufferWindow;
368  };
369 
371  class File {
372  public:
373  File();
374  File(const File & rhs);
375  File(std::string filename, bool create = false);
376  File & operator = (const File & rhs);
377  operator bool() const;
378  ~File();
379  Meta & getMeta();
380  long long int getLastReadPos();
381  bool writeHeader(std::string & header, bool force = false);
382  long long int addHeader(std::string & header);
383  long int getBytePosEOF();
384  long int getBytePos();
385  bool reachedEOF();
386  void seekNext();
387  void parseNext();
388  DTSC::Packet & getPacket();
389  bool seek_time(unsigned int ms);
390  bool seek_time(unsigned int ms, unsigned int trackNo, bool forceSeek = false);
391  bool seek_bpos(int bpos);
392  void rewritePacket(std::string & newPacket, int bytePos);
393  void writePacket(std::string & newPacket);
394  void writePacket(JSON::Value & newPacket);
395  bool atKeyframe();
396  void selectTracks(std::set<unsigned long> & tracks);
397  private:
398  long int endPos;
399  void readHeader(int pos);
400  DTSC::Packet myPack;
401  JSON::Value metaStorage;
402  Meta metadata;
403  std::map<unsigned int, std::string> trackMapping;
404  long long int currtime;
405  long long int lastreadpos;
406  int currframe;
407  FILE * F;
408  unsigned long headerSize;
409  void * buffer;
410  bool created;
411  std::set<seekPos> currentPositions;
412  std::set<unsigned long> selectedTracks;
413  };
414  //FileWriter
415 
416 }
417 
Class for storage of meta data.
Definition: dtsc.h:337
long long unsigned int bytePos
Stores the byteposition of the DTSC packet referenced by this structure.
Definition: dtsc.h:56
A simple structure used for ordering byte seek positions.
Definition: dtsc.h:136
packType
Definition: dtsc.h:60
unsigned int trackID
Stores the track the DTSC packet referenced by this structure is associated with. ...
Definition: dtsc.h:57
#define PACKED_KEY_SIZE
Definition: dtsc.h:238
A simple structure used for ordering byte seek positions.
Definition: dtsc.h:39
Stream Audio data.
Definition: dtsc.h:26
Pause marker.
Definition: dtsc.h:29
Definition: dtsc.h:63
A JSON::Value is either a string or an integer, but may also be an object, array or null...
Definition: json.h:21
This class allows scanning through raw binary format DTSC data.
Definition: dtsc.h:69
unsigned int trackID
Definition: dtsc.h:166
Stream Metadata.
Definition: dtsc.h:28
long long unsigned int seekTime
Definition: dtsc.h:165
datatype
This enum holds all possible datatypes for DTSC packets.
Definition: dtsc.h:25
Definition: dtsc.h:22
Basic class supporting initialization Vectors.
Definition: dtsc.h:173
Definition: dtsc.h:64
Modified header data.
Definition: dtsc.h:30
Stream Video data.
Definition: dtsc.h:27
char Magic_Packet2[]
The magic bytes for a DTSC packet version 2.
Definition: dtsc.h:36
Definition: dtsc.h:62
#define PACKED_FRAGMENT_SIZE
Definition: dtsc.h:273
long long unsigned int seekTime
Stores the timestamp of the DTSC packet referenced by this structure.
Definition: dtsc.h:55
Canvas prototype resize
Definition: jquery.flot.min.js:1
Definition: dtsc.h:61
char Magic_Header[]
The magic bytes for a DTSC header.
Definition: dtsc.h:34
Basic class for storage of data associated with keyframes.
Definition: dtsc.h:213
function init(plot)
Definition: jquery.flot.time.min.js:1
Basic class for storage of data associated with single DTSC packets, a.k.a. parts.
Definition: dtsc.h:191
A simple wrapper class that will open a file and allow easy reading/writing of DTSC data from/to it...
Definition: dtsc.h:371
char Magic_Packet[]
The magic bytes for a DTSC packet.
Definition: dtsc.h:35
livePos()
Definition: dtsc.h:137
Anything else or no data available.
Definition: dtsc.h:31
plot version
Definition: jquery.flot.min.js:2
mist send(function(){$(window).trigger("hashchange")},{},{timeout:5, hide:!0})})
This class is for easy communicating through sockets, either TCP or Unix.
Definition: socket.h:47
bool operator<(const seekPos &rhs) const
Less-than comparison for seekPos structures.
Definition: dtsc.h:43
DTSC::Packets can currently be three types: DTSC_HEAD packets are the "DTSC" header string...
Definition: dtsc.h:100
livePos(const livePos &rhs)
Definition: dtsc.h:141