DASH: Fix number mode bug to make it run. v5.0.96 (#3240)

* Add utc time utility
* Fix calculate duration in fmp4
* Refine dash code, use segment template timeline
* Shrink m4s file and cleanup
* Support play by dash.js
* Use SegmentTemplate timeline mode with $Number$

Co-authored-by: winlin <winlin@vip.126.com>
pull/3286/head
john 2 years ago committed by winlin
parent e6ccd8ec9a
commit d927996890

@ -14,9 +14,9 @@ http_server {
vhost __defaultVhost__ { vhost __defaultVhost__ {
dash { dash {
enabled on; enabled on;
dash_fragment 30; dash_fragment 10;
dash_update_period 150; dash_update_period 10;
dash_timeshift 300; dash_window_size 3;
dash_path ./objs/nginx/html; dash_path ./objs/nginx/html;
dash_mpd_file [app]/[stream].mpd; dash_mpd_file [app]/[stream].mpd;
} }

@ -18,6 +18,7 @@ The changelog for SRS.
## SRS 5.0 Changelog ## SRS 5.0 Changelog
* v5.0, 2022-11-24, For [#299](https://github.com/ossrs/srs/issues/299), DASH: Fix number mode bug to make it run. 5.0.96
* v5.0, 2022-11-23, For [#3176](https://github.com/ossrs/srs/pull/3176): GB28181: Error and logging for HEVC. v5.0.95 * v5.0, 2022-11-23, For [#3176](https://github.com/ossrs/srs/pull/3176): GB28181: Error and logging for HEVC. v5.0.95
* v5.0, 2022-11-22, Merge [#3236](https://github.com/ossrs/srs/pull/3236): Live: Limit cached max frames by gop_cache_max_frames. v5.0.93 * v5.0, 2022-11-22, Merge [#3236](https://github.com/ossrs/srs/pull/3236): Live: Limit cached max frames by gop_cache_max_frames. v5.0.93
* v5.0, 2022-11-22, Asan: Check libasan and show tips. v5.0.92 * v5.0, 2022-11-22, Asan: Check libasan and show tips. v5.0.92

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -101,6 +101,7 @@
<script type="text/javascript" src="js/bootstrap.min.js"></script> <script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/mpegts-1.6.11.min.js"></script> <script type="text/javascript" src="js/mpegts-1.6.11.min.js"></script>
<script type="text/javascript" src="js/hls-0.14.17.min.js"></script> <script type="text/javascript" src="js/hls-0.14.17.min.js"></script>
<script type="text/javascript" src="js/dash-v4.5.1.all.min.js"></script>
<script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/srs.page.js"></script> <script type="text/javascript" src="js/srs.page.js"></script>
<script type="text/javascript" src="js/srs.log.js"></script> <script type="text/javascript" src="js/srs.log.js"></script>
@ -110,6 +111,7 @@
var flvPlayer = null; var flvPlayer = null;
var tsPlayer = null; var tsPlayer = null;
var hlsPlayer = null; var hlsPlayer = null;
var dashPlayer = null;
var stopPlayers = function () { var stopPlayers = function () {
if (flvPlayer) { if (flvPlayer) {
@ -124,6 +126,10 @@
hlsPlayer.destroy(); hlsPlayer.destroy();
hlsPlayer = null; hlsPlayer = null;
} }
if (dashPlayer) {
dashPlayer.destroy();
dashPlayer = null;
}
}; };
var hide_for_error = function () { var hide_for_error = function () {
@ -229,6 +235,15 @@
return; return;
} }
// Start play MPEG-DASH.
if (r.stream.indexOf('.mpd') > 0) {
show_for_ok();
dashPlayer = dashjs.MediaPlayer().create();
dashPlayer.initialize(document.querySelector("#video_player"), r.url, true);
return;
}
console.error('不支持的URL', r.url, r); console.error('不支持的URL', r.url, r);
$('#video_player').hide(); $('#video_player').hide();
}; };

@ -2595,7 +2595,7 @@ srs_error_t SrsConfig::check_normal_config()
for (int j = 0; j < (int)conf->directives.size(); j++) { for (int j = 0; j < (int)conf->directives.size(); j++) {
string m = conf->at(j)->name; string m = conf->at(j)->name;
if (m != "enabled" && m != "dash_fragment" && m != "dash_update_period" && m != "dash_timeshift" && m != "dash_path" if (m != "enabled" && m != "dash_fragment" && m != "dash_update_period" && m != "dash_timeshift" && m != "dash_path"
&& m != "dash_mpd_file") { && m != "dash_mpd_file" && m != "dash_window_size" && m != "dash_dispose" && m != "dash_cleanup") {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.dash.%s of %s", m.c_str(), vhost->arg0().c_str()); return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.dash.%s of %s", m.c_str(), vhost->arg0().c_str());
} }
} }
@ -6454,7 +6454,7 @@ srs_utime_t SrsConfig::get_dash_fragment(string vhost)
{ {
SRS_OVERWRITE_BY_ENV_FLOAT_SECONDS("srs.vhost.dash.dash_fragment"); SRS_OVERWRITE_BY_ENV_FLOAT_SECONDS("srs.vhost.dash.dash_fragment");
static int DEFAULT = 30 * SRS_UTIME_SECONDS; static int DEFAULT = 10 * SRS_UTIME_SECONDS;
SrsConfDirective* conf = get_dash(vhost); SrsConfDirective* conf = get_dash(vhost);
if (!conf) { if (!conf) {
@ -6473,7 +6473,7 @@ srs_utime_t SrsConfig::get_dash_update_period(string vhost)
{ {
SRS_OVERWRITE_BY_ENV_FLOAT_SECONDS("srs.vhost.dash.dash_update_period"); SRS_OVERWRITE_BY_ENV_FLOAT_SECONDS("srs.vhost.dash.dash_update_period");
static srs_utime_t DEFAULT = 150 * SRS_UTIME_SECONDS; static srs_utime_t DEFAULT = 5 * SRS_UTIME_SECONDS;
SrsConfDirective* conf = get_dash(vhost); SrsConfDirective* conf = get_dash(vhost);
if (!conf) { if (!conf) {
@ -6545,6 +6545,63 @@ string SrsConfig::get_dash_mpd_file(string vhost)
return conf->arg0(); return conf->arg0();
} }
int SrsConfig::get_dash_window_size(std::string vhost)
{
SRS_OVERWRITE_BY_ENV_FLOAT_SECONDS("srs.vhost.dash.dash_window_size");
static int DEFAULT = 5;
SrsConfDirective* conf = get_dash(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("dash_window_size");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return ::atoi(conf->arg0().c_str());
}
bool SrsConfig::get_dash_cleanup(std::string vhost)
{
SRS_OVERWRITE_BY_ENV_BOOL2("srs.vhost.dash.dash_cleanup");
static bool DEFAULT = true;
SrsConfDirective* conf = get_dash(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("dash_cleanup");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return SRS_CONF_PERFER_TRUE(conf->arg0());
}
srs_utime_t SrsConfig::get_dash_dispose(std::string vhost)
{
SRS_OVERWRITE_BY_ENV_SECONDS("srs.vhost.dash.dash_dispose");
static srs_utime_t DEFAULT = 0;
SrsConfDirective* conf = get_dash(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("dash_dispose");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return (srs_utime_t)(::atoi(conf->arg0().c_str()) * SRS_UTIME_SECONDS);
}
SrsConfDirective* SrsConfig::get_hls(string vhost) SrsConfDirective* SrsConfig::get_hls(string vhost)
{ {
SrsConfDirective* conf = get_vhost(vhost); SrsConfDirective* conf = get_vhost(vhost);

@ -896,6 +896,12 @@ public:
virtual std::string get_dash_path(std::string vhost); virtual std::string get_dash_path(std::string vhost);
// Get the path for DASH MPD, to generate the MPD file. // Get the path for DASH MPD, to generate the MPD file.
virtual std::string get_dash_mpd_file(std::string vhost); virtual std::string get_dash_mpd_file(std::string vhost);
// Get the number of fragments in MPD file.
virtual int get_dash_window_size(std::string vhost);
// Whether cleanup the old m4s files.
virtual bool get_dash_cleanup(std::string vhost);
// The timeout in srs_utime_t to dispose the dash.
virtual srs_utime_t get_dash_dispose(std::string vhost);
// hls section // hls section
private: private:
// Get the hls directive of vhost. // Get the hls directive of vhost.

@ -19,8 +19,22 @@
#include <stdlib.h> #include <stdlib.h>
#include <sstream> #include <sstream>
#include <unistd.h>
using namespace std; using namespace std;
string srs_time_to_utc_format_str(srs_utime_t u)
{
time_t s = srsu2s(u);
struct tm t;
srs_assert(gmtime_r(&s, &t) != NULL);
char print_buf[256];
size_t ret = strftime(print_buf, sizeof(print_buf), "%Y-%m-%dT%H:%M:%SZ", &t);
return std::string(print_buf, ret);
}
SrsInitMp4::SrsInitMp4() SrsInitMp4::SrsInitMp4()
{ {
fw = new SrsFileWriter(); fw = new SrsFileWriter();
@ -65,20 +79,22 @@ SrsFragmentedMp4::~SrsFragmentedMp4()
srs_freep(fw); srs_freep(fw);
} }
srs_error_t SrsFragmentedMp4::initialize(SrsRequest* r, bool video, SrsMpdWriter* mpd, uint32_t tid) srs_error_t SrsFragmentedMp4::initialize(SrsRequest* r, bool video, int64_t time, SrsMpdWriter* mpd, uint32_t tid)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
string file_home; string file_home;
string file_name; string file_name;
int64_t sequence_number; int64_t sequence_number;
srs_utime_t basetime; if ((err = mpd->get_fragment(video, file_home, file_name, time, sequence_number)) != srs_success) {
if ((err = mpd->get_fragment(video, file_home, file_name, sequence_number, basetime)) != srs_success) { return srs_error_wrap(err, "get fragment, seq=%u, home=%s, file=%s",
return srs_error_wrap(err, "get fragment"); (uint32_t)sequence_number, file_home.c_str(), file_name.c_str());
} }
string home = _srs_config->get_dash_path(r->vhost); string home = _srs_config->get_dash_path(r->vhost);
set_path(home + "/" + file_home + "/" + file_name); set_path(home + "/" + file_home + "/" + file_name);
// Set number of the fragment, use in mpd SegmentTemplate@startNumber later.
set_number(sequence_number);
if ((err = create_dir()) != srs_success) { if ((err = create_dir()) != srs_success) {
return srs_error_wrap(err, "create dir"); return srs_error_wrap(err, "create dir");
@ -89,8 +105,8 @@ srs_error_t SrsFragmentedMp4::initialize(SrsRequest* r, bool video, SrsMpdWriter
return srs_error_wrap(err, "Open fmp4 failed, path=%s", path_tmp.c_str()); return srs_error_wrap(err, "Open fmp4 failed, path=%s", path_tmp.c_str());
} }
if ((err = enc->initialize(fw, (uint32_t)sequence_number, basetime, tid)) != srs_success) { if ((err = enc->initialize(fw, (uint32_t)sequence_number, time, tid)) != srs_success) {
return srs_error_wrap(err, "init encoder"); return srs_error_wrap(err, "init encoder, seq=%u, time=%" PRId64 ", tid=%u", (uint32_t)sequence_number, time, tid);
} }
return err; return err;
@ -146,13 +162,29 @@ SrsMpdWriter::SrsMpdWriter()
{ {
req = NULL; req = NULL;
timeshit = update_period = fragment = 0; timeshit = update_period = fragment = 0;
last_update_mpd = 0;
window_size_ = 0;
availability_start_time_ = 0;
video_number_ = 0;
audio_number_ = 0;
} }
SrsMpdWriter::~SrsMpdWriter() SrsMpdWriter::~SrsMpdWriter()
{ {
} }
void SrsMpdWriter::dispose()
{
if (req) {
string mpd_path = srs_path_build_stream(mpd_file, req->vhost, req->app, req->stream);
string full_path = home + "/" + mpd_path;
if (unlink(full_path.c_str()) < 0) {
srs_warn("ignore remove mpd failed, %s", full_path.c_str());
}
}
}
srs_error_t SrsMpdWriter::initialize(SrsRequest* r) srs_error_t SrsMpdWriter::initialize(SrsRequest* r)
{ {
req = r; req = r;
@ -171,8 +203,10 @@ srs_error_t SrsMpdWriter::on_publish()
string mpd_path = srs_path_build_stream(mpd_file, req->vhost, req->app, req->stream); string mpd_path = srs_path_build_stream(mpd_file, req->vhost, req->app, req->stream);
fragment_home = srs_path_dirname(mpd_path) + "/" + req->stream; fragment_home = srs_path_dirname(mpd_path) + "/" + req->stream;
window_size_ = _srs_config->get_dash_window_size(r->vhost);
srs_trace("DASH: Config fragment=%" PRId64 ", period=%" PRId64, fragment, update_period); srs_trace("DASH: Config fragment=%dms, period=%dms, window=%d, timeshit=%dms, home=%s, mpd=%s",
srsu2msi(fragment), srsu2msi(update_period), window_size_, srsu2msi(timeshit), home.c_str(), mpd_file.c_str());
return srs_success; return srs_success;
} }
@ -181,15 +215,14 @@ void SrsMpdWriter::on_unpublish()
{ {
} }
srs_error_t SrsMpdWriter::write(SrsFormat* format) srs_error_t SrsMpdWriter::write(SrsFormat* format, SrsFragmentWindow* afragments, SrsFragmentWindow* vfragments)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
// MPD is not expired? // TODO: FIXME: pure audio/video support.
if (last_update_mpd != 0 && srs_get_system_time() - last_update_mpd < update_period) { if (afragments->empty() || vfragments->empty()) {
return err; return err;
} }
last_update_mpd = srs_get_system_time();
string mpd_path = srs_path_build_stream(mpd_file, req->vhost, req->app, req->stream); string mpd_path = srs_path_build_stream(mpd_file, req->vhost, req->app, req->stream);
string full_path = home + "/" + mpd_path; string full_path = home + "/" + mpd_path;
@ -200,38 +233,66 @@ srs_error_t SrsMpdWriter::write(SrsFormat* format)
if ((err = srs_create_dir_recursively(full_home)) != srs_success) { if ((err = srs_create_dir_recursively(full_home)) != srs_success) {
return srs_error_wrap(err, "Create MPD home failed, home=%s", full_home.c_str()); return srs_error_wrap(err, "Create MPD home failed, home=%s", full_home.c_str());
} }
double last_duration = srsu2s(srs_max(vfragments->at(vfragments->size() - 1)->duration(), afragments->at(afragments->size() - 1)->duration()));
stringstream ss; stringstream ss;
ss << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl ss << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl
<< "<MPD profiles=\"urn:mpeg:dash:profile:isoff-live:2011,http://dashif.org/guidelines/dash-if-simple\" " << endl << "<MPD profiles=\"urn:mpeg:dash:profile:isoff-live:2011,http://dashif.org/guidelines/dash-if-simple\" " << endl
<< " ns1:schemaLocation=\"urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd\" " << endl << " ns1:schemaLocation=\"urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd\" " << endl
<< " xmlns=\"urn:mpeg:dash:schema:mpd:2011\" xmlns:ns1=\"http://www.w3.org/2001/XMLSchema-instance\" " << endl << " xmlns=\"urn:mpeg:dash:schema:mpd:2011\" xmlns:ns1=\"http://www.w3.org/2001/XMLSchema-instance\" " << endl
<< " type=\"dynamic\" minimumUpdatePeriod=\"PT" << update_period / SRS_UTIME_SECONDS << "S\" " << endl << " type=\"dynamic\" " << endl
<< " timeShiftBufferDepth=\"PT" << timeshit / SRS_UTIME_SECONDS << "S\" availabilityStartTime=\"1970-01-01T00:00:00Z\" " << endl << " minimumUpdatePeriod=\"PT" << srs_fmt("%.3f", srsu2s(update_period)) << "S\" " << endl
<< " maxSegmentDuration=\"PT" << fragment / SRS_UTIME_SECONDS << "S\" minBufferTime=\"PT" << fragment / SRS_UTIME_SECONDS << "S\" >" << endl << " timeShiftBufferDepth=\"PT" << srs_fmt("%.3f", last_duration * window_size_) << "S\" " << endl
<< " <BaseURL>" << req->stream << "/" << "</BaseURL>" << endl << " availabilityStartTime=\"" << srs_time_to_utc_format_str(availability_start_time_) << "\" " << endl
<< " <Period start=\"PT0S\">" << endl; << " publishTime=\"" << srs_time_to_utc_format_str(srs_get_system_time()) << "\" " << endl
if (format->acodec) { << " minBufferTime=\"PT" << srs_fmt("%.3f", 2 * last_duration) << "S\" >" << endl;
ss << " <AdaptationSet mimeType=\"audio/mp4\" segmentAlignment=\"true\" startWithSAP=\"1\">" << endl;
ss << " <SegmentTemplate duration=\"" << fragment / SRS_UTIME_SECONDS << "\" " ss << " <BaseURL>" << req->stream << "/" << "</BaseURL>" << endl;
<< "initialization=\"$RepresentationID$-init.mp4\" "
<< "media=\"$RepresentationID$-$Number$.m4s\" />" << endl; ss << " <Period start=\"PT0S\">" << endl;
ss << " <Representation id=\"audio\" bandwidth=\"48000\" codecs=\"mp4a.40.2\" />" << endl;
ss << " </AdaptationSet>" << endl; if (format->acodec && ! afragments->empty()) {
} int start_index = srs_max(0, afragments->size()-window_size_);
if (format->vcodec) { ss << " <AdaptationSet mimeType=\"audio/mp4\" segmentAlignment=\"true\" startWithSAP=\"1\">" << endl;
ss << " <Representation id=\"audio\" bandwidth=\"48000\" codecs=\"mp4a.40.2\">" << endl;
ss << " <SegmentTemplate initialization=\"$RepresentationID$-init.mp4\" "
<< "media=\"$RepresentationID$-$Number$.m4s\" "
<< "startNumber=\"" << afragments->at(start_index)->number() << "\" "
<< "timescale=\"1000\">" << endl;
ss << " <SegmentTimeline>" << endl;
for (int i = start_index; i < afragments->size(); ++i) {
ss << " <S t=\"" << srsu2ms(afragments->at(i)->get_start_dts()) << "\" "
<< "d=\"" << srsu2ms(afragments->at(i)->duration()) << "\" />" << endl;
}
ss << " </SegmentTimeline>" << endl;
ss << " </SegmentTemplate>" << endl;
ss << " </Representation>" << endl;
ss << " </AdaptationSet>" << endl;
}
if (format->vcodec && ! vfragments->empty()) {
int start_index = srs_max(0, vfragments->size()-window_size_);
int w = format->vcodec->width; int w = format->vcodec->width;
int h = format->vcodec->height; int h = format->vcodec->height;
ss << " <AdaptationSet mimeType=\"video/mp4\" segmentAlignment=\"true\" startWithSAP=\"1\">" << endl; ss << " <AdaptationSet mimeType=\"video/mp4\" segmentAlignment=\"true\" startWithSAP=\"1\">" << endl;
ss << " <SegmentTemplate duration=\"" << fragment / SRS_UTIME_SECONDS << "\" " ss << " <Representation id=\"video\" bandwidth=\"800000\" codecs=\"avc1.64001e\" " << "width=\"" << w << "\" height=\"" << h << "\">" << endl;
<< "initialization=\"$RepresentationID$-init.mp4\" " ss << " <SegmentTemplate initialization=\"$RepresentationID$-init.mp4\" "
<< "media=\"$RepresentationID$-$Number$.m4s\" />" << endl; << "media=\"$RepresentationID$-$Number$.m4s\" "
ss << " <Representation id=\"video\" bandwidth=\"800000\" codecs=\"avc1.64001e\" " << "startNumber=\"" << vfragments->at(start_index)->number() << "\" "
<< "width=\"" << w << "\" height=\"" << h << "\"/>" << endl; << "timescale=\"1000\">" << endl;
ss << " </AdaptationSet>" << endl; ss << " <SegmentTimeline>" << endl;
for (int i = start_index; i < vfragments->size(); ++i) {
ss << " <S t=\"" << srsu2ms(vfragments->at(i)->get_start_dts()) << "\" "
<< "d=\"" << srsu2ms(vfragments->at(i)->duration()) << "\" />" << endl;
}
ss << " </SegmentTimeline>" << endl;
ss << " </SegmentTemplate>" << endl;
ss << " </Representation>" << endl;
ss << " </AdaptationSet>" << endl;
} }
ss << " </Period>" << endl ss << " </Period>" << endl;
<< "</MPD>" << endl; ss << "</MPD>" << endl;
SrsFileWriter* fw = new SrsFileWriter(); SrsFileWriter* fw = new SrsFileWriter();
SrsAutoFree(SrsFileWriter, fw); SrsAutoFree(SrsFileWriter, fw);
@ -255,7 +316,7 @@ srs_error_t SrsMpdWriter::write(SrsFormat* format)
return err; return err;
} }
srs_error_t SrsMpdWriter::get_fragment(bool video, std::string& home, std::string& file_name, int64_t& sn, srs_utime_t& basetime) srs_error_t SrsMpdWriter::get_fragment(bool video, std::string& home, std::string& file_name, int64_t time, int64_t& sn)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -264,32 +325,42 @@ srs_error_t SrsMpdWriter::get_fragment(bool video, std::string& home, std::strin
// We name the segment as advanced N segments, because when we are generating segment at the current time, // We name the segment as advanced N segments, because when we are generating segment at the current time,
// the player may also request the current segment. // the player may also request the current segment.
srs_assert(fragment); srs_assert(fragment);
int64_t number = (srs_update_system_time() / fragment + 1);
// TOOD: FIXME: Should keep the segments continuous, or player may fail.
sn = number;
// The base time aligned with sn.
basetime = sn * fragment;
if (video) { if (video) {
sn = video_number_++;
file_name = "video-" + srs_int2str(sn) + ".m4s"; file_name = "video-" + srs_int2str(sn) + ".m4s";
} else { } else {
sn = audio_number_++;
file_name = "audio-" + srs_int2str(sn) + ".m4s"; file_name = "audio-" + srs_int2str(sn) + ".m4s";
} }
return err; return err;
} }
void SrsMpdWriter::set_availability_start_time(srs_utime_t t)
{
availability_start_time_ = t;
}
srs_utime_t SrsMpdWriter::get_availability_start_time()
{
return availability_start_time_;
}
SrsDashController::SrsDashController() SrsDashController::SrsDashController()
{ {
req = NULL; req = NULL;
video_tack_id = 0; format_ = NULL;
audio_track_id = 1; // trackid start from 1, because some player will check if track id is greater than 0
video_track_id = 1;
audio_track_id = 2;
mpd = new SrsMpdWriter(); mpd = new SrsMpdWriter();
vcurrent = acurrent = NULL; vcurrent = acurrent = NULL;
vfragments = new SrsFragmentWindow(); vfragments = new SrsFragmentWindow();
afragments = new SrsFragmentWindow(); afragments = new SrsFragmentWindow();
audio_dts = video_dts = 0; audio_dts = video_dts = 0;
first_dts_ = -1;
video_reaped_ = false;
fragment = 0; fragment = 0;
} }
@ -302,6 +373,28 @@ SrsDashController::~SrsDashController()
srs_freep(afragments); srs_freep(afragments);
} }
void SrsDashController::dispose()
{
srs_error_t err = srs_success;
vfragments->dispose();
afragments->dispose();
if (vcurrent && (err = vcurrent->unlink_tmpfile()) != srs_success) {
srs_warn("Unlink tmp video m4s failed %s", srs_error_desc(err).c_str());
srs_freep(err);
}
if (acurrent && (err = acurrent->unlink_tmpfile()) != srs_success) {
srs_warn("Unlink tmp audio m4s failed %s", srs_error_desc(err).c_str());
srs_freep(err);
}
mpd->dispose();
srs_trace("gracefully dispose dash %s", req? req->get_stream_url().c_str() : "");
}
srs_error_t SrsDashController::initialize(SrsRequest* r) srs_error_t SrsDashController::initialize(SrsRequest* r)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -329,16 +422,17 @@ srs_error_t SrsDashController::on_publish()
} }
srs_freep(vcurrent); srs_freep(vcurrent);
vcurrent = new SrsFragmentedMp4(); srs_freep(vfragments);
if ((err = vcurrent->initialize(req, true, mpd, video_tack_id)) != srs_success) { vfragments = new SrsFragmentWindow();
return srs_error_wrap(err, "video fragment");
}
srs_freep(acurrent); srs_freep(acurrent);
acurrent = new SrsFragmentedMp4(); srs_freep(afragments);
if ((err = acurrent->initialize(req, false, mpd, audio_track_id)) != srs_success) { afragments = new SrsFragmentWindow();
return srs_error_wrap(err, "audio fragment");
} audio_dts = 0;
video_dts = 0;
first_dts_ = -1;
video_reaped_ = false;
return err; return err;
} }
@ -349,28 +443,63 @@ void SrsDashController::on_unpublish()
srs_error_t err = srs_success; srs_error_t err = srs_success;
if ((err = vcurrent->reap(video_dts)) != srs_success) { if (vcurrent && (err = vcurrent->reap(video_dts)) != srs_success) {
srs_warn("reap video err %s", srs_error_desc(err).c_str()); srs_warn("reap video dts=%" PRId64 " err %s", video_dts, srs_error_desc(err).c_str());
srs_freep(err); srs_freep(err);
} }
srs_freep(vcurrent);
if ((err = acurrent->reap(audio_dts)) != srs_success) { if (vcurrent && vcurrent->duration()) {
srs_warn("reap audio err %s", srs_error_desc(err).c_str()); vfragments->append(vcurrent);
vcurrent = NULL;
}
if (acurrent && (err = acurrent->reap(audio_dts)) != srs_success) {
srs_warn("reap audio dts=%" PRId64 " err %s", audio_dts, srs_error_desc(err).c_str());
srs_freep(err); srs_freep(err);
} }
srs_freep(acurrent);
if (acurrent->duration() > 0) {
afragments->append(acurrent);
acurrent = NULL;
}
if ((err = refresh_mpd(format_)) != srs_success) {
srs_warn("Refresh the MPD failed, err=%s", srs_error_desc(err).c_str());
srs_error_reset(err);
}
} }
srs_error_t SrsDashController::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) srs_error_t SrsDashController::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
format_ = format;
if (format->is_aac_sequence_header()) { if (format->is_aac_sequence_header()) {
return refresh_init_mp4(shared_audio, format); return refresh_init_mp4(shared_audio, format);
} }
audio_dts = shared_audio->timestamp;
if (! acurrent) {
acurrent = new SrsFragmentedMp4();
if ((err = acurrent->initialize(req, false, audio_dts * SRS_UTIME_MILLISECONDS, mpd, audio_track_id)) != srs_success) {
return srs_error_wrap(err, "Initialize the audio fragment failed");
}
}
if (acurrent->duration() >= fragment) { if (first_dts_ == -1) {
first_dts_ = audio_dts;
mpd->set_availability_start_time(srs_get_system_time() - first_dts_ * SRS_UTIME_MILLISECONDS);
}
// TODO: FIXME: Support pure audio streaming.
if (video_reaped_) {
// The video is reaped, audio must be reaped right now to align the timestamp of video.
video_reaped_ = false;
// Append current timestamp to calculate right duration.
acurrent->append(shared_audio->timestamp);
if ((err = acurrent->reap(audio_dts)) != srs_success) { if ((err = acurrent->reap(audio_dts)) != srs_success) {
return srs_error_wrap(err, "reap current"); return srs_error_wrap(err, "reap current");
} }
@ -378,51 +507,109 @@ srs_error_t SrsDashController::on_audio(SrsSharedPtrMessage* shared_audio, SrsFo
afragments->append(acurrent); afragments->append(acurrent);
acurrent = new SrsFragmentedMp4(); acurrent = new SrsFragmentedMp4();
if ((err = acurrent->initialize(req, false, mpd, audio_track_id)) != srs_success) { if ((err = acurrent->initialize(req, false, audio_dts * SRS_UTIME_MILLISECONDS, mpd, audio_track_id)) != srs_success) {
return srs_error_wrap(err, "Initialize the audio fragment failed"); return srs_error_wrap(err, "Initialize the audio fragment failed");
} }
if ((err = refresh_mpd(format)) != srs_success) {
return srs_error_wrap(err, "Refresh the MPD failed");
}
} }
if ((err = acurrent->write(shared_audio, format)) != srs_success) { if ((err = acurrent->write(shared_audio, format)) != srs_success) {
return srs_error_wrap(err, "Write audio to fragment failed"); return srs_error_wrap(err, "Write audio to fragment failed");
} }
if ((err = refresh_mpd(format)) != srs_success) { srs_utime_t fragment = _srs_config->get_dash_fragment(req->vhost);
return srs_error_wrap(err, "Refresh the MPD failed"); int window_size = _srs_config->get_dash_window_size(req->vhost);
int dash_window = 2 * window_size * fragment;
if (afragments->size() > window_size) {
int w = 0;
for (int i = afragments->size() - window_size; i < afragments->size(); ++i) {
w += afragments->at(i)->duration();
}
dash_window = srs_max(dash_window, w);
// shrink the segments.
afragments->shrink(dash_window);
} }
bool dash_cleanup = _srs_config->get_dash_cleanup(req->vhost);
// remove the m4s file.
afragments->clear_expired(dash_cleanup);
return err; return err;
} }
srs_error_t SrsDashController::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) srs_error_t SrsDashController::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
format_ = format;
if (format->is_avc_sequence_header()) { if (format->is_avc_sequence_header()) {
return refresh_init_mp4(shared_video, format); return refresh_init_mp4(shared_video, format);
} }
video_dts = shared_video->timestamp;
if (! vcurrent) {
vcurrent = new SrsFragmentedMp4();
if ((err = vcurrent->initialize(req, true, video_dts * SRS_UTIME_MILLISECONDS, mpd, video_track_id)) != srs_success) {
return srs_error_wrap(err, "Initialize the video fragment failed");
}
}
if (first_dts_ == -1) {
first_dts_ = video_dts;
mpd->set_availability_start_time(srs_get_system_time() - first_dts_ * SRS_UTIME_MILLISECONDS);
}
bool reopen = format->video->frame_type == SrsVideoAvcFrameTypeKeyFrame && vcurrent->duration() >= fragment; bool reopen = format->video->frame_type == SrsVideoAvcFrameTypeKeyFrame && vcurrent->duration() >= fragment;
if (reopen) { if (reopen) {
// Append current timestamp to calculate right duration.
vcurrent->append(shared_video->timestamp);
if ((err = vcurrent->reap(video_dts)) != srs_success) { if ((err = vcurrent->reap(video_dts)) != srs_success) {
return srs_error_wrap(err, "reap current"); return srs_error_wrap(err, "reap current");
} }
// Mark the video has reaped, audio will reaped when recv next frame.
video_reaped_ = true;
vfragments->append(vcurrent); vfragments->append(vcurrent);
vcurrent = new SrsFragmentedMp4(); vcurrent = new SrsFragmentedMp4();
if ((err = vcurrent->initialize(req, true, mpd, video_tack_id)) != srs_success) { if ((err = vcurrent->initialize(req, true, video_dts * SRS_UTIME_MILLISECONDS, mpd, video_track_id)) != srs_success) {
return srs_error_wrap(err, "Initialize the video fragment failed"); return srs_error_wrap(err, "Initialize the video fragment failed");
} }
if ((err = refresh_mpd(format)) != srs_success) {
return srs_error_wrap(err, "Refresh the MPD failed");
}
} }
if ((err = vcurrent->write(shared_video, format)) != srs_success) { if ((err = vcurrent->write(shared_video, format)) != srs_success) {
return srs_error_wrap(err, "Write video to fragment failed"); return srs_error_wrap(err, "Write video to fragment failed");
} }
if ((err = refresh_mpd(format)) != srs_success) { srs_utime_t fragment = _srs_config->get_dash_fragment(req->vhost);
return srs_error_wrap(err, "Refresh the MPD failed"); int window_size = _srs_config->get_dash_window_size(req->vhost);
int dash_window = 2 * window_size * fragment;
if (vfragments->size() > window_size) {
int w = 0;
for (int i = vfragments->size() - window_size; i < vfragments->size(); ++i) {
w += vfragments->at(i)->duration();
}
dash_window = srs_max(dash_window, w);
// shrink the segments.
vfragments->shrink(dash_window);
} }
bool dash_cleanup = _srs_config->get_dash_cleanup(req->vhost);
// remove the m4s file.
vfragments->clear_expired(dash_cleanup);
return err; return err;
} }
@ -436,7 +623,7 @@ srs_error_t SrsDashController::refresh_mpd(SrsFormat* format)
return err; return err;
} }
if ((err = mpd->write(format)) != srs_success) { if ((err = mpd->write(format, afragments, vfragments)) != srs_success) {
return srs_error_wrap(err, "write mpd"); return srs_error_wrap(err, "write mpd");
} }
@ -470,7 +657,7 @@ srs_error_t SrsDashController::refresh_init_mp4(SrsSharedPtrMessage* msg, SrsFor
init_mp4->set_path(path); init_mp4->set_path(path);
int tid = msg->is_video()? video_tack_id:audio_track_id; int tid = msg->is_video()? video_track_id : audio_track_id;
if ((err = init_mp4->write(format, msg->is_video(), tid)) != srs_success) { if ((err = init_mp4->write(format, msg->is_video(), tid)) != srs_success) {
return srs_error_wrap(err, "write init"); return srs_error_wrap(err, "write init");
} }
@ -479,7 +666,7 @@ srs_error_t SrsDashController::refresh_init_mp4(SrsSharedPtrMessage* msg, SrsFor
return srs_error_wrap(err, "rename init"); return srs_error_wrap(err, "rename init");
} }
srs_trace("DASH: Refresh media success, file=%s", path.c_str()); srs_trace("DASH: Refresh media type=%s, file=%s", (msg->is_video() ? "video": "audio"), path.c_str());
return err; return err;
} }
@ -491,6 +678,8 @@ SrsDash::SrsDash()
controller = new SrsDashController(); controller = new SrsDashController();
enabled = false; enabled = false;
disposable_ = false;
last_update_time_ = 0;
} }
SrsDash::~SrsDash() SrsDash::~SrsDash()
@ -498,6 +687,53 @@ SrsDash::~SrsDash()
srs_freep(controller); srs_freep(controller);
} }
void SrsDash::dispose()
{
if (enabled) {
on_unpublish();
}
// Ignore when dash_dispose disabled.
srs_utime_t dash_dispose = _srs_config->get_dash_dispose(req->vhost);
if (!dash_dispose) {
return;
}
controller->dispose();
}
srs_error_t SrsDash::cycle()
{
srs_error_t err = srs_success;
if (last_update_time_ <= 0) {
last_update_time_ = srs_get_system_time();
}
if (!req) {
return err;
}
srs_utime_t dash_dispose = _srs_config->get_dash_dispose(req->vhost);
if (dash_dispose <= 0) {
return err;
}
if (srs_get_system_time() - last_update_time_ <= dash_dispose) {
return err;
}
last_update_time_ = srs_get_system_time();
if (!disposable_) {
return err;
}
disposable_ = false;
srs_trace("dash cycle to dispose dash %s, timeout=%dms", req->get_stream_url().c_str(), dash_dispose);
dispose();
return err;
}
srs_error_t SrsDash::initialize(SrsOriginHub* h, SrsRequest* r) srs_error_t SrsDash::initialize(SrsOriginHub* h, SrsRequest* r)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -526,9 +762,15 @@ srs_error_t SrsDash::on_publish()
} }
enabled = true; enabled = true;
// update the dash time, for dash_dispose.
last_update_time_ = srs_get_system_time();
if ((err = controller->on_publish()) != srs_success) { if ((err = controller->on_publish()) != srs_success) {
return srs_error_wrap(err, "controller"); return srs_error_wrap(err, "controller");
} }
// ok, the dash can be dispose, or need to be dispose.
disposable_ = true;
return err; return err;
} }
@ -545,6 +787,9 @@ srs_error_t SrsDash::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* form
return err; return err;
} }
// update the dash time, for dash_dispose.
last_update_time_ = srs_get_system_time();
if ((err = controller->on_audio(shared_audio, format)) != srs_success) { if ((err = controller->on_audio(shared_audio, format)) != srs_success) {
return srs_error_wrap(err, "Consume audio failed"); return srs_error_wrap(err, "Consume audio failed");
} }
@ -564,6 +809,9 @@ srs_error_t SrsDash::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* form
return err; return err;
} }
// update the dash time, for dash_dispose.
last_update_time_ = srs_get_system_time();
if ((err = controller->on_video(shared_video, format)) != srs_success) { if ((err = controller->on_video(shared_video, format)) != srs_success) {
return srs_error_wrap(err, "Consume video failed"); return srs_error_wrap(err, "Consume video failed");
} }

@ -48,7 +48,7 @@ public:
virtual ~SrsFragmentedMp4(); virtual ~SrsFragmentedMp4();
public: public:
// Initialize the fragment, create the home dir, open the file. // Initialize the fragment, create the home dir, open the file.
virtual srs_error_t initialize(SrsRequest* r, bool video, SrsMpdWriter* mpd, uint32_t tid); virtual srs_error_t initialize(SrsRequest* r, bool video, int64_t time, SrsMpdWriter* mpd, uint32_t tid);
// Write media message to fragment. // Write media message to fragment.
virtual srs_error_t write(SrsSharedPtrMessage* shared_msg, SrsFormat* format); virtual srs_error_t write(SrsSharedPtrMessage* shared_msg, SrsFormat* format);
// Reap the fragment, close the fd and rename tmp to official file. // Reap the fragment, close the fd and rename tmp to official file.
@ -60,7 +60,6 @@ class SrsMpdWriter
{ {
private: private:
SrsRequest* req; SrsRequest* req;
srs_utime_t last_update_mpd;
private: private:
// The duration of fragment in srs_utime_t. // The duration of fragment in srs_utime_t.
srs_utime_t fragment; srs_utime_t fragment;
@ -72,22 +71,35 @@ private:
std::string home; std::string home;
// The MPD path template, from which to build the file path. // The MPD path template, from which to build the file path.
std::string mpd_file; std::string mpd_file;
// The number of fragments in MPD file.
int window_size_;
// The availabilityStartTime in MPD file.
srs_utime_t availability_start_time_;
// The number of current video segment.
uint64_t video_number_;
// The number of current audio segment.
uint64_t audio_number_;
private: private:
// The home for fragment, relative to home. // The home for fragment, relative to home.
std::string fragment_home; std::string fragment_home;
public: public:
SrsMpdWriter(); SrsMpdWriter();
virtual ~SrsMpdWriter(); virtual ~SrsMpdWriter();
public:
virtual void dispose();
public: public:
virtual srs_error_t initialize(SrsRequest* r); virtual srs_error_t initialize(SrsRequest* r);
virtual srs_error_t on_publish(); virtual srs_error_t on_publish();
virtual void on_unpublish(); virtual void on_unpublish();
// Write MPD according to parsed format of stream. // Write MPD according to parsed format of stream.
virtual srs_error_t write(SrsFormat* format); virtual srs_error_t write(SrsFormat* format, SrsFragmentWindow* afragments, SrsFragmentWindow* vfragments);
public: public:
// Get the fragment relative home and filename. // Get the fragment relative home and filename.
// The basetime is the absolute time in srs_utime_t, while the sn(sequence number) is basetime/fragment. // The basetime is the absolute time in srs_utime_t, while the sn(sequence number) is basetime/fragment.
virtual srs_error_t get_fragment(bool video, std::string& home, std::string& filename, int64_t& sn, srs_utime_t& basetime); virtual srs_error_t get_fragment(bool video, std::string& home, std::string& filename, int64_t time, int64_t& sn);
// Set the availabilityStartTime once, map the timestamp in media to utc time.
virtual void set_availability_start_time(srs_utime_t t);
virtual srs_utime_t get_availability_start_time();
}; };
// The controller for DASH, control the MPD and FMP4 generating system. // The controller for DASH, control the MPD and FMP4 generating system.
@ -95,24 +107,33 @@ class SrsDashController
{ {
private: private:
SrsRequest* req; SrsRequest* req;
SrsFormat* format_;
SrsMpdWriter* mpd; SrsMpdWriter* mpd;
private: private:
SrsFragmentedMp4* vcurrent; SrsFragmentedMp4* vcurrent;
SrsFragmentWindow* vfragments; SrsFragmentWindow* vfragments;
SrsFragmentedMp4* acurrent; SrsFragmentedMp4* acurrent;
SrsFragmentWindow* afragments; SrsFragmentWindow* afragments;
// Current audio dts.
uint64_t audio_dts; uint64_t audio_dts;
// Current video dts.
uint64_t video_dts; uint64_t video_dts;
// First dts of the stream, use to calculate the availabilityStartTime in MPD.
int64_t first_dts_;
// Had the video reaped, use to align audio/video segment's timestamp.
bool video_reaped_;
private: private:
// The fragment duration in srs_utime_t to reap it. // The fragment duration in srs_utime_t to reap it.
srs_utime_t fragment; srs_utime_t fragment;
private: private:
std::string home; std::string home;
int video_tack_id; int video_track_id;
int audio_track_id; int audio_track_id;
public: public:
SrsDashController(); SrsDashController();
virtual ~SrsDashController(); virtual ~SrsDashController();
public:
virtual void dispose();
public: public:
virtual srs_error_t initialize(SrsRequest* r); virtual srs_error_t initialize(SrsRequest* r);
virtual srs_error_t on_publish(); virtual srs_error_t on_publish();
@ -129,6 +150,8 @@ class SrsDash
{ {
private: private:
bool enabled; bool enabled;
bool disposable_;
srs_utime_t last_update_time_;
private: private:
SrsRequest* req; SrsRequest* req;
SrsOriginHub* hub; SrsOriginHub* hub;
@ -136,6 +159,9 @@ private:
public: public:
SrsDash(); SrsDash();
virtual ~SrsDash(); virtual ~SrsDash();
public:
virtual void dispose();
virtual srs_error_t cycle();
public: public:
// Initalize the encoder. // Initalize the encoder.
virtual srs_error_t initialize(SrsOriginHub* h, SrsRequest* r); virtual srs_error_t initialize(SrsOriginHub* h, SrsRequest* r);

@ -19,6 +19,7 @@ SrsFragment::SrsFragment()
dur = 0; dur = 0;
start_dts = -1; start_dts = -1;
sequence_header = false; sequence_header = false;
number_ = 0;
} }
SrsFragment::~SrsFragment() SrsFragment::~SrsFragment()
@ -46,6 +47,11 @@ void SrsFragment::append(int64_t dts)
dur = dts_in_tbn - start_dts; dur = dts_in_tbn - start_dts;
} }
srs_utime_t SrsFragment::get_start_dts()
{
return start_dts;
}
srs_utime_t SrsFragment::duration() srs_utime_t SrsFragment::duration()
{ {
return dur; return dur;
@ -136,6 +142,16 @@ srs_error_t SrsFragment::rename()
return err; return err;
} }
void SrsFragment::set_number(uint64_t n)
{
number_ = n;
}
uint64_t SrsFragment::number()
{
return number_;
}
SrsFragmentWindow::SrsFragmentWindow() SrsFragmentWindow::SrsFragmentWindow()
{ {
} }

