mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
utest for amf0 object, refine the count of elem
This commit is contained in:
parent
8ed9ae6242
commit
59db1dd260
4 changed files with 123 additions and 40 deletions
|
@ -594,6 +594,23 @@ SrsAmf0Object::~SrsAmf0Object()
|
||||||
srs_freep(eof);
|
srs_freep(eof);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SrsAmf0Object::size()
|
||||||
|
{
|
||||||
|
int size = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < properties->size(); i++){
|
||||||
|
std::string name = key_at(i);
|
||||||
|
SrsAmf0Any* value = value_at(i);
|
||||||
|
|
||||||
|
size += SrsAmf0Size::utf8(name);
|
||||||
|
size += SrsAmf0Size::any(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
size += SrsAmf0Size::object_eof();
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
int SrsAmf0Object::read(SrsStream* stream)
|
int SrsAmf0Object::read(SrsStream* stream)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
@ -691,21 +708,9 @@ int SrsAmf0Object::write(SrsStream* stream)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsAmf0Object::size()
|
int SrsAmf0Object::count()
|
||||||
{
|
{
|
||||||
int size = 1;
|
return properties->size();
|
||||||
|
|
||||||
for (int i = 0; i < properties->size(); i++){
|
|
||||||
std::string name = key_at(i);
|
|
||||||
SrsAmf0Any* value = value_at(i);
|
|
||||||
|
|
||||||
size += SrsAmf0Size::utf8(name);
|
|
||||||
size += SrsAmf0Size::any(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
size += SrsAmf0Size::object_eof();
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string SrsAmf0Object::key_at(int index)
|
std::string SrsAmf0Object::key_at(int index)
|
||||||
|
@ -751,6 +756,23 @@ SrsAmf0EcmaArray::~SrsAmf0EcmaArray()
|
||||||
srs_freep(eof);
|
srs_freep(eof);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SrsAmf0EcmaArray::size()
|
||||||
|
{
|
||||||
|
int size = 1 + 4;
|
||||||
|
|
||||||
|
for (int i = 0; i < properties->size(); i++){
|
||||||
|
std::string name = key_at(i);
|
||||||
|
SrsAmf0Any* value = value_at(i);
|
||||||
|
|
||||||
|
size += SrsAmf0Size::utf8(name);
|
||||||
|
size += SrsAmf0Size::any(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
size += SrsAmf0Size::object_eof();
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
int SrsAmf0EcmaArray::read(SrsStream* stream)
|
int SrsAmf0EcmaArray::read(SrsStream* stream)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
@ -782,7 +804,7 @@ int SrsAmf0EcmaArray::read(SrsStream* stream)
|
||||||
srs_verbose("amf0 read ecma_array count success. count=%d", count);
|
srs_verbose("amf0 read ecma_array count success. count=%d", count);
|
||||||
|
|
||||||
// value
|
// value
|
||||||
this->count = count;
|
this->_count = count;
|
||||||
|
|
||||||
while (!stream->empty()) {
|
while (!stream->empty()) {
|
||||||
// detect whether is eof.
|
// detect whether is eof.
|
||||||
|
@ -837,8 +859,8 @@ int SrsAmf0EcmaArray::write(SrsStream* stream)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
stream->write_4bytes(this->count);
|
stream->write_4bytes(this->_count);
|
||||||
srs_verbose("amf0 write ecma_array count success. count=%d", value->count);
|
srs_verbose("amf0 write ecma_array count success. count=%d", _count);
|
||||||
|
|
||||||
// value
|
// value
|
||||||
for (int i = 0; i < properties->size(); i++) {
|
for (int i = 0; i < properties->size(); i++) {
|
||||||
|
@ -868,28 +890,16 @@ int SrsAmf0EcmaArray::write(SrsStream* stream)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsAmf0EcmaArray::size()
|
|
||||||
{
|
|
||||||
int size = 1 + 4;
|
|
||||||
|
|
||||||
for (int i = 0; i < properties->size(); i++){
|
|
||||||
std::string name = key_at(i);
|
|
||||||
SrsAmf0Any* value = value_at(i);
|
|
||||||
|
|
||||||
size += SrsAmf0Size::utf8(name);
|
|
||||||
size += SrsAmf0Size::any(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
size += SrsAmf0Size::object_eof();
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SrsAmf0EcmaArray::clear()
|
void SrsAmf0EcmaArray::clear()
|
||||||
{
|
{
|
||||||
properties->clear();
|
properties->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SrsAmf0EcmaArray::count()
|
||||||
|
{
|
||||||
|
return properties->size();
|
||||||
|
}
|
||||||
|
|
||||||
std::string SrsAmf0EcmaArray::key_at(int index)
|
std::string SrsAmf0EcmaArray::key_at(int index)
|
||||||
{
|
{
|
||||||
return properties->key_at(index);
|
return properties->key_at(index);
|
||||||
|
|
|
@ -158,14 +158,20 @@ private:
|
||||||
public:
|
public:
|
||||||
virtual ~SrsAmf0Object();
|
virtual ~SrsAmf0Object();
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual int size();
|
||||||
virtual int read(SrsStream* stream);
|
virtual int read(SrsStream* stream);
|
||||||
virtual int write(SrsStream* stream);
|
virtual int write(SrsStream* stream);
|
||||||
|
|
||||||
virtual int size();
|
public:
|
||||||
|
virtual int count();
|
||||||
|
// @remark: max index is count().
|
||||||
virtual std::string key_at(int index);
|
virtual std::string key_at(int index);
|
||||||
|
// @remark: max index is count().
|
||||||
virtual SrsAmf0Any* value_at(int index);
|
virtual SrsAmf0Any* value_at(int index);
|
||||||
|
|
||||||
|
public:
|
||||||
virtual void set(std::string key, SrsAmf0Any* value);
|
virtual void set(std::string key, SrsAmf0Any* value);
|
||||||
|
|
||||||
virtual SrsAmf0Any* get_property(std::string name);
|
virtual SrsAmf0Any* get_property(std::string name);
|
||||||
virtual SrsAmf0Any* ensure_property_string(std::string name);
|
virtual SrsAmf0Any* ensure_property_string(std::string name);
|
||||||
virtual SrsAmf0Any* ensure_property_number(std::string name);
|
virtual SrsAmf0Any* ensure_property_number(std::string name);
|
||||||
|
@ -182,7 +188,7 @@ class SrsAmf0EcmaArray : public SrsAmf0Any
|
||||||
private:
|
private:
|
||||||
__SrsUnSortedHashtable* properties;
|
__SrsUnSortedHashtable* properties;
|
||||||
__SrsAmf0ObjectEOF* eof;
|
__SrsAmf0ObjectEOF* eof;
|
||||||
int32_t count;
|
int32_t _count;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// use SrsAmf0Any::ecma_array() to create it.
|
// use SrsAmf0Any::ecma_array() to create it.
|
||||||
|
@ -191,15 +197,21 @@ private:
|
||||||
public:
|
public:
|
||||||
virtual ~SrsAmf0EcmaArray();
|
virtual ~SrsAmf0EcmaArray();
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual int size();
|
||||||
virtual int read(SrsStream* stream);
|
virtual int read(SrsStream* stream);
|
||||||
virtual int write(SrsStream* stream);
|
virtual int write(SrsStream* stream);
|
||||||
|
|
||||||
virtual int size();
|
public:
|
||||||
virtual void clear();
|
virtual void clear();
|
||||||
|
virtual int count();
|
||||||
|
// @remark: max index is count().
|
||||||
virtual std::string key_at(int index);
|
virtual std::string key_at(int index);
|
||||||
|
// @remark: max index is count().
|
||||||
virtual SrsAmf0Any* value_at(int index);
|
virtual SrsAmf0Any* value_at(int index);
|
||||||
virtual void set(std::string key, SrsAmf0Any* value);
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void set(std::string key, SrsAmf0Any* value);
|
||||||
virtual SrsAmf0Any* get_property(std::string name);
|
virtual SrsAmf0Any* get_property(std::string name);
|
||||||
virtual SrsAmf0Any* ensure_property_string(std::string name);
|
virtual SrsAmf0Any* ensure_property_string(std::string name);
|
||||||
};
|
};
|
||||||
|
|
|
@ -3183,7 +3183,7 @@ int SrsOnMetaDataPacket::decode(SrsStream* stream)
|
||||||
SrsAmf0EcmaArray* arr = any->to_ecma_array();
|
SrsAmf0EcmaArray* arr = any->to_ecma_array();
|
||||||
|
|
||||||
// if ecma array, copy to object.
|
// if ecma array, copy to object.
|
||||||
for (int i = 0; i < arr->size(); i++) {
|
for (int i = 0; i < arr->count(); i++) {
|
||||||
metadata->set(arr->key_at(i), arr->value_at(i));
|
metadata->set(arr->key_at(i), arr->value_at(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -748,3 +748,64 @@ VOID TEST(AMF0Test, AnyAssert)
|
||||||
EXPECT_EQ(1+4+3, s.pos());
|
EXPECT_EQ(1+4+3, s.pos());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VOID TEST(AMF0Test, ObjectProps)
|
||||||
|
{
|
||||||
|
SrsAmf0Object* o = NULL;
|
||||||
|
|
||||||
|
// get/set property
|
||||||
|
if (true) {
|
||||||
|
o = SrsAmf0Any::object();
|
||||||
|
SrsAutoFree(SrsAmf0Object, o, false);
|
||||||
|
|
||||||
|
EXPECT_TRUE(NULL == o->get_property("name"));
|
||||||
|
|
||||||
|
o->set("name", SrsAmf0Any::str("winlin"));
|
||||||
|
EXPECT_TRUE(NULL != o->get_property("name"));
|
||||||
|
|
||||||
|
EXPECT_TRUE(NULL == o->get_property("age"));
|
||||||
|
|
||||||
|
o->set("age", SrsAmf0Any::number(100));
|
||||||
|
EXPECT_TRUE(NULL != o->get_property("age"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// index property
|
||||||
|
if (true) {
|
||||||
|
o = SrsAmf0Any::object();
|
||||||
|
SrsAutoFree(SrsAmf0Object, o, false);
|
||||||
|
|
||||||
|
o->set("name", SrsAmf0Any::str("winlin"));
|
||||||
|
EXPECT_STREQ("name", o->key_at(0).c_str());
|
||||||
|
ASSERT_TRUE(o->value_at(0)->is_string());
|
||||||
|
EXPECT_STREQ("winlin", o->value_at(0)->to_str().c_str());
|
||||||
|
|
||||||
|
o->set("age", SrsAmf0Any::number(100));
|
||||||
|
EXPECT_STREQ("name", o->key_at(0).c_str());
|
||||||
|
ASSERT_TRUE(o->value_at(0)->is_string());
|
||||||
|
EXPECT_STREQ("winlin", o->value_at(0)->to_str().c_str());
|
||||||
|
|
||||||
|
EXPECT_STREQ("age", o->key_at(1).c_str());
|
||||||
|
ASSERT_TRUE(o->value_at(1)->is_number());
|
||||||
|
EXPECT_DOUBLE_EQ(100, o->value_at(1)->to_number());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure property
|
||||||
|
if (true) {
|
||||||
|
o = SrsAmf0Any::object();
|
||||||
|
SrsAutoFree(SrsAmf0Object, o, false);
|
||||||
|
|
||||||
|
EXPECT_TRUE(NULL == o->ensure_property_string("name"));
|
||||||
|
EXPECT_TRUE(NULL == o->ensure_property_number("age"));
|
||||||
|
|
||||||
|
o->set("name", SrsAmf0Any::str("winlin"));
|
||||||
|
EXPECT_TRUE(NULL != o->ensure_property_string("name"));
|
||||||
|
EXPECT_TRUE(NULL == o->ensure_property_number("name"));
|
||||||
|
EXPECT_TRUE(NULL == o->ensure_property_number("age"));
|
||||||
|
|
||||||
|
o->set("age", SrsAmf0Any::number(100));
|
||||||
|
EXPECT_TRUE(NULL != o->ensure_property_string("name"));
|
||||||
|
EXPECT_TRUE(NULL == o->ensure_property_number("name"));
|
||||||
|
EXPECT_TRUE(NULL != o->ensure_property_number("age"));
|
||||||
|
EXPECT_TRUE(NULL == o->ensure_property_string("age"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue