winlin

support amf0 and json to convert with each other.

@@ -344,6 +344,7 @@ Remark: @@ -344,6 +344,7 @@ Remark:
344 344
345 ## History 345 ## History
346 346
  347 +* v3.0, 2015-09-19, support amf0 and json to convert with each other.
347 * v3.0, 2015-09-19, json objects support dumps to string. 348 * v3.0, 2015-09-19, json objects support dumps to string.
348 * v3.0, 2015-09-14, fix [#459][bug #459], support dvr raw api. 3.0.4 349 * v3.0, 2015-09-14, fix [#459][bug #459], support dvr raw api. 3.0.4
349 * v3.0, 2015-09-14, fix [#459][bug #459], dvr support apply filter for ng-control dvr module. 350 * v3.0, 2015-09-14, fix [#459][bug #459], dvr support apply filter for ng-control dvr module.
@@ -5573,22 +5573,22 @@ vector<SrsConfDirective*> SrsConfig::get_exec_publishs(string vhost) @@ -5573,22 +5573,22 @@ vector<SrsConfDirective*> SrsConfig::get_exec_publishs(string vhost)
5573 5573
5574 vector<SrsConfDirective*> SrsConfig::get_ingesters(string vhost) 5574 vector<SrsConfDirective*> SrsConfig::get_ingesters(string vhost)
5575 { 5575 {
5576 - vector<SrsConfDirective*> ingeters; 5576 + vector<SrsConfDirective*> integers;
5577 5577
5578 SrsConfDirective* conf = get_vhost(vhost); 5578 SrsConfDirective* conf = get_vhost(vhost);
5579 if (!conf) { 5579 if (!conf) {
5580 - return ingeters; 5580 + return integers;
5581 } 5581 }
5582 5582
5583 for (int i = 0; i < (int)conf->directives.size(); i++) { 5583 for (int i = 0; i < (int)conf->directives.size(); i++) {
5584 SrsConfDirective* ingester = conf->directives[i]; 5584 SrsConfDirective* ingester = conf->directives[i];
5585 5585
5586 if (ingester->name == "ingest") { 5586 if (ingester->name == "ingest") {
5587 - ingeters.push_back(ingester); 5587 + integers.push_back(ingester);
5588 } 5588 }
5589 } 5589 }
5590 5590
5591 - return ingeters; 5591 + return integers;
5592 } 5592 }
5593 5593
5594 SrsConfDirective* SrsConfig::get_ingest_by_id(string vhost, string ingest_id) 5594 SrsConfDirective* SrsConfig::get_ingest_by_id(string vhost, string ingest_id)
@@ -81,7 +81,7 @@ int srs_api_response_jsonp_code(ISrsHttpResponseWriter* w, string callback, int @@ -81,7 +81,7 @@ int srs_api_response_jsonp_code(ISrsHttpResponseWriter* w, string callback, int
81 SrsJsonObject* obj = SrsJsonAny::object(); 81 SrsJsonObject* obj = SrsJsonAny::object();
82 SrsAutoFree(SrsJsonObject, obj); 82 SrsAutoFree(SrsJsonObject, obj);
83 83
84 - obj->set("code", SrsJsonAny::ingeter(code)); 84 + obj->set("code", SrsJsonAny::integer(code));
85 85
86 return srs_api_response_jsonp(w, callback, obj->to_json()); 86 return srs_api_response_jsonp(w, callback, obj->to_json());
87 } 87 }
@@ -101,7 +101,7 @@ int srs_api_response_json_code(ISrsHttpResponseWriter* w, int code) @@ -101,7 +101,7 @@ int srs_api_response_json_code(ISrsHttpResponseWriter* w, int code)
101 SrsJsonObject* obj = SrsJsonAny::object(); 101 SrsJsonObject* obj = SrsJsonAny::object();
102 SrsAutoFree(SrsJsonObject, obj); 102 SrsAutoFree(SrsJsonObject, obj);
103 103
104 - obj->set("code", SrsJsonAny::ingeter(code)); 104 + obj->set("code", SrsJsonAny::integer(code));
105 105
106 return srs_api_response_json(w, obj->to_json()); 106 return srs_api_response_json(w, obj->to_json());
107 } 107 }
@@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 using namespace std; 27 using namespace std;
28 28
29 #include <srs_kernel_log.hpp> 29 #include <srs_kernel_log.hpp>
  30 +#include <srs_rtmp_amf0.hpp>
30 31
31 /* json encode 32 /* json encode
32 cout<< SRS_JOBJECT_START 33 cout<< SRS_JOBJECT_START
@@ -357,6 +358,40 @@ string SrsJsonAny::to_json() @@ -357,6 +358,40 @@ string SrsJsonAny::to_json()
357 return "null"; 358 return "null";
358 } 359 }
359 360
  361 +SrsAmf0Any* SrsJsonAny::to_amf0()
  362 +{
  363 + switch (marker) {
  364 + case SRS_JSON_String: {
  365 + return SrsAmf0Any::str(to_str().c_str());
  366 + }
  367 + case SRS_JSON_Boolean: {
  368 + return SrsAmf0Any::boolean(to_boolean());
  369 + }
  370 + case SRS_JSON_Integer: {
  371 + return SrsAmf0Any::number(to_integer());
  372 + }
  373 + case SRS_JSON_Number: {
  374 + return SrsAmf0Any::number(to_number());
  375 + }
  376 + case SRS_JSON_Null: {
  377 + return SrsAmf0Any::null();
  378 + }
  379 + case SRS_JSON_Object: {
  380 + // json object must override this method.
  381 + srs_assert(false);
  382 + }
  383 + case SRS_JSON_Array: {
  384 + // json array must override this method.
  385 + srs_assert(false);
  386 + }
  387 + default: {
  388 + break;
  389 + }
  390 + }
  391 +
  392 + return SrsAmf0Any::null();
  393 +}
  394 +
360 SrsJsonAny* SrsJsonAny::str(const char* value) 395 SrsJsonAny* SrsJsonAny::str(const char* value)
361 { 396 {
362 return new SrsJsonString(value); 397 return new SrsJsonString(value);
@@ -367,7 +402,7 @@ SrsJsonAny* SrsJsonAny::boolean(bool value) @@ -367,7 +402,7 @@ SrsJsonAny* SrsJsonAny::boolean(bool value)
367 return new SrsJsonBoolean(value); 402 return new SrsJsonBoolean(value);
368 } 403 }
369 404
370 -SrsJsonAny* SrsJsonAny::ingeter(int64_t value) 405 +SrsJsonAny* SrsJsonAny::integer(int64_t value)
371 { 406 {
372 return new SrsJsonInteger(value); 407 return new SrsJsonInteger(value);
373 } 408 }
@@ -405,7 +440,7 @@ SrsJsonAny* srs_json_parse_tree_nx_json(const nx_json* node) @@ -405,7 +440,7 @@ SrsJsonAny* srs_json_parse_tree_nx_json(const nx_json* node)
405 case NX_JSON_STRING: 440 case NX_JSON_STRING:
406 return SrsJsonAny::str(node->text_value); 441 return SrsJsonAny::str(node->text_value);
407 case NX_JSON_INTEGER: 442 case NX_JSON_INTEGER:
408 - return SrsJsonAny::ingeter(node->int_value); 443 + return SrsJsonAny::integer(node->int_value);
409 case NX_JSON_DOUBLE: 444 case NX_JSON_DOUBLE:
410 return SrsJsonAny::number(node->dbl_value); 445 return SrsJsonAny::number(node->dbl_value);
411 case NX_JSON_BOOL: 446 case NX_JSON_BOOL:
@@ -513,6 +548,20 @@ string SrsJsonObject::to_json() @@ -513,6 +548,20 @@ string SrsJsonObject::to_json()
513 return ss.str(); 548 return ss.str();
514 } 549 }
515 550
  551 +SrsAmf0Any* SrsJsonObject::to_amf0()
  552 +{
  553 + SrsAmf0Object* obj = SrsAmf0Any::object();
  554 +
  555 + for (int i = 0; i < (int)properties.size(); i++) {
  556 + std::string name = this->key_at(i);
  557 + SrsJsonAny* any = this->value_at(i);
  558 +
  559 + obj->set(name, any->to_amf0());
  560 + }
  561 +
  562 + return obj;
  563 +}
  564 +
516 void SrsJsonObject::set(string key, SrsJsonAny* value) 565 void SrsJsonObject::set(string key, SrsJsonAny* value)
517 { 566 {
518 if (!value) { 567 if (!value) {
@@ -686,6 +735,19 @@ string SrsJsonArray::to_json() @@ -686,6 +735,19 @@ string SrsJsonArray::to_json()
686 return ss.str(); 735 return ss.str();
687 } 736 }
688 737
  738 +SrsAmf0Any* SrsJsonArray::to_amf0()
  739 +{
  740 + SrsAmf0StrictArray* arr = SrsAmf0Any::strict_array();
  741 +
  742 + for (int i = 0; i < (int)properties.size(); i++) {
  743 + SrsJsonAny* any = properties[i];
  744 +
  745 + arr->append(any->to_amf0());
  746 + }
  747 +
  748 + return arr;
  749 +}
  750 +
689 #ifdef SRS_JSON_USE_NXJSON 751 #ifdef SRS_JSON_USE_NXJSON
690 752
691 //////////////////////////////////////////////////////////////////////////////////////////////// 753 ////////////////////////////////////////////////////////////////////////////////////////////////
@@ -60,6 +60,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -60,6 +60,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60 // @see: https://bitbucket.org/yarosla/nxjson 60 // @see: https://bitbucket.org/yarosla/nxjson
61 // @see: https://github.com/udp/json-parser 61 // @see: https://github.com/udp/json-parser
62 62
  63 +class SrsAmf0Any;
63 class SrsJsonArray; 64 class SrsJsonArray;
64 class SrsJsonObject; 65 class SrsJsonObject;
65 66
@@ -112,16 +113,13 @@ public: @@ -112,16 +113,13 @@ public:
112 * user must ensure the type is a ecma array, or assert failed. 113 * user must ensure the type is a ecma array, or assert failed.
113 */ 114 */
114 virtual SrsJsonArray* to_array(); 115 virtual SrsJsonArray* to_array();
115 -// json  
116 public: 116 public:
117 - /**  
118 - * convert the amf0 stuff to json.  
119 - */  
120 virtual std::string to_json(); 117 virtual std::string to_json();
  118 + virtual SrsAmf0Any* to_amf0();
121 public: 119 public:
122 static SrsJsonAny* str(const char* value = NULL); 120 static SrsJsonAny* str(const char* value = NULL);
123 static SrsJsonAny* boolean(bool value = false); 121 static SrsJsonAny* boolean(bool value = false);
124 - static SrsJsonAny* ingeter(int64_t value = 0); 122 + static SrsJsonAny* integer(int64_t value = 0);
125 static SrsJsonAny* number(double value = 0.0); 123 static SrsJsonAny* number(double value = 0.0);
126 static SrsJsonAny* null(); 124 static SrsJsonAny* null();
127 static SrsJsonObject* object(); 125 static SrsJsonObject* object();
@@ -151,12 +149,9 @@ public: @@ -151,12 +149,9 @@ public:
151 virtual std::string key_at(int index); 149 virtual std::string key_at(int index);
152 // @remark: max index is count(). 150 // @remark: max index is count().
153 virtual SrsJsonAny* value_at(int index); 151 virtual SrsJsonAny* value_at(int index);
154 -// json  
155 public: 152 public:
156 - /**  
157 - * convert the amf0 object to json.  
158 - */  
159 virtual std::string to_json(); 153 virtual std::string to_json();
  154 + virtual SrsAmf0Any* to_amf0();
160 public: 155 public:
161 virtual void set(std::string key, SrsJsonAny* value); 156 virtual void set(std::string key, SrsJsonAny* value);
162 virtual SrsJsonAny* get_property(std::string name); 157 virtual SrsJsonAny* get_property(std::string name);
@@ -185,12 +180,9 @@ public: @@ -185,12 +180,9 @@ public:
185 virtual void add(SrsJsonAny* value); 180 virtual void add(SrsJsonAny* value);
186 // alias to add. 181 // alias to add.
187 virtual void append(SrsJsonAny* value); 182 virtual void append(SrsJsonAny* value);
188 -// json  
189 public: 183 public:
190 - /**  
191 - * convert the amf0 ecma array to json.  
192 - */  
193 virtual std::string to_json(); 184 virtual std::string to_json();
  185 + virtual SrsAmf0Any* to_amf0();
194 }; 186 };
195 187
196 //////////////////////////////////////////////////////////////////////// 188 ////////////////////////////////////////////////////////////////////////
@@ -282,6 +282,53 @@ char* SrsAmf0Any::human_print(char** pdata, int* psize) @@ -282,6 +282,53 @@ char* SrsAmf0Any::human_print(char** pdata, int* psize)
282 return data; 282 return data;
283 } 283 }
284 284
  285 +SrsJsonAny* SrsAmf0Any::to_json()
  286 +{
  287 + switch (marker) {
  288 + case RTMP_AMF0_String: {
  289 + return SrsJsonAny::str(to_str().c_str());
  290 + }
  291 + case RTMP_AMF0_Boolean: {
  292 + return SrsJsonAny::boolean(to_boolean());
  293 + }
  294 + case RTMP_AMF0_Number: {
  295 + double dv = to_number();
  296 + int64_t iv = (int64_t)dv;
  297 + if (iv == dv) {
  298 + return SrsJsonAny::integer(iv);
  299 + } else {
  300 + return SrsJsonAny::number(dv);
  301 + }
  302 + }
  303 + case RTMP_AMF0_Null: {
  304 + return SrsJsonAny::null();
  305 + }
  306 + case RTMP_AMF0_Undefined: {
  307 + return SrsJsonAny::null();
  308 + }
  309 + case RTMP_AMF0_Object: {
  310 + // amf0 object implements it.
  311 + srs_assert(false);
  312 + }
  313 + case RTMP_AMF0_EcmaArray: {
  314 + // amf0 ecma array implements it.
  315 + srs_assert(false);
  316 + }
  317 + case RTMP_AMF0_StrictArray: {
  318 + // amf0 strict array implements it.
  319 + srs_assert(false);
  320 + }
  321 + case RTMP_AMF0_Date: {
  322 + // TODO: FIXME: implements it.
  323 + return SrsJsonAny::null();
  324 + }
  325 + default: {
  326 + return SrsJsonAny::null();
  327 + }
  328 + }
  329 +
  330 +}
  331 +
285 SrsAmf0Any* SrsAmf0Any::str(const char* value) 332 SrsAmf0Any* SrsAmf0Any::str(const char* value)
286 { 333 {
287 return new SrsAmf0String(value); 334 return new SrsAmf0String(value);
@@ -743,6 +790,20 @@ SrsAmf0Any* SrsAmf0Object::copy() @@ -743,6 +790,20 @@ SrsAmf0Any* SrsAmf0Object::copy()
743 return copy; 790 return copy;
744 } 791 }
745 792
  793 +SrsJsonAny* SrsAmf0Object::to_json()
  794 +{
  795 + SrsJsonObject* obj = SrsJsonAny::object();
  796 +
  797 + for (int i = 0; i < properties->count(); i++) {
  798 + std::string name = this->key_at(i);
  799 + SrsAmf0Any* any = this->value_at(i);
  800 +
  801 + obj->set(name, any->to_json());
  802 + }
  803 +
  804 + return obj;
  805 +}
  806 +
746 void SrsAmf0Object::clear() 807 void SrsAmf0Object::clear()
747 { 808 {
748 properties->clear(); 809 properties->clear();
@@ -944,6 +1005,20 @@ SrsAmf0Any* SrsAmf0EcmaArray::copy() @@ -944,6 +1005,20 @@ SrsAmf0Any* SrsAmf0EcmaArray::copy()
944 return copy; 1005 return copy;
945 } 1006 }
946 1007
  1008 +SrsJsonAny* SrsAmf0EcmaArray::to_json()
  1009 +{
  1010 + SrsJsonObject* obj = SrsJsonAny::object();
  1011 +
  1012 + for (int i = 0; i < properties->count(); i++) {
  1013 + std::string name = this->key_at(i);
  1014 + SrsAmf0Any* any = this->value_at(i);
  1015 +
  1016 + obj->set(name, any->to_json());
  1017 + }
  1018 +
  1019 + return obj;
  1020 +}
  1021 +
