Twitch SDK (Internal)
amf0.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #ifndef AMF0_H
10 #define AMF0_H
11 
12 #include <ctime>
13 
14 namespace ttv
15 {
16  namespace broadcast
17  {
18  class IAMF0;
19  class AMF0Nop;
20  class AMF0Printer;
21  class AMF0StringDecoder;
22  class AMF0PropertyDecoder;
23  class AMF0NumberDecoder;
24 
25  const uint8_t* DecodeAMF (const uint8_t* data, std::shared_ptr<IAMF0> output);
26  }
27 }
28 
33 {
34 public:
35  enum AMFType : uint8_t
36  {
37  // Tested Types */
38  number = 0x00,
39  boolean = 0x01,
40  string = 0x02,
41  object = 0x03,
42  ecmaArray = 0x08,
43  objectEnd = 0x09,
44  strictArray = 0x0A,
45  date = 0x0B,
46 
47  // Untested but implemented types
48  null = 0x05,
49  undefined = 0x06,
50 
51  // Unimplemented types
52  reference = 0x07,
53  longString = 0x0C,
54  xmlDocument = 0x0F,
55  typedObject = 0x10,
56 
57  // Unsupported types
58  movieclip = 0x04, // reserved, not supported
59  recordset = 0x0E, // reserved, not supported
60  unsupported = 0x0D
61  };
62 
63  virtual ~IAMF0();
64  virtual void Number(double value) = 0;
65  virtual void Boolean(bool flag) = 0;
66  virtual void String(std::string param) = 0;
67  virtual void Object() = 0;
68  virtual void ObjectProperty(std::string propertyName) = 0;
69  virtual void Movieclip() = 0;
70  virtual void Null() = 0;
71  virtual void Undefined() = 0;
72  virtual void Reference() = 0;
73  virtual void EcmaArray(uint32_t elements) = 0;
74  virtual void EcmaArrayKey(std::string keyName) = 0;
75  virtual void ObjectEnd() = 0;
76  virtual void StrictArray(uint32_t elements) = 0;
77  virtual void Date(double date) = 0;
78  virtual void LongString() = 0;
79  virtual void Unsupported() = 0;
80  virtual void Recordset() = 0;
81  virtual void XmlDocument() = 0;
82  virtual void TypedObject() = 0;
83 };
84 
89 {
90 public:
91  virtual void Number(double /*value*/);
92  virtual void Boolean(bool /*flag*/);
93  virtual void String(std::string /*param*/);
94  virtual void Object();
95  virtual void ObjectProperty(std::string /*propertyName*/);
96  virtual void Movieclip();
97  virtual void Null();
98  virtual void Undefined();
99  virtual void Reference();
100  virtual void EcmaArray(uint32_t /*elements*/);
101  virtual void EcmaArrayKey(std::string /*keyName*/);
102  virtual void ObjectEnd();
103  virtual void StrictArray(uint32_t /*elements*/);
104  virtual void Date(double /*date*/);
105  virtual void LongString();
106  virtual void Unsupported();
107  virtual void Recordset();
108  virtual void XmlDocument();
109  virtual void TypedObject();
110 };
111 
112 
117 {
118 public:
119  void Number(double value)
120  {
121  printf ("number %f\n", value);
122  }
123  void Boolean(bool flag)
124  {
125  printf ("boolean is '%s'\n", flag ? "true" : "false");
126  }
127  void String(std::string param)
128  {
129  printf ("String '%s'\n", param.c_str());
130  }
131  void Object()
132  {
133  printf ("object START\n");
134  }
135  void ObjectProperty(std::string propetyName)
136  {
137  printf ("ObjectProperty '%s'\n", propetyName.c_str());
138  }
139  void Movieclip()
140  {
141  printf ("movieclip\n");
142  }
143  void Null()
144  {
145  printf ("null\n");
146  }
147  void Undefined()
148  {
149  printf ("undefined\n");
150  }
151  void Reference()
152  {
153  printf ("reference\n");
154  }
155  void EcmaArray(uint32_t elements)
156  {
157  printf ("ecmaArray with %u elements\n", elements);
158  }
159  void EcmaArrayKey(std::string keyName)
160  {
161  printf ("ecmaArrayKey '%s'\n", keyName.c_str());
162  }
163  void ObjectEnd()
164  {
165  printf ("objectEnd\n");
166  }
167  void StrictArray(uint32_t elements)
168  {
169  printf ("strictArray with %u elements\n", elements);
170  }
171  void Date(double date)
172  {
173  char prettyDateTime[128];
174 
175  time_t timeSince1970 = static_cast<time_t> (date/1000.0);
176 
177  tm* localTime = localtime(&timeSince1970);
178  strftime(prettyDateTime, 128, "%c", localTime);
179 
180  printf ("%s\n", prettyDateTime);
181  }
182  void LongString()
183  {
184  printf ("longString\n");
185  }
186  void Unsupported()
187  {
188  printf ("unsupported\n");
189  }
190  void Recordset()
191  {
192  printf ("recordset\n");
193  }
194  void XmlDocument()
195  {
196  printf ("xmlDocument\n");
197  }
198  void TypedObject()
199  {
200  printf ("typedObject\n");
201  }
202 };
203 
205 {
206 public:
208 
209  virtual void String(std::string param);
210 
211  const std::string& GetCommandName() const { return mCommandName; }
212 private:
213  std::string mCommandName;
214 };
215 
217 {
218 public:
219  AMF0PropertyDecoder(const std::string& searchTerm);
220 
221  virtual void String(std::string param);
222  virtual void ObjectProperty(std::string propertyName);
223 
224  const std::string& GetFoundValue() const { return mFoundValue; }
225 
226 private:
227  AMF0PropertyDecoder operator=(const AMF0PropertyDecoder& rhs);
228  const std::string mSearchTerm;
229  std::string mFoundValue;
231 };
232 
234 {
235 public:
237 
238  virtual void Number(double value);
239 
240  double GetValue() const { return mValue; }
241 private:
242  double mValue;
243 };
244 
245 #endif
virtual void Null()=0
bool mFoundProperty
Definition: amf0.h:230
virtual void ObjectProperty(std::string propertyName)=0
virtual void Number(double value)=0
Definition: amf0.h:38
const std::string & GetFoundValue() const
Definition: amf0.h:224
void Boolean(bool flag)
Definition: amf0.h:123
void ObjectEnd()
Definition: amf0.h:163
std::string mFoundValue
Definition: amf0.h:229
double mValue
Definition: amf0.h:242
void TypedObject()
Definition: amf0.h:198
AMFType
Definition: amf0.h:35
void XmlDocument()
Definition: amf0.h:194
virtual void Unsupported()=0
double GetValue() const
Definition: amf0.h:240
Definition: amf0.h:32
virtual void Object()=0
void Recordset()
Definition: amf0.h:190
void Null()
Definition: amf0.h:143
virtual void Undefined()=0
Definition: amf0.h:204
Definition: amf0.h:88
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
virtual void ObjectEnd()=0
const std::string mSearchTerm
Definition: amf0.h:228
void Movieclip()
Definition: amf0.h:139
void Undefined()
Definition: amf0.h:147
const uint8_t * DecodeAMF(const uint8_t *data, std::shared_ptr< IAMF0 > output)
Definition: amf0.h:48
virtual void XmlDocument()=0
void EcmaArrayKey(std::string keyName)
Definition: amf0.h:159
void Number(double value)
Definition: amf0.h:119
virtual void Reference()=0
virtual void String(std::string param)=0
void Reference()
Definition: amf0.h:151
void Unsupported()
Definition: amf0.h:186
std::string mCommandName
Definition: amf0.h:213
virtual void LongString()=0
Definition: amf0.h:45
virtual void EcmaArrayKey(std::string keyName)=0
Definition: amf0.h:233
Definition: amf0.h:116
virtual void Date(double date)=0
void ObjectProperty(std::string propetyName)
Definition: amf0.h:135
void String(std::string param)
Definition: amf0.h:127
virtual void Boolean(bool flag)=0
virtual void EcmaArray(uint32_t elements)=0
void Object()
Definition: amf0.h:131
void LongString()
Definition: amf0.h:182
virtual void Recordset()=0
virtual void TypedObject()=0
void EcmaArray(uint32_t elements)
Definition: amf0.h:155
void StrictArray(uint32_t elements)
Definition: amf0.h:167
const std::string & GetCommandName() const
Definition: amf0.h:211
void Date(double date)
Definition: amf0.h:171
virtual void StrictArray(uint32_t elements)=0
virtual void Movieclip()=0