winlin

amf0 utest: add usage for amf0

@@ -37,6 +37,35 @@ class SrsStream; @@ -37,6 +37,35 @@ class SrsStream;
37 class SrsAmf0Object; 37 class SrsAmf0Object;
38 class SrsAmf0EcmaArray; 38 class SrsAmf0EcmaArray;
39 39
  40 +////////////////////////////////////////////////////////////////////////
  41 +////////////////////////////////////////////////////////////////////////
  42 +////////////////////////////////////////////////////////////////////////
  43 +// amf0 codec
  44 +// 1. SrsAmf0Any: read any from stream
  45 +// SrsAmf0Any* pany = NULL;
  46 +// if ((ret = srs_amf0_read_any(stream, &pany)) != ERROR_SUCCESS) {
  47 +// return ret;
  48 +// }
  49 +// 2. SrsAmf0Any: convert to specifid type, for instance, string
  50 +// SrsAmf0Any* pany = ...
  51 +// if (pany && pany->is_string()) {
  52 +// string v = pany->to_str();
  53 +// }
  54 +// 3. SrsAmf0Any: parse specified type to any, for instance, string
  55 +// SrsAmf0Any* pany = SrsAmf0Any::str("winlin");
  56 +// 4. SrsAmf0Size: get amf0 instance size
  57 +// int size = SrsAmf0Size::str("winlin");
  58 +// 5. SrsAmf0Object: the amf0 object, directly new is ok.
  59 +// SrsAmf0Object* obj = SrsAmf0Any::object();
  60 +// SrsAmf0Object* obj = new SrsAmf0Object();
  61 +// 5. SrsAmf0EcmaArray: the amf0 ecma array, directly new is ok.
  62 +// SrsAmf0EcmaArray* arr = SrsAmf0Any::array();
  63 +// SrsAmf0EcmaArray* arr = new SrsAmf0EcmaArray();
  64 +// for detail usage, see interfaces of each object.
  65 +////////////////////////////////////////////////////////////////////////
  66 +////////////////////////////////////////////////////////////////////////
  67 +////////////////////////////////////////////////////////////////////////
  68 +
40 /** 69 /**
41 * any amf0 value. 70 * any amf0 value.
42 * 2.1 Types Overview 71 * 2.1 Types Overview
@@ -2624,6 +2624,7 @@ int SrsPlayPacket::decode(SrsStream* stream) @@ -2624,6 +2624,7 @@ int SrsPlayPacket::decode(SrsStream* stream)
2624 } 2624 }
2625 SrsAutoFree(SrsAmf0Any, reset_value, false); 2625 SrsAutoFree(SrsAmf0Any, reset_value, false);
2626 2626
  2627 + if (reset_value) {
2627 // check if the value is bool or number 2628 // check if the value is bool or number
2628 // An optional Boolean value or number that specifies whether 2629 // An optional Boolean value or number that specifies whether
2629 // to flush any previous playlist 2630 // to flush any previous playlist
@@ -2636,6 +2637,7 @@ int SrsPlayPacket::decode(SrsStream* stream) @@ -2636,6 +2637,7 @@ int SrsPlayPacket::decode(SrsStream* stream)
2636 srs_error("amf0 invalid type=%#x, requires number or bool, ret=%d", reset_value->marker, ret); 2637 srs_error("amf0 invalid type=%#x, requires number or bool, ret=%d", reset_value->marker, ret);
2637 return ret; 2638 return ret;
2638 } 2639 }
  2640 + }
2639 2641
2640 srs_info("amf0 decode play packet success"); 2642 srs_info("amf0 decode play packet success");
2641 2643
@@ -3188,7 +3190,7 @@ int SrsOnMetaDataPacket::decode(SrsStream* stream) @@ -3188,7 +3190,7 @@ int SrsOnMetaDataPacket::decode(SrsStream* stream)
3188 return ret; 3190 return ret;
3189 } 3191 }
3190 3192
3191 - if (any->is_object()) { 3193 + if (any && any->is_object()) {
3192 srs_freep(metadata); 3194 srs_freep(metadata);
3193 metadata = dynamic_cast<SrsAmf0Object*>(any); 3195 metadata = dynamic_cast<SrsAmf0Object*>(any);
3194 srs_info("decode metadata object success"); 3196 srs_info("decode metadata object success");
@@ -3199,10 +3201,11 @@ int SrsOnMetaDataPacket::decode(SrsStream* stream) @@ -3199,10 +3201,11 @@ int SrsOnMetaDataPacket::decode(SrsStream* stream)
3199 if (!arr) { 3201 if (!arr) {
3200 ret = ERROR_RTMP_AMF0_DECODE; 3202 ret = ERROR_RTMP_AMF0_DECODE;
3201 srs_error("decode metadata array failed. ret=%d", ret); 3203 srs_error("decode metadata array failed. ret=%d", ret);
3202 - srs_freep(any);  
3203 return ret; 3204 return ret;
3204 } 3205 }
3205 3206
  3207 + // if ecma array, copy to object.
  3208 + SrsAutoFree(SrsAmf0EcmaArray, arr, false);
3206 for (int i = 0; i < arr->size(); i++) { 3209 for (int i = 0; i < arr->size(); i++) {
3207 metadata->set(arr->key_at(i), arr->value_at(i)); 3210 metadata->set(arr->key_at(i), arr->value_at(i));
3208 } 3211 }