ZLMediaKit/src/Rtmp/amf.h

251 lines
5.6 KiB
C
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* 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.
*/
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>
2017-04-01 16:35:56 +08:00
enum AMFType {
AMF_NUMBER,
AMF_INTEGER,
AMF_BOOLEAN,
AMF_STRING,
AMF_OBJECT,
AMF_NULL,
AMF_UNDEFINED,
AMF_ECMA_ARRAY,
AMF_STRICT_ARRAY,
};
class AMFValue;
class AMFValue {
public:
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);
AMFValue(AMFValue &&from);
AMFValue &operator =(const AMFValue &from);
AMFValue &operator =(AMFValue &&from);
~AMFValue();
void clear() {
2018-10-24 15:43:52 +08:00
switch (_type) {
2017-04-01 16:35:56 +08:00
case AMF_STRING:
2018-10-24 15:43:52 +08:00
_value.string->clear();
2017-04-01 16:35:56 +08:00
break;
case AMF_OBJECT:
case AMF_ECMA_ARRAY:
2018-10-24 15:43:52 +08:00
_value.object->clear();
2017-04-01 16:35:56 +08:00
break;
default:
break;
}
}
AMFType type() const {
2018-10-24 15:43:52 +08:00
return _type;
2017-04-01 16:35:56 +08:00
}
const std::string &as_string() const {
2018-10-24 15:43:52 +08:00
if(_type != AMF_STRING){
2017-04-01 16:35:56 +08:00
throw std::runtime_error("AMF not a string");
}
2018-10-24 15:43:52 +08:00
return *_value.string;
2017-04-01 16:35:56 +08:00
}
double as_number() const {
2018-10-24 15:43:52 +08:00
switch (_type) {
2017-04-01 16:35:56 +08:00
case AMF_NUMBER:
2018-10-24 15:43:52 +08:00
return _value.number;
2017-04-01 16:35:56 +08:00
case AMF_INTEGER:
2018-10-24 15:43:52 +08:00
return _value.integer;
2017-04-01 16:35:56 +08:00
case AMF_BOOLEAN:
2018-10-24 15:43:52 +08:00
return _value.boolean;
2017-04-01 16:35:56 +08:00
break;
default:
throw std::runtime_error("AMF not a number");
break;
}
}
int as_integer() const {
2018-10-24 15:43:52 +08:00
switch (_type) {
2017-04-01 16:35:56 +08:00
case AMF_NUMBER:
2018-10-24 15:43:52 +08:00
return _value.number;
2017-04-01 16:35:56 +08:00
case AMF_INTEGER:
2018-10-24 15:43:52 +08:00
return _value.integer;
2017-04-01 16:35:56 +08:00
case AMF_BOOLEAN:
2018-10-24 15:43:52 +08:00
return _value.boolean;
2017-04-01 16:35:56 +08:00
break;
default:
throw std::runtime_error("AMF not a integer");
break;
}
}
bool as_boolean() const {
2018-10-24 15:43:52 +08:00
switch (_type) {
2017-04-01 16:35:56 +08:00
case AMF_NUMBER:
2018-10-24 15:43:52 +08:00
return _value.number;
2017-04-01 16:35:56 +08:00
case AMF_INTEGER:
2018-10-24 15:43:52 +08:00
return _value.integer;
2017-04-01 16:35:56 +08:00
case AMF_BOOLEAN:
2018-10-24 15:43:52 +08:00
return _value.boolean;
2017-04-01 16:35:56 +08:00
break;
default:
throw std::runtime_error("AMF not a boolean");
break;
}
}
2017-05-13 17:25:31 +08:00
const AMFValue &operator[](const char *str) const {
2018-10-24 15:43:52 +08:00
if (_type != AMF_OBJECT && _type != AMF_ECMA_ARRAY) {
2017-04-01 16:35:56 +08:00
throw std::runtime_error("AMF not a object");
}
2018-10-24 15:43:52 +08:00
auto i = _value.object->find(str);
if (i == _value.object->end()) {
2017-04-01 16:35:56 +08:00
static AMFValue val(AMF_NULL);
return val;
}
return i->second;
}
template<typename FUN>
void object_for_each(const FUN &fun) const {
2018-10-24 15:43:52 +08:00
if (_type != AMF_OBJECT && _type != AMF_ECMA_ARRAY) {
2017-04-01 16:35:56 +08:00
throw std::runtime_error("AMF not a object");
}
2018-10-24 15:43:52 +08:00
for (auto & pr : *(_value.object)) {
2017-04-01 16:35:56 +08:00
fun(pr.first, pr.second);
}
}
2017-05-13 17:25:31 +08:00
operator bool() const{
2018-10-24 15:43:52 +08:00
return _type != AMF_NULL;
2017-04-01 16:35:56 +08:00
}
void set(const std::string &s, const AMFValue &val) {
2018-10-24 15:43:52 +08:00
if (_type != AMF_OBJECT && _type != AMF_ECMA_ARRAY) {
2017-04-01 16:35:56 +08:00
throw std::runtime_error("AMF not a object");
}
2018-10-24 15:43:52 +08:00
_value.object->emplace(s, val);
2017-04-01 16:35:56 +08:00
}
void add(const AMFValue &val) {
2018-10-24 15:43:52 +08:00
if (_type != AMF_STRICT_ARRAY) {
2017-04-01 16:35:56 +08:00
throw std::runtime_error("AMF not a array");
}
2018-10-24 15:43:52 +08:00
assert(_type == AMF_STRICT_ARRAY);
_value.array->push_back(val);
2017-04-01 16:35:56 +08:00
}
private:
2017-05-13 17:25:31 +08:00
typedef std::map<std::string, AMFValue> mapType;
2017-04-01 16:35:56 +08:00
typedef std::vector<AMFValue> arrayType;
2018-10-24 15:43:52 +08:00
AMFType _type;
2017-04-01 16:35:56 +08:00
union {
std::string *string;
double number;
int integer;
bool boolean;
mapType *object;
arrayType *array;
2018-10-24 15:43:52 +08:00
} _value;
2017-04-01 16:35:56 +08:00
friend class AMFEncoder;
const mapType &getMap() const {
2018-10-24 15:43:52 +08:00
if (_type != AMF_OBJECT && _type != AMF_ECMA_ARRAY) {
2017-04-01 16:35:56 +08:00
throw std::runtime_error("AMF not a object");
}
2018-10-24 15:43:52 +08:00
return *_value.object;
2017-04-01 16:35:56 +08:00
}
const arrayType &getArr() const {
2018-10-24 15:43:52 +08:00
if (_type != AMF_STRICT_ARRAY) {
2017-04-01 16:35:56 +08:00
throw std::runtime_error("AMF not a array");
}
2018-10-24 15:43:52 +08:00
return *_value.array;
2017-04-01 16:35:56 +08:00
}
inline void destroy();
inline void init();
};
class AMFDecoder {
public:
AMFDecoder(const std::string &_buf, size_t _pos, int _version = 0) :
buf(_buf), pos(_pos), version(_version) {
}
int getVersion() const {
return version;
}
template<typename TP>
TP load();
private:
const std::string &buf;
size_t pos;
int version;
std::string load_key();
AMFValue load_object();
AMFValue load_ecma();
AMFValue load_arr();
uint8_t front();
uint8_t pop_front();
};
class AMFEncoder {
public:
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 {
return buf;
}
void clear() {
buf.clear();
}
private:
void write_key(const std::string &s);
AMFEncoder &write_undefined();
std::string buf;
};
#endif