From 59db1dd2608fc8cefb266f074bd7b14ab1647390 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 8 Mar 2014 22:51:13 +0800 Subject: [PATCH] utest for amf0 object, refine the count of elem --- trunk/src/rtmp/srs_protocol_amf0.cpp | 76 ++++++++++++---------- trunk/src/rtmp/srs_protocol_amf0.hpp | 22 +++++-- trunk/src/rtmp/srs_protocol_rtmp_stack.cpp | 2 +- trunk/src/utest/srs_utest_amf0.cpp | 61 +++++++++++++++++ 4 files changed, 122 insertions(+), 39 deletions(-) diff --git a/trunk/src/rtmp/srs_protocol_amf0.cpp b/trunk/src/rtmp/srs_protocol_amf0.cpp index 5a6d317d7..1cd0476a1 100644 --- a/trunk/src/rtmp/srs_protocol_amf0.cpp +++ b/trunk/src/rtmp/srs_protocol_amf0.cpp @@ -594,6 +594,23 @@ SrsAmf0Object::~SrsAmf0Object() 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 ret = ERROR_SUCCESS; @@ -691,21 +708,9 @@ int SrsAmf0Object::write(SrsStream* stream) return ret; } -int SrsAmf0Object::size() +int SrsAmf0Object::count() { - 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; + return properties->size(); } std::string SrsAmf0Object::key_at(int index) @@ -751,6 +756,23 @@ SrsAmf0EcmaArray::~SrsAmf0EcmaArray() 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 ret = ERROR_SUCCESS; @@ -782,7 +804,7 @@ int SrsAmf0EcmaArray::read(SrsStream* stream) srs_verbose("amf0 read ecma_array count success. count=%d", count); // value - this->count = count; + this->_count = count; while (!stream->empty()) { // detect whether is eof. @@ -837,8 +859,8 @@ int SrsAmf0EcmaArray::write(SrsStream* stream) return ret; } - stream->write_4bytes(this->count); - srs_verbose("amf0 write ecma_array count success. count=%d", value->count); + stream->write_4bytes(this->_count); + srs_verbose("amf0 write ecma_array count success. count=%d", _count); // value for (int i = 0; i < properties->size(); i++) { @@ -868,26 +890,14 @@ int SrsAmf0EcmaArray::write(SrsStream* stream) return ret; } -int SrsAmf0EcmaArray::size() +void SrsAmf0EcmaArray::clear() { - 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; + properties->clear(); } -void SrsAmf0EcmaArray::clear() +int SrsAmf0EcmaArray::count() { - properties->clear(); + return properties->size(); } std::string SrsAmf0EcmaArray::key_at(int index) diff --git a/trunk/src/rtmp/srs_protocol_amf0.hpp b/trunk/src/rtmp/srs_protocol_amf0.hpp index 61ab3c870..58c773ddb 100644 --- a/trunk/src/rtmp/srs_protocol_amf0.hpp +++ b/trunk/src/rtmp/srs_protocol_amf0.hpp @@ -158,14 +158,20 @@ private: public: virtual ~SrsAmf0Object(); +public: + virtual int size(); virtual int read(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); + // @remark: max index is count(). virtual SrsAmf0Any* value_at(int index); + +public: virtual void set(std::string key, SrsAmf0Any* value); - virtual SrsAmf0Any* get_property(std::string name); virtual SrsAmf0Any* ensure_property_string(std::string name); virtual SrsAmf0Any* ensure_property_number(std::string name); @@ -182,7 +188,7 @@ class SrsAmf0EcmaArray : public SrsAmf0Any private: __SrsUnSortedHashtable* properties; __SrsAmf0ObjectEOF* eof; - int32_t count; + int32_t _count; private: // use SrsAmf0Any::ecma_array() to create it. @@ -191,15 +197,21 @@ private: public: virtual ~SrsAmf0EcmaArray(); +public: + virtual int size(); virtual int read(SrsStream* stream); virtual int write(SrsStream* stream); - virtual int size(); +public: virtual void clear(); + virtual int count(); + // @remark: max index is count(). virtual std::string key_at(int index); + // @remark: max index is count(). 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* ensure_property_string(std::string name); }; diff --git a/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp b/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp index bf729f13e..7e583423a 100644 --- a/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp +++ b/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp @@ -3183,7 +3183,7 @@ int SrsOnMetaDataPacket::decode(SrsStream* stream) SrsAmf0EcmaArray* arr = any->to_ecma_array(); // 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)); } diff --git a/trunk/src/utest/srs_utest_amf0.cpp b/trunk/src/utest/srs_utest_amf0.cpp index de5936d82..1852dfc5a 100644 --- a/trunk/src/utest/srs_utest_amf0.cpp +++ b/trunk/src/utest/srs_utest_amf0.cpp @@ -748,3 +748,64 @@ VOID TEST(AMF0Test, AnyAssert) 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")); + } +}