Blame view

trunk/src/rtmp/srs_protocol_amf0.cpp 45.3 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
24
#include <srs_protocol_amf0.hpp>
winlin authored
25 26

#include <utility>
27
#include <vector>
28
#include <sstream>
29
using namespace std;
winlin authored
30
31
#include <srs_kernel_log.hpp>
32
#include <srs_kernel_error.hpp>
winlin authored
33
#include <srs_kernel_stream.hpp>
winlin authored
34
35 36
using namespace _srs_internal;
winlin authored
37
// AMF0 marker
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#define RTMP_AMF0_Number                     0x00
#define RTMP_AMF0_Boolean                     0x01
#define RTMP_AMF0_String                     0x02
#define RTMP_AMF0_Object                     0x03
#define RTMP_AMF0_MovieClip                 0x04 // reserved, not supported
#define RTMP_AMF0_Null                         0x05
#define RTMP_AMF0_Undefined                 0x06
#define RTMP_AMF0_Reference                 0x07
#define RTMP_AMF0_EcmaArray                 0x08
#define RTMP_AMF0_ObjectEnd                 0x09
#define RTMP_AMF0_StrictArray                 0x0A
#define RTMP_AMF0_Date                         0x0B
#define RTMP_AMF0_LongString                 0x0C
#define RTMP_AMF0_UnSupported                 0x0D
#define RTMP_AMF0_RecordSet                 0x0E // reserved, not supported
#define RTMP_AMF0_XmlDocument                 0x0F
#define RTMP_AMF0_TypedObject                 0x10
winlin authored
55
// AVM+ object is the AMF3 object.
56
#define RTMP_AMF0_AVMplusObject             0x11
winlin authored
57
// origin array whos data takes the same form as LengthValueBytes
58
#define RTMP_AMF0_OriginStrictArray         0x20
winlin authored
59 60

// User defined
61
#define RTMP_AMF0_Invalid                     0x3F
winlin authored
62 63 64

SrsAmf0Any::SrsAmf0Any()
{
65
    marker = RTMP_AMF0_Invalid;
winlin authored
66 67 68 69 70 71 72 73
}

SrsAmf0Any::~SrsAmf0Any()
{
}

bool SrsAmf0Any::is_string()
{
74
    return marker == RTMP_AMF0_String;
winlin authored
75 76 77 78
}

bool SrsAmf0Any::is_boolean()
{
79
    return marker == RTMP_AMF0_Boolean;
winlin authored
80 81 82 83
}

bool SrsAmf0Any::is_number()
{
84
    return marker == RTMP_AMF0_Number;
winlin authored
85 86 87 88
}

bool SrsAmf0Any::is_null()
{
89
    return marker == RTMP_AMF0_Null;
winlin authored
90 91 92 93
}

bool SrsAmf0Any::is_undefined()
{
94
    return marker == RTMP_AMF0_Undefined;
winlin authored
95 96 97 98
}

bool SrsAmf0Any::is_object()
{
99
    return marker == RTMP_AMF0_Object;
winlin authored
100 101 102 103
}

bool SrsAmf0Any::is_ecma_array()
{
104
    return marker == RTMP_AMF0_EcmaArray;
winlin authored
105 106
}
107 108 109 110 111
bool SrsAmf0Any::is_strict_array()
{
    return marker == RTMP_AMF0_StrictArray;
}
112 113 114 115 116
bool SrsAmf0Any::is_date()
{
    return marker == RTMP_AMF0_Date;
}
117 118 119 120 121
bool SrsAmf0Any::is_complex_object()
{
    return is_object() || is_object_eof() || is_ecma_array() || is_strict_array();
}
winlin authored
122
string SrsAmf0Any::to_str()
123
{
124
    SrsAmf0String* p = dynamic_cast<SrsAmf0String*>(this);
125 126
    srs_assert(p != NULL);
    return p->value;
127 128
}
129 130
const char* SrsAmf0Any::to_str_raw()
{
131
    SrsAmf0String* p = dynamic_cast<SrsAmf0String*>(this);
132 133 134 135
    srs_assert(p != NULL);
    return p->value.data();
}
winlin authored
136 137
bool SrsAmf0Any::to_boolean()
{
138
    SrsAmf0Boolean* p = dynamic_cast<SrsAmf0Boolean*>(this);
139 140
    srs_assert(p != NULL);
    return p->value;
winlin authored
141 142
}
143 144
double SrsAmf0Any::to_number()
{
145
    SrsAmf0Number* p = dynamic_cast<SrsAmf0Number*>(this);
146 147
    srs_assert(p != NULL);
    return p->value;
148 149
}
150 151 152 153 154 155 156 157 158 159 160 161 162 163
int64_t SrsAmf0Any::to_date()
{
    SrsAmf0Date* p = dynamic_cast<SrsAmf0Date*>(this);
    srs_assert(p != NULL);
    return p->date();
}

int16_t SrsAmf0Any::to_date_time_zone()
{
    SrsAmf0Date* p = dynamic_cast<SrsAmf0Date*>(this);
    srs_assert(p != NULL);
    return p->time_zone();
}
164 165
SrsAmf0Object* SrsAmf0Any::to_object()
{
166 167 168
    SrsAmf0Object* p = dynamic_cast<SrsAmf0Object*>(this);
    srs_assert(p != NULL);
    return p;
169 170
}
171
SrsAmf0EcmaArray* SrsAmf0Any::to_ecma_array()
172
{
173 174 175
    SrsAmf0EcmaArray* p = dynamic_cast<SrsAmf0EcmaArray*>(this);
    srs_assert(p != NULL);
    return p;
176 177
}
178 179 180 181 182 183 184
SrsAmf0StrictArray* SrsAmf0Any::to_strict_array()
{
    SrsAmf0StrictArray* p = dynamic_cast<SrsAmf0StrictArray*>(this);
    srs_assert(p != NULL);
    return p;
}
185 186
void SrsAmf0Any::set_number(double value)
{
187
    SrsAmf0Number* p = dynamic_cast<SrsAmf0Number*>(this);
188 189 190 191
    srs_assert(p != NULL);
    p->value = value;
}
winlin authored
192 193
bool SrsAmf0Any::is_object_eof()
{
194
    return marker == RTMP_AMF0_ObjectEnd;
winlin authored
195 196
}
197 198 199 200 201 202 203 204 205 206 207 208 209 210
void __srs_fill_level_spaces(stringstream& ss, int level)
{
    for (int i = 0; i < level; i++) {
        ss << "    ";
    }
}
void __srs_amf0_do_print(SrsAmf0Any* any, stringstream& ss, int level)
{
    if (any->is_boolean()) {
        ss << "Boolean " << (any->to_boolean()? "true":"false") << endl;
    } else if (any->is_number()) {
        ss << "Number " << std::fixed << any->to_number() << endl;
    } else if (any->is_string()) {
        ss << "String " << any->to_str() << endl;
211 212 213
    } else if (any->is_date()) {
        ss << "Date " << std::hex << any->to_date() 
            << "/" << std::hex << any->to_date_time_zone() << endl;
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    } else if (any->is_null()) {
        ss << "Null" << endl;
    } else if (any->is_ecma_array()) {
        SrsAmf0EcmaArray* obj = any->to_ecma_array();
        ss << "EcmaArray " << "(" << obj->count() << " items)" << endl;
        for (int i = 0; i < obj->count(); i++) {
            __srs_fill_level_spaces(ss, level + 1);
            ss << "Elem '" << obj->key_at(i) << "' ";
            if (obj->value_at(i)->is_complex_object()) {
                __srs_amf0_do_print(obj->value_at(i), ss, level + 1);
            } else {
                __srs_amf0_do_print(obj->value_at(i), ss, 0);
            }
        }
    } else if (any->is_strict_array()) {
        SrsAmf0StrictArray* obj = any->to_strict_array();
        ss << "StrictArray " << "(" << obj->count() << " items)" << endl;
        for (int i = 0; i < obj->count(); i++) {
            __srs_fill_level_spaces(ss, level + 1);
            ss << "Elem ";
            if (obj->at(i)->is_complex_object()) {
                __srs_amf0_do_print(obj->at(i), ss, level + 1);
            } else {
                __srs_amf0_do_print(obj->at(i), ss, 0);
            }
        }
    } else if (any->is_object()) {
        SrsAmf0Object* obj = any->to_object();
        ss << "Object " << "(" << obj->count() << " items)" << endl;
        for (int i = 0; i < obj->count(); i++) {
            __srs_fill_level_spaces(ss, level + 1);
            ss << "Property '" << obj->key_at(i) << "' ";
            if (obj->value_at(i)->is_complex_object()) {
                __srs_amf0_do_print(obj->value_at(i), ss, level + 1);
            } else {
                __srs_amf0_do_print(obj->value_at(i), ss, 0);
            }
        }
    } else {
        ss << "Unknown" << endl;
    }
}

char* SrsAmf0Any::human_print(char** pdata, int* psize)
{
    stringstream ss;
    
    ss.precision(1);
    
    __srs_amf0_do_print(this, ss, 0);
    
    string str = ss.str();
    if (str.empty()) {
        return NULL;
    }
    
    char* data = new char[str.length() + 1];
    memcpy(data, str.data(), str.length());
    data[str.length()] = 0;
    
    if (pdata) {
        *pdata = data;
    }
    if (psize) {
        *psize = str.length();
    }
    
    return data;
}
284 285
SrsAmf0Any* SrsAmf0Any::str(const char* value)
{
286
    return new SrsAmf0String(value);
287 288
}
winlin authored
289
SrsAmf0Any* SrsAmf0Any::boolean(bool value)
winlin authored
290
{
291
    return new SrsAmf0Boolean(value);
292 293
}
294
SrsAmf0Any* SrsAmf0Any::number(double value)
295
{
296
    return new SrsAmf0Number(value);
297 298
}
299
SrsAmf0Any* SrsAmf0Any::null()
winlin authored
300
{
301
    return new SrsAmf0Null();
winlin authored
302 303
}
304
SrsAmf0Any* SrsAmf0Any::undefined()
winlin authored
305
{
306
    return new SrsAmf0Undefined();
winlin authored
307 308
}
309 310
SrsAmf0Object* SrsAmf0Any::object()
{
311
    return new SrsAmf0Object();
312 313
}
314 315
SrsAmf0Any* SrsAmf0Any::object_eof()
{
316
    return new SrsAmf0ObjectEOF();
317 318
}
319
SrsAmf0EcmaArray* SrsAmf0Any::ecma_array()
320
{
321
    return new SrsAmf0EcmaArray();
322 323
}
324 325 326 327 328
SrsAmf0StrictArray* SrsAmf0Any::strict_array()
{
    return new SrsAmf0StrictArray();
}
329 330 331 332 333
SrsAmf0Any* SrsAmf0Any::date(int64_t value)
{
    return new SrsAmf0Date(value);
}
334 335
int SrsAmf0Any::discovery(SrsStream* stream, SrsAmf0Any** ppvalue)
{
336 337 338 339
    int ret = ERROR_SUCCESS;
    
    // detect the object-eof specially
    if (srs_amf0_is_object_eof(stream)) {
340
        *ppvalue = new SrsAmf0ObjectEOF();
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
        return ret;
    }
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read any marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    srs_verbose("amf0 any marker success");
    
    // backward the 1byte marker.
    stream->skip(-1);
    
    switch (marker) {
        case RTMP_AMF0_String: {
            *ppvalue = SrsAmf0Any::str();
            return ret;
        }
        case RTMP_AMF0_Boolean: {
            *ppvalue = SrsAmf0Any::boolean();
            return ret;
        }
        case RTMP_AMF0_Number: {
            *ppvalue = SrsAmf0Any::number();
            return ret;
        }
        case RTMP_AMF0_Null: {
            *ppvalue = SrsAmf0Any::null();
            return ret;
        }
        case RTMP_AMF0_Undefined: {
            *ppvalue = SrsAmf0Any::undefined();
            return ret;
        }
        case RTMP_AMF0_Object: {
            *ppvalue = SrsAmf0Any::object();
            return ret;
        }
        case RTMP_AMF0_EcmaArray: {
            *ppvalue = SrsAmf0Any::ecma_array();
            return ret;
        }
386 387 388 389
        case RTMP_AMF0_StrictArray: {
            *ppvalue = SrsAmf0Any::strict_array();
            return ret;
        }
390 391 392 393
        case RTMP_AMF0_Date: {
            *ppvalue = SrsAmf0Any::date();
            return ret;
        }
394 395 396 397 398 399 400
        case RTMP_AMF0_Invalid:
        default: {
            ret = ERROR_RTMP_AMF0_INVALID;
            srs_error("invalid amf0 message type. marker=%#x, ret=%d", marker, ret);
            return ret;
        }
    }
401 402
}
403
SrsUnSortedHashtable::SrsUnSortedHashtable()
404 405 406
{
}
407
SrsUnSortedHashtable::~SrsUnSortedHashtable()
winlin authored
408
{
409
    clear();
winlin authored
410 411
}
412
int SrsUnSortedHashtable::count()
winlin authored
413
{
414
    return (int)properties.size();
winlin authored
415 416
}
417
void SrsUnSortedHashtable::clear()
winlin authored
418
{
419 420 421 422 423 424
    std::vector<SrsAmf0ObjectPropertyType>::iterator it;
    for (it = properties.begin(); it != properties.end(); ++it) {
        SrsAmf0ObjectPropertyType& elem = *it;
        SrsAmf0Any* any = elem.second;
        srs_freep(any);
    }
425
    properties.clear();
winlin authored
426 427
}
428
string SrsUnSortedHashtable::key_at(int index)
winlin authored
429
{
430
    srs_assert(index < count());
431
    SrsAmf0ObjectPropertyType& elem = properties[index];
432
    return elem.first;
winlin authored
433 434
}
435
const char* SrsUnSortedHashtable::key_raw_at(int index)
436 437 438 439 440 441
{
    srs_assert(index < count());
    SrsAmf0ObjectPropertyType& elem = properties[index];
    return elem.first.data();
}
442
SrsAmf0Any* SrsUnSortedHashtable::value_at(int index)
winlin authored
443
{
444
    srs_assert(index < count());
445
    SrsAmf0ObjectPropertyType& elem = properties[index];
446
    return elem.second;
winlin authored
447 448
}
449
void SrsUnSortedHashtable::set(string key, SrsAmf0Any* value)
winlin authored
450
{
451 452 453 454 455
    if (!value) {
        srs_warn("add a NULL propertity %s", key.c_str());
        return;
    }
    
456
    std::vector<SrsAmf0ObjectPropertyType>::iterator it;
457 458
    
    for (it = properties.begin(); it != properties.end(); ++it) {
459
        SrsAmf0ObjectPropertyType& elem = *it;
460 461 462 463 464 465 466 467 468 469 470
        std::string name = elem.first;
        SrsAmf0Any* any = elem.second;
        
        if (key == name) {
            srs_freep(any);
            properties.erase(it);
            break;
        }
    }
    
    properties.push_back(std::make_pair(key, value));
winlin authored
471 472
}
473
SrsAmf0Any* SrsUnSortedHashtable::get_property(string name)
winlin authored
474
{
475
    std::vector<SrsAmf0ObjectPropertyType>::iterator it;
476 477
    
    for (it = properties.begin(); it != properties.end(); ++it) {
478
        SrsAmf0ObjectPropertyType& elem = *it;
479 480 481 482 483 484 485 486
        std::string key = elem.first;
        SrsAmf0Any* any = elem.second;
        if (key == name) {
            return any;
        }
    }
    
    return NULL;
winlin authored
487 488
}
489
SrsAmf0Any* SrsUnSortedHashtable::ensure_property_string(string name)
winlin authored
490
{
491 492 493 494 495 496 497 498 499 500 501
    SrsAmf0Any* prop = get_property(name);
    
    if (!prop) {
        return NULL;
    }
    
    if (!prop->is_string()) {
        return NULL;
    }
    
    return prop;
winlin authored
502 503
}
504
SrsAmf0Any* SrsUnSortedHashtable::ensure_property_number(string name)
winlin authored
505
{
506 507 508 509 510 511 512 513 514 515 516
    SrsAmf0Any* prop = get_property(name);
    
    if (!prop) {
        return NULL;
    }
    
    if (!prop->is_number()) {
        return NULL;
    }
    
    return prop;
winlin authored
517 518
}
519
void SrsUnSortedHashtable::copy(SrsUnSortedHashtable* src)
520 521 522 523 524 525 526 527 528 529
{
    std::vector<SrsAmf0ObjectPropertyType>::iterator it;
    for (it = src->properties.begin(); it != src->properties.end(); ++it) {
        SrsAmf0ObjectPropertyType& elem = *it;
        std::string key = elem.first;
        SrsAmf0Any* any = elem.second;
        set(key, any->copy());
    }
}
530
SrsAmf0ObjectEOF::SrsAmf0ObjectEOF()
531
{
532
    marker = RTMP_AMF0_ObjectEnd;
533 534
}
535
SrsAmf0ObjectEOF::~SrsAmf0ObjectEOF()
536 537 538
{
}
539
int SrsAmf0ObjectEOF::total_size()
540
{
541
    return SrsAmf0Size::object_eof();
542 543
}
544
int SrsAmf0ObjectEOF::read(SrsStream* stream)
545
{
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    int ret = ERROR_SUCCESS;
    
    // value
    if (!stream->require(2)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read object eof value failed. ret=%d", ret);
        return ret;
    }
    int16_t temp = stream->read_2bytes();
    if (temp != 0x00) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read object eof value check failed. "
            "must be 0x00, actual is %#x, ret=%d", temp, ret);
        return ret;
    }
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read object eof marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_ObjectEnd) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check object eof marker failed. "
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_ObjectEnd, ret);
        return ret;
    }
    srs_verbose("amf0 read object eof marker success");
    
    srs_verbose("amf0 read object eof success");
    
    return ret;
581
}
582
int SrsAmf0ObjectEOF::write(SrsStream* stream)
583
{
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    int ret = ERROR_SUCCESS;
    
    // value
    if (!stream->require(2)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write object eof value failed. ret=%d", ret);
        return ret;
    }
    stream->write_2bytes(0x00);
    srs_verbose("amf0 write object eof value success");
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write object eof marker failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_1bytes(RTMP_AMF0_ObjectEnd);
    
    srs_verbose("amf0 read object eof success");
    
    return ret;
607 608
}
609
SrsAmf0Any* SrsAmf0ObjectEOF::copy()
610
{
611
    return new SrsAmf0ObjectEOF();
612 613
}
winlin authored
614 615
SrsAmf0Object::SrsAmf0Object()
{
616 617
    properties = new SrsUnSortedHashtable();
    eof = new SrsAmf0ObjectEOF();
618
    marker = RTMP_AMF0_Object;
winlin authored
619 620 621 622
}

SrsAmf0Object::~SrsAmf0Object()
{
623 624
    srs_freep(properties);
    srs_freep(eof);
winlin authored
625 626
}
627
int SrsAmf0Object::total_size()
628
{
629 630 631 632 633 634 635 636 637 638 639 640 641
    int size = 1;
    
    for (int i = 0; i < properties->count(); 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;
642 643
}
644 645
int SrsAmf0Object::read(SrsStream* stream)
{
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read object marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_Object) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check object marker failed. "
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_Object, ret);
        return ret;
    }
    srs_verbose("amf0 read object marker success");
    
    // value
    while (!stream->empty()) {
        // detect whether is eof.
        if (srs_amf0_is_object_eof(stream)) {
668
            SrsAmf0ObjectEOF pbj_eof;
669 670 671 672 673 674 675 676 677 678
            if ((ret = pbj_eof.read(stream)) != ERROR_SUCCESS) {
                srs_error("amf0 object read eof failed. ret=%d", ret);
                return ret;
            }
            srs_info("amf0 read object EOF.");
            break;
        }
        
        // property-name: utf8 string
        std::string property_name;
679
        if ((ret = srs_amf0_read_utf8(stream, property_name)) != ERROR_SUCCESS) {
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
            srs_error("amf0 object read property name failed. ret=%d", ret);
            return ret;
        }
        // property-value: any
        SrsAmf0Any* property_value = NULL;
        if ((ret = srs_amf0_read_any(stream, &property_value)) != ERROR_SUCCESS) {
            srs_error("amf0 object read property_value failed. "
                "name=%s, ret=%d", property_name.c_str(), ret);
            srs_freep(property_value);
            return ret;
        }
        
        // add property
        this->set(property_name, property_value);
    }
    
    return ret;
697 698 699 700
}

int SrsAmf0Object::write(SrsStream* stream)
{
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write object marker failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_1bytes(RTMP_AMF0_Object);
    srs_verbose("amf0 write object marker success");
    
    // value
    for (int i = 0; i < properties->count(); i++) {
        std::string name = this->key_at(i);
        SrsAmf0Any* any = this->value_at(i);
        
        if ((ret = srs_amf0_write_utf8(stream, name)) != ERROR_SUCCESS) {
            srs_error("write object property name failed. ret=%d", ret);
            return ret;
        }
        
        if ((ret = srs_amf0_write_any(stream, any)) != ERROR_SUCCESS) {
            srs_error("write object property value failed. ret=%d", ret);
            return ret;
        }
        
        srs_verbose("write amf0 property success. name=%s", name.c_str());
    }
    
    if ((ret = eof->write(stream)) != ERROR_SUCCESS) {
        srs_error("write object eof failed. ret=%d", ret);
        return ret;
    }
    
    srs_verbose("write amf0 object success.");
    
    return ret;
739 740
}
741 742 743 744 745 746 747
SrsAmf0Any* SrsAmf0Object::copy()
{
    SrsAmf0Object* copy = new SrsAmf0Object();
    copy->properties->copy(properties);
    return copy;
}
winlin authored
748 749 750 751 752
void SrsAmf0Object::clear()
{
    properties->clear();
}
753
int SrsAmf0Object::count()
winlin authored
754
{
755
    return properties->count();
winlin authored
756 757
}
758
string SrsAmf0Object::key_at(int index)
winlin authored
759
{
760
    return properties->key_at(index);
winlin authored
761 762
}
763 764 765 766 767
const char* SrsAmf0Object::key_raw_at(int index)
{
    return properties->key_raw_at(index);
}
winlin authored
768 769
SrsAmf0Any* SrsAmf0Object::value_at(int index)
{
770
    return properties->value_at(index);
winlin authored
771 772
}
773
void SrsAmf0Object::set(string key, SrsAmf0Any* value)
winlin authored
774
{
775
    properties->set(key, value);
winlin authored
776 777
}
778
SrsAmf0Any* SrsAmf0Object::get_property(string name)
winlin authored
779
{
780
    return properties->get_property(name);
winlin authored
781 782
}
783
SrsAmf0Any* SrsAmf0Object::ensure_property_string(string name)
winlin authored
784
{
785
    return properties->ensure_property_string(name);
winlin authored
786 787
}
788
SrsAmf0Any* SrsAmf0Object::ensure_property_number(string name)
winlin authored
789
{
790
    return properties->ensure_property_number(name);
winlin authored
791 792
}
793
SrsAmf0EcmaArray::SrsAmf0EcmaArray()
winlin authored
794
{
795
    _count = 0;
796 797
    properties = new SrsUnSortedHashtable();
    eof = new SrsAmf0ObjectEOF();
798
    marker = RTMP_AMF0_EcmaArray;
winlin authored
799 800
}
801 802
SrsAmf0EcmaArray::~SrsAmf0EcmaArray()
{
803 804
    srs_freep(properties);
    srs_freep(eof);
805 806
}
807
int SrsAmf0EcmaArray::total_size()
808
{
809 810 811 812 813 814 815 816 817 818 819 820 821
    int size = 1 + 4;
    
    for (int i = 0; i < properties->count(); 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;
822 823
}
824
int SrsAmf0EcmaArray::read(SrsStream* stream)
winlin authored
825
{
826 827 828 829 830 831 832 833 834 835 836 837 838
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read ecma_array marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_EcmaArray) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check ecma_array marker failed. "
839
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_EcmaArray, ret);
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
        return ret;
    }
    srs_verbose("amf0 read ecma_array marker success");

    // count
    if (!stream->require(4)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read ecma_array count failed. ret=%d", ret);
        return ret;
    }
    
    int32_t count = stream->read_4bytes();
    srs_verbose("amf0 read ecma_array count success. count=%d", count);
    
    // value
    this->_count = count;

    while (!stream->empty()) {
        // detect whether is eof.
        if (srs_amf0_is_object_eof(stream)) {
860
            SrsAmf0ObjectEOF pbj_eof;
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
            if ((ret = pbj_eof.read(stream)) != ERROR_SUCCESS) {
                srs_error("amf0 ecma_array read eof failed. ret=%d", ret);
                return ret;
            }
            srs_info("amf0 read ecma_array EOF.");
            break;
        }
        
        // property-name: utf8 string
        std::string property_name;
        if ((ret =srs_amf0_read_utf8(stream, property_name)) != ERROR_SUCCESS) {
            srs_error("amf0 ecma_array read property name failed. ret=%d", ret);
            return ret;
        }
        // property-value: any
        SrsAmf0Any* property_value = NULL;
        if ((ret = srs_amf0_read_any(stream, &property_value)) != ERROR_SUCCESS) {
            srs_error("amf0 ecma_array read property_value failed. "
                "name=%s, ret=%d", property_name.c_str(), ret);
            return ret;
        }
        
        // add property
        this->set(property_name, property_value);
    }
    
    return ret;
winlin authored
888
}
889 890
int SrsAmf0EcmaArray::write(SrsStream* stream)
{
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write ecma_array marker failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_1bytes(RTMP_AMF0_EcmaArray);
    srs_verbose("amf0 write ecma_array marker success");

    // count
    if (!stream->require(4)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write ecma_array count failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_4bytes(this->_count);
    srs_verbose("amf0 write ecma_array count success. count=%d", _count);
    
    // value
    for (int i = 0; i < properties->count(); i++) {
        std::string name = this->key_at(i);
        SrsAmf0Any* any = this->value_at(i);
        
        if ((ret = srs_amf0_write_utf8(stream, name)) != ERROR_SUCCESS) {
            srs_error("write ecma_array property name failed. ret=%d", ret);
            return ret;
        }
        
        if ((ret = srs_amf0_write_any(stream, any)) != ERROR_SUCCESS) {
            srs_error("write ecma_array property value failed. ret=%d", ret);
            return ret;
        }
        
        srs_verbose("write amf0 property success. name=%s", name.c_str());
    }
    
    if ((ret = eof->write(stream)) != ERROR_SUCCESS) {
        srs_error("write ecma_array eof failed. ret=%d", ret);
        return ret;
    }
    
    srs_verbose("write ecma_array object success.");
    
    return ret;
939 940
}
941 942 943 944 945 946 947 948
SrsAmf0Any* SrsAmf0EcmaArray::copy()
{
    SrsAmf0EcmaArray* copy = new SrsAmf0EcmaArray();
    copy->properties->copy(properties);
    copy->_count = _count;
    return copy;
}
949
void SrsAmf0EcmaArray::clear()
winlin authored
950
{
951
    properties->clear();
winlin authored
952 953
}
954
int SrsAmf0EcmaArray::count()
winlin authored
955
{
956
    return properties->count();
winlin authored
957 958
}
959
string SrsAmf0EcmaArray::key_at(int index)
winlin authored
960
{
961
    return properties->key_at(index);
winlin authored
962 963
}
964 965 966 967 968
const char* SrsAmf0EcmaArray::key_raw_at(int index)
{
    return properties->key_raw_at(index);
}
969
SrsAmf0Any* SrsAmf0EcmaArray::value_at(int index)
winlin authored
970
{
971
    return properties->value_at(index);
winlin authored
972 973
}
974
void SrsAmf0EcmaArray::set(string key, SrsAmf0Any* value)
winlin authored
975
{
976
    properties->set(key, value);
winlin authored
977 978
}
979
SrsAmf0Any* SrsAmf0EcmaArray::get_property(string name)
winlin authored
980
{
981
    return properties->get_property(name);
winlin authored
982 983
}
984
SrsAmf0Any* SrsAmf0EcmaArray::ensure_property_string(string name)
winlin authored
985
{
986
    return properties->ensure_property_string(name);
winlin authored
987 988
}
989 990
SrsAmf0Any* SrsAmf0EcmaArray::ensure_property_number(string name)
{
991
    return properties->ensure_property_number(name);
992 993
}
994 995 996
SrsAmf0StrictArray::SrsAmf0StrictArray()
{
    marker = RTMP_AMF0_StrictArray;
997
    _count = 0;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
}

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. "
1037
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_StrictArray, ret);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
        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;
        }
        
1102
        srs_verbose("write amf0 property success.");
1103 1104 1105 1106 1107 1108 1109
    }
    
    srs_verbose("write strict_array object success.");
    
    return ret;
}
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
SrsAmf0Any* SrsAmf0StrictArray::copy()
{
    SrsAmf0StrictArray* copy = new SrsAmf0StrictArray();
    
    std::vector<SrsAmf0Any*>::iterator it;
    for (it = properties.begin(); it != properties.end(); ++it) {
        SrsAmf0Any* any = *it;
        copy->append(any->copy());
    }
    
    copy->_count = _count;
    return copy;
}
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
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);
}
winlin authored
1140 1141 1142
void SrsAmf0StrictArray::append(SrsAmf0Any* any)
{
    properties.push_back(any);
1143
    _count = (int32_t)properties.size();
winlin authored
1144 1145
}
1146 1147
int SrsAmf0Size::utf8(string value)
{
1148
    return 2 + value.length();
1149 1150 1151 1152
}

int SrsAmf0Size::str(string value)
{
1153
    return 1 + SrsAmf0Size::utf8(value);
1154 1155 1156 1157
}

int SrsAmf0Size::number()
{
1158
    return 1 + 8;
1159 1160
}
1161 1162 1163 1164 1165
int SrsAmf0Size::date()
{
    return 1 + 8 + 2;
}
1166 1167
int SrsAmf0Size::null()
{
1168
    return 1;
1169 1170 1171 1172
}

int SrsAmf0Size::undefined()
{
1173
    return 1;
1174 1175 1176 1177
}

int SrsAmf0Size::boolean()
{
1178
    return 1 + 1;
1179 1180 1181 1182
}

int SrsAmf0Size::object(SrsAmf0Object* obj)
{
1183 1184 1185 1186 1187
    if (!obj) {
        return 0;
    }
    
    return obj->total_size();
1188 1189 1190 1191
}

int SrsAmf0Size::object_eof()
{
1192
    return 2 + 1;
1193 1194
}
1195
int SrsAmf0Size::ecma_array(SrsAmf0EcmaArray* arr)
1196
{
1197 1198 1199 1200 1201
    if (!arr) {
        return 0;
    }
    
    return arr->total_size();
1202 1203
}
1204 1205 1206 1207 1208 1209 1210 1211 1212
int SrsAmf0Size::strict_array(SrsAmf0StrictArray* arr)
{
    if (!arr) {
        return 0;
    }
    
    return arr->total_size();
}
1213 1214
int SrsAmf0Size::any(SrsAmf0Any* o)
{
1215 1216 1217 1218 1219
    if (!o) {
        return 0;
    }
    
    return o->total_size();
1220 1221
}
1222
SrsAmf0String::SrsAmf0String(const char* _value)
winlin authored
1223
{
1224 1225 1226 1227
    marker = RTMP_AMF0_String;
    if (_value) {
        value = _value;
    }
winlin authored
1228 1229
}
1230
SrsAmf0String::~SrsAmf0String()
winlin authored
1231 1232 1233
{
}
1234
int SrsAmf0String::total_size()
winlin authored
1235
{
1236
    return SrsAmf0Size::str(value);
winlin authored
1237 1238
}
1239
int SrsAmf0String::read(SrsStream* stream)
1240
{
1241
    return srs_amf0_read_string(stream, value);
1242 1243
}
1244
int SrsAmf0String::write(SrsStream* stream)
1245
{
1246
    return srs_amf0_write_string(stream, value);
1247 1248
}
1249
SrsAmf0Any* SrsAmf0String::copy()
1250
{
1251
    SrsAmf0String* copy = new SrsAmf0String(value.c_str());
1252 1253 1254
    return copy;
}
1255
SrsAmf0Boolean::SrsAmf0Boolean(bool _value)
winlin authored
1256
{
1257 1258
    marker = RTMP_AMF0_Boolean;
    value = _value;
winlin authored
1259 1260
}
1261
SrsAmf0Boolean::~SrsAmf0Boolean()
winlin authored
1262 1263 1264
{
}
1265
int SrsAmf0Boolean::total_size()
winlin authored
1266
{
1267
    return SrsAmf0Size::boolean();
winlin authored
1268 1269
}
1270
int SrsAmf0Boolean::read(SrsStream* stream)
1271
{
1272
    return srs_amf0_read_boolean(stream, value);
1273 1274
}
1275
int SrsAmf0Boolean::write(SrsStream* stream)
1276
{
1277
    return srs_amf0_write_boolean(stream, value);
1278 1279
}
1280
SrsAmf0Any* SrsAmf0Boolean::copy()
1281
{
1282
    SrsAmf0Boolean* copy = new SrsAmf0Boolean(value);
1283 1284 1285
    return copy;
}
1286
SrsAmf0Number::SrsAmf0Number(double _value)
1287
{
1288 1289
    marker = RTMP_AMF0_Number;
    value = _value;
1290 1291
}
1292
SrsAmf0Number::~SrsAmf0Number()
1293 1294 1295
{
}
1296
int SrsAmf0Number::total_size()
1297
{
1298
    return SrsAmf0Size::number();
1299 1300
}
1301
int SrsAmf0Number::read(SrsStream* stream)
1302
{
1303
    return srs_amf0_read_number(stream, value);
1304 1305
}
1306
int SrsAmf0Number::write(SrsStream* stream)
1307
{
1308
    return srs_amf0_write_number(stream, value);
1309 1310
}
1311
SrsAmf0Any* SrsAmf0Number::copy()
1312
{
1313
    SrsAmf0Number* copy = new SrsAmf0Number(value);
1314 1315 1316
    return copy;
}
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
SrsAmf0Date::SrsAmf0Date(int64_t value)
{
    marker = RTMP_AMF0_Date;
    _date_value = value;
    _time_zone = 0;
}

SrsAmf0Date::~SrsAmf0Date()
{
}

int SrsAmf0Date::total_size()
{
    return SrsAmf0Size::date();
}

int SrsAmf0Date::read(SrsStream* stream)
{
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read date marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_Date) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check date marker failed. "
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_Date, ret);
        return ret;
    }
    srs_verbose("amf0 read date marker success");

    // date value
    // An ActionScript Date is serialized as the number of milliseconds 
    // elapsed since the epoch of midnight on 1st Jan 1970 in the UTC 
    // time zone.
    if (!stream->require(8)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read date failed. ret=%d", ret);
        return ret;
    }
    
    _date_value = stream->read_8bytes();
    srs_verbose("amf0 read date success. date=%"PRId64, _date_value);
    
    // time zone
    // While the design of this type reserves room for time zone offset 
    // information, it should not be filled in, nor used, as it is unconventional 
    // to change time zones when serializing dates on a network. It is suggested 
    // that the time zone be queried independently as needed.
    if (!stream->require(2)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read time zone failed. ret=%d", ret);
        return ret;
    }
    
    _time_zone = stream->read_2bytes();
    srs_verbose("amf0 read time zone success. zone=%d", _time_zone);
    
    return ret;
}
int SrsAmf0Date::write(SrsStream* stream)
{
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write date marker failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_1bytes(RTMP_AMF0_Date);
    srs_verbose("amf0 write date marker success");

    // date value
    if (!stream->require(8)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write date failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_8bytes(_date_value);
    srs_verbose("amf0 write date success. date=%"PRId64, _date_value);

    // time zone
    if (!stream->require(2)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write time zone failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_2bytes(_time_zone);
    srs_verbose("amf0 write time zone success. date=%d", _time_zone);
    
    srs_verbose("write date object success.");
    
    return ret;
}

SrsAmf0Any* SrsAmf0Date::copy()
{
    SrsAmf0Date* copy = new SrsAmf0Date(0);
    
    copy->_date_value = _date_value;
    copy->_time_zone = _time_zone;
    
    return copy;
}

int64_t SrsAmf0Date::date()
{
    return _date_value;
}

int16_t SrsAmf0Date::time_zone()
{
    return _time_zone;
}
1441
SrsAmf0Null::SrsAmf0Null()
1442
{
1443
    marker = RTMP_AMF0_Null;
1444 1445
}
1446
SrsAmf0Null::~SrsAmf0Null()
1447 1448 1449
{
}
1450
int SrsAmf0Null::total_size()
1451
{
1452
    return SrsAmf0Size::null();
1453 1454
}
1455
int SrsAmf0Null::read(SrsStream* stream)
1456
{
1457
    return srs_amf0_read_null(stream);
1458 1459
}
1460
int SrsAmf0Null::write(SrsStream* stream)
1461
{
1462
    return srs_amf0_write_null(stream);
1463 1464
}
1465
SrsAmf0Any* SrsAmf0Null::copy()
1466
{
1467
    SrsAmf0Null* copy = new SrsAmf0Null();
1468 1469 1470
    return copy;
}
1471
SrsAmf0Undefined::SrsAmf0Undefined()
1472
{
1473
    marker = RTMP_AMF0_Undefined;
1474 1475
}
1476
SrsAmf0Undefined::~SrsAmf0Undefined()
1477 1478 1479
{
}
1480
int SrsAmf0Undefined::total_size()
1481
{
1482
    return SrsAmf0Size::undefined();
1483 1484
}
1485
int SrsAmf0Undefined::read(SrsStream* stream)
1486
{
1487
    return srs_amf0_read_undefined(stream);
1488 1489
}
1490
int SrsAmf0Undefined::write(SrsStream* stream)
1491
{
1492
    return srs_amf0_write_undefined(stream);
1493 1494
}
1495
SrsAmf0Any* SrsAmf0Undefined::copy()
1496
{
1497
    SrsAmf0Undefined* copy = new SrsAmf0Undefined();
1498 1499 1500
    return copy;
}
1501 1502
int srs_amf0_read_any(SrsStream* stream, SrsAmf0Any** ppvalue)
{
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
    int ret = ERROR_SUCCESS;
    
    if ((ret = SrsAmf0Any::discovery(stream, ppvalue)) != ERROR_SUCCESS) {
        srs_error("amf0 discovery any elem failed. ret=%d", ret);
        return ret;
    }
    
    srs_assert(*ppvalue);
    
    if ((ret = (*ppvalue)->read(stream)) != ERROR_SUCCESS) {
        srs_error("amf0 parse elem failed. ret=%d", ret);
        srs_freep(*ppvalue);
        return ret;
    }
    
    return ret;
1519 1520
}
1521
int srs_amf0_read_string(SrsStream* stream, string& value)
winlin authored
1522
{
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read string marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_String) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check string marker failed. "
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_String, ret);
        return ret;
    }
    srs_verbose("amf0 read string marker success");
    
    return srs_amf0_read_utf8(stream, value);
winlin authored
1542 1543
}
1544
int srs_amf0_write_string(SrsStream* stream, string value)
winlin authored
1545
{
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write string marker failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_1bytes(RTMP_AMF0_String);
    srs_verbose("amf0 write string marker success");
    
    return srs_amf0_write_utf8(stream, value);
winlin authored
1559 1560 1561 1562
}

int srs_amf0_read_boolean(SrsStream* stream, bool& value)
{
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read bool marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_Boolean) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check bool marker failed. "
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_Boolean, ret);
        return ret;
    }
    srs_verbose("amf0 read bool marker success");

    // value
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read bool value failed. ret=%d", ret);
        return ret;
    }
1588
    value = (stream->read_1bytes() != 0);
1589 1590 1591 1592
    
    srs_verbose("amf0 read bool value success. value=%d", value);
    
    return ret;
winlin authored
1593 1594 1595
}
int srs_amf0_write_boolean(SrsStream* stream, bool value)
{
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write bool marker failed. ret=%d", ret);
        return ret;
    }
    stream->write_1bytes(RTMP_AMF0_Boolean);
    srs_verbose("amf0 write bool marker success");

    // value
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write bool value failed. ret=%d", ret);
        return ret;
    }

    if (value) {
        stream->write_1bytes(0x01);
    } else {
        stream->write_1bytes(0x00);
    }
    
    srs_verbose("amf0 write bool value success. value=%d", value);
    
    return ret;
winlin authored
1623 1624 1625 1626
}

int srs_amf0_read_number(SrsStream* stream, double& value)
{
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read number marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_Number) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check number marker failed. "
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_Number, ret);
        return ret;
    }
    srs_verbose("amf0 read number marker success");

    // value
    if (!stream->require(8)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read number value failed. ret=%d", ret);
        return ret;
    }

    int64_t temp = stream->read_8bytes();
    memcpy(&value, &temp, 8);
    
    srs_verbose("amf0 read number value success. value=%.2f", value);
    
    return ret;
winlin authored
1658 1659 1660
}
int srs_amf0_write_number(SrsStream* stream, double value)
{
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write number marker failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_1bytes(RTMP_AMF0_Number);
    srs_verbose("amf0 write number marker success");

    // value
    if (!stream->require(8)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write number value failed. ret=%d", ret);
        return ret;
    }

    int64_t temp = 0x00;
    memcpy(&temp, &value, 8);
    stream->write_8bytes(temp);
    
    srs_verbose("amf0 write number value success. value=%.2f", value);
    
    return ret;
winlin authored
1687 1688 1689 1690
}

int srs_amf0_read_null(SrsStream* stream)
{
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read null marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_Null) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check null marker failed. "
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_Null, ret);
        return ret;
    }
    srs_verbose("amf0 read null success");
    
    return ret;
winlin authored
1710 1711 1712
}
int srs_amf0_write_null(SrsStream* stream)
{
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write null marker failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_1bytes(RTMP_AMF0_Null);
    srs_verbose("amf0 write null marker success");
    
    return ret;
winlin authored
1726 1727 1728 1729
}

int srs_amf0_read_undefined(SrsStream* stream)
{
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 read undefined marker failed. ret=%d", ret);
        return ret;
    }
    
    char marker = stream->read_1bytes();
    if (marker != RTMP_AMF0_Undefined) {
        ret = ERROR_RTMP_AMF0_DECODE;
        srs_error("amf0 check undefined marker failed. "
            "marker=%#x, required=%#x, ret=%d", marker, RTMP_AMF0_Undefined, ret);
        return ret;
    }
    srs_verbose("amf0 read undefined success");
    
    return ret;
winlin authored
1749 1750 1751
}
int srs_amf0_write_undefined(SrsStream* stream)
{
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    int ret = ERROR_SUCCESS;
    
    // marker
    if (!stream->require(1)) {
        ret = ERROR_RTMP_AMF0_ENCODE;
        srs_error("amf0 write undefined marker failed. ret=%d", ret);
        return ret;
    }
    
    stream->write_1bytes(RTMP_AMF0_Undefined);
    srs_verbose("amf0 write undefined marker success");
    
    return ret;
winlin authored
1765 1766
}
1767
1768
namespace _srs_internal
winlin authored
1769
{
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
    int srs_amf0_read_utf8(SrsStream* stream, string& value)
    {
        int ret = ERROR_SUCCESS;
        
        // len
        if (!stream->require(2)) {
            ret = ERROR_RTMP_AMF0_DECODE;
            srs_error("amf0 read string length failed. ret=%d", ret);
            return ret;
        }
        int16_t len = stream->read_2bytes();
        srs_verbose("amf0 read string length success. len=%d", len);
        
        // empty string
        if (len <= 0) {
            srs_verbose("amf0 read empty string. ret=%d", ret);
            return ret;
        }
        
        // data
        if (!stream->require(len)) {
            ret = ERROR_RTMP_AMF0_DECODE;
            srs_error("amf0 read string data failed. ret=%d", ret);
            return ret;
        }
        std::string str = stream->read_string(len);
        
        // support utf8-1 only
        // 1.3.1 Strings and UTF-8
        // UTF8-1 = %x00-7F
        // TODO: support other utf-8 strings
        /*for (int i = 0; i < len; i++) {
            char ch = *(str.data() + i);
            if ((ch & 0x80) != 0) {
                ret = ERROR_RTMP_AMF0_DECODE;
                srs_error("ignored. only support utf8-1, 0x00-0x7F, actual is %#x. ret=%d", (int)ch, ret);
                ret = ERROR_SUCCESS;
            }
        }*/
        
        value = str;
        srs_verbose("amf0 read string data success. str=%s", str.c_str());
        
1813 1814
        return ret;
    }
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
    int srs_amf0_write_utf8(SrsStream* stream, string value)
    {
        int ret = ERROR_SUCCESS;
        
        // len
        if (!stream->require(2)) {
            ret = ERROR_RTMP_AMF0_ENCODE;
            srs_error("amf0 write string length failed. ret=%d", ret);
            return ret;
        }
        stream->write_2bytes(value.length());
        srs_verbose("amf0 write string length success. len=%d", (int)value.length());
        
        // empty string
        if (value.length() <= 0) {
            srs_verbose("amf0 write empty string. ret=%d", ret);
            return ret;
        }
        
        // data
        if (!stream->require(value.length())) {
            ret = ERROR_RTMP_AMF0_ENCODE;
            srs_error("amf0 write string data failed. ret=%d", ret);
            return ret;
        }
        stream->write_string(value);
        srs_verbose("amf0 write string data success. str=%s", value.c_str());
        
1843 1844 1845
        return ret;
    }
    
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
    bool srs_amf0_is_object_eof(SrsStream* stream) 
    {
        // detect the object-eof specially
        if (stream->require(3)) {
            int32_t flag = stream->read_3bytes();
            stream->skip(-3);
            
            return 0x09 == flag;
        }
        
        return false;
    }
1858
    
1859
    int srs_amf0_write_object_eof(SrsStream* stream, SrsAmf0ObjectEOF* value)
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
    {
        int ret = ERROR_SUCCESS;
        
        srs_assert(value != NULL);
        
        // value
        if (!stream->require(2)) {
            ret = ERROR_RTMP_AMF0_ENCODE;
            srs_error("amf0 write object eof value failed. ret=%d", ret);
            return ret;
        }
        stream->write_2bytes(0x00);
        srs_verbose("amf0 write object eof value success");
        
        // marker
        if (!stream->require(1)) {
            ret = ERROR_RTMP_AMF0_ENCODE;
            srs_error("amf0 write object eof marker failed. ret=%d", ret);
            return ret;
        }
        
        stream->write_1bytes(RTMP_AMF0_ObjectEnd);
        
        srs_verbose("amf0 read object eof success");
        
        return ret;
    }

    int srs_amf0_write_any(SrsStream* stream, SrsAmf0Any* value)
    {
        srs_assert(value != NULL);
        return value->write(stream);
    }
winlin authored
1893
}
1894