Blame view

trunk/src/app/srs_app_hls.cpp 42.3 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

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_app_hls.hpp>
25 26 27 28 29 30

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
31
#include <math.h>
32
33
#include <algorithm>
34
#include <sstream>
35
using namespace std;
36
37
#include <srs_kernel_error.hpp>
winlin authored
38
#include <srs_kernel_codec.hpp>
39 40
#include <srs_rtmp_amf0.hpp>
#include <srs_rtmp_stack.hpp>
41 42
#include <srs_app_config.hpp>
#include <srs_app_source.hpp>
43
#include <srs_core_autofree.hpp>
44
#include <srs_rtmp_sdk.hpp>
45
#include <srs_app_pithy_print.hpp>
46
#include <srs_kernel_utility.hpp>
47
#include <srs_kernel_codec.hpp>
48
#include <srs_kernel_file.hpp>
49
#include <srs_protocol_buffer.hpp>
50
#include <srs_kernel_ts.hpp>
51
#include <srs_app_utility.hpp>
52
#include <srs_app_http_hooks.hpp>
53
54
// drop the segment when duration of ts too small.
55
#define SRS_AUTO_HLS_SEGMENT_MIN_DURATION_MS 100
56
57
// fragment plus the deviation percent.
58
#define SRS_HLS_FLOOR_REAP_PERCENT 0.3
59
// reset the piece id when deviation overflow this.
60
#define SRS_JUMP_WHEN_PIECE_DEVIATION 20
61
62 63 64 65 66 67 68 69
ISrsHlsHandler::ISrsHlsHandler()
{
}

ISrsHlsHandler::~ISrsHlsHandler()
{
}
winlin authored
70 71 72 73 74
/**
 * * the HLS section, only available when HLS enabled.
 * */
#ifdef SRS_AUTO_HLS
75 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 107 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 139 140
SrsHlsCacheWriter::SrsHlsCacheWriter(bool write_cache, bool write_file)
{
    should_write_cache = write_cache;
    should_write_file = write_file;
}

SrsHlsCacheWriter::~SrsHlsCacheWriter()
{
}

int SrsHlsCacheWriter::open(string file)
{
    if (!should_write_file) {
        return ERROR_SUCCESS;
    }

    return impl.open(file);
}

void SrsHlsCacheWriter::close()
{
    if (!should_write_file) {
        return;
    }

    impl.close();
}

bool SrsHlsCacheWriter::is_open()
{
    if (!should_write_file) {
        return true;
    }

    return impl.is_open();
}

int64_t SrsHlsCacheWriter::tellg()
{
    if (!should_write_file) {
        return 0;
    }

    return impl.tellg();
}

int SrsHlsCacheWriter::write(void* buf, size_t count, ssize_t* pnwrite)
{
    if (should_write_cache) {
        if (count > 0) {
            data.append((char*)buf, count);
        }
    }

    if (should_write_file) {
        return impl.write(buf, count, pnwrite);
    }

    return ERROR_SUCCESS;
}

string SrsHlsCacheWriter::cache()
{
    return data;
}
141
SrsHlsSegment::SrsHlsSegment(SrsTsContext* c, bool write_cache, bool write_file, SrsCodecAudio ac, SrsCodecVideo vc)
142
{
143 144 145
    duration = 0;
    sequence_no = 0;
    segment_start_dts = 0;
winlin authored
146
    is_sequence_header = false;
147
    writer = new SrsHlsCacheWriter(write_cache, write_file);
148
    muxer = new SrsTSMuxer(writer, c, ac, vc);
149 150
}
151
SrsHlsSegment::~SrsHlsSegment()
152
{
153
    srs_freep(muxer);
154
    srs_freep(writer);
winlin authored
155
}
156
157
void SrsHlsSegment::update_duration(int64_t current_frame_dts)
winlin authored
158
{
159 160 161 162 163 164 165 166
    // we use video/audio to update segment duration,
    // so when reap segment, some previous audio frame will
    // update the segment duration, which is nagetive,
    // just ignore it.
    if (current_frame_dts < segment_start_dts) {
        return;
    }
    
167
    duration = (current_frame_dts - segment_start_dts) / 90000.0;
168 169
    srs_assert(duration >= 0);
    
170
    return;
171 172
}
173
SrsDvrAsyncCallOnHls::SrsDvrAsyncCallOnHls(SrsRequest* r, string p, string t, string m, string mu, int s, double d)
174
{
175
    req = r->copy();
176
    path = p;
177 178 179
    ts_url = t;
    m3u8 = m;
    m3u8_url = mu;
180
    seq_no = s;
181
    duration = d;
182 183 184 185
}

SrsDvrAsyncCallOnHls::~SrsDvrAsyncCallOnHls()
{
186
    srs_freep(req);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

int SrsDvrAsyncCallOnHls::call()
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_CALLBACK
    // http callback for on_hls in config.
    if (_srs_config->get_vhost_http_hooks_enabled(req->vhost)) {
        // HTTP: on_hls
        SrsConfDirective* on_hls = _srs_config->get_vhost_on_hls(req->vhost);
        if (!on_hls) {
            srs_info("ignore the empty http callback: on_hls");
            return ret;
        }
        
        std::string file = path;
        int sn = seq_no;
        for (int i = 0; i < (int)on_hls->args.size(); i++) {
            std::string url = on_hls->args.at(i);
207
            if ((ret = SrsHttpHooks::on_hls(url, req, file, ts_url, m3u8, m3u8_url, sn, duration)) != ERROR_SUCCESS) {
208 209 210 211 212 213 214 215 216 217 218 219
                srs_error("hook client on_hls failed. url=%s, ret=%d", url.c_str(), ret);
                return ret;
            }
        }
    }
#endif
    
    return ret;
}

string SrsDvrAsyncCallOnHls::to_string()
{
220 221 222 223 224
    return "on_hls: " + path;
}

SrsDvrAsyncCallOnHlsNotify::SrsDvrAsyncCallOnHlsNotify(SrsRequest* r, string u)
{
225
    req = r->copy();
226 227 228 229 230
    ts_url = u;
}

SrsDvrAsyncCallOnHlsNotify::~SrsDvrAsyncCallOnHlsNotify()
{
231
    srs_freep(req);
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

int SrsDvrAsyncCallOnHlsNotify::call()
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_CALLBACK
    // http callback for on_hls_notify in config.
    if (_srs_config->get_vhost_http_hooks_enabled(req->vhost)) {
        // HTTP: on_hls
        SrsConfDirective* on_hls = _srs_config->get_vhost_on_hls_notify(req->vhost);
        if (!on_hls) {
            srs_info("ignore the empty http callback: on_hls_notify");
            return ret;
        }
        
248 249 250 251 252 253 254
        std::string url;
        if (true) {
            static u_int32_t nb_call = 0;
            int index = nb_call++ % on_hls->args.size();
            url = on_hls->args.at(index);
        }
        
255
        int nb_notify = _srs_config->get_vhost_hls_nb_notify(req->vhost);
256 257 258
        if ((ret = SrsHttpHooks::on_hls_notify(url, req, ts_url, nb_notify)) != ERROR_SUCCESS) {
            srs_error("hook client on_hls_notify failed. url=%s, ts=%s, ret=%d", url.c_str(), ts_url.c_str(), ret);
            return ret;
259 260 261 262 263 264 265 266 267 268
        }
    }
#endif
    
    return ret;
}

