Blame view

trunk/src/rtmp/srs_protocol_amf0.hpp 8.9 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
24 25
#ifndef SRS_RTMP_PROTOCOL_AMF0_HPP
#define SRS_RTMP_PROTOCOL_AMF0_HPP
winlin authored
26 27

/*
28
#include <srs_protocol_amf0.hpp>
winlin authored
29 30 31 32 33 34 35
*/

#include <srs_core.hpp>

#include <string>

class SrsStream;
36 37
class SrsAmf0Object;
class SrsAmf0EcmaArray;
38 39
class __SrsUnSortedHashtable;
class __SrsAmf0ObjectEOF;
winlin authored
40
41 42 43 44 45
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// amf0 codec
// 1. SrsAmf0Any: read any from stream
46 47 48 49 50
//        SrsAmf0Any* pany = NULL;
//        if ((ret = srs_amf0_read_any(stream, &pany)) != ERROR_SUCCESS) {
//            return ret;
//         }
//        srs_assert(pany); // if success, always valid object.
51
// 2. SrsAmf0Any: convert to specifid type, for instance, string
52 53 54 55
//        SrsAmf0Any* pany = ...
//        if (pany->is_string()) {
//            string v = pany->to_str();
//        }
56
// 3. SrsAmf0Any: parse specified type to any, for instance, string
57
//        SrsAmf0Any* pany = SrsAmf0Any::str("winlin");
58
// 4. SrsAmf0Size: get amf0 instance size
59
//        int size = SrsAmf0Size::str("winlin");
60
// 5. SrsAmf0Object: create the amf0 object.
61
//        SrsAmf0Object* obj = SrsAmf0Any::object();
62
// 5. SrsAmf0EcmaArray: create the amf0 ecma array.
63
//        SrsAmf0EcmaArray* arr = SrsAmf0Any::ecma_array();
winlin authored
64 65 66 67
//
// please carefully the size and count of amf0 any:
// 1. total_size(): the total memory size the object wrote to buffer.
// 2. count(): the total element count of object, for instance, the properties
68
//        of amf0 object, used for key_at/value_at loop.
winlin authored
69
//
70 71 72 73 74
// for detail usage, see interfaces of each object.
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
winlin authored
75 76 77 78
/**
* any amf0 value.
* 2.1 Types Overview
* value-type = number-type | boolean-type | string-type | object-type 
79 80 81
*         | null-marker | undefined-marker | reference-type | ecma-array-type 
*         | strict-array-type | date-type | long-string-type | xml-document-type 
*         | typed-object-type
winlin authored
82
*/
83
class SrsAmf0Any
winlin authored
84
{
85
public:
86
    char marker;
87
public:
88 89
    SrsAmf0Any();
    virtual ~SrsAmf0Any();
90
public:
91 92 93 94 95 96 97 98
    virtual bool is_string();
    virtual bool is_boolean();
    virtual bool is_number();
    virtual bool is_null();
    virtual bool is_undefined();
    virtual bool is_object();
    virtual bool is_object_eof();
    virtual bool is_ecma_array();
99
public:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    /**
    * get the string of any when is_string() indicates true.
    * user must ensure the type is a string, or assert failed.
    */
    virtual std::string to_str();
    /**
    * get the boolean of any when is_boolean() indicates true.
    * user must ensure the type is a boolean, or assert failed.
    */
    virtual bool to_boolean();
    /**
    * get the number of any when is_number() indicates true.
    * user must ensure the type is a number, or assert failed.
    */
    virtual double to_number();
    /**
    * get the object of any when is_object() indicates true.
    * user must ensure the type is a object, or assert failed.
    */
    virtual SrsAmf0Object* to_object();
    /**
    * get the ecma array of any when is_ecma_array() indicates true.
    * user must ensure the type is a ecma array, or assert failed.
    */
    virtual SrsAmf0EcmaArray* to_ecma_array();
125
public:
126 127 128 129 130 131 132 133 134
    /**
    * get the size of amf0 any, including the marker size.
    */
    virtual int total_size() = 0;
    /**
    * read elem from stream
    */
    virtual int read(SrsStream* stream) = 0;
    virtual int write(SrsStream* stream) = 0;
135
public:
136 137 138 139 140 141 142 143
    static SrsAmf0Any* str(const char* value = NULL); 
    static SrsAmf0Any* boolean(bool value = false);
    static SrsAmf0Any* number(double value = 0.0);
    static SrsAmf0Any* null();
    static SrsAmf0Any* undefined();
    static SrsAmf0Object* object();
    static SrsAmf0Any* object_eof();
    static SrsAmf0EcmaArray* ecma_array();
144
public:
145
    static int discovery(SrsStream* stream, SrsAmf0Any** ppvalue);
winlin authored
146 147 148 149 150 151 152
};

/**
* 2.5 Object Type
* anonymous-object-type = object-marker *(object-property)
* object-property = (UTF-8 value-type) | (UTF-8-empty object-end-marker)
*/
153
class SrsAmf0Object : public SrsAmf0Any
winlin authored
154 155
{
private:
156 157
    __SrsUnSortedHashtable* properties;
    __SrsAmf0ObjectEOF* eof;
winlin authored
158
159
private:
160 161 162
    // use SrsAmf0Any::object() to create it.
    friend class SrsAmf0Any;
    SrsAmf0Object();
163
public:
164
    virtual ~SrsAmf0Object();
165
166
public:
167 168 169 170
    virtual int total_size();
    virtual int read(SrsStream* stream);
    virtual int write(SrsStream* stream);
    
171
public:
172 173 174 175 176 177
    virtual int count();
    // @remark: max index is count().
    virtual std::string key_at(int index);
    // @remark: max index is count().
    virtual SrsAmf0Any* value_at(int index);
    
178
public:
179 180 181 182
    virtual void set(std::string key, SrsAmf0Any* value);
    virtual SrsAmf0Any* get_property(std::string name);
    virtual SrsAmf0Any* ensure_property_string(std::string name);
    virtual SrsAmf0Any* ensure_property_number(std::string name);
winlin authored
183 184 185 186 187 188 189 190
};

/**
* 2.10 ECMA Array Type
* ecma-array-type = associative-count *(object-property)
* associative-count = U32
* object-property = (UTF-8 value-type) | (UTF-8-empty object-end-marker)
*/
191
class SrsAmf0EcmaArray : public SrsAmf0Any
winlin authored
192 193
{
private:
194 195 196
    __SrsUnSortedHashtable* properties;
    __SrsAmf0ObjectEOF* eof;
    int32_t _count;
winlin authored
197
198
private:
199 200 201
    // use SrsAmf0Any::ecma_array() to create it.
    friend class SrsAmf0Any;
    SrsAmf0EcmaArray();
202
public:
203
    virtual ~SrsAmf0EcmaArray();
204
205
public:
206 207 208 209
    virtual int total_size();
    virtual int read(SrsStream* stream);
    virtual int write(SrsStream* stream);
    
210
public:
211 212 213 214 215 216
    virtual void clear();
    virtual int count();
    // @remark: max index is count().
    virtual std::string key_at(int index);
    // @remark: max index is count().
    virtual SrsAmf0Any* value_at(int index);
winlin authored
217
218
public:
219 220 221 222
    virtual void set(std::string key, SrsAmf0Any* value);
    virtual SrsAmf0Any* get_property(std::string name);
    virtual SrsAmf0Any* ensure_property_string(std::string name);
    virtual SrsAmf0Any* ensure_property_number(std::string name);
winlin authored
223 224 225
};

/**
226 227 228 229 230
* the class to get amf0 object size
*/
class SrsAmf0Size
{
public:
231 232 233 234 235 236 237 238 239 240
    static int utf8(std::string value);
    static int str(std::string value);
    static int number();
    static int null();
    static int undefined();
    static int boolean();
    static int object(SrsAmf0Object* obj);
    static int object_eof();
    static int ecma_array(SrsAmf0EcmaArray* arr);
    static int any(SrsAmf0Any* o);
241 242 243
};

/**
244
* read anything from stream.
245
* @param ppvalue, the output amf0 any elem.
246
*         NULL if error; otherwise, never NULL and user must free it.
247 248 249 250
*/
extern int srs_amf0_read_any(SrsStream* stream, SrsAmf0Any** ppvalue);

/**
winlin authored
251 252 253 254 255 256 257 258 259 260 261
* read amf0 string from stream.
* 2.4 String Type
* string-type = string-marker UTF-8
*/
extern int srs_amf0_read_string(SrsStream* stream, std::string& value);
extern int srs_amf0_write_string(SrsStream* stream, std::string value);

/**
* read amf0 boolean from stream.
* 2.4 String Type
* boolean-type = boolean-marker U8
262
*         0 is false, <> 0 is true
winlin authored
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
*/
extern int srs_amf0_read_boolean(SrsStream* stream, bool& value);
extern int srs_amf0_write_boolean(SrsStream* stream, bool value);

/**
* read amf0 number from stream.
* 2.2 Number Type
* number-type = number-marker DOUBLE
*/
extern int srs_amf0_read_number(SrsStream* stream, double& value);
extern int srs_amf0_write_number(SrsStream* stream, double value);

/**
* read amf0 null from stream.
* 2.7 null Type
* null-type = null-marker
*/
extern int srs_amf0_read_null(SrsStream* stream);
extern int srs_amf0_write_null(SrsStream* stream);

/**
* read amf0 undefined from stream.
* 2.8 undefined Type
* undefined-type = undefined-marker
*/
extern int srs_amf0_read_undefined(SrsStream* stream);
extern int srs_amf0_write_undefined(SrsStream* stream);
winlin authored
291
#endif