amf.h 2.89 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xiongziliang committed
9
 */
xiongziliang committed
10

xzl committed
11 12 13 14
#ifndef __amf_h
#define __amf_h

#include <assert.h>
xiongzilaing committed
15
#include <string>
xzl committed
16
#include <vector>
xiongziliang committed
17
#include <map>
xiongziliang committed
18
#include <functional>
mtdxc committed
19 20 21
namespace toolkit {
    class BufferLikeString;
}
xzl committed
22
enum AMFType {
23 24 25 26 27 28 29 30 31
    AMF_NUMBER,
    AMF_INTEGER,
    AMF_BOOLEAN,
    AMF_STRING,
    AMF_OBJECT,
    AMF_NULL,
    AMF_UNDEFINED,
    AMF_ECMA_ARRAY,
    AMF_STRICT_ARRAY,
xzl committed
32 33 34 35 36 37
};

class AMFValue;

class AMFValue {
public:
xiongziliang committed
38
    friend class AMFEncoder;
夏楚 committed
39 40 41

    using mapType = std::map<std::string, AMFValue>;
    using arrayType = std::vector<AMFValue>;
xzl committed
42

43
    ~AMFValue();
44 45 46 47 48 49 50
    AMFValue(AMFType type = AMF_NULL);
    AMFValue(const char *s);
    AMFValue(const std::string &s);
    AMFValue(double n);
    AMFValue(int i);
    AMFValue(bool b);
    AMFValue(const AMFValue &from);
51
    AMFValue &operator = (const AMFValue &from);
xzl committed
52

53 54 55 56 57
    void clear();
    AMFType type() const ;
    const std::string &as_string() const;
    double as_number() const;
    int as_integer() const;
xiongziliang committed
58
    bool as_boolean() const;
夏楚 committed
59
    std::string to_string() const;
60
    const AMFValue &operator[](const char *str) const;
夏楚 committed
61
    void object_for_each(const std::function<void(const std::string &key, const AMFValue &val)> &fun) const ;
62 63 64
    operator bool() const;
    void set(const std::string &s, const AMFValue &val);
    void add(const AMFValue &val);
夏楚 committed
65

xiongziliang committed
66 67 68 69 70
private:
    const mapType &getMap() const;
    const arrayType &getArr() const;
    void destroy();
    void init();
夏楚 committed
71

xzl committed
72
private:
73 74 75 76 77 78 79 80 81
    AMFType _type;
    union {
        std::string *string;
        double number;
        int integer;
        bool boolean;
        mapType *object;
        arrayType *array;
    } _value;
xzl committed
82 83 84 85
};

class AMFDecoder {
public:
夏楚 committed
86
    AMFDecoder(const toolkit::BufferLikeString &buf, size_t pos, int version = 0);
87 88
    template<typename TP>
    TP load();
夏楚 committed
89

xzl committed
90
private:
91 92 93 94 95 96
    std::string load_key();
    AMFValue load_object();
    AMFValue load_ecma();
    AMFValue load_arr();
    uint8_t front();
    uint8_t pop_front();
夏楚 committed
97

xiongziliang committed
98
private:
夏楚 committed
99
    const toolkit::BufferLikeString &buf;
100 101
    size_t pos;
    int version;
xzl committed
102 103 104 105
};

class AMFEncoder {
public:
106 107 108 109 110 111 112 113 114
    AMFEncoder & operator <<(const char *s);
    AMFEncoder & operator <<(const std::string &s);
    AMFEncoder & operator <<(std::nullptr_t);
    AMFEncoder & operator <<(const int n);
    AMFEncoder & operator <<(const double n);
    AMFEncoder & operator <<(const bool b);
    AMFEncoder & operator <<(const AMFValue &value);
    const std::string& data() const ;
    void clear() ;
夏楚 committed
115

xzl committed
116
private:
117 118
    void write_key(const std::string &s);
    AMFEncoder &write_undefined();
夏楚 committed
119

xiongziliang committed
120 121
private:
    std::string buf;
xzl committed
122 123 124 125
};


#endif