Blame view

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

Copyright (c) 2013-2014 winlin

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_rtmp.hpp>
winlin authored
25
26
#include <srs_core_autofree.hpp>
27
#include <srs_kernel_log.hpp>
28
#include <srs_kernel_error.hpp>
29
#include <srs_protocol_io.hpp>
30
#include <srs_protocol_amf0.hpp>
31 32 33
#include <srs_protocol_handshake.hpp>
#include <srs_protocol_rtmp_stack.hpp>
#include <srs_protocol_utility.hpp>
winlin authored
34 35 36 37 38 39

using namespace std;

/**
* the signature for packets to client.
*/
40 41 42
#define RTMP_SIG_FMS_VER                     "3,5,3,888"
#define RTMP_SIG_AMF0_VER                     0
#define RTMP_SIG_CLIENT_ID                     "ASAICiss"
winlin authored
43 44 45 46

/**
* onStatus consts.
*/
47 48 49 50 51
#define StatusLevel                         "level"
#define StatusCode                             "code"
#define StatusDescription                     "description"
#define StatusDetails                         "details"
#define StatusClientId                         "clientid"
winlin authored
52
// status value
53
#define StatusLevelStatus                     "status"
winlin authored
54 55 56
// status error
#define StatusLevelError                    "error"
// code value
57 58 59 60 61 62 63 64 65
#define StatusCodeConnectSuccess             "NetConnection.Connect.Success"
#define StatusCodeConnectRejected             "NetConnection.Connect.Rejected"
#define StatusCodeStreamReset                 "NetStream.Play.Reset"
#define StatusCodeStreamStart                 "NetStream.Play.Start"
#define StatusCodeStreamPause                 "NetStream.Pause.Notify"
#define StatusCodeStreamUnpause             "NetStream.Unpause.Notify"
#define StatusCodePublishStart                 "NetStream.Publish.Start"
#define StatusCodeDataStart                 "NetStream.Data.Start"
#define StatusCodeUnpublishSuccess             "NetStream.Unpublish.Success"
winlin authored
66 67

// FMLE
68 69
#define RTMP_AMF0_COMMAND_ON_FC_PUBLISH        "onFCPublish"
#define RTMP_AMF0_COMMAND_ON_FC_UNPUBLISH    "onFCUnpublish"
winlin authored
70 71

// default stream id for response the createStream request.
72
#define SRS_DEFAULT_SID                     1
winlin authored
73 74 75

SrsRequest::SrsRequest()
{
76
    objectEncoding = RTMP_SIG_AMF0_VER;
winlin authored
77 78 79 80 81 82 83 84
}

SrsRequest::~SrsRequest()
{
}

SrsRequest* SrsRequest::copy()
{
85 86 87 88 89
    SrsRequest* cp = new SrsRequest();
    
    cp->app = app;
    cp->objectEncoding = objectEncoding;
    cp->pageUrl = pageUrl;
90
    cp->host = host;
91 92 93 94 95 96 97 98
    cp->port = port;
    cp->schema = schema;
    cp->stream = stream;
    cp->swfUrl = swfUrl;
    cp->tcUrl = tcUrl;
    cp->vhost = vhost;
    
    return cp;
winlin authored
99 100 101 102
}

int SrsRequest::discovery_app()
{
103 104 105 106 107 108 109 110 111 112 113 114
    int ret = ERROR_SUCCESS;
    
    size_t pos = std::string::npos;
    std::string url = tcUrl;
    
    if ((pos = url.find("://")) != std::string::npos) {
        schema = url.substr(0, pos);
        url = url.substr(schema.length() + 3);
        srs_verbose("discovery schema=%s", schema.c_str());
    }
    
    if ((pos = url.find("/")) != std::string::npos) {
115 116 117
        host = url.substr(0, pos);
        url = url.substr(host.length() + 1);
        srs_verbose("discovery host=%s", host.c_str());
118 119 120
    }

    port = RTMP_DEFAULT_PORT;
121 122 123 124
    if ((pos = host.find(":")) != std::string::npos) {
        port = host.substr(pos + 1);
        host = host.substr(0, pos);
        srs_verbose("discovery host=%s, port=%s", host.c_str(), port.c_str());
125 126 127
    }
    
    app = url;
128
    vhost = host;
129 130 131 132
    srs_vhost_resolve(vhost, app);
    strip();
    
    return ret;
winlin authored
133 134 135 136
}

string SrsRequest::get_stream_url()
{
137 138 139 140 141 142 143 144 145
    std::string url = "";
    
    url += vhost;
    url += "/";
    url += app;
    url += "/";
    url += stream;

    return url;
winlin authored
146 147 148 149
}

void SrsRequest::strip()
{
150 151 152
    trim(vhost, "/ \n\r\t");
    trim(app, "/ \n\r\t");
    trim(stream, "/ \n\r\t");
winlin authored
153 154
}
155
string& SrsRequest::trim(string& str, string chs)
winlin authored
156
{
157 158 159 160 161 162 163 164 165 166 167 168 169
    for (int i = 0; i < (int)chs.length(); i++) {
        char ch = chs.at(i);
        
        for (std::string::iterator it = str.begin(); it != str.end();) {
            if (ch == *it) {
                it = str.erase(it);
            } else {
                ++it;
            }
        }
    }
    
    return str;
winlin authored
170 171 172 173
}

SrsResponse::SrsResponse()
{
174
    stream_id = SRS_DEFAULT_SID;
winlin authored
175 176 177 178 179 180
}

SrsResponse::~SrsResponse()
{
}
181 182
string srs_client_type_string(SrsClientType type)
{
183 184 185 186 187 188 189
    switch (type) {
        case SrsClientPlay: return "Play";
        case SrsClientFlashPublish: return "FlashPublish";
        case SrsClientFMLEPublish: return "FMLEPublish";
        default: return "Unknown";
    }
    return "Unknown";
190 191
}
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
SrsHandshakeBytes::SrsHandshakeBytes() 
{
    c0c1 = s0s1s2 = c2 = NULL;
}

SrsHandshakeBytes::~SrsHandshakeBytes() 
{
    srs_freepa(c0c1);
    srs_freepa(s0s1s2);
    srs_freepa(c2);
}

int SrsHandshakeBytes::read_c0c1(ISrsProtocolReaderWriter* io)
{
    int ret = ERROR_SUCCESS;
    
    if (c0c1) {
        return ret;
    }
    
    ssize_t nsize;
    
    c0c1 = new char[1537];
    if ((ret = io->read_fully(c0c1, 1537, &nsize)) != ERROR_SUCCESS) {
        srs_warn("read c0c1 failed. ret=%d", ret);
        return ret;
    }
    srs_verbose("read c0c1 success.");
    
    return ret;
}

int SrsHandshakeBytes::read_s0s1s2(ISrsProtocolReaderWriter* io)
{
    int ret = ERROR_SUCCESS;
    
    if (s0s1s2) {
        return ret;
    }
    
    ssize_t nsize;
    
winlin authored
234
    s0s1s2 = new char[3073];
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
    if ((ret = io->read_fully(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) {
        srs_warn("read s0s1s2 failed. ret=%d", ret);
        return ret;
    }
    srs_verbose("read s0s1s2 success.");
    
    return ret;
}

int SrsHandshakeBytes::read_c2(ISrsProtocolReaderWriter* io)
{
    int ret = ERROR_SUCCESS;
    
    if (c2) {
        return ret;
    }
    
    ssize_t nsize;
    
    c2 = new char[1536];
    if ((ret = io->read_fully(c2, 1536, &nsize)) != ERROR_SUCCESS) {
        srs_warn("read c2 failed. ret=%d", ret);
        return ret;
    }
    srs_verbose("read c2 success.");
    
    return ret;
}

int SrsHandshakeBytes::create_c0c1()
{
    int ret = ERROR_SUCCESS;
    
    if (c0c1) {
        return ret;
    }
    
    c0c1 = new char[1537];
    srs_random_generate(c0c1, 1537);
    
275 276 277 278 279
    // plain text required.
    c0c1[0] = 0x03;
    *(int32_t*)(c0c1 + 1) = ::time(NULL);
    *(int32_t*)(c0c1 + 1 + 4) = 0x00;
    
280 281 282 283 284 285 286 287 288 289 290 291 292 293
    return ret;
}

int SrsHandshakeBytes::create_s0s1s2()
{
    int ret = ERROR_SUCCESS;
    
    if (s0s1s2) {
        return ret;
    }
    
    s0s1s2 = new char[3073];
    srs_random_generate(s0s1s2, 3073);
    
294 295 296 297 298 299 300 301
    // plain text required.
    s0s1s2[0] = 0x03;
    *(int32_t*)(s0s1s2 + 1) = ::time(NULL);
    // s2 time2 copy from c1
    if (c0c1) {
        *(int32_t*)(s0s1s2 + 1 + 4) = *(int32_t*)(c0c1 + 1);
    }
    
302 303 304 305 306 307 308 309 310 311 312 313 314 315
    return ret;
}

int SrsHandshakeBytes::create_c2()
{
    int ret = ERROR_SUCCESS;
    
    if (c2) {
        return ret;
    }
    
    c2 = new char[1536];
    srs_random_generate(c2, 1536);
    
316 317 318 319 320 321 322
    // time
    *(int32_t*)(c2) = ::time(NULL);
    // c2 time2 copy from s1
    if (s0s1s2) {
        *(int32_t*)(c2 + 4) = *(int32_t*)(s0s1s2 + 1);
    }
    
323 324 325
    return ret;
}
326
SrsRtmpClient::SrsRtmpClient(ISrsProtocolReaderWriter* skt)
winlin authored
327
{
328 329
    io = skt;
    protocol = new SrsProtocol(skt);
330
    hs_bytes = new SrsHandshakeBytes();
winlin authored
331 332 333 334
}

SrsRtmpClient::~SrsRtmpClient()
{
335
    srs_freep(protocol);
336
    srs_freep(hs_bytes);
winlin authored
337 338 339 340
}

void SrsRtmpClient::set_recv_timeout(int64_t timeout_us)
{
341
    protocol->set_recv_timeout(timeout_us);
winlin authored
342 343 344 345
}

void SrsRtmpClient::set_send_timeout(int64_t timeout_us)
{
346
    protocol->set_send_timeout(timeout_us);
winlin authored
347 348 349 350
}

int64_t SrsRtmpClient::get_recv_bytes()
{
351
    return protocol->get_recv_bytes();
winlin authored
352 353 354 355
}

int64_t SrsRtmpClient::get_send_bytes()
{
356
    return protocol->get_send_bytes();
winlin authored
357 358 359 360
}

int SrsRtmpClient::get_recv_kbps()
{
361
    return protocol->get_recv_kbps();
winlin authored
362 363 364 365
}

int SrsRtmpClient::get_send_kbps()
{
366
    return protocol->get_send_kbps();
winlin authored
367 368 369 370
}

int SrsRtmpClient::recv_message(SrsCommonMessage** pmsg)
{
371
    return protocol->recv_message(pmsg);
winlin authored
372 373 374 375
}

int SrsRtmpClient::send_message(ISrsMessage* msg)
{
376
    return protocol->send_message(msg);
winlin authored
377 378 379 380
}

int SrsRtmpClient::handshake()
{
381
    int ret = ERROR_SUCCESS;
winlin authored
382
    
383 384
    srs_assert(hs_bytes);
    
winlin authored
385
    SrsComplexHandshake complex_hs;
386 387 388 389 390 391 392
    if ((ret = complex_hs.handshake_with_server(hs_bytes, io)) != ERROR_SUCCESS) {
        if (ret == ERROR_RTMP_TRY_SIMPLE_HS) {
            SrsSimpleHandshake simple_hs;
            if ((ret = simple_hs.handshake_with_server(hs_bytes, io)) != ERROR_SUCCESS) {
                return ret;
            }
        }
winlin authored
393 394 395
        return ret;
    }
    
396 397
    srs_freep(hs_bytes);
    
winlin authored
398 399 400
    return ret;
}
401 402
int SrsRtmpClient::simple_handshake()
{
403
    int ret = ERROR_SUCCESS;
404
    
405 406
    srs_assert(hs_bytes);
    
407
    SrsSimpleHandshake simple_hs;
408
    if ((ret = simple_hs.handshake_with_server(hs_bytes, io)) != ERROR_SUCCESS) {
409 410 411
        return ret;
    }
    
412 413
    srs_freep(hs_bytes);
    
414 415 416 417 418
    return ret;
}

int SrsRtmpClient::complex_handshake()
{
419 420 421 422 423 424 425 426 427 428 429 430
    int ret = ERROR_SUCCESS;
    
    srs_assert(hs_bytes);
    
    SrsComplexHandshake complex_hs;
    if ((ret = complex_hs.handshake_with_server(hs_bytes, io)) != ERROR_SUCCESS) {
        return ret;
    }
    
    srs_freep(hs_bytes);
    
    return ret;
431 432
}
winlin authored
433 434
int SrsRtmpClient::connect_app(string app, string tc_url)
{
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    int ret = ERROR_SUCCESS;
    
    // Connect(vhost, app)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsConnectAppPacket* pkt = new SrsConnectAppPacket();
        msg->set_packet(pkt, 0);
        
        pkt->command_object = SrsAmf0Any::object();
        pkt->command_object->set("app", SrsAmf0Any::str(app.c_str()));
        pkt->command_object->set("swfUrl", SrsAmf0Any::str());
        pkt->command_object->set("tcUrl", SrsAmf0Any::str(tc_url.c_str()));
        pkt->command_object->set("fpad", SrsAmf0Any::boolean(false));
        pkt->command_object->set("capabilities", SrsAmf0Any::number(239));
        pkt->command_object->set("audioCodecs", SrsAmf0Any::number(3575));
        pkt->command_object->set("videoCodecs", SrsAmf0Any::number(252));
        pkt->command_object->set("videoFunction", SrsAmf0Any::number(1));
        pkt->command_object->set("pageUrl", SrsAmf0Any::str());
        pkt->command_object->set("objectEncoding", SrsAmf0Any::number(0));
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            return ret;
        }
    }
    
    // Set Window Acknowledgement size(2500000)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsSetWindowAckSizePacket* pkt = new SrsSetWindowAckSizePacket();
    
        pkt->ackowledgement_window_size = 2500000;
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            return ret;
        }
    }
    
    // expect connect _result
    SrsCommonMessage* msg = NULL;
    SrsConnectAppResPacket* pkt = NULL;
    if ((ret = srs_rtmp_expect_message<SrsConnectAppResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
        srs_error("expect connect app response message failed. ret=%d", ret);
        return ret;
    }
    SrsAutoFree(SrsCommonMessage, msg, false);
    srs_info("get connect app response message");
    
winlin authored
483 484 485 486 487
    return ret;
}

int SrsRtmpClient::create_stream(int& stream_id)
{
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    int ret = ERROR_SUCCESS;
    
    // CreateStream
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsCreateStreamPacket* pkt = new SrsCreateStreamPacket();
    
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            return ret;
        }
    }

    // CreateStream _result.
    if (true) {
        SrsCommonMessage* msg = NULL;
        SrsCreateStreamResPacket* pkt = NULL;
        if ((ret = srs_rtmp_expect_message<SrsCreateStreamResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
            srs_error("expect create stream response message failed. ret=%d", ret);
            return ret;
        }
        SrsAutoFree(SrsCommonMessage, msg, false);
        srs_info("get create stream response message");

        stream_id = (int)pkt->stream_id;
    }
    
    return ret;
winlin authored
517 518 519 520
}

int SrsRtmpClient::play(string stream, int stream_id)
{
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 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
    int ret = ERROR_SUCCESS;

    // Play(stream)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsPlayPacket* pkt = new SrsPlayPacket();
    
        pkt->stream_name = stream;
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send play stream failed. "
                "stream=%s, stream_id=%d, ret=%d", 
                stream.c_str(), stream_id, ret);
            return ret;
        }
    }
    
    // SetBufferLength(1000ms)
    int buffer_length_ms = 1000;
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsUserControlPacket* pkt = new SrsUserControlPacket();
    
        pkt->event_type = SrcPCUCSetBufferLength;
        pkt->event_data = stream_id;
        pkt->extra_data = buffer_length_ms;
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send set buffer length failed. "
                "stream=%s, stream_id=%d, bufferLength=%d, ret=%d", 
                stream.c_str(), stream_id, buffer_length_ms, ret);
            return ret;
        }
    }
    
    // SetChunkSize
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsSetChunkSizePacket* pkt = new SrsSetChunkSizePacket();
    
        pkt->chunk_size = SRS_CONF_DEFAULT_CHUNK_SIZE;
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send set chunk size failed. "
                "stream=%s, chunk_size=%d, ret=%d", 
                stream.c_str(), SRS_CONF_DEFAULT_CHUNK_SIZE, ret);
            return ret;
        }
    }
    
    return ret;
winlin authored
575 576 577 578
}

int SrsRtmpClient::publish(string stream, int stream_id)
{
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    int ret = ERROR_SUCCESS;
    
    // SetChunkSize
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsSetChunkSizePacket* pkt = new SrsSetChunkSizePacket();
    
        pkt->chunk_size = SRS_CONF_DEFAULT_CHUNK_SIZE;
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send set chunk size failed. "
                "stream=%s, chunk_size=%d, ret=%d", 
                stream.c_str(), SRS_CONF_DEFAULT_CHUNK_SIZE, ret);
            return ret;
        }
    }

    // publish(stream)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsPublishPacket* pkt = new SrsPublishPacket();
    
        pkt->stream_name = stream;
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send publish message failed. "
                "stream=%s, stream_id=%d, ret=%d", 
                stream.c_str(), stream_id, ret);
            return ret;
        }
    }
    
    return ret;
winlin authored
614 615
}
616 617
int SrsRtmpClient::fmle_publish(string stream, int& stream_id)
{
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
    stream_id = 0;
    
    int ret = ERROR_SUCCESS;
    
    // SrsFMLEStartPacket
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsFMLEStartPacket* pkt = SrsFMLEStartPacket::create_release_stream(stream);
    
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send FMLE publish "
                "release stream failed. stream=%s, ret=%d", stream.c_str(), ret);
            return ret;
        }
    }
    
    // FCPublish
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsFMLEStartPacket* pkt = SrsFMLEStartPacket::create_FC_publish(stream);
    
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send FMLE publish "
                "FCPublish failed. stream=%s, ret=%d", stream.c_str(), ret);
            return ret;
        }
    }
    
    // CreateStream
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsCreateStreamPacket* pkt = new SrsCreateStreamPacket();
    
        pkt->transaction_id = 4;
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send FMLE publish "
                "createStream failed. stream=%s, ret=%d", stream.c_str(), ret);
            return ret;
        }
    }
    
    // expect result of CreateStream
    if (true) {
        SrsCommonMessage* msg = NULL;
        SrsCreateStreamResPacket* pkt = NULL;
        if ((ret = srs_rtmp_expect_message<SrsCreateStreamResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
            srs_error("expect create stream response message failed. ret=%d", ret);
            return ret;
        }
        SrsAutoFree(SrsCommonMessage, msg, false);
        srs_info("get create stream response message");

        stream_id = (int)pkt->stream_id;
    }

    // publish(stream)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsPublishPacket* pkt = new SrsPublishPacket();
    
        pkt->stream_name = stream;
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send FMLE publish publish failed. "
                "stream=%s, stream_id=%d, ret=%d", stream.c_str(), stream_id, ret);
            return ret;
        }
    }
    
    return ret;
695 696
}
697
SrsRtmpServer::SrsRtmpServer(ISrsProtocolReaderWriter* skt)
winlin authored
698
{
699 700
    io = skt;
    protocol = new SrsProtocol(skt);
701
    hs_bytes = new SrsHandshakeBytes();
winlin authored
702 703
}
704
SrsRtmpServer::~SrsRtmpServer()
winlin authored
705
{
706
    srs_freep(protocol);
707
    srs_freep(hs_bytes);
winlin authored
708 709
}
710
SrsProtocol* SrsRtmpServer::get_protocol()
winlin authored
711
{
712
    return protocol;
winlin authored
713 714
}
715
void SrsRtmpServer::set_recv_timeout(int64_t timeout_us)
winlin authored
716
{
717
    protocol->set_recv_timeout(timeout_us);
winlin authored
718 719
}
720
int64_t SrsRtmpServer::get_recv_timeout()
winlin authored
721
{
722
    return protocol->get_recv_timeout();
winlin authored
723 724
}
725
void SrsRtmpServer::set_send_timeout(int64_t timeout_us)
winlin authored
726
{
727
    protocol->set_send_timeout(timeout_us);
winlin authored
728 729
}
730
int64_t SrsRtmpServer::get_send_timeout()
winlin authored
731
{
732
    return protocol->get_send_timeout();
winlin authored
733 734
}
735
int64_t SrsRtmpServer::get_recv_bytes()
winlin authored
736
{
737
    return protocol->get_recv_bytes();
winlin authored
738 739
}
740
int64_t SrsRtmpServer::get_send_bytes()
winlin authored
741
{
742
    return protocol->get_send_bytes();
winlin authored
743 744
}
745
int SrsRtmpServer::get_recv_kbps()
winlin authored
746
{
747
    return protocol->get_recv_kbps();
winlin authored
748 749
}
750
int SrsRtmpServer::get_send_kbps()
winlin authored
751
{
752
    return protocol->get_send_kbps();
winlin authored
753 754
}
755
int SrsRtmpServer::recv_message(SrsCommonMessage** pmsg)
winlin authored
756
{
757
    return protocol->recv_message(pmsg);
winlin authored
758 759
}
760
int SrsRtmpServer::send_message(ISrsMessage* msg)
winlin authored
761
{
762
    return protocol->send_message(msg);
winlin authored
763 764
}
765
int SrsRtmpServer::handshake()
winlin authored
766
{
767
    int ret = ERROR_SUCCESS;
winlin authored
768
    
769 770
    srs_assert(hs_bytes);
    
winlin authored
771
    SrsComplexHandshake complex_hs;
772 773 774 775 776 777 778
    if ((ret = complex_hs.handshake_with_client(hs_bytes, io)) != ERROR_SUCCESS) {
        if (ret == ERROR_RTMP_TRY_SIMPLE_HS) {
            SrsSimpleHandshake simple_hs;
            if ((ret = simple_hs.handshake_with_client(hs_bytes, io)) != ERROR_SUCCESS) {
                return ret;
            }
        }
winlin authored
779 780 781
        return ret;
    }
    
782 783
    srs_freep(hs_bytes);
    
winlin authored
784 785 786
    return ret;
}
787
int SrsRtmpServer::connect_app(SrsRequest* req)
winlin authored
788
{
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    int ret = ERROR_SUCCESS;
    
    SrsCommonMessage* msg = NULL;
    SrsConnectAppPacket* pkt = NULL;
    if ((ret = srs_rtmp_expect_message<SrsConnectAppPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
        srs_error("expect connect app message failed. ret=%d", ret);
        return ret;
    }
    SrsAutoFree(SrsCommonMessage, msg, false);
    srs_info("get connect app message");
    
    SrsAmf0Any* prop = NULL;
    
    if ((prop = pkt->command_object->ensure_property_string("tcUrl")) == NULL) {
        ret = ERROR_RTMP_REQ_CONNECT;
        srs_error("invalid request, must specifies the tcUrl. ret=%d", ret);
        return ret;
    }
    req->tcUrl = prop->to_str();
    
    if ((prop = pkt->command_object->ensure_property_string("pageUrl")) != NULL) {
        req->pageUrl = prop->to_str();
    }
    
    if ((prop = pkt->command_object->ensure_property_string("swfUrl")) != NULL) {
        req->swfUrl = prop->to_str();
    }
    
    if ((prop = pkt->command_object->ensure_property_number("objectEncoding")) != NULL) {
        req->objectEncoding = prop->to_number();
    }
    
    srs_info("get connect app message params success.");
    
    return req->discovery_app();
winlin authored
824 825
}
826
int SrsRtmpServer::set_window_ack_size(int ack_size)
winlin authored
827
{
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    int ret = ERROR_SUCCESS;
    
    SrsCommonMessage* msg = new SrsCommonMessage();
    SrsSetWindowAckSizePacket* pkt = new SrsSetWindowAckSizePacket();
    
    pkt->ackowledgement_window_size = ack_size;
    msg->set_packet(pkt, 0);
    
    if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
        srs_error("send ack size message failed. ret=%d", ret);
        return ret;
    }
    srs_info("send ack size message success. ack_size=%d", ack_size);
    
    return ret;
winlin authored
843 844
}
845
int SrsRtmpServer::set_peer_bandwidth(int bandwidth, int type)
winlin authored
846
{
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
    int ret = ERROR_SUCCESS;
    
    SrsCommonMessage* msg = new SrsCommonMessage();
    SrsSetPeerBandwidthPacket* pkt = new SrsSetPeerBandwidthPacket();
    
    pkt->bandwidth = bandwidth;
    pkt->type = type;
    msg->set_packet(pkt, 0);
    
    if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
        srs_error("send set bandwidth message failed. ret=%d", ret);
        return ret;
    }
    srs_info("send set bandwidth message "
        "success. bandwidth=%d, type=%d", bandwidth, type);
    
    return ret;
winlin authored
864 865
}
866
int SrsRtmpServer::response_connect_app(SrsRequest *req, const char* server_ip)
winlin authored
867
{
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
    int ret = ERROR_SUCCESS;
    
    SrsCommonMessage* msg = new SrsCommonMessage();
    SrsConnectAppResPacket* pkt = new SrsConnectAppResPacket();
    
    pkt->props->set("fmsVer", SrsAmf0Any::str("FMS/"RTMP_SIG_FMS_VER));
    pkt->props->set("capabilities", SrsAmf0Any::number(127));
    pkt->props->set("mode", SrsAmf0Any::number(1));
    
    pkt->info->set(StatusLevel, SrsAmf0Any::str(StatusLevelStatus));
    pkt->info->set(StatusCode, SrsAmf0Any::str(StatusCodeConnectSuccess));
    pkt->info->set(StatusDescription, SrsAmf0Any::str("Connection succeeded"));
    pkt->info->set("objectEncoding", SrsAmf0Any::number(req->objectEncoding));
    SrsAmf0EcmaArray* data = SrsAmf0Any::ecma_array();
    pkt->info->set("data", data);
    
    data->set("version", SrsAmf0Any::str(RTMP_SIG_FMS_VER));
    data->set("srs_sig", SrsAmf0Any::str(RTMP_SIG_SRS_KEY));
    data->set("srs_server", SrsAmf0Any::str(RTMP_SIG_SRS_KEY" "RTMP_SIG_SRS_VERSION" ("RTMP_SIG_SRS_URL_SHORT")"));
    data->set("srs_license", SrsAmf0Any::str(RTMP_SIG_SRS_LICENSE));
    data->set("srs_role", SrsAmf0Any::str(RTMP_SIG_SRS_ROLE));
    data->set("srs_url", SrsAmf0Any::str(RTMP_SIG_SRS_URL));
    data->set("srs_version", SrsAmf0Any::str(RTMP_SIG_SRS_VERSION));
    data->set("srs_site", SrsAmf0Any::str(RTMP_SIG_SRS_WEB));
    data->set("srs_email", SrsAmf0Any::str(RTMP_SIG_SRS_EMAIL));
    data->set("srs_copyright", SrsAmf0Any::str(RTMP_SIG_SRS_COPYRIGHT));
    data->set("srs_primary_authors", SrsAmf0Any::str(RTMP_SIG_SRS_PRIMARY_AUTHROS));
    
winlin authored
896
    if (server_ip) {
897
        data->set("srs_server_ip", SrsAmf0Any::str(server_ip));
winlin authored
898
    }
899 900 901 902 903 904 905 906 907
    
    msg->set_packet(pkt, 0);
    
    if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
        srs_error("send connect app response message failed. ret=%d", ret);
        return ret;
    }
    srs_info("send connect app response message success.");
    
winlin authored
908 909 910
    return ret;
}
911
void SrsRtmpServer::response_connect_reject(SrsRequest *req, const char* desc)
winlin authored
912 913 914 915 916
{
    int ret = ERROR_SUCCESS;

    SrsConnectAppResPacket* pkt = new SrsConnectAppResPacket();
    pkt->command_name = "_error";
917 918 919
    pkt->props->set(StatusLevel, SrsAmf0Any::str(StatusLevelError));
    pkt->props->set(StatusCode, SrsAmf0Any::str(StatusCodeConnectRejected));
    pkt->props->set(StatusDescription, SrsAmf0Any::str(desc));
920
    //pkt->props->set("objectEncoding", SrsAmf0Any::number(req->objectEncoding));
winlin authored
921 922 923 924 925 926 927 928 929 930 931

    SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
    if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
        srs_error("send connect app response rejected message failed. ret=%d", ret);
        return;
    }
    srs_info("send connect app response rejected message success.");

    return;
}
932
int SrsRtmpServer::on_bw_done()
winlin authored
933
{
934 935 936 937 938 939 940 941 942 943 944 945 946 947
    int ret = ERROR_SUCCESS;
    
    SrsCommonMessage* msg = new SrsCommonMessage();
    SrsOnBWDonePacket* pkt = new SrsOnBWDonePacket();
    
    msg->set_packet(pkt, 0);
    
    if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
        srs_error("send onBWDone message failed. ret=%d", ret);
        return ret;
    }
    srs_info("send onBWDone message success.");
    
    return ret;
winlin authored
948 949
}
950
int SrsRtmpServer::identify_client(int stream_id, SrsClientType& type, string& stream_name)
winlin authored
951
{
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    type = SrsClientUnknown;
    int ret = ERROR_SUCCESS;
    
    while (true) {
        SrsCommonMessage* msg = NULL;
        if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
            srs_error("recv identify client message failed. ret=%d", ret);
            return ret;
        }

        SrsAutoFree(SrsCommonMessage, msg, false);

        if (!msg->header.is_amf0_command() && !msg->header.is_amf3_command()) {
            srs_trace("identify ignore messages except "
                "AMF0/AMF3 command message. type=%#x", msg->header.message_type);
            continue;
        }
        
        if ((ret = msg->decode_packet(protocol)) != ERROR_SUCCESS) {
            srs_error("identify decode message failed. ret=%d", ret);
            return ret;
        }
        
        SrsPacket* pkt = msg->get_packet();
        if (dynamic_cast<SrsCreateStreamPacket*>(pkt)) {
            srs_info("identify client by create stream, play or flash publish.");
            return identify_create_stream_client(dynamic_cast<SrsCreateStreamPacket*>(pkt), stream_id, type, stream_name);
        }
        if (dynamic_cast<SrsFMLEStartPacket*>(pkt)) {
            srs_info("identify client by releaseStream, fmle publish.");
            return identify_fmle_publish_client(dynamic_cast<SrsFMLEStartPacket*>(pkt), type, stream_name);
        }
        if (dynamic_cast<SrsPlayPacket*>(pkt)) {
            srs_info("level0 identify client by play.");
            return identify_play_client(dynamic_cast<SrsPlayPacket*>(pkt), type, stream_name);
        }
        
        srs_trace("ignore AMF0/AMF3 command message.");
    }
    
    return ret;
winlin authored
993 994
}
995
int SrsRtmpServer::set_chunk_size(int chunk_size)
winlin authored
996
{
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    int ret = ERROR_SUCCESS;
    
    SrsCommonMessage* msg = new SrsCommonMessage();
    SrsSetChunkSizePacket* pkt = new SrsSetChunkSizePacket();
    
    pkt->chunk_size = chunk_size;
    msg->set_packet(pkt, 0);
    
    if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
        srs_error("send set chunk size message failed. ret=%d", ret);
        return ret;
    }
    srs_info("send set chunk size message success. chunk_size=%d", chunk_size);
    
    return ret;
winlin authored
1012 1013
}
1014
int SrsRtmpServer::start_play(int stream_id)
winlin authored
1015
{
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 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 1102 1103 1104 1105 1106
    int ret = ERROR_SUCCESS;
    
    // StreamBegin
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsUserControlPacket* pkt = new SrsUserControlPacket();
        
        pkt->event_type = SrcPCUCStreamBegin;
        pkt->event_data = stream_id;
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send PCUC(StreamBegin) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send PCUC(StreamBegin) message success.");
    }
    
    // onStatus(NetStream.Play.Reset)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
        
        pkt->data->set(StatusLevel, SrsAmf0Any::str(StatusLevelStatus));
        pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodeStreamReset));
        pkt->data->set(StatusDescription, SrsAmf0Any::str("Playing and resetting stream."));
        pkt->data->set(StatusDetails, SrsAmf0Any::str("stream"));
        pkt->data->set(StatusClientId, SrsAmf0Any::str(RTMP_SIG_CLIENT_ID));
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send onStatus(NetStream.Play.Reset) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send onStatus(NetStream.Play.Reset) message success.");
    }
    
    // onStatus(NetStream.Play.Start)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
        
        pkt->data->set(StatusLevel, SrsAmf0Any::str(StatusLevelStatus));
        pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodeStreamStart));
        pkt->data->set(StatusDescription, SrsAmf0Any::str("Started playing stream."));
        pkt->data->set(StatusDetails, SrsAmf0Any::str("stream"));
        pkt->data->set(StatusClientId, SrsAmf0Any::str(RTMP_SIG_CLIENT_ID));
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send onStatus(NetStream.Play.Reset) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send onStatus(NetStream.Play.Reset) message success.");
    }
    
    // |RtmpSampleAccess(false, false)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsSampleAccessPacket* pkt = new SrsSampleAccessPacket();
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send |RtmpSampleAccess(false, false) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send |RtmpSampleAccess(false, false) message success.");
    }
    
    // onStatus(NetStream.Data.Start)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsOnStatusDataPacket* pkt = new SrsOnStatusDataPacket();
        
        pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodeDataStart));
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send onStatus(NetStream.Data.Start) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send onStatus(NetStream.Data.Start) message success.");
    }
    
    srs_info("start play success.");
    
    return ret;
winlin authored
1107 1108
}
1109
int SrsRtmpServer::on_play_client_pause(int stream_id, bool is_pause)
winlin authored
1110
{
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
    int ret = ERROR_SUCCESS;
    
    if (is_pause) {
        // onStatus(NetStream.Pause.Notify)
        if (true) {
            SrsCommonMessage* msg = new SrsCommonMessage();
            SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
            
            pkt->data->set(StatusLevel, SrsAmf0Any::str(StatusLevelStatus));
            pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodeStreamPause));
            pkt->data->set(StatusDescription, SrsAmf0Any::str("Paused stream."));
            
            msg->set_packet(pkt, stream_id);
            
            if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
                srs_error("send onStatus(NetStream.Pause.Notify) message failed. ret=%d", ret);
                return ret;
            }
            srs_info("send onStatus(NetStream.Pause.Notify) message success.");
        }
        // StreamEOF
        if (true) {
            SrsCommonMessage* msg = new SrsCommonMessage();
            SrsUserControlPacket* pkt = new SrsUserControlPacket();
            
            pkt->event_type = SrcPCUCStreamEOF;
            pkt->event_data = stream_id;
            msg->set_packet(pkt, 0);
            
            if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
                srs_error("send PCUC(StreamEOF) message failed. ret=%d", ret);
                return ret;
            }
            srs_info("send PCUC(StreamEOF) message success.");
        }
    } else {
        // onStatus(NetStream.Unpause.Notify)
        if (true) {
            SrsCommonMessage* msg = new SrsCommonMessage();
            SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
            
            pkt->data->set(StatusLevel, SrsAmf0Any::str(StatusLevelStatus));
            pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodeStreamUnpause));
            pkt->data->set(StatusDescription, SrsAmf0Any::str("Unpaused stream."));
            
            msg->set_packet(pkt, stream_id);
            
            if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
                srs_error("send onStatus(NetStream.Unpause.Notify) message failed. ret=%d", ret);
                return ret;
            }
            srs_info("send onStatus(NetStream.Unpause.Notify) message success.");
        }
        // StreanBegin
        if (true) {
            SrsCommonMessage* msg = new SrsCommonMessage();
            SrsUserControlPacket* pkt = new SrsUserControlPacket();
            
            pkt->event_type = SrcPCUCStreamBegin;
            pkt->event_data = stream_id;
            msg->set_packet(pkt, 0);
            
            if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
                srs_error("send PCUC(StreanBegin) message failed. ret=%d", ret);
                return ret;
            }
            srs_info("send PCUC(StreanBegin) message success.");
        }
    }
    
    return ret;
winlin authored
1182 1183
}
1184
int SrsRtmpServer::start_fmle_publish(int stream_id)
winlin authored
1185
{
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    int ret = ERROR_SUCCESS;
    
    // FCPublish
    double fc_publish_tid = 0;
    if (true) {
        SrsCommonMessage* msg = NULL;
        SrsFMLEStartPacket* pkt = NULL;
        if ((ret = srs_rtmp_expect_message<SrsFMLEStartPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
            srs_error("recv FCPublish message failed. ret=%d", ret);
            return ret;
        }
        srs_info("recv FCPublish request message success.");
        
        SrsAutoFree(SrsCommonMessage, msg, false);
        fc_publish_tid = pkt->transaction_id;
    }
    // FCPublish response
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsFMLEStartResPacket* pkt = new SrsFMLEStartResPacket(fc_publish_tid);
        
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send FCPublish response message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send FCPublish response message success.");
    }
    
    // createStream
    double create_stream_tid = 0;
    if (true) {
        SrsCommonMessage* msg = NULL;
        SrsCreateStreamPacket* pkt = NULL;
        if ((ret = srs_rtmp_expect_message<SrsCreateStreamPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
            srs_error("recv createStream message failed. ret=%d", ret);
            return ret;
        }
        srs_info("recv createStream request message success.");
        
        SrsAutoFree(SrsCommonMessage, msg, false);
        create_stream_tid = pkt->transaction_id;
    }
    // createStream response
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsCreateStreamResPacket* pkt = new SrsCreateStreamResPacket(create_stream_tid, stream_id);
        
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send createStream response message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send createStream response message success.");
    }
    
    // publish
    if (true) {
        SrsCommonMessage* msg = NULL;
        SrsPublishPacket* pkt = NULL;
        if ((ret = srs_rtmp_expect_message<SrsPublishPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
            srs_error("recv publish message failed. ret=%d", ret);
            return ret;
        }
        srs_info("recv publish request message success.");
        
        SrsAutoFree(SrsCommonMessage, msg, false);
    }
    // publish response onFCPublish(NetStream.Publish.Start)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
        
        pkt->command_name = RTMP_AMF0_COMMAND_ON_FC_PUBLISH;
        pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodePublishStart));
        pkt->data->set(StatusDescription, SrsAmf0Any::str("Started publishing stream."));
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send onFCPublish(NetStream.Publish.Start) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send onFCPublish(NetStream.Publish.Start) message success.");
    }
    // publish response onStatus(NetStream.Publish.Start)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
        
        pkt->data->set(StatusLevel, SrsAmf0Any::str(StatusLevelStatus));
        pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodePublishStart));
        pkt->data->set(StatusDescription, SrsAmf0Any::str("Started publishing stream."));
        pkt->data->set(StatusClientId, SrsAmf0Any::str(RTMP_SIG_CLIENT_ID));
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send onStatus(NetStream.Publish.Start) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send onStatus(NetStream.Publish.Start) message success.");
    }
    
    srs_info("FMLE publish success.");
    
    return ret;
winlin authored
1295 1296
}
1297
int SrsRtmpServer::fmle_unpublish(int stream_id, double unpublish_tid)
winlin authored
1298
{
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 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
    int ret = ERROR_SUCCESS;
    
    // publish response onFCUnpublish(NetStream.unpublish.Success)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
        
        pkt->command_name = RTMP_AMF0_COMMAND_ON_FC_UNPUBLISH;
        pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodeUnpublishSuccess));
        pkt->data->set(StatusDescription, SrsAmf0Any::str("Stop publishing stream."));
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send onFCUnpublish(NetStream.unpublish.Success) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send onFCUnpublish(NetStream.unpublish.Success) message success.");
    }
    // FCUnpublish response
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsFMLEStartResPacket* pkt = new SrsFMLEStartResPacket(unpublish_tid);
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send FCUnpublish response message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send FCUnpublish response message success.");
    }
    // publish response onStatus(NetStream.Unpublish.Success)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
        
        pkt->data->set(StatusLevel, SrsAmf0Any::str(StatusLevelStatus));
        pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodeUnpublishSuccess));
        pkt->data->set(StatusDescription, SrsAmf0Any::str("Stream is now unpublished"));
        pkt->data->set(StatusClientId, SrsAmf0Any::str(RTMP_SIG_CLIENT_ID));
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send onStatus(NetStream.Unpublish.Success) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send onStatus(NetStream.Unpublish.Success) message success.");
    }
    
    srs_info("FMLE unpublish success.");
    
    return ret;
winlin authored
1353 1354
}
1355
int SrsRtmpServer::start_flash_publish(int stream_id)
winlin authored
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
    int ret = ERROR_SUCCESS;
    
    // publish response onStatus(NetStream.Publish.Start)
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket();
        
        pkt->data->set(StatusLevel, SrsAmf0Any::str(StatusLevelStatus));
        pkt->data->set(StatusCode, SrsAmf0Any::str(StatusCodePublishStart));
        pkt->data->set(StatusDescription, SrsAmf0Any::str("Started publishing stream."));
        pkt->data->set(StatusClientId, SrsAmf0Any::str(RTMP_SIG_CLIENT_ID));
        
        msg->set_packet(pkt, stream_id);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send onStatus(NetStream.Publish.Start) message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send onStatus(NetStream.Publish.Start) message success.");
    }
    
    srs_info("flash publish success.");
    
    return ret;
winlin authored
1381 1382
}
1383
int SrsRtmpServer::identify_create_stream_client(SrsCreateStreamPacket* req, int stream_id, SrsClientType& type, string& stream_name)
winlin authored
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
    int ret = ERROR_SUCCESS;
    
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsCreateStreamResPacket* pkt = new SrsCreateStreamResPacket(req->transaction_id, stream_id);
        
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send createStream response message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send createStream response message success.");
    }
    
    while (true) {
        SrsCommonMessage* msg = NULL;
        if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
            srs_error("recv identify client message failed. ret=%d", ret);
            return ret;
        }

        SrsAutoFree(SrsCommonMessage, msg, false);

        if (!msg->header.is_amf0_command() && !msg->header.is_amf3_command()) {
            srs_trace("identify ignore messages except "
                "AMF0/AMF3 command message. type=%#x", msg->header.message_type);
            continue;
        }
        
        if ((ret = msg->decode_packet(protocol)) != ERROR_SUCCESS) {
            srs_error("identify decode message failed. ret=%d", ret);
            return ret;
        }
        
        SrsPacket* pkt = msg->get_packet();
        if (dynamic_cast<SrsPlayPacket*>(pkt)) {
            srs_info("level1 identify client by play.");
            return identify_play_client(dynamic_cast<SrsPlayPacket*>(pkt), type, stream_name);
        }
        if (dynamic_cast<SrsPublishPacket*>(pkt)) {
            srs_info("identify client by publish, falsh publish.");
            return identify_flash_publish_client(dynamic_cast<SrsPublishPacket*>(pkt), type, stream_name);
        }
        
        srs_trace("ignore AMF0/AMF3 command message.");
    }
    
    return ret;
winlin authored
1434 1435
}
1436
int SrsRtmpServer::identify_fmle_publish_client(SrsFMLEStartPacket* req, SrsClientType& type, string& stream_name)
winlin authored
1437
{
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
    int ret = ERROR_SUCCESS;
    
    type = SrsClientFMLEPublish;
    stream_name = req->stream_name;
    
    // releaseStream response
    if (true) {
        SrsCommonMessage* msg = new SrsCommonMessage();
        SrsFMLEStartResPacket* pkt = new SrsFMLEStartResPacket(req->transaction_id);
        
        msg->set_packet(pkt, 0);
        
        if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
            srs_error("send releaseStream response message failed. ret=%d", ret);
            return ret;
        }
        srs_info("send releaseStream response message success.");
    }
    
    return ret;
winlin authored
1458 1459
}
1460
int SrsRtmpServer::identify_flash_publish_client(SrsPublishPacket* req, SrsClientType& type, string& stream_name)
winlin authored
1461
{
1462 1463 1464 1465 1466 1467
    int ret = ERROR_SUCCESS;
    
    type = SrsClientFlashPublish;
    stream_name = req->stream_name;
    
    return ret;
winlin authored
1468
}
1469
1470
int SrsRtmpServer::identify_play_client(SrsPlayPacket* req, SrsClientType& type, string& stream_name)
1471
{
1472 1473 1474 1475 1476 1477 1478 1479
    int ret = ERROR_SUCCESS;
    
    type = SrsClientPlay;
    stream_name = req->stream_name;
    
    srs_trace("identity client type=play, stream_name=%s", stream_name.c_str());

    return ret;
1480 1481
}