@ -25,6 +25,8 @@ private:
srs_utime_t start_dts; srs_utime_t start_dts;
// Whether current segement contains sequence header. // Whether current segement contains sequence header.
bool sequence_header; bool sequence_header;
// The number of this segment, use in dash mpd.
uint64_t number_;
public: public:
SrsFragment(); SrsFragment();
virtual ~SrsFragment(); virtual ~SrsFragment();
@ -32,6 +34,8 @@ public:
// Append a frame with dts into fragment. // Append a frame with dts into fragment.
// @dts The dts of frame in ms. // @dts The dts of frame in ms.
virtual void append(int64_t dts); virtual void append(int64_t dts);
// Get the start dts of fragment.
virtual srs_utime_t get_start_dts();
// Get the duration of fragment in srs_utime_t. // Get the duration of fragment in srs_utime_t.
virtual srs_utime_t duration(); virtual srs_utime_t duration();
// Whether the fragment contains any sequence header. // Whether the fragment contains any sequence header.
@ -54,6 +58,10 @@ public:
virtual srs_error_t unlink_tmpfile(); virtual srs_error_t unlink_tmpfile();
// Rename the temp file to final file. // Rename the temp file to final file.
virtual srs_error_t rename(); virtual srs_error_t rename();
public:
// Get or set the number of this fragment.
virtual void set_number(uint64_t n);
virtual uint64_t number();
}; };
// The fragment window manage a series of fragment. // The fragment window manage a series of fragment.

@ -895,8 +895,7 @@ srs_error_t SrsOriginHub::initialize(SrsLiveSource* s, SrsRequest* r)
void SrsOriginHub::dispose() void SrsOriginHub::dispose()
{ {
hls->dispose(); hls->dispose();
dash->dispose();
// TODO: Support dispose DASH.
} }
srs_error_t SrsOriginHub::cycle() srs_error_t SrsOriginHub::cycle()
@ -907,7 +906,9 @@ srs_error_t SrsOriginHub::cycle()
return srs_error_wrap(err, "hls cycle"); return srs_error_wrap(err, "hls cycle");
} }
// TODO: Support cycle DASH. if ((err = dash->cycle()) != srs_success) {
return srs_error_wrap(err, "dash cycle");
}
return err; return err;
} }

@ -23,6 +23,10 @@ typedef int64_t srs_utime_t;
#define srsu2ms(us) ((us) / SRS_UTIME_MILLISECONDS) #define srsu2ms(us) ((us) / SRS_UTIME_MILLISECONDS)
#define srsu2msi(us) int((us) / SRS_UTIME_MILLISECONDS) #define srsu2msi(us) int((us) / SRS_UTIME_MILLISECONDS)
// Convert srs_utime_t as second.
#define srsu2s(us) ((us) / SRS_UTIME_SECONDS)
#define srsu2si(us) ((us) / SRS_UTIME_SECONDS)
// Them time duration = end - start. return 0, if start or end is 0. // Them time duration = end - start. return 0, if start or end is 0.
srs_utime_t srs_duration(srs_utime_t start, srs_utime_t end); srs_utime_t srs_duration(srs_utime_t start, srs_utime_t end);

@ -9,6 +9,6 @@
#define VERSION_MAJOR 5 #define VERSION_MAJOR 5
#define VERSION_MINOR 0 #define VERSION_MINOR 0
#define VERSION_REVISION 95 #define VERSION_REVISION 96
#endif #endif

@ -4912,7 +4912,7 @@ srs_error_t SrsMp4SampleManager::write(SrsMp4MovieBox* moov)
return err; return err;
} }
srs_error_t SrsMp4SampleManager::write(SrsMp4MovieFragmentBox* moof, uint64_t& dts) srs_error_t SrsMp4SampleManager::write(SrsMp4MovieFragmentBox* moof, uint64_t dts)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -4936,11 +4936,12 @@ srs_error_t SrsMp4SampleManager::write(SrsMp4MovieFragmentBox* moof, uint64_t& d
entry->sample_flags = 0x01000000; entry->sample_flags = 0x01000000;
} }
entry->sample_duration = (uint32_t)srs_min(100, sample->dts - dts); vector<SrsMp4Sample*>::iterator iter = (it + 1);
if (entry->sample_duration == 0) { if (iter == samples.end()) {
entry->sample_duration = 40; entry->sample_duration = dts - sample->dts;
} else {
entry->sample_duration = (*iter)->dts - sample->dts;
} }
dts = sample->dts;
entry->sample_size = sample->nb_data; entry->sample_size = sample->nb_data;
entry->sample_composition_time_offset = (int64_t)(sample->pts - sample->dts); entry->sample_composition_time_offset = (int64_t)(sample->pts - sample->dts);
@ -6445,8 +6446,7 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts)
uint64_t duration = 0; uint64_t duration = 0;
if (samples && !samples->samples.empty()) { if (samples && !samples->samples.empty()) {
SrsMp4Sample* first = samples->samples[0]; SrsMp4Sample* first = samples->samples[0];
SrsMp4Sample* last = samples->samples[samples->samples.size() - 1]; duration = srs_max(0, dts - first->dts);
duration = srs_max(0, last->dts - first->dts);
} }
SrsMp4SegmentIndexEntry entry; SrsMp4SegmentIndexEntry entry;