string SrsDvrAsyncCallOnHlsNotify::to_string()
{
    return "on_hls_notify: " + ts_url;
269 270
}
271
SrsHlsMuxer::SrsHlsMuxer()
winlin authored
272
{
273
    req = NULL;
274
    handler = NULL;
275
    hls_fragment = hls_window = 0;
276
    hls_aof_ratio = 1.0;
277
    deviation_ts = 0;
278
    hls_cleanup = true;
279
    hls_wait_keyframe = true;
280
    previous_floor_ts = 0;
winlin authored
281
    accept_floor_ts = 0;
282
    hls_ts_floor = false;
283
    target_duration = 0;
284
    _sequence_no = 0;
285
    current = NULL;
286
    acodec = SrsCodecAudioReserved1;
287 288
    should_write_cache = false;
    should_write_file = true;
289
    async = new SrsAsyncCallWorker();
290
    context = new SrsTsContext();
winlin authored
291
}
winlin authored
292
293
SrsHlsMuxer::~SrsHlsMuxer()
winlin authored
294
{
295
    std::vector<SrsHlsSegment*>::iterator it;
296
    for (it = segments.begin(); it != segments.end(); ++it) {
297
        SrsHlsSegment* segment = *it;
298 299 300 301 302
        srs_freep(segment);
    }
    segments.clear();
    
    srs_freep(current);
303
    srs_freep(req);
304
    srs_freep(async);
305
    srs_freep(context);
winlin authored
306 307
}
308 309
void SrsHlsMuxer::dispose()
{
310 311 312 313 314 315 316 317
    if (should_write_file) {
        std::vector<SrsHlsSegment*>::iterator it;
        for (it = segments.begin(); it != segments.end(); ++it) {
            SrsHlsSegment* segment = *it;
            if (unlink(segment->full_path.c_str()) < 0) {
                srs_warn("dispose unlink path failed, file=%s.", segment->full_path.c_str());
            }
            srs_freep(segment);
318
        }
319 320 321 322 323 324 325 326 327 328 329 330
        segments.clear();
        
        if (current) {
            std::string path = current->full_path + ".tmp";
            if (unlink(path.c_str()) < 0) {
                srs_warn("dispose unlink path failed, file=%s", path.c_str());
            }
            srs_freep(current);
        }
        
        if (unlink(m3u8.c_str()) < 0) {
            srs_warn("dispose unlink path failed. file=%s", m3u8.c_str());
331 332 333
        }
    }
    
334 335
    // TODO: FIXME: support hls dispose in HTTP cache.
    
336 337 338
    srs_trace("gracefully dispose hls %s", req? req->get_stream_url().c_str() : "");
}
339 340 341 342 343
int SrsHlsMuxer::sequence_no()
{
    return _sequence_no;
}
344 345 346 347 348 349 350 351 352 353
string SrsHlsMuxer::ts_url()
{
    return current? current->uri:"";
}

double SrsHlsMuxer::duration()
{
    return current? current->duration:0;
}
354
int SrsHlsMuxer::deviation()
355
{
356 357 358 359 360
    // no floor, no deviation.
    if (!hls_ts_floor) {
        return 0;
    }
    
361
    return deviation_ts;
362 363
}
364 365 366 367 368 369 370 371 372 373 374 375 376
int SrsHlsMuxer::initialize(ISrsHlsHandler* h)
{
    int ret = ERROR_SUCCESS;
    
    handler = h;
    
    if ((ret = async->start()) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}
377
int SrsHlsMuxer::update_config(SrsRequest* r, string entry_prefix,
378
    string path, string m3u8_file, string ts_file, double fragment, double window,
379
    bool ts_floor, double aof_ratio, bool cleanup, bool wait_keyframe
380
) {
381 382
    int ret = ERROR_SUCCESS;
    
383 384 385
    srs_freep(req);
    req = r->copy();
386
    hls_entry_prefix = entry_prefix;
387
    hls_path = path;
388
    hls_ts_file = ts_file;
389
    hls_fragment = fragment;
390
    hls_aof_ratio = aof_ratio;
391
    hls_ts_floor = ts_floor;
392
    hls_cleanup = cleanup;
393
    hls_wait_keyframe = wait_keyframe;
394
    previous_floor_ts = 0;
winlin authored
395
    accept_floor_ts = 0;
396
    hls_window = window;
397
    deviation_ts = 0;
398 399
    
    // generate the m3u8 dir and path.
400 401
    m3u8_url = srs_path_build_stream(m3u8_file, req->vhost, req->app, req->stream);
    m3u8 = path + "/" + m3u8_url;
402 403 404

    // we always keep the target duration increasing.
    int max_td = srs_max(target_duration, (int)(fragment * _srs_config->get_hls_td_ratio(r->vhost)));
405
    srs_info("hls update target duration %d=>%d, aof=%.2f", target_duration, max_td, aof_ratio);
406
    target_duration = max_td;
407 408 409 410 411 412 413 414 415 416 417 418 419

    std::string storage = _srs_config->get_hls_storage(r->vhost);
    if (storage == "ram") {
        should_write_cache = true;
        should_write_file = false;
    } else if (storage == "disk") {
        should_write_cache = false;
        should_write_file = true;
    } else {
        srs_assert(storage == "both");
        should_write_cache = true;
        should_write_file = true;
    }
420
    
winlin authored
421 422 423 424 425 426 427 428
    // create m3u8 dir once.
    m3u8_dir = srs_path_dirname(m3u8);
    if (should_write_file && (ret = srs_create_dir_recursively(m3u8_dir)) != ERROR_SUCCESS) {
        srs_error("create app dir %s failed. ret=%d", m3u8_dir.c_str(), ret);
        return ret;
    }
    srs_info("create m3u8 dir %s ok", m3u8_dir.c_str());
    
429
    return ret;
430 431
}
432
int SrsHlsMuxer::segment_open(int64_t segment_start_dts)
winlin authored
433
{
434 435 436 437 438 439 440 441 442
    int ret = ERROR_SUCCESS;
    
    if (current) {
        srs_warn("ignore the segment open, for segment is already open.");
        return ret;
    }
    
    // when segment open, the current segment must be NULL.
    srs_assert(!current);
443 444 445

    // load the default acodec from config.
    SrsCodecAudio default_acodec = SrsCodecAudioAAC;
446 447 448 449 450 451 452 453
    if (true) {
        std::string default_acodec_str = _srs_config->get_hls_acodec(req->vhost);
        if (default_acodec_str == "mp3") {
            default_acodec = SrsCodecAudioMP3;
            srs_info("hls: use default mp3 acodec");
        } else if (default_acodec_str == "aac") {
            default_acodec = SrsCodecAudioAAC;
            srs_info("hls: use default aac acodec");
454 455 456
        } else if (default_acodec_str == "an") {
            default_acodec = SrsCodecAudioDisabled;
            srs_info("hls: use default an acodec for pure video");
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
        } else {
            srs_warn("hls: use aac for other codec=%s", default_acodec_str.c_str());
        }
    }

    // load the default vcodec from config.
    SrsCodecVideo default_vcodec = SrsCodecVideoAVC;
    if (true) {
        std::string default_vcodec_str = _srs_config->get_hls_vcodec(req->vhost);
        if (default_vcodec_str == "h264") {
            default_vcodec = SrsCodecVideoAVC;
            srs_info("hls: use default h264 vcodec");
        } else if (default_vcodec_str == "vn") {
            default_vcodec = SrsCodecVideoDisabled;
            srs_info("hls: use default vn vcodec for pure audio");
        } else {
            srs_warn("hls: use h264 for other codec=%s", default_vcodec_str.c_str());
        }
475
    }
476 477
    
    // new segment.
478
    current = new SrsHlsSegment(context, should_write_cache, should_write_file, default_acodec, default_vcodec);
479
    current->sequence_no = _sequence_no++;
480
    current->segment_start_dts = segment_start_dts;
481 482
    
    // generate filename.
483 484
    std::string ts_file = hls_ts_file;
    ts_file = srs_path_build_stream(ts_file, req->vhost, req->app, req->stream);
485
    if (hls_ts_floor) {
winlin authored
486
        // accept the floor ts for the first piece.
487
        int64_t current_floor_ts = (int64_t)(srs_update_system_time_ms() / (1000 * hls_fragment));
winlin authored
488
        if (!accept_floor_ts) {
489
            accept_floor_ts = current_floor_ts - 1;
winlin authored
490 491 492 493
        } else {
            accept_floor_ts++;
        }
        
494 495 496 497 498 499 500 501 502
        // jump when deviation more than 10p
        if (accept_floor_ts - current_floor_ts > SRS_JUMP_WHEN_PIECE_DEVIATION) {
            srs_warn("hls: jmp for ts deviation, current=%"PRId64", accept=%"PRId64, current_floor_ts, accept_floor_ts);
            accept_floor_ts = current_floor_ts - 1;
        }
        
        // when reap ts, adjust the deviation.
        deviation_ts = (int)(accept_floor_ts - current_floor_ts);
        
503
        // dup/jmp detect for ts in floor mode.
504
        if (previous_floor_ts && previous_floor_ts != current_floor_ts - 1) {
505
            srs_warn("hls: dup/jmp ts, previous=%"PRId64", current=%"PRId64", accept=%"PRId64", deviation=%d",
506
                     previous_floor_ts, current_floor_ts, accept_floor_ts, deviation_ts);
507
        }
508
        previous_floor_ts = current_floor_ts;
509 510 511 512 513 514 515 516 517 518
        
        // we always ensure the piece is increase one by one.
        std::stringstream ts_floor;
        ts_floor << accept_floor_ts;
        ts_file = srs_string_replace(ts_file, "[timestamp]", ts_floor.str());
        
        // TODO: FIMXE: we must use the accept ts floor time to generate the hour variable.
        ts_file = srs_path_build_timestamp(ts_file);
    } else {
        ts_file = srs_path_build_timestamp(ts_file);
519
    }
520 521 522 523 524 525
    if (true) {
        std::stringstream ss;
        ss << current->sequence_no;
        ts_file = srs_string_replace(ts_file, "[seq]", ss.str());
    }
    current->full_path = hls_path + "/" + ts_file;
526
    srs_info("hls: generate ts path %s, tmpl=%s, floor=%d", ts_file.c_str(), hls_ts_file.c_str(), hls_ts_floor);
527
    
528 529 530 531 532 533 534 535
    // the ts url, relative or absolute url.
    std::string ts_url = current->full_path;
    if (srs_string_starts_with(ts_url, m3u8_dir)) {
        ts_url = ts_url.substr(m3u8_dir.length());
    }
    while (srs_string_starts_with(ts_url, "/")) {
        ts_url = ts_url.substr(1);
    }
536 537 538 539
    current->uri += hls_entry_prefix;
    if (!hls_entry_prefix.empty() && !srs_string_ends_with(hls_entry_prefix, "/")) {
        current->uri += "/";
    }
540 541 542
    current->uri += ts_url;
    
    // create dir recursively for hls.
winlin authored
543 544 545
    std::string ts_dir = srs_path_dirname(current->full_path);
    if (should_write_file && (ret = srs_create_dir_recursively(ts_dir)) != ERROR_SUCCESS) {
        srs_error("create app dir %s failed. ret=%d", ts_dir.c_str(), ret);
546 547
        return ret;
    }
winlin authored
548
    srs_info("create ts dir %s ok", ts_dir.c_str());
549
    
550
    // open temp ts file.
551 552
    std::string tmp_file = current->full_path + ".tmp";
    if ((ret = current->muxer->open(tmp_file.c_str())) != ERROR_SUCCESS) {
553 554 555
        srs_error("open hls muxer failed. ret=%d", ret);
        return ret;
    }
556
    srs_info("open HLS muxer success. path=%s, tmp=%s",
557
        current->full_path.c_str(), tmp_file.c_str());
558 559 560 561 562

    // set the segment muxer audio codec.
    if (acodec != SrsCodecAudioReserved1) {
        current->muxer->update_acodec(acodec);
    }
563 564
    
    return ret;
winlin authored
565 566
}
winlin authored
567 568 569 570 571 572 573 574 575 576 577 578 579
int SrsHlsMuxer::on_sequence_header()
{
    int ret = ERROR_SUCCESS;
    
    srs_assert(current);
    
    // set the current segment to sequence header,
    // when close the segement, it will write a discontinuity to m3u8 file.
    current->is_sequence_header = true;
    
    return ret;
}
580 581 582
bool SrsHlsMuxer::is_segment_overflow()
{
    srs_assert(current);
583
    
584
    // to prevent very small segment.
585
    if (current->duration * 1000 < 2 * SRS_AUTO_HLS_SEGMENT_MIN_DURATION_MS) {
586 587 588
        return false;
    }
    
589
    // use N% deviation, to smoother.
590
    double deviation = hls_ts_floor? SRS_HLS_FLOOR_REAP_PERCENT * deviation_ts * hls_fragment : 0.0;
591 592
    srs_info("hls: dur=%.2f, tar=%.2f, dev=%.2fms/%dp, frag=%.2f",
        current->duration, hls_fragment + deviation, deviation, deviation_ts, hls_fragment);
593 594
    
    return current->duration >= hls_fragment + deviation;
595 596
}
597 598 599 600 601
bool SrsHlsMuxer::wait_keyframe()
{
    return hls_wait_keyframe;
}
602 603
bool SrsHlsMuxer::is_segment_absolutely_overflow()
{
604
    // @see https://github.com/simple-rtmp-server/srs/issues/151#issuecomment-83553950
605
    srs_assert(current);
606
    
607
    // to prevent very small segment.
608
    if (current->duration * 1000 < 2 * SRS_AUTO_HLS_SEGMENT_MIN_DURATION_MS) {
609 610 611
        return false;
    }
    
612 613 614 615 616 617
    // use N% deviation, to smoother.
    double deviation = hls_ts_floor? SRS_HLS_FLOOR_REAP_PERCENT * deviation_ts * hls_fragment : 0.0;
    srs_info("hls: dur=%.2f, tar=%.2f, dev=%.2fms/%dp, frag=%.2f",
             current->duration, hls_fragment + deviation, deviation, deviation_ts, hls_fragment);
    
    return current->duration >= hls_aof_ratio * hls_fragment + deviation;
618 619
}
620
int SrsHlsMuxer::update_acodec(SrsCodecAudio ac)
621 622 623
{
    srs_assert(current);
    srs_assert(current->muxer);
624 625
    acodec = ac;
    return current->muxer->update_acodec(ac);
626 627
}
628
int SrsHlsMuxer::flush_audio(SrsTsCache* cache)
winlin authored
629
{
630 631 632 633 634 635 636 637
    int ret = ERROR_SUCCESS;

    // if current is NULL, segment is not open, ignore the flush event.
    if (!current) {
        srs_warn("flush audio ignored, for segment is not open.");
        return ret;
    }
    
638
    if (!cache->audio || cache->audio->payload->length() <= 0) {
639 640 641
        return ret;
    }
    
642
    // update the duration of segment.
643
    current->update_duration(cache->audio->pts);
644
    
645
    if ((ret = current->muxer->write_audio(cache->audio)) != ERROR_SUCCESS) {
646 647 648
        return ret;
    }
    
649 650
    // write success, clear and free the msg
    srs_freep(cache->audio);
651 652

    return ret;
winlin authored
653 654
}
655
int SrsHlsMuxer::flush_video(SrsTsCache* cache)
winlin authored
656
{
657 658 659 660 661 662 663 664
    int ret = ERROR_SUCCESS;

    // if current is NULL, segment is not open, ignore the flush event.
    if (!current) {
        srs_warn("flush video ignored, for segment is not open.");
        return ret;
    }
    
665 666 667 668
    if (!cache->video || cache->video->payload->length() <= 0) {
        return ret;
    }
    
669 670 671
    srs_assert(current);
    
    // update the duration of segment.
672
    current->update_duration(cache->video->dts);
673
    
674
    if ((ret = current->muxer->write_video(cache->video)) != ERROR_SUCCESS) {
675 676 677
        return ret;
    }
    
678 679
    // write success, clear and free the msg
    srs_freep(cache->video);
680
    
681
    return ret;
winlin authored
682 683
}
684
int SrsHlsMuxer::segment_close(string log_desc)
winlin authored
685
{
686 687 688 689 690 691 692 693 694 695 696
    int ret = ERROR_SUCCESS;
    
    if (!current) {
        srs_warn("ignore the segment close, for segment is not open.");
        return ret;
    }
    
    // when close current segment, the current segment must not be NULL.
    srs_assert(current);

    // assert segment duplicate.
697
    std::vector<SrsHlsSegment*>::iterator it;
698 699 700
    it = std::find(segments.begin(), segments.end(), current);
    srs_assert(it == segments.end());
701
    // valid, add to segments if segment duration is ok
702
    if (current->duration * 1000 >= SRS_AUTO_HLS_SEGMENT_MIN_DURATION_MS) {
703
        segments.push_back(current);
704
        
705
        // use async to call the http hooks, for it will cause thread switch.
706
        if ((ret = async->execute(new SrsDvrAsyncCallOnHls(req,
707 708 709
            current->full_path, current->uri, m3u8, m3u8_url,
            current->sequence_no, current->duration))) != ERROR_SUCCESS)
        {
710 711
            return ret;
        }
712 713
        
        // use async to call the http hooks, for it will cause thread switch.
714
        if ((ret = async->execute(new SrsDvrAsyncCallOnHlsNotify(req, current->uri))) != ERROR_SUCCESS) {
715 716
            return ret;
        }
717
    
718
        srs_info("%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,
719
            log_desc.c_str(), current->sequence_no, current->uri.c_str(), current->duration, 
720
            current->segment_start_dts);
721 722 723 724 725 726 727
        
        // notify handler for update ts.
        srs_assert(current->writer);
        if (handler && (ret = handler->on_update_ts(req, current->uri, current->writer->cache())) != ERROR_SUCCESS) {
            srs_error("notify handler for update ts failed. ret=%d", ret);
            return ret;
        }
728
    
729 730
        // close the muxer of finished segment.
        srs_freep(current->muxer);
731 732 733
        std::string full_path = current->full_path;
        current = NULL;
        
734
        // rename from tmp to real path
735
        std::string tmp_file = full_path + ".tmp";
736
        if (should_write_file && rename(tmp_file.c_str(), full_path.c_str()) < 0) {
737 738
            ret = ERROR_HLS_WRITE_FAILED;
            srs_error("rename ts file failed, %s => %s. ret=%d", 
739
                tmp_file.c_str(), full_path.c_str(), ret);
740 741 742 743
            return ret;
        }
    } else {
        // reuse current segment index.
744
        _sequence_no--;
745 746 747 748 749 750 751
        
        srs_trace("%s drop ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64"",
            log_desc.c_str(), current->sequence_no, current->uri.c_str(), current->duration, 
            current->segment_start_dts);
        
        // rename from tmp to real path
        std::string tmp_file = current->full_path + ".tmp";
752 753
        if (should_write_file) {
            unlink(tmp_file.c_str());
754 755 756
            if (unlink(tmp_file.c_str()) < 0) {
                srs_warn("drop unlink path failed, file=%s.", tmp_file.c_str());
            }
757
        }
758 759
        
        srs_freep(current);
760
    }
761 762
    
    // the segments to remove
763
    std::vector<SrsHlsSegment*> segment_to_remove;
764 765 766 767
    
    // shrink the segments.
    double duration = 0;
    int remove_index = -1;
768
    for (int i = (int)segments.size() - 1; i >= 0; i--) {
769
        SrsHlsSegment* segment = segments[i];
770 771 772 773 774 775 776 777
        duration += segment->duration;
        
        if ((int)duration > hls_window) {
            remove_index = i;
            break;
        }
    }
    for (int i = 0; i < remove_index && !segments.empty(); i++) {
778
        SrsHlsSegment* segment = *segments.begin();
779 780 781 782 783 784 785 786 787
        segments.erase(segments.begin());
        segment_to_remove.push_back(segment);
    }
    
    // refresh the m3u8, donot contains the removed ts
    ret = refresh_m3u8();

    // remove the ts file.
    for (int i = 0; i < (int)segment_to_remove.size(); i++) {
788
        SrsHlsSegment* segment = segment_to_remove[i];
789
        
790
        if (hls_cleanup && should_write_file) {
791 792 793
            if (unlink(segment->full_path.c_str()) < 0) {
                srs_warn("cleanup unlink path failed, file=%s.", segment->full_path.c_str());
            }
794 795
        }
        
796 797 798 799 800 801 802
        if (should_write_cache) {
            if ((ret = handler->on_remove_ts(req, segment->uri)) != ERROR_SUCCESS) {
                srs_warn("remove the ts from ram hls failed. ret=%d", ret);
                return ret;
            }
        }
        
803 804 805 806 807 808 809 810 811 812 813
        srs_freep(segment);
    }
    segment_to_remove.clear();
    
    // check ret of refresh m3u8
    if (ret != ERROR_SUCCESS) {
        srs_error("refresh m3u8 failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
winlin authored
814 815
}
816
int SrsHlsMuxer::refresh_m3u8()
winlin authored
817
{
818 819
    int ret = ERROR_SUCCESS;
    
820 821 822
    std::string temp_m3u8 = m3u8 + ".temp";
    if ((ret = _refresh_m3u8(temp_m3u8)) == ERROR_SUCCESS) {
        if (should_write_file && rename(temp_m3u8.c_str(), m3u8.c_str()) < 0) {
823
            ret = ERROR_HLS_WRITE_FAILED;
824
            srs_error("rename m3u8 file failed. %s => %s, ret=%d", temp_m3u8.c_str(), m3u8.c_str(), ret);
825 826 827 828
        }
    }
    
    // remove the temp file.
829
    unlink(temp_m3u8.c_str());
830 831
    
    return ret;
winlin authored
832 833
}
834
int SrsHlsMuxer::_refresh_m3u8(string m3u8_file)
winlin authored
835
{
836 837 838 839 840 841
    int ret = ERROR_SUCCESS;
    
    // no segments, return.
    if (segments.size() == 0) {
        return ret;
    }
842 843 844

    SrsHlsCacheWriter writer(should_write_cache, should_write_file);
    if ((ret = writer.open(m3u8_file)) != ERROR_SUCCESS) {
845 846 847 848 849
        srs_error("open m3u8 file %s failed. ret=%d", m3u8_file.c_str(), ret);
        return ret;
    }
    srs_info("open m3u8 file %s success.", m3u8_file.c_str());
    
850 851
    // #EXTM3U\n
    // #EXT-X-VERSION:3\n
852
    // #EXT-X-ALLOW-CACHE:YES\n
853 854 855
    std::stringstream ss;
    ss << "#EXTM3U" << SRS_CONSTS_LF
        << "#EXT-X-VERSION:3" << SRS_CONSTS_LF
856
        << "#EXT-X-ALLOW-CACHE:YES" << SRS_CONSTS_LF;
857 858 859
    srs_verbose("write m3u8 header success.");
    
    // #EXT-X-MEDIA-SEQUENCE:4294967295\n
860
    SrsHlsSegment* first = *segments.begin();
861
    ss << "#EXT-X-MEDIA-SEQUENCE:" << first->sequence_no << SRS_CONSTS_LF;
862 863 864
    srs_verbose("write m3u8 sequence success.");
    
    // #EXT-X-TARGETDURATION:4294967295\n
865 866 867 868 869 870 871 872
    /**
    * @see hls-m3u8-draft-pantos-http-live-streaming-12.pdf, page 25
    * The Media Playlist file MUST contain an EXT-X-TARGETDURATION tag.
    * Its value MUST be equal to or greater than the EXTINF duration of any
    * media segment that appears or will appear in the Playlist file,
    * rounded to the nearest integer. Its value MUST NOT change. A
    * typical target duration is 10 seconds.
    */
873
    // @see https://github.com/simple-rtmp-server/srs/issues/304#issuecomment-74000081
874
    std::vector<SrsHlsSegment*>::iterator it;
875
    for (it = segments.begin(); it != segments.end(); ++it) {
876
        SrsHlsSegment* segment = *it;
877
        target_duration = srs_max(target_duration, (int)ceil(segment->duration));
878
    }
879
    ss << "#EXT-X-TARGETDURATION:" << target_duration << SRS_CONSTS_LF;
880 881 882 883
    srs_verbose("write m3u8 duration success.");
    
    // write all segments
    for (it = segments.begin(); it != segments.end(); ++it) {
884
        SrsHlsSegment* segment = *it;
885
        
winlin authored
886 887
        if (segment->is_sequence_header) {
            // #EXT-X-DISCONTINUITY\n
888
            ss << "#EXT-X-DISCONTINUITY" << SRS_CONSTS_LF;
winlin authored
889 890 891
            srs_verbose("write m3u8 segment discontinuity success.");
        }
        
892
        // "#EXTINF:4294967295.208,\n"
893 894
        ss.precision(3);
        ss.setf(std::ios::fixed, std::ios::floatfield);
895
        ss << "#EXTINF:" << segment->duration << ", no desc" << SRS_CONSTS_LF;
winlin authored
896
        srs_verbose("write m3u8 segment info success.");
897
        
898
        // {file name}\n
899
        ss << segment->uri << SRS_CONSTS_LF;
900 901
        srs_verbose("write m3u8 segment uri success.");
    }
902 903 904 905 906 907 908

    // write m3u8 to writer.
    std::string m3u8 = ss.str();
    if ((ret = writer.write((char*)m3u8.c_str(), (int)m3u8.length(), NULL)) != ERROR_SUCCESS) {
        srs_error("write m3u8 failed. ret=%d", ret);
        return ret;
    }
909
    srs_info("write m3u8 %s success.", m3u8_file.c_str());
910 911 912 913 914 915

    // notify handler for update m3u8.
    if (handler && (ret = handler->on_update_m3u8(req, writer.cache())) != ERROR_SUCCESS) {
        srs_error("notify handler for update m3u8 failed. ret=%d", ret);
        return ret;
    }
916 917
    
    return ret;
918 919
}
920
SrsHlsCache::SrsHlsCache()
winlin authored
921
{
922
    cache = new SrsTsCache();
winlin authored
923 924
}
925
SrsHlsCache::~SrsHlsCache()
winlin authored
926
{
927
    srs_freep(cache);
928
}
929 930 931 932 933 934 935 936

int SrsHlsCache::on_publish(SrsHlsMuxer* muxer, SrsRequest* req, int64_t segment_start_dts)
{
    int ret = ERROR_SUCCESS;

    std::string vhost = req->vhost;
    std::string stream = req->stream;
    std::string app = req->app;
937
    
938 939
    double hls_fragment = _srs_config->get_hls_fragment(vhost);
    double hls_window = _srs_config->get_hls_window(vhost);
940
    
941 942
    // get the hls m3u8 ts list entry prefix config
    std::string entry_prefix = _srs_config->get_hls_entry_prefix(vhost);
943
    // get the hls path config
944 945 946
    std::string path = _srs_config->get_hls_path(vhost);
    std::string m3u8_file = _srs_config->get_hls_m3u8_file(vhost);
    std::string ts_file = _srs_config->get_hls_ts_file(vhost);
947
    bool cleanup = _srs_config->get_hls_cleanup(vhost);
948
    bool wait_keyframe = _srs_config->get_hls_wait_keyframe(vhost);
949 950
    // the audio overflow, for pure audio to reap segment.
    double hls_aof_ratio = _srs_config->get_hls_aof_ratio(vhost);
951 952
    // whether use floor(timestamp/hls_fragment) for variable timestamp
    bool ts_floor = _srs_config->get_hls_ts_floor(vhost);
953
    
954 955 956
    // TODO: FIXME: support load exists m3u8, to continue publish stream.
    // for the HLS donot requires the EXT-X-MEDIA-SEQUENCE be monotonically increase.
    
957
    // open muxer
958
    if ((ret = muxer->update_config(req, entry_prefix,
959
        path, m3u8_file, ts_file, hls_fragment, hls_window, ts_floor, hls_aof_ratio,
960
        cleanup, wait_keyframe)) != ERROR_SUCCESS
961
    ) {
962 963 964 965 966 967 968 969
        srs_error("m3u8 muxer update config failed. ret=%d", ret);
        return ret;
    }
    
    if ((ret = muxer->segment_open(segment_start_dts)) != ERROR_SUCCESS) {
        srs_error("m3u8 muxer open segment failed. ret=%d", ret);
        return ret;
    }
970
    srs_trace("hls: win=%.2f, frag=%.2f, prefix=%s, path=%s, m3u8=%s, ts=%s, aof=%.2f, floor=%d, clean=%d, waitk=%d",
971
        hls_window, hls_fragment, entry_prefix.c_str(), path.c_str(), m3u8_file.c_str(),
972
        ts_file.c_str(), hls_aof_ratio, ts_floor, cleanup, wait_keyframe);
973 974 975 976 977 978 979 980
    
    return ret;
}

int SrsHlsCache::on_unpublish(SrsHlsMuxer* muxer)
{
    int ret = ERROR_SUCCESS;
    
981
    if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) {
982 983 984 985
        srs_error("m3u8 muxer flush audio failed. ret=%d", ret);
        return ret;
    }
    
986
    if ((ret = muxer->segment_close("unpublish")) != ERROR_SUCCESS) {
987 988 989 990 991
        return ret;
    }
    
    return ret;
}
winlin authored
992 993 994 995 996 997 998 999 1000 1001 1002

int SrsHlsCache::on_sequence_header(SrsHlsMuxer* muxer)
{
    // TODO: support discontinuity for the same stream
    // currently we reap and insert discontinity when encoder republish,
    // but actually, event when stream is not republish, the 
    // sequence header may change, for example,
    // ffmpeg ingest a external rtmp stream and push to srs,
    // when the sequence header changed, the stream is not republish.
    return muxer->on_sequence_header();
}
1003
    
1004
int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t pts, SrsCodecSample* sample)
1005
{
1006 1007 1008
    int ret = ERROR_SUCCESS;
    
    // write audio to cache.
1009
    if ((ret = cache->cache_audio(codec, pts, sample)) != ERROR_SUCCESS) {
1010 1011 1012 1013
        return ret;
    }
    
    // flush if buffer exceed max size.
1014 1015
    if (cache->audio->payload->length() > SRS_AUTO_HLS_AUDIO_CACHE_SIZE) {
        if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) {
1016 1017 1018
            return ret;
        }
    }
1019
1020 1021 1022 1023
    // TODO: config it.
    // in ms, audio delay to flush the audios.
    int64_t audio_delay = SRS_CONF_DEFAULT_AAC_DELAY;
    // flush if audio delay exceed
1024 1025 1026
    // cache->audio will be free in flush_audio
    // so we must check whether it's null ptr.
    if (cache->audio && pts - cache->audio->start_pts > audio_delay * 90) {
1027
        if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) {
1028 1029 1030 1031
            return ret;
        }
    }
    
1032 1033 1034 1035 1036
    // reap when current source is pure audio.
    // it maybe changed when stream info changed,
    // for example, pure audio when start, audio/video when publishing,
    // pure audio again for audio disabled.
    // so we reap event when the audio incoming when segment overflow.
1037
    // @see https://github.com/simple-rtmp-server/srs/issues/151
1038
    // we use absolutely overflow of segment to make jwplayer/ffplay happy
1039
    // @see https://github.com/simple-rtmp-server/srs/issues/151#issuecomment-71155184
1040
    if (cache->audio && muxer->is_segment_absolutely_overflow()) {
1041
        srs_info("hls: absolute audio reap segment.");
1042
        if ((ret = reap_segment("audio", muxer, cache->audio->pts)) != ERROR_SUCCESS) {
1043 1044 1045 1046
            return ret;
        }
    }
    
1047
    return ret;
1048
}
1049
    
1050
int SrsHlsCache::write_video(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t dts, SrsCodecSample* sample)
1051
{
1052 1053 1054
    int ret = ERROR_SUCCESS;
    
    // write video to cache.
1055
    if ((ret = cache->cache_video(codec, dts, sample)) != ERROR_SUCCESS) {
1056 1057 1058
        return ret;
    }
    
1059 1060 1061 1062 1063
    // when segment overflow, reap if possible.
    if (muxer->is_segment_overflow()) {
        // do reap ts if any of:
        //      a. wait keyframe and got keyframe.
        //      b. always reap when not wait keyframe.
1064
        if (!muxer->wait_keyframe()|| sample->frame_type == SrsCodecVideoAVCFrameKeyFrame) {
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
            // when wait keyframe, there must exists idr frame in sample.
            if (!sample->has_idr && muxer->wait_keyframe()) {
                srs_warn("hls: ts starts without IDR, first nalu=%d, idr=%d", sample->first_nalu_type, sample->has_idr);
            }
            
            // reap the segment, which will also flush the video.
            if ((ret = reap_segment("video", muxer, cache->video->dts)) != ERROR_SUCCESS) {
                return ret;
            }
            
            // the video must be flushed, just return.
1076 1077 1078 1079
            return ret;
        }
    }
    
1080
    // flush video when got one
1081
    if ((ret = muxer->flush_video(cache)) != ERROR_SUCCESS) {
1082 1083 1084 1085
        srs_error("m3u8 muxer flush video failed. ret=%d", ret);
        return ret;
    }
    
1086 1087 1088
    return ret;
}
1089
int SrsHlsCache::reap_segment(string log_desc, SrsHlsMuxer* muxer, int64_t segment_start_dts)
1090 1091
{
    int ret = ERROR_SUCCESS;
winlin authored
1092 1093 1094
    
    // TODO: flush audio before or after segment?
    // TODO: fresh segment begin with audio or video?
1095
winlin authored
1096
    // close current ts.
1097
    if ((ret = muxer->segment_close(log_desc)) != ERROR_SUCCESS) {
1098 1099 1100 1101
        srs_error("m3u8 muxer close segment failed. ret=%d", ret);
        return ret;
    }
    
winlin authored
1102
    // open new ts.
1103 1104 1105 1106
    if ((ret = muxer->segment_open(segment_start_dts)) != ERROR_SUCCESS) {
        srs_error("m3u8 muxer open segment failed. ret=%d", ret);
        return ret;
    }
1107 1108
    
    // segment open, flush video first.
1109
    if ((ret = muxer->flush_video(cache)) != ERROR_SUCCESS) {
1110 1111 1112
        srs_error("m3u8 muxer flush video failed. ret=%d", ret);
        return ret;
    }
winlin authored
1113
    
1114 1115 1116
    // segment open, flush the audio.
    // @see: ngx_rtmp_hls_open_fragment
    /* start fragment with audio to make iPhone happy */
1117
    if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) {
1118 1119 1120
        srs_error("m3u8 muxer flush audio failed. ret=%d", ret);
        return ret;
    }
1121 1122
    
    return ret;
1123 1124
}
1125
SrsHls::SrsHls()
1126
{
winlin authored
1127
    _req = NULL;
1128 1129
    source = NULL;
    handler = NULL;
1130
    
1131
    hls_enabled = false;
1132 1133
    hls_can_dispose = false;
    last_update_time = 0;
1134
1135
    codec = new SrsAvcAacCodec();
1136 1137 1138
    sample = new SrsCodecSample();
    jitter = new SrsRtmpJitter();
    
1139
    muxer = new SrsHlsMuxer();
1140
    hls_cache = new SrsHlsCache();
1141
1142
    pprint = SrsPithyPrint::create_hls();
1143
    stream_dts = 0;
1144 1145 1146 1147
}

SrsHls::~SrsHls()
{
winlin authored
1148
    srs_freep(_req);
1149 1150 1151 1152 1153
    srs_freep(codec);
    srs_freep(sample);
    srs_freep(jitter);
    
    srs_freep(muxer);
1154
    srs_freep(hls_cache);
1155
    
1156
    srs_freep(pprint);
winlin authored
1157 1158
}
1159 1160
void SrsHls::dispose()
{
1161 1162 1163 1164
    if (hls_enabled) {
        on_unpublish();
    }
    
1165 1166 1167 1168 1169 1170 1171 1172
    // only dispose hls when positive.
    if (_req) {
        int hls_dispose = _srs_config->get_hls_dispose(_req->vhost);
        if (hls_dispose <= 0) {
            return;
        }
    }
    
1173
    muxer->dispose();
1174 1175 1176 1177
}

int SrsHls::cycle()
{
1178 1179
    int ret = ERROR_SUCCESS;
    
1180
    srs_info("hls cycle for source %d", source->source_id());
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    
    if (last_update_time <= 0) {
        last_update_time = srs_get_system_time_ms();
    }
    
    if (!_req) {
        return ret;
    }
    
    int hls_dispose = _srs_config->get_hls_dispose(_req->vhost) * 1000;
    if (srs_get_system_time_ms() - last_update_time <= hls_dispose) {
        return ret;
    }
    last_update_time = srs_get_system_time_ms();
    
    if (!hls_can_dispose) {
        return ret;
    }
    hls_can_dispose = false;
    
    srs_trace("hls cycle to dispose hls %s, timeout=%dms", _req->get_stream_url().c_str(), hls_dispose);
    dispose();
    
    return ret;
1205 1206
}
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
int SrsHls::initialize(SrsSource* s, ISrsHlsHandler* h)
{
    int ret = ERROR_SUCCESS;

    source = s;
    handler = h;

    if ((ret = muxer->initialize(h)) != ERROR_SUCCESS) {
        return ret;
    }

    return ret;
}
winlin authored
1221 1222
int SrsHls::on_publish(SrsRequest* req)
{
1223 1224
    int ret = ERROR_SUCCESS;
    
winlin authored
1225 1226
    srs_freep(_req);
    _req = req->copy();
1227 1228 1229 1230
    
    // update the hls time, for hls_dispose.
    last_update_time = srs_get_system_time_ms();
    
1231 1232 1233 1234 1235
    // support multiple publish.
    if (hls_enabled) {
        return ret;
    }
    
1236
    std::string vhost = req->vhost;
1237 1238 1239 1240
    if (!_srs_config->get_hls_enabled(vhost)) {
        return ret;
    }
    
1241
    if ((ret = hls_cache->on_publish(muxer, req, stream_dts)) != ERROR_SUCCESS) {
1242 1243 1244
        return ret;
    }
    
winlin authored
1245 1246 1247
    // if enabled, open the muxer.
    hls_enabled = true;
    
1248 1249 1250
    // ok, the hls can be dispose, or need to be dispose.
    hls_can_dispose = true;
    
1251
    // notice the source to get the cached sequence header.
1252 1253
    // when reload to start hls, hls will never get the sequence header in stream,
    // use the SrsSource.on_hls_start to push the sequence header to HLS.
1254 1255 1256 1257 1258 1259
    if ((ret = source->on_hls_start()) != ERROR_SUCCESS) {
        srs_error("callback source hls start failed. ret=%d", ret);
        return ret;
    }

    return ret;
winlin authored
1260 1261 1262 1263
}

void SrsHls::on_unpublish()
{
1264 1265 1266 1267 1268 1269 1270
    int ret = ERROR_SUCCESS;
    
    // support multiple unpublish.
    if (!hls_enabled) {
        return;
    }
1271
    if ((ret = hls_cache->on_unpublish(muxer)) != ERROR_SUCCESS) {
1272 1273 1274 1275
        srs_error("ignore m3u8 muxer flush/close audio failed. ret=%d", ret);
    }
    
    hls_enabled = false;
winlin authored
1276 1277
}
1278
int SrsHls::on_meta_data(SrsAmf0Object* metadata)
winlin authored
1279
{
1280 1281 1282 1283 1284 1285 1286
    int ret = ERROR_SUCCESS;

    if (!metadata) {
        srs_trace("no metadata persent, hls ignored it.");
        return ret;
    }
    
1287
    if (metadata->count() <= 0) {
1288 1289 1290 1291 1292
        srs_trace("no metadata persent, hls ignored it.");
        return ret;
    }
    
    return ret;
winlin authored
1293 1294
}
1295
int SrsHls::on_audio(SrsSharedPtrMessage* shared_audio)
winlin authored
1296
{
1297 1298 1299 1300 1301
    int ret = ERROR_SUCCESS;
    
    if (!hls_enabled) {
        return ret;
    }
1302 1303 1304
    
    // update the hls time, for hls_dispose.
    last_update_time = srs_get_system_time_ms();
1305
1306
    SrsSharedPtrMessage* audio = shared_audio->copy();
1307
    SrsAutoFree(SrsSharedPtrMessage, audio);
1308 1309 1310
    
    sample->clear();
    if ((ret = codec->audio_aac_demux(audio->payload, audio->size, sample)) != ERROR_SUCCESS) {
1311 1312 1313 1314 1315 1316 1317 1318
        if (ret != ERROR_HLS_TRY_MP3) {
            srs_error("hls aac demux audio failed. ret=%d", ret);
            return ret;
        }
        if ((ret = codec->audio_mp3_demux(audio->payload, audio->size, sample)) != ERROR_SUCCESS) {
            srs_error("hls mp3 demux audio failed. ret=%d", ret);
            return ret;
        }
1319
    }
1320 1321
    srs_info("audio decoded, type=%d, codec=%d, cts=%d, size=%d, time=%"PRId64, 
        sample->frame_type, codec->audio_codec_id, sample->cts, audio->size, audio->timestamp);
1322
    SrsCodecAudio acodec = (SrsCodecAudio)codec->audio_codec_id;
1323
    
1324 1325 1326 1327 1328 1329 1330 1331
    // ts support audio codec: aac/mp3
    if (acodec != SrsCodecAudioAAC && acodec != SrsCodecAudioMP3) {
        return ret;
    }

    // when codec changed, write new header.
    if ((ret = muxer->update_acodec(acodec)) != ERROR_SUCCESS) {
        srs_error("http: ts audio write header failed. ret=%d", ret);
1332 1333 1334 1335
        return ret;
    }
    
    // ignore sequence header
1336
    if (acodec == SrsCodecAudioAAC && sample->aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
winlin authored
1337
        return hls_cache->on_sequence_header(muxer);
1338 1339
    }
    
1340
    if ((ret = jitter->correct(audio, 0, 0, SrsRtmpJitterAlgorithmFULL)) != ERROR_SUCCESS) {
1341 1342 1343 1344
        srs_error("rtmp jitter correct audio failed. ret=%d", ret);
        return ret;
    }
    
1345 1346
    // the dts calc from rtmp/flv header.
    int64_t dts = audio->timestamp * 90;
1347
    
1348
    // for pure audio, we need to update the stream dts also.
1349
    stream_dts = dts;
1350
    
1351
    if ((ret = hls_cache->write_audio(codec, muxer, dts, sample)) != ERROR_SUCCESS) {
1352
        srs_error("hls cache write audio failed. ret=%d", ret);
1353 1354 1355 1356
        return ret;
    }
    
    return ret;
winlin authored
1357 1358
}
1359
int SrsHls::on_video(SrsSharedPtrMessage* shared_video)
winlin authored
1360
{
1361 1362 1363 1364 1365
    int ret = ERROR_SUCCESS;
    
    if (!hls_enabled) {
        return ret;
    }
1366 1367 1368
    
    // update the hls time, for hls_dispose.
    last_update_time = srs_get_system_time_ms();
1369
1370
    SrsSharedPtrMessage* video = shared_video->copy();
1371
    SrsAutoFree(SrsSharedPtrMessage, video);
1372 1373 1374
    
    sample->clear();
    if ((ret = codec->video_avc_demux(video->payload, video->size, sample)) != ERROR_SUCCESS) {
1375
        srs_error("hls codec demux video failed. ret=%d", ret);
1376 1377
        return ret;
    }
1378 1379
    srs_info("video decoded, type=%d, codec=%d, avc=%d, cts=%d, size=%d, time=%"PRId64, 
        sample->frame_type, codec->video_codec_id, sample->avc_packet_type, sample->cts, video->size, video->timestamp);
1380
    
1381
    // ignore info frame,
1382
    // @see https://github.com/simple-rtmp-server/srs/issues/288#issuecomment-69863909
1383 1384 1385 1386
    if (sample->frame_type == SrsCodecVideoAVCFrameVideoInfoFrame) {
        return ret;
    }
    
1387 1388 1389 1390 1391 1392 1393
    if (codec->video_codec_id != SrsCodecVideoAVC) {
        return ret;
    }
    
    // ignore sequence header
    if (sample->frame_type == SrsCodecVideoAVCFrameKeyFrame
         && sample->avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {
winlin authored
1394
        return hls_cache->on_sequence_header(muxer);
1395 1396
    }
    
1397
    if ((ret = jitter->correct(video, 0, 0, SrsRtmpJitterAlgorithmFULL)) != ERROR_SUCCESS) {
1398 1399 1400 1401
        srs_error("rtmp jitter correct video failed. ret=%d", ret);
        return ret;
    }
    
1402
    int64_t dts = video->timestamp * 90;
1403 1404 1405
    stream_dts = dts;
    if ((ret = hls_cache->write_video(codec, muxer, dts, sample)) != ERROR_SUCCESS) {
        srs_error("hls cache write video failed. ret=%d", ret);
1406 1407 1408
        return ret;
    }
    
1409 1410
    // pithy print message.
    hls_show_mux_log();
1411 1412
    
    return ret;
1413 1414
}
1415
void SrsHls::hls_show_mux_log()
winlin authored
1416
{
1417 1418
    pprint->elapse();
1419
    // reportable
1420
    if (pprint->can_print()) {
1421
        // the run time is not equals to stream time,
1422
        // @see: https://github.com/simple-rtmp-server/srs/issues/81#issuecomment-48100994
1423
        // it's ok.
1424
        srs_trace("-> "SRS_CONSTS_LOG_HLS" time=%"PRId64", stream dts=%"PRId64"(%"PRId64"ms), sno=%d, ts=%s, dur=%.2f, dva=%dp",
1425
            pprint->age(), stream_dts, stream_dts / 90, muxer->sequence_no(), muxer->ts_url().c_str(),
1426
            muxer->duration(), muxer->deviation());
1427
    }
winlin authored
1428 1429
}
1430 1431
#endif
1432