amf.h 2.86 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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>
xiongzilaing committed
17
#include <unordered_map>
xiongziliang committed
18
#include <map>
19
#include <stdexcept>
xiongziliang committed
20 21 22
#include <functional>
using namespace std;

xzl committed
23
enum AMFType {
24 25 26 27 28 29 30 31 32
    AMF_NUMBER,
    AMF_INTEGER,
    AMF_BOOLEAN,
    AMF_STRING,
    AMF_OBJECT,
    AMF_NULL,
    AMF_UNDEFINED,
    AMF_ECMA_ARRAY,
    AMF_STRICT_ARRAY,
xzl committed
33 34 35 36 37 38
};

class AMFValue;

class AMFValue {
public:
xiongziliang committed
39 40 41
    friend class AMFEncoder;
    typedef std::map<std::string, AMFValue> mapType;
    typedef std::vector<AMFValue> arrayType;
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;
59 60 61 62 63 64
    string to_string() const;
    const AMFValue &operator[](const char *str) const;
    void object_for_each(const function<void(const string &key, const AMFValue &val)> &fun) const ;
    operator bool() const;
    void set(const std::string &s, const AMFValue &val);
    void add(const AMFValue &val);
xiongziliang committed
65 66 67 68 69
private:
    const mapType &getMap() const;
    const arrayType &getArr() const;
    void destroy();
    void init();
xzl committed
70
private:
71 72 73 74 75 76 77 78 79
    AMFType _type;
    union {
        std::string *string;
        double number;
        int integer;
        bool boolean;
        mapType *object;
        arrayType *array;
    } _value;
xzl committed
80 81 82 83
};

class AMFDecoder {
public:
84 85 86
    AMFDecoder(const std::string &buf, size_t pos, int version = 0);
    template<typename TP>
    TP load();
xzl committed
87
private:
88 89 90 91 92 93
    std::string load_key();
    AMFValue load_object();
    AMFValue load_ecma();
    AMFValue load_arr();
    uint8_t front();
    uint8_t pop_front();
xiongziliang committed
94
private:
95 96 97
    const std::string &buf;
    size_t pos;
    int version;
xzl committed
98 99 100 101
};

class AMFEncoder {
public:
102 103 104 105 106 107 108 109 110
    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() ;
xzl committed
111
private:
112 113
    void write_key(const std::string &s);
    AMFEncoder &write_undefined();
xiongziliang committed
114 115
private:
    std::string buf;
xzl committed
116 117 118 119
};


#endif