Blame view

trunk/src/app/srs_app_http_conn.cpp 37.0 KB
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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.
*/

#include <srs_app_http_conn.hpp>
26
#ifdef SRS_AUTO_HTTP_SERVER
27 28 29 30

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
31 32 33 34
#include <stdlib.h>

#include <sstream>
using namespace std;
35 36 37

#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
38
#include <srs_app_st_socket.hpp>
39 40
#include <srs_core_autofree.hpp>
#include <srs_app_config.hpp>
41
#include <srs_kernel_utility.hpp>
42 43
#include <srs_kernel_file.hpp>
#include <srs_kernel_flv.hpp>
44
#include <srs_rtmp_sdk.hpp>
45
#include <srs_app_source.hpp>
46
#include <srs_rtmp_msg_array.hpp>
47
#include <srs_kernel_aac.hpp>
48
#include <srs_kernel_mp3.hpp>
49
#include <srs_kernel_ts.hpp>
50
#include <srs_app_pithy_print.hpp>
51 52
#include <srs_app_source.hpp>
#include <srs_app_server.hpp>
53 54

SrsVodStream::SrsVodStream(string root_dir)
55
    : SrsHttpFileServer(root_dir)
56 57 58 59 60 61 62
{
}

SrsVodStream::~SrsVodStream()
{
}
63
int SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, SrsHttpMessage* r, string fullpath, int offset)
64 65 66 67 68 69 70 71 72 73 74
{
    int ret = ERROR_SUCCESS;
    
    SrsFileReader fs;
    
    // open flv file
    if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
        return ret;
    }
    
    if (offset > fs.filesize()) {
75
        ret = ERROR_HTTP_REMUX_OFFSET_OVERFLOW;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        srs_warn("http flv streaming %s overflow. size=%"PRId64", offset=%d, ret=%d", 
            fullpath.c_str(), fs.filesize(), offset, ret);
        return ret;
    }
    
    SrsFlvVodStreamDecoder ffd;
    
    // open fast decoder
    if ((ret = ffd.initialize(&fs)) != ERROR_SUCCESS) {
        return ret;
    }
    
    // save header, send later.
    char flv_header[13];
    
    // send flv header
    if ((ret = ffd.read_header_ext(flv_header)) != ERROR_SUCCESS) {
        return ret;
    }
    
    // save sequence header, send later
    char* sh_data = NULL;
    int sh_size = 0;
    
    if (true) {
        // send sequence header
        int64_t start = 0;
        if ((ret = ffd.read_sequence_header_summary(&start, &sh_size)) != ERROR_SUCCESS) {
            return ret;
        }
        if (sh_size <= 0) {
107
            ret = ERROR_HTTP_REMUX_SEQUENCE_HEADER;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            srs_warn("http flv streaming no sequence header. size=%d, ret=%d", sh_size, ret);
            return ret;
        }
    }
    sh_data = new char[sh_size];
    SrsAutoFree(char, sh_data);
    if ((ret = fs.read(sh_data, sh_size, NULL)) != ERROR_SUCCESS) {
        return ret;
    }

    // seek to data offset
    int64_t left = fs.filesize() - offset;

    // write http header for ts.
    w->header()->set_content_length((int)(sizeof(flv_header) + sh_size + left));
    w->header()->set_content_type("video/x-flv");
    
    // write flv header and sequence header.
    if ((ret = w->write(flv_header, sizeof(flv_header))) != ERROR_SUCCESS) {
        return ret;
    }
    if (sh_size > 0 && (ret = w->write(sh_data, sh_size)) != ERROR_SUCCESS) {
        return ret;
    }
    
    // write body.
    if ((ret = ffd.lseek(offset)) != ERROR_SUCCESS) {
        return ret;
    }
    
    // send data
winlin authored
139
    if ((ret = copy(w, &fs, r, (int)left)) != ERROR_SUCCESS) {
140 141
        srs_warn("read flv=%s size=%d failed, ret=%d", fullpath.c_str(), left, ret);
        return ret;
142 143 144 145 146
    }
    
    return ret;
}
147
int SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, SrsHttpMessage* r, string fullpath, int start, int end)
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
    int ret = ERROR_SUCCESS;

    srs_assert(start >= 0);
    srs_assert(end == -1 || end >= 0);
    
    SrsFileReader fs;
    
    // open flv file
    if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
        return ret;
    }

    // parse -1 to whole file.
    if (end == -1) {
winlin authored
163
        end = (int)fs.filesize();
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    }
    
    if (end > fs.filesize() || start > end) {
        ret = ERROR_HTTP_REMUX_OFFSET_OVERFLOW;
        srs_warn("http mp4 streaming %s overflow. size=%"PRId64", offset=%d, ret=%d", 
            fullpath.c_str(), fs.filesize(), start, ret);
        return ret;
    }

    // seek to data offset, [start, end] for range.
    int64_t left = end - start + 1;

    // write http header for ts.
    w->header()->set_content_length(left);
    w->header()->set_content_type("video/mp4");

    // status code 206 to make dash.as happy.
    w->write_header(SRS_CONSTS_HTTP_PartialContent);

    // response the content range header.
    std::stringstream content_range;
    content_range << "bytes " << start << "-" << end << "/" << fs.filesize();
    w->header()->set("Content-Range", content_range.str());
    
    // write body.
    fs.lseek(start);
    
    // send data
winlin authored
192
    if ((ret = copy(w, &fs, r, (int)left)) != ERROR_SUCCESS) {
193 194
        srs_warn("read mp4=%s size=%d failed, ret=%d", fullpath.c_str(), left, ret);
        return ret;
195 196 197 198
    }
    
    return ret;
}
199
200
SrsStreamCache::SrsStreamCache(SrsSource* s, SrsRequest* r)
201
{
202
    req = r->copy();
203
    source = s;
204 205
    queue = new SrsMessageQueue(true);
    pthread = new SrsThread("http-stream", this, 0, false);
206 207 208 209 210 211
}

SrsStreamCache::~SrsStreamCache()
{
    pthread->stop();
    srs_freep(pthread);
212 213
    
    srs_freep(queue);
214
    srs_freep(req);
215 216 217 218 219 220 221 222 223 224
}

int SrsStreamCache::start()
{
    return pthread->start();
}

int SrsStreamCache::dump_cache(SrsConsumer* consumer)
{
    int ret = ERROR_SUCCESS;
225 226 227 228 229 230 231

    double fast_cache = _srs_config->get_vhost_http_remux_fast_cache(req->vhost);

    if (fast_cache <= 0) {
        srs_info("http: ignore dump fast cache.");
        return ret;
    }
232
    
233
    // TODO: FIXME: config it.
234 235 236 237
    if ((ret = queue->dump_packets(consumer, false, 0, 0, SrsRtmpJitterAlgorithmOFF)) != ERROR_SUCCESS) {
        return ret;
    }
    
238
    srs_trace("http: dump cache %d msgs, duration=%dms, cache=%.2fs", 
239
        queue->size(), queue->duration(), fast_cache);
240
    
241 242 243 244 245 246
    return ret;
}

int SrsStreamCache::cycle()
{
    int ret = ERROR_SUCCESS;
247 248 249 250 251 252 253
    
    SrsConsumer* consumer = NULL;
    if ((ret = source->create_consumer(consumer, false, false, true)) != ERROR_SUCCESS) {
        srs_error("http: create consumer failed. ret=%d", ret);
        return ret;
    }
    SrsAutoFree(SrsConsumer, consumer);
254 255 256

    SrsPithyPrint* pprint = SrsPithyPrint::create_http_stream_cache();
    SrsAutoFree(SrsPithyPrint, pprint);
257 258
    
    SrsMessageArray msgs(SRS_PERF_MW_MSGS);
259
260
    // TODO: FIXME: support reload.
261 262 263 264
    double fast_cache = _srs_config->get_vhost_http_remux_fast_cache(req->vhost);
    if (fast_cache > 0) {
        queue->set_queue_size(fast_cache);
    }
265 266
    
    while (true) {
267 268
        pprint->elapse();
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
        // get messages from consumer.
        // each msg in msgs.msgs must be free, for the SrsMessageArray never free them.
        int count = 0;
        if ((ret = consumer->dump_packets(&msgs, count)) != ERROR_SUCCESS) {
            srs_error("http: get messages from consumer failed. ret=%d", ret);
            return ret;
        }
        
        if (count <= 0) {
            srs_info("http: mw sleep %dms for no msg", mw_sleep);
            // directly use sleep, donot use consumer wait.
            st_usleep(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
            
            // ignore when nothing got.
            continue;
        }
285 286 287

        if (pprint->can_print()) {
            srs_trace("-> "SRS_CONSTS_LOG_HTTP_STREAM_CACHE" http: got %d msgs, age=%d, min=%d, mw=%d", 
tufang14 authored
288
                count, pprint->age(), SRS_PERF_MW_MIN_MSGS, SRS_CONSTS_RTMP_PULSE_TIMEOUT_US / 1000);
289
        }
290 291 292 293
    
        // free the messages.
        for (int i = 0; i < count; i++) {
            SrsSharedPtrMessage* msg = msgs.msgs[i];
294 295 296 297 298
            if (fast_cache > 0) {
                queue->enqueue(msg);
            } else {
                srs_freep(msg);
            }
299 300 301
        }
    }
    
302 303 304
    return ret;
}
305 306 307 308 309 310 311 312
ISrsStreamEncoder::ISrsStreamEncoder()
{
}

ISrsStreamEncoder::~ISrsStreamEncoder()
{
}
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
SrsTsStreamEncoder::SrsTsStreamEncoder()
{
    enc = new SrsTsEncoder();
}

SrsTsStreamEncoder::~SrsTsStreamEncoder()
{
    srs_freep(enc);
}

int SrsTsStreamEncoder::initialize(SrsFileWriter* w, SrsStreamCache* /*c*/)
{
    int ret = ERROR_SUCCESS;
    
    if ((ret = enc->initialize(w)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}

int SrsTsStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{
    return enc->write_audio(timestamp, data, size);
}

int SrsTsStreamEncoder::write_video(int64_t timestamp, char* data, int size)
{
    return enc->write_video(timestamp, data, size);
}

int SrsTsStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
    return ERROR_SUCCESS;
}

bool SrsTsStreamEncoder::has_cache()
{
    // for ts stream, use gop cache of SrsSource is ok.
    return false;
}

int SrsTsStreamEncoder::dump_cache(SrsConsumer* /*consumer*/)
{
    // for ts stream, ignore cache.
    return ERROR_SUCCESS;
}
361 362 363 364 365 366 367 368 369 370
SrsFlvStreamEncoder::SrsFlvStreamEncoder()
{
    enc = new SrsFlvEncoder();
}

SrsFlvStreamEncoder::~SrsFlvStreamEncoder()
{
    srs_freep(enc);
}
371
int SrsFlvStreamEncoder::initialize(SrsFileWriter* w, SrsStreamCache* /*c*/)
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
{
    int ret = ERROR_SUCCESS;
    
    if ((ret = enc->initialize(w)) != ERROR_SUCCESS) {
        return ret;
    }
    
    // write flv header.
    if ((ret = enc->write_header())  != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}

int SrsFlvStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{
    return enc->write_audio(timestamp, data, size);
}

int SrsFlvStreamEncoder::write_video(int64_t timestamp, char* data, int size)
{
    return enc->write_video(timestamp, data, size);
}

int SrsFlvStreamEncoder::write_metadata(int64_t timestamp, char* data, int size)
{
399
    return enc->write_metadata(SrsCodecFlvTagScript, data, size);
400 401
}
402 403 404 405 406 407 408 409 410 411 412 413
bool SrsFlvStreamEncoder::has_cache()
{
    // for flv stream, use gop cache of SrsSource is ok.
    return false;
}

int SrsFlvStreamEncoder::dump_cache(SrsConsumer* /*consumer*/)
{
    // for flv stream, ignore cache.
    return ERROR_SUCCESS;
}
414 415 416
SrsAacStreamEncoder::SrsAacStreamEncoder()
{
    enc = new SrsAacEncoder();
417
    cache = NULL;
418 419 420 421 422 423 424
}

SrsAacStreamEncoder::~SrsAacStreamEncoder()
{
    srs_freep(enc);
}
425
int SrsAacStreamEncoder::initialize(SrsFileWriter* w, SrsStreamCache* c)
426 427 428
{
    int ret = ERROR_SUCCESS;
    
429 430
    cache = c;
    
431 432 433 434 435 436 437 438 439 440 441 442
    if ((ret = enc->initialize(w)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}

int SrsAacStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{
    return enc->write_audio(timestamp, data, size);
}
443
int SrsAacStreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
444
{
445 446
    // aac ignore any flv video.
    return ERROR_SUCCESS;
447 448
}
449
int SrsAacStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
450
{
451 452
    // aac ignore any flv metadata.
    return ERROR_SUCCESS;
453 454
}
455 456 457 458 459 460 461 462 463 464 465
bool SrsAacStreamEncoder::has_cache()
{
    return true;
}

int SrsAacStreamEncoder::dump_cache(SrsConsumer* consumer)
{
    srs_assert(cache);
    return cache->dump_cache(consumer);
}
466 467 468
SrsMp3StreamEncoder::SrsMp3StreamEncoder()
{
    enc = new SrsMp3Encoder();
469
    cache = NULL;
470 471 472 473 474 475 476
}

SrsMp3StreamEncoder::~SrsMp3StreamEncoder()
{
    srs_freep(enc);
}
477
int SrsMp3StreamEncoder::initialize(SrsFileWriter* w, SrsStreamCache* c)
478 479 480
{
    int ret = ERROR_SUCCESS;
    
481 482
    cache = c;
    
483 484 485 486
    if ((ret = enc->initialize(w)) != ERROR_SUCCESS) {
        return ret;
    }
    
487 488 489 490
    if ((ret = enc->write_header()) != ERROR_SUCCESS) {
        return ret;
    }
    
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    return ret;
}

int SrsMp3StreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{
    return enc->write_audio(timestamp, data, size);
}

int SrsMp3StreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
    // mp3 ignore any flv video.
    return ERROR_SUCCESS;
}

int SrsMp3StreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
    // mp3 ignore any flv metadata.
    return ERROR_SUCCESS;
}
511 512 513 514 515 516 517 518 519 520 521
bool SrsMp3StreamEncoder::has_cache()
{
    return true;
}

int SrsMp3StreamEncoder::dump_cache(SrsConsumer* consumer)
{
    srs_assert(cache);
    return cache->dump_cache(consumer);
}
522
SrsStreamWriter::SrsStreamWriter(ISrsHttpResponseWriter* w)
523 524 525 526
{
    writer = w;
}
527
SrsStreamWriter::~SrsStreamWriter()
528 529 530
{
}
531
int SrsStreamWriter::open(std::string /*file*/)
532 533 534 535
{
    return ERROR_SUCCESS;
}
536
void SrsStreamWriter::close()
537 538 539
{
}
540
bool SrsStreamWriter::is_open()
541 542 543 544
{
    return true;
}
545
int64_t SrsStreamWriter::tellg()
546 547 548 549
{
    return 0;
}
550
int SrsStreamWriter::write(void* buf, size_t count, ssize_t* pnwrite)
551 552 553 554 555 556 557
{
    if (pnwrite) {
        *pnwrite = count;
    }
    return writer->write((char*)buf, (int)count);
}
558
SrsLiveStream::SrsLiveStream(SrsSource* s, SrsRequest* r, SrsStreamCache* c)
winlin authored
559
{
560
    source = s;
561
    cache = c;
562
    req = r->copy();
winlin authored
563 564 565 566
}

SrsLiveStream::~SrsLiveStream()
{
567
    srs_freep(req);
winlin authored
568 569
}
570
int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r)
winlin authored
571 572
{
    int ret = ERROR_SUCCESS;
573
    
574
    ISrsStreamEncoder* enc = NULL;
575 576 577
    
    srs_assert(entry);
    if (srs_string_ends_with(entry->pattern, ".flv")) {
578 579
        w->header()->set_content_type("video/x-flv");
        enc = new SrsFlvStreamEncoder();
580
    } else if (srs_string_ends_with(entry->pattern, ".aac")) {
581 582
        w->header()->set_content_type("audio/x-aac");
        enc = new SrsAacStreamEncoder();
583
    } else if (srs_string_ends_with(entry->pattern, ".mp3")) {
584 585
        w->header()->set_content_type("audio/mpeg");
        enc = new SrsMp3StreamEncoder();
586 587 588
    } else if (srs_string_ends_with(entry->pattern, ".ts")) {
        w->header()->set_content_type("video/MP2T");
        enc = new SrsTsStreamEncoder();
589 590 591 592 593
    } else {
        ret = ERROR_HTTP_LIVE_STREAM_EXT;
        srs_error("http: unsupported pattern %s", entry->pattern.c_str());
        return ret;
    }
594
    SrsAutoFree(ISrsStreamEncoder, enc);
595
    
596
    // create consumer of souce, ignore gop cache, use the audio gop cache.
597
    SrsConsumer* consumer = NULL;
598
    if ((ret = source->create_consumer(consumer, true, true, !enc->has_cache())) != ERROR_SUCCESS) {
599 600 601 602 603
        srs_error("http: create consumer failed. ret=%d", ret);
        return ret;
    }
    SrsAutoFree(SrsConsumer, consumer);
    srs_verbose("http: consumer created success.");
604 605 606

    SrsPithyPrint* pprint = SrsPithyPrint::create_http_stream();
    SrsAutoFree(SrsPithyPrint, pprint);
607 608 609 610
    
    SrsMessageArray msgs(SRS_PERF_MW_MSGS);
    
    // the memory writer.
611
    SrsStreamWriter writer(w);
612 613
    if ((ret = enc->initialize(&writer, cache)) != ERROR_SUCCESS) {
        srs_error("http: initialize stream encoder failed. ret=%d", ret);
614 615 616
        return ret;
    }
    
617 618 619 620 621 622 623 624
    // if gop cache enabled for encoder, dump to consumer.
    if (enc->has_cache()) {
        if ((ret = enc->dump_cache(consumer)) != ERROR_SUCCESS) {
            srs_error("http: dump cache to consumer failed. ret=%d", ret);
            return ret;
        }
    }
    
625
    while (true) {
626 627
        pprint->elapse();
628 629 630 631
        // get messages from consumer.
        // each msg in msgs.msgs must be free, for the SrsMessageArray never free them.
        int count = 0;
        if ((ret = consumer->dump_packets(&msgs, count)) != ERROR_SUCCESS) {
632
            srs_error("http: get messages from consumer failed. ret=%d", ret);
633 634 635 636
            return ret;
        }
        
        if (count <= 0) {
637
            srs_info("http: mw sleep %dms for no msg", mw_sleep);
638 639 640 641 642 643
            // directly use sleep, donot use consumer wait.
            st_usleep(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
            
            // ignore when nothing got.
            continue;
        }
644 645 646

        if (pprint->can_print()) {
            srs_info("-> "SRS_CONSTS_LOG_HTTP_STREAM" http: got %d msgs, age=%d, min=%d, mw=%d", 
tufang14 authored
647
                count, pprint->age(), SRS_PERF_MW_MIN_MSGS, SRS_CONSTS_RTMP_PULSE_TIMEOUT_US / 1000);
648
        }
649 650
        
        // sendout all messages.
651
        ret = streaming_send_messages(enc, msgs.msgs, count);
652 653 654 655 656 657 658 659 660 661
    
        // free the messages.
        for (int i = 0; i < count; i++) {
            SrsSharedPtrMessage* msg = msgs.msgs[i];
            srs_freep(msg);
        }
        
        // check send error code.
        if (ret != ERROR_SUCCESS) {
            if (!srs_is_client_gracefully_close(ret)) {
662
                srs_error("http: send messages to client failed. ret=%d", ret);
663 664 665 666 667 668 669 670
            }
            return ret;
        }
    }
    
    return ret;
}
671
int SrsLiveStream::streaming_send_messages(ISrsStreamEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs)
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
{
    int ret = ERROR_SUCCESS;
    
    for (int i = 0; i < nb_msgs; i++) {
        SrsSharedPtrMessage* msg = msgs[i];
        
        if (msg->is_audio()) {
            ret = enc->write_audio(msg->timestamp, msg->payload, msg->size);
        } else if (msg->is_video()) {
            ret = enc->write_video(msg->timestamp, msg->payload, msg->size);
        } else {
            ret = enc->write_metadata(msg->timestamp, msg->payload, msg->size);
        }
        
        if (ret != ERROR_SUCCESS) {
            return ret;
        }
    }
    
winlin authored
691 692 693
    return ret;
}
694
SrsLiveEntry::SrsLiveEntry(std::string m, bool h)
695
{
696 697 698
    mount = m;
    hstrs = h;
    
699
    stream = NULL;
700
    cache = NULL;
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
    
    std::string ext;
    size_t pos = string::npos;
    if ((pos = m.rfind(".")) != string::npos) {
        ext = m.substr(pos);
    }
    _is_flv = (ext == ".flv");
    _is_ts = (ext == ".ts");
    _is_mp3 = (ext == ".mp3");
    _is_aac = (ext == ".aac");
}

bool SrsLiveEntry::is_flv()
{
    return _is_flv;
}

bool SrsLiveEntry::is_ts()
{
    return _is_ts;
}

bool SrsLiveEntry::is_aac()
{
    return _is_aac;
}

bool SrsLiveEntry::is_mp3()
{
    return _is_mp3;
731 732
}
733 734 735 736 737 738 739 740 741 742 743 744 745
SrsHlsM3u8Stream::SrsHlsM3u8Stream()
{
}

SrsHlsM3u8Stream::~SrsHlsM3u8Stream()
{
}

void SrsHlsM3u8Stream::set_m3u8(std::string v)
{
    m3u8 = v;
}
746
int SrsHlsM3u8Stream::serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r)
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
{
    int ret = ERROR_SUCCESS;
    
    std::string data = m3u8;
    
    w->header()->set_content_length((int)data.length());
    w->header()->set_content_type("application/x-mpegURL;charset=utf-8");

    if ((ret = w->write((char*)data.data(), (int)data.length())) != ERROR_SUCCESS) {
        if (!srs_is_client_gracefully_close(ret)) {
            srs_error("send m3u8 failed. ret=%d", ret);
        }
        return ret;
    }

    return ret;
}

SrsHlsTsStream::SrsHlsTsStream()
{
}

SrsHlsTsStream::~SrsHlsTsStream()
{
}

void SrsHlsTsStream::set_ts(std::string v)
{
    ts = v;
}
778
int SrsHlsTsStream::serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r)
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
{
    int ret = ERROR_SUCCESS;
    
    std::string data = ts;
    
    w->header()->set_content_length((int)data.length());
    w->header()->set_content_type("video/MP2T");

    if ((ret = w->write((char*)data.data(), (int)data.length())) != ERROR_SUCCESS) {
        if (!srs_is_client_gracefully_close(ret)) {
            srs_error("send ts failed. ret=%d", ret);
        }
        return ret;
    }

    return ret;
}

SrsHlsEntry::SrsHlsEntry()
{
}
801
SrsHttpServer::SrsHttpServer(SrsServer* svr)
802
{
803 804
    server = svr;
    
805
    mux.hijack(this);
806 807
}
808
SrsHttpServer::~SrsHttpServer()
809
{
810 811
    mux.unhijack(this);
    
812 813
    if (true) {
        std::map<std::string, SrsLiveEntry*>::iterator it;
814
        for (it = tflvs.begin(); it != tflvs.end(); ++it) {
815 816 817
            SrsLiveEntry* entry = it->second;
            srs_freep(entry);
        }
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
        tflvs.clear();
    }
    if (true) {
        std::map<std::string, SrsLiveEntry*>::iterator it;
        for (it = sflvs.begin(); it != sflvs.end(); ++it) {
            SrsLiveEntry* entry = it->second;
            srs_freep(entry);
        }
        sflvs.clear();
    }
    if (true) {
        std::map<std::string, SrsHlsEntry*>::iterator it;
        for (it = thls.begin(); it != thls.end(); ++it) {
            SrsHlsEntry* entry = it->second;
            srs_freep(entry);
        }
        thls.clear();
835 836 837
    }
    if (true) {
        std::map<std::string, SrsHlsEntry*>::iterator it;
838
        for (it = shls.begin(); it != shls.end(); ++it) {
839 840 841
            SrsHlsEntry* entry = it->second;
            srs_freep(entry);
        }
842
        shls.clear();
843
    }
844 845
}
846
int SrsHttpServer::initialize()
847 848
{
    int ret = ERROR_SUCCESS;
849
    
winlin authored
850 851
    // static file
    // flv vod streaming.
852
    if ((ret = initialize_static_file()) != ERROR_SUCCESS) {
winlin authored
853 854 855 856
        return ret;
    }
    
    // remux rtmp to flv live streaming
857 858 859 860 861 862
    if ((ret = initialize_flv_streaming()) != ERROR_SUCCESS) {
        return ret;
    }
    
    // remux rtmp to hls live streaming
    if ((ret = initialize_hls_streaming()) != ERROR_SUCCESS) {
winlin authored
863 864 865 866 867 868
        return ret;
    }
    
    return ret;
}
869
int SrsHttpServer::http_mount(SrsSource* s, SrsRequest* r)
870 871 872
{
    int ret = ERROR_SUCCESS;
    
873 874 875
    // the id to identify stream.
    std::string sid = r->get_stream_url();
    SrsLiveEntry* entry = NULL;
876
    
877 878 879 880 881 882
    // create stream from template when not found.
    if (sflvs.find(sid) == sflvs.end()) {
        if (tflvs.find(r->vhost) == tflvs.end()) {
            srs_info("ignore mount flv stream for disabled");
            return ret;
        }
883
    
884
        SrsLiveEntry* tmpl = tflvs[r->vhost];
885
886
        std::string mount = tmpl->mount;
887
    
888 889 890 891
        // replace the vhost variable
        mount = srs_string_replace(mount, "[vhost]", r->vhost);
        mount = srs_string_replace(mount, "[app]", r->app);
        mount = srs_string_replace(mount, "[stream]", r->stream);
892
    
893 894 895
        // remove the default vhost mount
        mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
        
896
        entry = new SrsLiveEntry(mount, tmpl->hstrs);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
    
        entry->cache = new SrsStreamCache(s, r);
        entry->stream = new SrsLiveStream(s, r, entry->cache);
        
        sflvs[sid] = entry;
        
        // start http stream cache thread
        if ((ret = entry->cache->start()) != ERROR_SUCCESS) {
            srs_error("http: start stream cache failed. ret=%d", ret);
            return ret;
        }
        
        // mount the http flv stream.
        if ((ret = mux.handle(mount, entry->stream)) != ERROR_SUCCESS) {
            srs_error("http: mount flv stream for vhost=%s failed. ret=%d", sid.c_str(), ret);
            return ret;
        }
        srs_trace("http: mount flv stream for vhost=%s, mount=%s", sid.c_str(), mount.c_str());
    } else {
        entry = sflvs[sid];
917
    }
918
    
919 920 921
    // TODO: FIXME: supports reload.
    if (entry->stream) {
        entry->stream->entry->enabled = true;
922 923 924 925 926 927
        return ret;
    }
    
    return ret;
}
928
void SrsHttpServer::http_unmount(SrsSource* s, SrsRequest* r)
929
{
930 931 932
    std::string sid = r->get_stream_url();
    
    if (sflvs.find(sid) == sflvs.end()) {
933 934 935
        srs_info("ignore unmount flv stream for disabled");
        return;
    }
936
937
    SrsLiveEntry* entry = sflvs[sid];
938
    entry->stream->entry->enabled = false;
939 940
}
941 942 943 944
int SrsHttpServer::mount_hls(SrsRequest* r)
{
    int ret = ERROR_SUCCESS;
    
945 946 947
    std::string sid = r->get_stream_url();
    
    if (shls.find(sid) == shls.end()) {
948 949 950 951
        srs_info("ignore mount hls stream for disabled");
        return ret;
    }
    
952
    SrsHlsEntry* entry = shls[sid];
953 954
    
    // TODO: FIXME: supports reload.
955
    std::map<std::string, ISrsHttpHandler*>::iterator it;
956
    for (it = entry->streams.begin(); it != entry->streams.end(); ++it) {
957
        ISrsHttpHandler* stream = it->second;
958 959 960 961 962 963 964 965 966
        stream->entry->enabled = true;
    }

    return ret;
}

int SrsHttpServer::hls_update_m3u8(SrsRequest* r, string m3u8)
{
    int ret = ERROR_SUCCESS;
967
tufang14 authored
968
    std::string mount;
969
    
970 971
    std::string sid = r->get_stream_url();
    SrsHlsEntry* entry = NULL;
winlin authored
972
    
973 974 975 976
    // create stream from template when not found.
    if (shls.find(sid) == shls.end()) {
        if (thls.find(r->vhost) == thls.end()) {
            srs_info("ignore mount hls stream for disabled");
977 978
            return ret;
        }
979 980 981 982
    
        SrsHlsEntry* tmpl = thls[r->vhost];
        
        entry = new SrsHlsEntry();
tufang14 authored
983
        mount = tmpl->mount;
984
        
tufang14 authored
985 986 987 988 989 990 991 992 993
        // replace the vhost variable
        mount = srs_string_replace(mount, "[vhost]", r->vhost);
        mount = srs_string_replace(mount, "[app]", r->app);
        mount = srs_string_replace(mount, "[stream]", r->stream);
    
        // remove the default vhost mount
        mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
        
        entry->mount = mount;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        shls[sid] = entry;
    
        if (entry->streams.find(mount) == entry->streams.end()) {
            ISrsHttpHandler* he = new SrsHlsM3u8Stream();
            entry->streams[mount] = he;
    
            if ((ret = mux.handle(mount, he)) != ERROR_SUCCESS) {
                srs_error("handle mount=%s failed. ret=%d", mount.c_str(), ret);
                return ret;
            }
        }
    } else {
        entry = shls[sid];
1007 1008
    }
tufang14 authored
1009 1010
    mount = entry->mount;
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
    // update the m3u8 stream.
    SrsHlsM3u8Stream* hms = dynamic_cast<SrsHlsM3u8Stream*>(entry->streams[mount]);
    if (hms) {
        hms->set_m3u8(m3u8);
    }
    srs_trace("hls update m3u8 ok, mount=%s", mount.c_str());

    return ret;
}

int SrsHttpServer::hls_update_ts(SrsRequest* r, string uri, string ts)
{
    int ret = ERROR_SUCCESS;
    
1025 1026
    std::string sid = r->get_stream_url();
    
winlin authored
1027
    // when no hls mounted, ignore.
1028
    if (shls.find(sid) == shls.end()) {
winlin authored
1029 1030 1031
        return ret;
    }
1032
    SrsHlsEntry* entry = shls[sid];
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    srs_assert(entry);

    std::string mount = entry->mount;
    
    // the ts is relative from the m3u8, the same start dir.
    size_t pos = string::npos;
    if ((pos = mount.rfind("/")) != string::npos) {
        mount = mount.substr(0, pos);
    }

    // replace the vhost variable
    mount = srs_string_replace(mount, "[vhost]", r->vhost);
    mount = srs_string_replace(mount, "[app]", r->app);

    // remove the default vhost mount
    mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");

    // mount with ts.
    mount += "/";
    mount += uri;

    if (entry->streams.find(mount) == entry->streams.end()) {
1055
        ISrsHttpHandler* he = new SrsHlsTsStream();
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
        entry->streams[mount] = he;

        if ((ret = mux.handle(mount, he)) != ERROR_SUCCESS) {
            srs_error("handle mount=%s failed. ret=%d", mount.c_str(), ret);
            return ret;
        }
    }

    // update the ts stream.
    SrsHlsTsStream* hts = dynamic_cast<SrsHlsTsStream*>(entry->streams[mount]);
    if (hts) {
        hts->set_ts(ts);
    }
    srs_trace("hls update ts ok, mount=%s", mount.c_str());

    return ret;
}

void SrsHttpServer::unmount_hls(SrsRequest* r)
{
1076 1077 1078
    std::string sid = r->get_stream_url();
    
    if (shls.find(sid) == shls.end()) {
1079 1080 1081 1082
        srs_info("ignore unmount hls stream for disabled");
        return;
    }
1083
    SrsHlsEntry* entry = shls[sid];
1084
1085
    std::map<std::string, ISrsHttpHandler*>::iterator it;
1086
    for (it = entry->streams.begin(); it != entry->streams.end(); ++it) {
1087
        ISrsHttpHandler* stream = it->second;
1088 1089 1090 1091
        stream->entry->enabled = false;
    }
}
winlin authored
1092 1093 1094 1095 1096 1097 1098
int SrsHttpServer::on_reload_vhost_http_updated()
{
    int ret = ERROR_SUCCESS;
    // TODO: FIXME: implements it.
    return ret;
}
1099
int SrsHttpServer::on_reload_vhost_http_remux_updated()
winlin authored
1100 1101 1102 1103 1104 1105
{
    int ret = ERROR_SUCCESS;
    // TODO: FIXME: implements it.
    return ret;
}
1106 1107 1108 1109 1110 1111 1112
int SrsHttpServer::on_reload_vhost_hls(string vhost)
{
    int ret = ERROR_SUCCESS;
    // TODO: FIXME: implements it.
    return ret;
}
1113 1114 1115 1116 1117
int SrsHttpServer::hijack(SrsHttpMessage* request, ISrsHttpHandler** ph)
{
    int ret = ERROR_SUCCESS;
    
    // when handler not the root, we think the handler is ok.
tufang14 authored
1118 1119
    ISrsHttpHandler* h = *ph? *ph : NULL;
    if (h && h->entry && h->entry->pattern != "/") {
1120 1121 1122 1123 1124 1125 1126 1127
        return ret;
    }
    
    // only hijack for http streaming, http-flv/ts/mp3/aac.
    std::string ext = request->ext();
    if (ext.empty()) {
        return ret;
    }
1128 1129 1130 1131
    
    // find the actually request vhost.
    SrsConfDirective* vhost = _srs_config->get_vhost(request->host());
    if (!vhost || !_srs_config->get_vhost_enabled(vhost)) {
1132 1133 1134
        return ret;
    }
    
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 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
    // find the entry template for the stream.
    SrsLiveEntry* entry = NULL;
    if (true) {
        // no http streaming on vhost, ignore.
        std::map<std::string, SrsLiveEntry*>::iterator it = tflvs.find(vhost->arg0());
        if (it == tflvs.end()) {
            return ret;
        }
        
        // hstrs not enabled, ignore.
        entry = it->second;
        if (!entry->hstrs) {
            return ret;
        }

        // check entry and request extension.
        if (entry->is_flv()) {
            if (ext != ".flv") {
                return ret;
            }
        } else if (entry->is_ts()) {
            if (ext != ".ts") {
                return ret;
            }
        } else if (entry->is_mp3()) {
            if (ext != ".mp3") {
                return ret;
            }
        } else if (entry->is_aac()) {
            if (ext != ".aac") {
                return ret;
            }
        } else {
            return ret;
        }
    }
    
    // hijack for entry.
    SrsRequest* r = request->to_request(vhost->arg0());
    SrsAutoFree(SrsRequest, r);
    SrsSource* s = SrsSource::fetch(r);
    if (!s) {
        if ((ret = SrsSource::create(r, server, server, &s)) != ERROR_SUCCESS) {
            return ret;
        }
    }
    srs_assert(s != NULL);
    
    // create http streaming handler.
    if ((ret = http_mount(s, r)) != ERROR_SUCCESS) {
        return ret;
    }
    
    // use the handler if exists.
    if (ph) {
        std::string sid = r->get_stream_url();
        if (sflvs.find(sid) != sflvs.end()) {
            entry = sflvs[sid];
            *ph = entry->stream;
1194 1195 1196 1197 1198 1199 1200 1201
        }
    }
    
    // trigger edge to fetch from origin.
    bool vhost_is_edge = _srs_config->get_vhost_is_edge(r->vhost);
    srs_trace("hstrs: source url=%s, is_edge=%d, source_id=%d[%d]",
        r->get_stream_url().c_str(), vhost_is_edge, s->source_id(), s->source_id());
    
winlin authored
1202
    // TODO: FIXME: disconnect when all connection closed.
1203 1204 1205 1206 1207
    if (vhost_is_edge) {
        // notice edge to start for the first client.
        if ((ret = s->on_edge_start_play()) != ERROR_SUCCESS) {
            srs_error("notice edge start play stream failed. ret=%d", ret);
            return ret;
1208 1209 1210
        }
    }
    
1211 1212 1213
    return ret;
}
1214
int SrsHttpServer::initialize_static_file()
winlin authored
1215 1216 1217
{
    int ret = ERROR_SUCCESS;
    
1218
    bool default_root_exists = false;
1219
    
winlin authored
1220
    // http static file and flv vod stream mount for each vhost.
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
    SrsConfDirective* root = _srs_config->get_root();
    for (int i = 0; i < (int)root->directives.size(); i++) {
        SrsConfDirective* conf = root->at(i);
        
        if (!conf->is_vhost()) {
            continue;
        }
        
        std::string vhost = conf->arg0();
        if (!_srs_config->get_vhost_http_enabled(vhost)) {
            continue;
        }
        
        std::string mount = _srs_config->get_vhost_http_mount(vhost);
        std::string dir = _srs_config->get_vhost_http_dir(vhost);
1236 1237 1238

        // replace the vhost variable
        mount = srs_string_replace(mount, "[vhost]", vhost);
winlin authored
1239 1240

        // remove the default vhost mount
1241
        mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
1242
        
1243 1244 1245 1246 1247
        // the dir mount must always ends with "/"
        if (mount != "/" && mount.rfind("/") != mount.length() - 1) {
            mount += "/";
        }
        
1248
        // mount the http of vhost.
1249
        if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) {
1250 1251 1252
            srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret);
            return ret;
        }
1253 1254 1255
        
        if (mount == "/") {
            default_root_exists = true;
1256
            srs_warn("http: root mount to %s", dir.c_str());
1257
        }
1258
        srs_trace("http: vhost=%s mount to %s", vhost.c_str(), mount.c_str());
1259 1260 1261 1262
    }
    
    if (!default_root_exists) {
        // add root
1263
        std::string dir = _srs_config->get_http_stream_dir();
1264
        if ((ret = mux.handle("/", new SrsVodStream(dir))) != ERROR_SUCCESS) {
1265
            srs_error("http: mount root dir=%s failed. ret=%d", dir.c_str(), ret);
1266 1267
            return ret;
        }
1268
        srs_trace("http: root mount to %s", dir.c_str());
1269
    }
1270 1271 1272 1273
    
    return ret;
}
1274
int SrsHttpServer::initialize_flv_streaming()
1275 1276
{
    int ret = ERROR_SUCCESS;
winlin authored
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    
    // http flv live stream mount for each vhost.
    SrsConfDirective* root = _srs_config->get_root();
    for (int i = 0; i < (int)root->directives.size(); i++) {
        SrsConfDirective* conf = root->at(i);
        
        if (!conf->is_vhost()) {
            continue;
        }
        
        std::string vhost = conf->arg0();
1288
        if (!_srs_config->get_vhost_http_remux_enabled(vhost)) {
winlin authored
1289 1290 1291
            continue;
        }
        
1292 1293 1294 1295
        SrsLiveEntry* entry = new SrsLiveEntry(
            _srs_config->get_vhost_http_remux_mount(vhost),
            _srs_config->get_vhost_http_remux_hstrs(vhost)
        );
1296
        tflvs[vhost] = entry;
winlin authored
1297
        srs_trace("http flv live stream, vhost=%s, mount=%s", 
1298
            vhost.c_str(), entry->mount.c_str());
winlin authored
1299 1300
    }
    
1301 1302 1303
    return ret;
}
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
int SrsHttpServer::initialize_hls_streaming()
{
    int ret = ERROR_SUCCESS;
    
    // http hls live stream mount for each vhost.
    SrsConfDirective* root = _srs_config->get_root();
    for (int i = 0; i < (int)root->directives.size(); i++) {
        SrsConfDirective* conf = root->at(i);
        
        if (!conf->is_vhost()) {
            continue;
        }
        
        std::string vhost = conf->arg0();
        if (!_srs_config->get_hls_enabled(vhost)) {
            continue;
        }

        std::string storage = _srs_config->get_hls_storage(vhost);
        if (storage != "ram" && storage != "both") {
            continue;
        }
        
        SrsHlsEntry* entry = new SrsHlsEntry();
        entry->mount = _srs_config->get_hls_mount(vhost);
1329
        thls[vhost] = entry;
1330 1331 1332 1333 1334 1335 1336
        srs_trace("http hls live stream, vhost=%s, mount=%s", 
            vhost.c_str(), entry->mount.c_str());
    }
    
    return ret;
}
1337 1338
SrsHttpConn::SrsHttpConn(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m)
    : SrsConnection(cm, fd)
1339 1340
{
    parser = new SrsHttpParser();
1341
    http_mux = m;
1342 1343 1344 1345 1346 1347 1348
}

SrsHttpConn::~SrsHttpConn()
{
    srs_freep(parser);
}
1349
void SrsHttpConn::resample()
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
{
    // TODO: FIXME: implements it
}

int64_t SrsHttpConn::get_send_bytes_delta()
{
    // TODO: FIXME: implements it
    return 0;
}

int64_t SrsHttpConn::get_recv_bytes_delta()
{
    // TODO: FIXME: implements it
    return 0;
}
1366 1367 1368 1369 1370
void SrsHttpConn::cleanup()
{
    // TODO: FIXME: implements it
}
1371 1372 1373 1374
int SrsHttpConn::do_cycle()
{
    int ret = ERROR_SUCCESS;
    
winlin authored
1375
    srs_trace("HTTP client ip=%s", ip.c_str());
1376 1377 1378 1379 1380 1381 1382 1383
    
    // initialize parser
    if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
        srs_error("http initialize http parser failed. ret=%d", ret);
        return ret;
    }
    
    // underlayer socket
1384
    SrsStSocket skt(stfd);
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
    
    // process http messages.
    for (;;) {
        SrsHttpMessage* req = NULL;
        
        // get a http message
        if ((ret = parser->parse_message(&skt, &req)) != ERROR_SUCCESS) {
            return ret;
        }
1395
        // if SUCCESS, always NOT-NULL.
1396 1397 1398
        srs_assert(req);
        
        // always free it in this scope.
1399
        SrsAutoFree(SrsHttpMessage, req);
1400
        
1401 1402
        // may should discard the body.
        if ((ret = on_got_http_message(req)) != ERROR_SUCCESS) {
1403 1404 1405
            return ret;
        }
        
1406
        // ok, handle http request.
1407
        SrsHttpResponseWriter writer(&skt);
1408
        if ((ret = process_request(&writer, req)) != ERROR_SUCCESS) {
1409 1410 1411 1412 1413 1414 1415
            return ret;
        }
    }
        
    return ret;
}
1416
int SrsHttpConn::process_request(ISrsHttpResponseWriter* w, SrsHttpMessage* r) 
1417 1418 1419
{
    int ret = ERROR_SUCCESS;
    
winlin authored
1420
    srs_trace("HTTP %s %s, content-length=%"PRId64"", 
1421
        r->method_str().c_str(), r->url().c_str(), r->content_length());
1422
    
1423
    // use default server mux to serve http request.
1424
    if ((ret = http_mux->serve_http(w, r)) != ERROR_SUCCESS) {
1425 1426 1427
        if (!srs_is_client_gracefully_close(ret)) {
            srs_error("serve http msg failed. ret=%d", ret);
        }
1428 1429 1430 1431 1432 1433
        return ret;
    }
    
    return ret;
}
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
SrsStaticHttpConn::SrsStaticHttpConn(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m)
    : SrsHttpConn(cm, fd, m)
{
}

SrsStaticHttpConn::~SrsStaticHttpConn()
{
}

int SrsStaticHttpConn::on_got_http_message(SrsHttpMessage* msg)
{
    int ret = ERROR_SUCCESS;
    
    // TODO: FIXME: use the post body.
    std::string res;
    
    // get response body.
    if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}
1458
#endif
1459