@ -1906,7 +1906,7 @@ public:
virtual srs_error_t write(SrsMp4MovieBox* moov); virtual srs_error_t write(SrsMp4MovieBox* moov);
// Write the samples info to moof. // Write the samples info to moof.
// @param The dts is the dts of last segment. // @param The dts is the dts of last segment.
virtual srs_error_t write(SrsMp4MovieFragmentBox* moof, uint64_t& dts); virtual srs_error_t write(SrsMp4MovieFragmentBox* moof, uint64_t dts);
private: private:
virtual srs_error_t write_track(SrsFrameType track, virtual srs_error_t write_track(SrsFrameType track,
SrsMp4DecodingTime2SampleBox* stts, SrsMp4SyncSampleBox* stss, SrsMp4CompositionTime2SampleBox* ctts, SrsMp4DecodingTime2SampleBox* stts, SrsMp4SyncSampleBox* stss, SrsMp4CompositionTime2SampleBox* ctts,

@ -461,8 +461,7 @@ srs_error_t SrsHttpFileServer::serve_file(ISrsHttpResponseWriter* w, ISrsHttpMes
_mime[".jpg"] = "image/jpeg"; _mime[".jpg"] = "image/jpeg";
_mime[".gif"] = "image/gif"; _mime[".gif"] = "image/gif";
// For MPEG-DASH. // For MPEG-DASH.
//_mime[".mpd"] = "application/dash+xml"; _mime[".mpd"] = "application/dash+xml";
_mime[".mpd"] = "text/xml";
_mime[".m4s"] = "video/iso.segment"; _mime[".m4s"] = "video/iso.segment";
_mime[".mp4v"] = "video/mp4"; _mime[".mp4v"] = "video/mp4";
} }

@ -1967,8 +1967,8 @@ VOID TEST(ConfigUnitTest, CheckDefaultValuesVhost)
if (true) { if (true) {
HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF)); HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF));
EXPECT_EQ(30 * SRS_UTIME_SECONDS, conf.get_dash_fragment("")); EXPECT_EQ(10 * SRS_UTIME_SECONDS, conf.get_dash_fragment(""));
EXPECT_EQ(150 * SRS_UTIME_SECONDS, conf.get_dash_update_period("")); EXPECT_EQ(5 * SRS_UTIME_SECONDS, conf.get_dash_update_period(""));
EXPECT_EQ(300 * SRS_UTIME_SECONDS, conf.get_dash_timeshift("")); EXPECT_EQ(300 * SRS_UTIME_SECONDS, conf.get_dash_timeshift(""));
HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "vhost v{dash{dash_fragment 4;dash_update_period 40;dash_timeshift 70;}}")); HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "vhost v{dash{dash_fragment 4;dash_update_period 40;dash_timeshift 70;}}"));

Loading…
Cancel
Save