mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
support amf0 StrictArray(0x0a). 0.9.111.
This commit is contained in:
parent
c8f80414ef
commit
7f48590239
5 changed files with 272 additions and 29 deletions
|
@ -251,6 +251,16 @@ bool SrsAmf0Any::is_ecma_array()
|
|||
return marker == RTMP_AMF0_EcmaArray;
|
||||
}
|
||||
|
||||
bool SrsAmf0Any::is_strict_array()
|
||||
{
|
||||
return marker == RTMP_AMF0_StrictArray;
|
||||
}
|
||||
|
||||
bool SrsAmf0Any::is_complex_object()
|
||||
{
|
||||
return is_object() || is_object_eof() || is_ecma_array() || is_strict_array();
|
||||
}
|
||||
|
||||
string SrsAmf0Any::to_str()
|
||||
{
|
||||
__SrsAmf0String* p = dynamic_cast<__SrsAmf0String*>(this);
|
||||
|
@ -293,6 +303,13 @@ SrsAmf0EcmaArray* SrsAmf0Any::to_ecma_array()
|
|||
return p;
|
||||
}
|
||||
|
||||
SrsAmf0StrictArray* SrsAmf0Any::to_strict_array()
|
||||
{
|
||||
SrsAmf0StrictArray* p = dynamic_cast<SrsAmf0StrictArray*>(this);
|
||||
srs_assert(p != NULL);
|
||||
return p;
|
||||
}
|
||||
|
||||
bool SrsAmf0Any::is_object_eof()
|
||||
{
|
||||
return marker == RTMP_AMF0_ObjectEnd;
|
||||
|
@ -338,6 +355,11 @@ SrsAmf0EcmaArray* SrsAmf0Any::ecma_array()
|
|||
return new SrsAmf0EcmaArray();
|
||||
}
|
||||
|
||||
SrsAmf0StrictArray* SrsAmf0Any::strict_array()
|
||||
{
|
||||
return new SrsAmf0StrictArray();
|
||||
}
|
||||
|
||||
int SrsAmf0Any::discovery(SrsStream* stream, SrsAmf0Any** ppvalue)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
@ -390,6 +412,10 @@ int SrsAmf0Any::discovery(SrsStream* stream, SrsAmf0Any** ppvalue)
|
|||
*ppvalue = SrsAmf0Any::ecma_array();
|
||||
return ret;
|
||||
}
|
||||
case RTMP_AMF0_StrictArray: {
|
||||
*ppvalue = SrsAmf0Any::strict_array();
|
||||
return ret;
|
||||
}
|
||||
case RTMP_AMF0_Invalid:
|
||||
default: {
|
||||
ret = ERROR_RTMP_AMF0_INVALID;
|
||||
|
@ -956,6 +982,137 @@ SrsAmf0Any* SrsAmf0EcmaArray::ensure_property_number(string name)
|
|||
return properties->ensure_property_number(name);
|
||||
}
|
||||
|
||||
SrsAmf0StrictArray::SrsAmf0StrictArray()
|
||||
{
|
||||
marker = RTMP_AMF0_StrictArray;
|
||||
}
|
||||
|
||||
SrsAmf0StrictArray::~SrsAmf0StrictArray()
|
||||
{
|
||||
std::vector<SrsAmf0Any*>::iterator it;
|
||||
for (it = properties.begin(); it != properties.end(); ++it) {
|
||||
SrsAmf0Any* any = *it;
|
||||
srs_freep(any);
|
||||
}
|
||||
properties.clear();
|
||||
}
|
||||
|
||||
int SrsAmf0StrictArray::total_size()
|
||||
{
|
||||
int size = 1 + 4;
|
||||
|
||||
for (int i = 0; i < (int)properties.size(); i++){
|
||||
SrsAmf0Any* any = properties[i];
|
||||
size += any->total_size();
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int SrsAmf0StrictArray::read(SrsStream* stream)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// marker
|
||||
if (!stream->require(1)) {
|
||||
ret = ERROR_RTMP_AMF0_DECODE;
|
||||
srs_error("amf0 read strict_array marker failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char marker = stream->read_1bytes();
|
||||
if (marker != RTMP_AMF0_StrictArray) {
|
||||
ret = ERROR_RTMP_AMF0_DECODE;
|
||||
srs_error("amf0 check strict_array marker failed. "
|
||||
"marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_Object, ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("amf0 read strict_array marker success");
|
||||
|
||||
// count
|
||||
if (!stream->require(4)) {
|
||||
ret = ERROR_RTMP_AMF0_DECODE;
|
||||
srs_error("amf0 read strict_array count failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int32_t count = stream->read_4bytes();
|
||||
srs_verbose("amf0 read strict_array count success. count=%d", count);
|
||||
|
||||
// value
|
||||
this->_count = count;
|
||||
|
||||
for (int i = 0; i < count && !stream->empty(); i++) {
|
||||
// property-value: any
|
||||
SrsAmf0Any* elem = NULL;
|
||||
if ((ret = srs_amf0_read_any(stream, &elem)) != ERROR_SUCCESS) {
|
||||
srs_error("amf0 strict_array read value failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// add property
|
||||
properties.push_back(elem);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
int SrsAmf0StrictArray::write(SrsStream* stream)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// marker
|
||||
if (!stream->require(1)) {
|
||||
ret = ERROR_RTMP_AMF0_ENCODE;
|
||||
srs_error("amf0 write strict_array marker failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
stream->write_1bytes(RTMP_AMF0_StrictArray);
|
||||
srs_verbose("amf0 write strict_array marker success");
|
||||
|
||||
// count
|
||||
if (!stream->require(4)) {
|
||||
ret = ERROR_RTMP_AMF0_ENCODE;
|
||||
srs_error("amf0 write strict_array count failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
stream->write_4bytes(this->_count);
|
||||
srs_verbose("amf0 write strict_array count success. count=%d", _count);
|
||||
|
||||
// value
|
||||
for (int i = 0; i < (int)properties.size(); i++) {
|
||||
SrsAmf0Any* any = properties[i];
|
||||
|
||||
if ((ret = srs_amf0_write_any(stream, any)) != ERROR_SUCCESS) {
|
||||
srs_error("write strict_array property value failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_verbose("write amf0 property success. name=%s", name.c_str());
|
||||
}
|
||||
|
||||
srs_verbose("write strict_array object success.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsAmf0StrictArray::clear()
|
||||
{
|
||||
properties.clear();
|
||||
}
|
||||
|
||||
int SrsAmf0StrictArray::count()
|
||||
{
|
||||
return properties.size();
|
||||
}
|
||||
|
||||
SrsAmf0Any* SrsAmf0StrictArray::at(int index)
|
||||
{
|
||||
srs_assert(index < (int)properties.size());
|
||||
return properties.at(index);
|
||||
}
|
||||
|
||||
int SrsAmf0Size::utf8(string value)
|
||||
{
|
||||
return 2 + value.length();
|
||||
|
@ -1009,6 +1166,15 @@ int SrsAmf0Size::ecma_array(SrsAmf0EcmaArray* arr)
|
|||
return arr->total_size();
|
||||
}
|
||||
|
||||
int SrsAmf0Size::strict_array(SrsAmf0StrictArray* arr)
|
||||
{
|
||||
if (!arr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return arr->total_size();
|
||||
}
|
||||
|
||||
int SrsAmf0Size::any(SrsAmf0Any* o)
|
||||
{
|
||||
if (!o) {
|
||||
|
|
|
@ -31,10 +31,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class SrsStream;
|
||||
class SrsAmf0Object;
|
||||
class SrsAmf0EcmaArray;
|
||||
class SrsAmf0StrictArray;
|
||||
class __SrsUnSortedHashtable;
|
||||
class __SrsAmf0ObjectEOF;
|
||||
|
||||
|
@ -96,6 +98,9 @@ public:
|
|||
virtual bool is_object();
|
||||
virtual bool is_object_eof();
|
||||
virtual bool is_ecma_array();
|
||||
virtual bool is_strict_array();
|
||||
public:
|
||||
virtual bool is_complex_object();
|
||||
public:
|
||||
/**
|
||||
* get the string of any when is_string() indicates true.
|
||||
|
@ -123,6 +128,7 @@ public:
|
|||
* user must ensure the type is a ecma array, or assert failed.
|
||||
*/
|
||||
virtual SrsAmf0EcmaArray* to_ecma_array();
|
||||
virtual SrsAmf0StrictArray* to_strict_array();
|
||||
public:
|
||||
/**
|
||||
* get the size of amf0 any, including the marker size.
|
||||
|
@ -142,6 +148,7 @@ public:
|
|||
static SrsAmf0Object* object();
|
||||
static SrsAmf0Any* object_eof();
|
||||
static SrsAmf0EcmaArray* ecma_array();
|
||||
static SrsAmf0StrictArray* strict_array();
|
||||
public:
|
||||
static int discovery(SrsStream* stream, SrsAmf0Any** ppvalue);
|
||||
};
|
||||
|
@ -225,6 +232,36 @@ public:
|
|||
virtual SrsAmf0Any* ensure_property_number(std::string name);
|
||||
};
|
||||
|
||||
/**
|
||||
* 2.12 Strict Array Type
|
||||
* array-count = U32
|
||||
* strict-array-type = array-count *(value-type)
|
||||
*/
|
||||
class SrsAmf0StrictArray : public SrsAmf0Any
|
||||
{
|
||||
private:
|
||||
std::vector<SrsAmf0Any*> properties;
|
||||
int32_t _count;
|
||||
|
||||
private:
|
||||
// use SrsAmf0Any::strict_array() to create it.
|
||||
friend class SrsAmf0Any;
|
||||
SrsAmf0StrictArray();
|
||||
public:
|
||||
virtual ~SrsAmf0StrictArray();
|
||||
|
||||
public:
|
||||
virtual int total_size();
|
||||
virtual int read(SrsStream* stream);
|
||||
virtual int write(SrsStream* stream);
|
||||
|
||||
public:
|
||||
virtual void clear();
|
||||
virtual int count();
|
||||
// @remark: max index is count().
|
||||
virtual SrsAmf0Any* at(int index);
|
||||
};
|
||||
|
||||
/**
|
||||
* the class to get amf0 object size
|
||||
*/
|
||||
|
@ -240,6 +277,7 @@ public:
|
|||
static int object(SrsAmf0Object* obj);
|
||||
static int object_eof();
|
||||
static int ecma_array(SrsAmf0EcmaArray* arr);
|
||||
static int strict_array(SrsAmf0StrictArray* arr);
|
||||
static int any(SrsAmf0Any* o);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue