2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*/
|
2020-04-04 20:30:09 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#ifndef __amf_h
|
|
|
|
|
#define __amf_h
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <string>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <vector>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <unordered_map>
|
2017-05-13 17:25:31 +08:00
|
|
|
|
#include <map>
|
2019-08-19 15:46:42 +08:00
|
|
|
|
#include <stdexcept>
|
2019-09-20 10:37:41 +08:00
|
|
|
|
#include <functional>
|
2020-11-01 03:41:35 +08:00
|
|
|
|
#include "Network/Buffer.h"
|
2019-09-20 10:37:41 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
enum AMFType {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
AMF_NUMBER,
|
|
|
|
|
AMF_INTEGER,
|
|
|
|
|
AMF_BOOLEAN,
|
|
|
|
|
AMF_STRING,
|
|
|
|
|
AMF_OBJECT,
|
|
|
|
|
AMF_NULL,
|
|
|
|
|
AMF_UNDEFINED,
|
|
|
|
|
AMF_ECMA_ARRAY,
|
|
|
|
|
AMF_STRICT_ARRAY,
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AMFValue;
|
|
|
|
|
|
|
|
|
|
class AMFValue {
|
|
|
|
|
public:
|
2019-09-20 10:37:41 +08:00
|
|
|
|
friend class AMFEncoder;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
|
|
|
|
using mapType = std::map<std::string, AMFValue>;
|
|
|
|
|
using arrayType = std::vector<AMFValue>;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-05-28 17:03:12 +08:00
|
|
|
|
~AMFValue();
|
2020-03-20 11:51:24 +08:00
|
|
|
|
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);
|
2020-05-28 17:03:12 +08:00
|
|
|
|
AMFValue &operator = (const AMFValue &from);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void clear();
|
|
|
|
|
AMFType type() const ;
|
|
|
|
|
const std::string &as_string() const;
|
|
|
|
|
double as_number() const;
|
|
|
|
|
int as_integer() const;
|
2019-09-20 10:37:41 +08:00
|
|
|
|
bool as_boolean() const;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string to_string() const;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
const AMFValue &operator[](const char *str) const;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void object_for_each(const std::function<void(const std::string &key, const AMFValue &val)> &fun) const ;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
operator bool() const;
|
|
|
|
|
void set(const std::string &s, const AMFValue &val);
|
|
|
|
|
void add(const AMFValue &val);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2019-09-20 10:37:41 +08:00
|
|
|
|
private:
|
|
|
|
|
const mapType &getMap() const;
|
|
|
|
|
const arrayType &getArr() const;
|
|
|
|
|
void destroy();
|
|
|
|
|
void init();
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
AMFType _type;
|
|
|
|
|
union {
|
|
|
|
|
std::string *string;
|
|
|
|
|
double number;
|
|
|
|
|
int integer;
|
|
|
|
|
bool boolean;
|
|
|
|
|
mapType *object;
|
|
|
|
|
arrayType *array;
|
|
|
|
|
} _value;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AMFDecoder {
|
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
AMFDecoder(const toolkit::BufferLikeString &buf, size_t pos, int version = 0);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
template<typename TP>
|
|
|
|
|
TP load();
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
std::string load_key();
|
|
|
|
|
AMFValue load_object();
|
|
|
|
|
AMFValue load_ecma();
|
|
|
|
|
AMFValue load_arr();
|
|
|
|
|
uint8_t front();
|
|
|
|
|
uint8_t pop_front();
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2019-09-20 10:37:41 +08:00
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const toolkit::BufferLikeString &buf;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
size_t pos;
|
|
|
|
|
int version;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AMFEncoder {
|
|
|
|
|
public:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
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() ;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void write_key(const std::string &s);
|
|
|
|
|
AMFEncoder &write_undefined();
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2019-09-20 10:37:41 +08:00
|
|
|
|
private:
|
|
|
|
|
std::string buf;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|