947 void SrsAmf0EcmaArray::clear() 1022 void SrsAmf0EcmaArray::clear()
948 { 1023 {
949 properties->clear(); 1024 properties->clear();
@@ -1119,6 +1194,19 @@ SrsAmf0Any* SrsAmf0StrictArray::copy() @@ -1119,6 +1194,19 @@ SrsAmf0Any* SrsAmf0StrictArray::copy()
1119 return copy; 1194 return copy;
1120 } 1195 }
1121 1196
  1197 +SrsJsonAny* SrsAmf0StrictArray::to_json()
  1198 +{
  1199 + SrsJsonArray* arr = SrsJsonAny::array();
  1200 +
  1201 + for (int i = 0; i < (int)properties.size(); i++) {
  1202 + SrsAmf0Any* any = properties[i];
  1203 +
  1204 + arr->append(any->to_json());
  1205 + }
  1206 +
  1207 + return arr;
  1208 +}
  1209 +
1122 void SrsAmf0StrictArray::clear() 1210 void SrsAmf0StrictArray::clear()
1123 { 1211 {
1124 properties.clear(); 1212 properties.clear();
@@ -37,6 +37,7 @@ class SrsStream; @@ -37,6 +37,7 @@ class SrsStream;
37 class SrsAmf0Object; 37 class SrsAmf0Object;
38 class SrsAmf0EcmaArray; 38 class SrsAmf0EcmaArray;
39 class SrsAmf0StrictArray; 39 class SrsAmf0StrictArray;
  40 +class SrsJsonAny;
40 41
41 // internal objects, user should never use it. 42 // internal objects, user should never use it.
42 namespace _srs_internal 43 namespace _srs_internal
@@ -272,6 +273,10 @@ public: @@ -272,6 +273,10 @@ public:
272 * @remark user must free the data returned or output by pdata. 273 * @remark user must free the data returned or output by pdata.
273 */ 274 */
274 virtual char* human_print(char** pdata, int* psize); 275 virtual char* human_print(char** pdata, int* psize);
  276 + /**
  277 + * convert amf0 to json.
  278 + */
  279 + virtual SrsJsonAny* to_json();
275 // create AMF0 instance. 280 // create AMF0 instance.
276 public: 281 public:
277 /** 282 /**
@@ -351,6 +356,10 @@ public: @@ -351,6 +356,10 @@ public:
351 virtual int read(SrsStream* stream); 356 virtual int read(SrsStream* stream);
352 virtual int write(SrsStream* stream); 357 virtual int write(SrsStream* stream);
353 virtual SrsAmf0Any* copy(); 358 virtual SrsAmf0Any* copy();
  359 + /**
  360 + * convert amf0 to json.
  361 + */
  362 + virtual SrsJsonAny* to_json();
354 // properties iteration 363 // properties iteration
355 public: 364 public:
356 /** 365 /**
@@ -434,6 +443,10 @@ public: @@ -434,6 +443,10 @@ public:
434 virtual int read(SrsStream* stream); 443 virtual int read(SrsStream* stream);
435 virtual int write(SrsStream* stream); 444 virtual int write(SrsStream* stream);
436 virtual SrsAmf0Any* copy(); 445 virtual SrsAmf0Any* copy();
  446 + /**
  447 + * convert amf0 to json.
  448 + */
  449 + virtual SrsJsonAny* to_json();
437 // properties iteration 450 // properties iteration
438 public: 451 public:
439 /** 452 /**
@@ -515,6 +528,10 @@ public: @@ -515,6 +528,10 @@ public:
515 virtual int read(SrsStream* stream); 528 virtual int read(SrsStream* stream);
516 virtual int write(SrsStream* stream); 529 virtual int write(SrsStream* stream);
517 virtual SrsAmf0Any* copy(); 530 virtual SrsAmf0Any* copy();
  531 + /**
  532 + * convert amf0 to json.
  533 + */
  534 + virtual SrsJsonAny* to_json();
518 // properties iteration 535 // properties iteration
519 public: 536 public:
520 /** 537 /**