Skip to content

Commit 8a28a11

Browse files
committed
For #299, increase dash segment size for avsync issue. 3.0.89
1 parent d11a7b2 commit 8a28a11

File tree

7 files changed

+101
-67
lines changed

7 files changed

+101
-67
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ For previous versions, please read:
146146

147147
## V3 changes
148148

149+
* v3.0, 2019-12-29, For [#299][bug #299], increase dash segment size for avsync issue. 3.0.89
149150
* v3.0, 2019-12-27, For [#299][bug #299], fix some bugs in dash, it works now. 3.0.88
150151
* v3.0, 2019-12-27, For [#1544][bug #1544], fix memory leaking for complex error. 3.0.87
151152
* v3.0, 2019-12-27, Add links for flv.js, hls.js and dash.js.

trunk/conf/full.conf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -965,14 +965,14 @@ vhost dash.srs.com {
965965
# Default: off
966966
enabled on;
967967
# The duration of segment in seconds.
968-
# Default: 3
969-
dash_fragment 3;
970-
# The period to update the MPD in seconds.
971968
# Default: 30
972-
dash_update_period 30;
969+
dash_fragment 30;
970+
# The period to update the MPD in seconds.
971+
# Default: 150
972+
dash_update_period 150;
973973
# The depth of timeshift buffer in seconds.
974-
# Default: 60
975-
dash_timeshift 60;
974+
# Default: 300
975+
dash_timeshift 300;
976976
# The base/home dir/path for dash.
977977
# All init and segment files will write under this dir.
978978
dash_path ./objs/nginx/html;

trunk/src/app/srs_app_config.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5810,7 +5810,7 @@ bool SrsConfig::get_dash_enabled(string vhost)
58105810

58115811
srs_utime_t SrsConfig::get_dash_fragment(string vhost)
58125812
{
5813-
static int DEFAULT = 3 * SRS_UTIME_SECONDS;
5813+
static int DEFAULT = 30 * SRS_UTIME_SECONDS;
58145814

58155815
SrsConfDirective* conf = get_dash(vhost);
58165816
if (!conf) {
@@ -5827,7 +5827,7 @@ srs_utime_t SrsConfig::get_dash_fragment(string vhost)
58275827

58285828
srs_utime_t SrsConfig::get_dash_update_period(string vhost)
58295829
{
5830-
static srs_utime_t DEFAULT = 30 * SRS_UTIME_SECONDS;
5830+
static srs_utime_t DEFAULT = 150 * SRS_UTIME_SECONDS;
58315831

58325832
SrsConfDirective* conf = get_dash(vhost);
58335833
if (!conf) {
@@ -5844,7 +5844,7 @@ srs_utime_t SrsConfig::get_dash_update_period(string vhost)
58445844

58455845
srs_utime_t SrsConfig::get_dash_timeshift(string vhost)
58465846
{
5847-
static srs_utime_t DEFAULT = 60 * SRS_UTIME_SECONDS;
5847+
static srs_utime_t DEFAULT = 300 * SRS_UTIME_SECONDS;
58485848

58495849
SrsConfDirective* conf = get_dash(vhost);
58505850
if (!conf) {

trunk/src/app/srs_app_dash.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,18 @@ void SrsDashController::on_unpublish()
363363
{
364364
mpd->on_unpublish();
365365

366+
srs_error_t err = srs_success;
367+
368+
if ((err = vcurrent->reap(video_dts)) != srs_success) {
369+
srs_warn("reap video err %s", srs_error_desc(err).c_str());
370+
srs_freep(err);
371+
}
366372
srs_freep(vcurrent);
373+
374+
if ((err = acurrent->reap(audio_dts)) != srs_success) {
375+
srs_warn("reap audio err %s", srs_error_desc(err).c_str());
376+
srs_freep(err);
377+
}
367378
srs_freep(acurrent);
368379
}
369380

trunk/src/core/srs_core.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// The version config.
2828
#define VERSION_MAJOR 3
2929
#define VERSION_MINOR 0
30-
#define VERSION_REVISION 88
30+
#define VERSION_REVISION 89
3131

3232
// The macros generated by configure script.
3333
#include <srs_auto_headers.hpp>

trunk/src/kernel/srs_kernel_mp4.cpp

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ using namespace std;
4141

4242
#define SRS_MP4_BUF_SIZE 4096
4343

44+
srs_error_t srs_mp4_write_box(ISrsWriter* writer, ISrsCodec* box)
45+
{
46+
srs_error_t err = srs_success;
47+
48+
int nb_data = box->nb_bytes();
49+
std::vector<char> data(nb_data);
50+
51+
SrsBuffer* buffer = new SrsBuffer(&data[0], nb_data);
52+
SrsAutoFree(SrsBuffer, buffer);
53+
54+
if ((err = box->encode(buffer)) != srs_success) {
55+
return srs_error_wrap(err, "encode box");
56+
}
57+
58+
if ((err = writer->write(&data[0], nb_data, NULL)) != srs_success) {
59+
return srs_error_wrap(err, "write box");
60+
}
61+
62+
return err;
63+
}
64+
4465
stringstream& srs_padding(stringstream& ss, SrsMp4DumpContext dc, int tab = 4)
4566
{
4667
for (int i = 0; i < (int)dc.level; i++) {
@@ -4587,7 +4608,7 @@ SrsMp4SegmentIndexBox::~SrsMp4SegmentIndexBox()
45874608

45884609
int SrsMp4SegmentIndexBox::nb_header()
45894610
{
4590-
return SrsMp4Box::nb_header() + 4+4+4 + (version? 4:8) + 4+4 + 12*entries.size();
4611+
return SrsMp4Box::nb_header() + 4+4+4 + (!version? 8:16) + 4 + 12*entries.size();
45914612
}
45924613

45934614
srs_error_t SrsMp4SegmentIndexBox::encode_header(SrsBuffer* buf)
@@ -6041,18 +6062,24 @@ srs_error_t SrsMp4M2tsInitEncoder::write(SrsFormat* format, bool video, int tid)
60416062
srs_error_t err = srs_success;
60426063

60436064
// Write ftyp box.
6044-
SrsMp4FileTypeBox* ftyp = new SrsMp4FileTypeBox();
6045-
SrsAutoFree(SrsMp4FileTypeBox, ftyp);
60466065
if (true) {
6066+
SrsMp4FileTypeBox* ftyp = new SrsMp4FileTypeBox();
6067+
SrsAutoFree(SrsMp4FileTypeBox, ftyp);
6068+
60476069
ftyp->major_brand = SrsMp4BoxBrandISO5;
60486070
ftyp->minor_version = 512;
60496071
ftyp->set_compatible_brands(SrsMp4BoxBrandISO6, SrsMp4BoxBrandMP41);
6072+
6073+
if ((err = srs_mp4_write_box(writer, ftyp)) != srs_success) {
6074+
return srs_error_wrap(err, "write ftyp");
6075+
}
60506076
}
60516077

60526078
// Write moov.
6053-
SrsMp4MovieBox* moov = new SrsMp4MovieBox();
6054-
SrsAutoFree(SrsMp4MovieBox, moov);
60556079
if (true) {
6080+
SrsMp4MovieBox* moov = new SrsMp4MovieBox();
6081+
SrsAutoFree(SrsMp4MovieBox, moov);
6082+
60566083
SrsMp4MovieHeaderBox* mvhd = new SrsMp4MovieHeaderBox();
60576084
moov->set_mvhd(mvhd);
60586085

@@ -6244,24 +6271,10 @@ srs_error_t SrsMp4M2tsInitEncoder::write(SrsFormat* format, bool video, int tid)
62446271
trex->track_ID = tid;
62456272
trex->default_sample_description_index = 1;
62466273
}
6247-
}
6248-
6249-
int nb_data = ftyp->nb_bytes() + moov->nb_bytes();
6250-
uint8_t* data = new uint8_t[nb_data];
6251-
SrsAutoFreeA(uint8_t, data);
6252-
6253-
SrsBuffer* buffer = new SrsBuffer((char*)data, nb_data);
6254-
SrsAutoFree(SrsBuffer, buffer);
6255-
6256-
if ((err = ftyp->encode(buffer)) != srs_success) {
6257-
return srs_error_wrap(err, "encode ftyp");
6258-
}
6259-
if ((err = moov->encode(buffer)) != srs_success) {
6260-
return srs_error_wrap(err, "encode moov");
6261-
}
6262-
6263-
if ((err = writer->write(data, nb_data, NULL)) != srs_success) {
6264-
return srs_error_wrap(err, "write ftyp and moov");
6274+
6275+
if ((err = srs_mp4_write_box(writer, moov)) != srs_success) {
6276+
return srs_error_wrap(err, "write moov");
6277+
}
62656278
}
62666279

62676280
return err;
@@ -6275,6 +6288,7 @@ SrsMp4M2tsSegmentEncoder::SrsMp4M2tsSegmentEncoder()
62756288
buffer = new SrsBuffer();
62766289
sequence_number = 0;
62776290
decode_basetime = 0;
6291+
styp_bytes = 0;
62786292
mdat_bytes = 0;
62796293
}
62806294

@@ -6301,19 +6315,11 @@ srs_error_t SrsMp4M2tsSegmentEncoder::initialize(ISrsWriter* w, uint32_t sequenc
63016315
styp->major_brand = SrsMp4BoxBrandMSDH;
63026316
styp->minor_version = 0;
63036317
styp->set_compatible_brands(SrsMp4BoxBrandMSDH, SrsMp4BoxBrandMSIX);
6304-
6305-
int nb_data = styp->nb_bytes();
6306-
std::vector<char> data(nb_data);
6307-
6308-
SrsBuffer* buffer = new SrsBuffer(&data[0], nb_data);
6309-
SrsAutoFree(SrsBuffer, buffer);
6310-
6311-
if ((err = styp->encode(buffer)) != srs_success) {
6312-
return srs_error_wrap(err, "encode styp");
6313-
}
6314-
6315-
// TODO: FIXME: Ensure write ok.
6316-
if ((err = writer->write(&data[0], nb_data, NULL)) != srs_success) {
6318+
6319+
// Used for sidx to calcalute the referenced size.
6320+
styp_bytes = styp->nb_bytes();
6321+
6322+
if ((err = srs_mp4_write_box(writer, styp)) != srs_success) {
63176323
return srs_error_wrap(err, "write styp");
63186324
}
63196325
}
@@ -6365,16 +6371,36 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts)
63656371
if (!nb_audios && !nb_videos) {
63666372
return srs_error_new(ERROR_MP4_ILLEGAL_MOOF, "Missing audio and video track");
63676373
}
6368-
6374+
6375+
// Although the sidx is not required to start play DASH, but it's required for AV sync.
6376+
SrsMp4SegmentIndexBox* sidx = new SrsMp4SegmentIndexBox();
6377+
SrsAutoFree(SrsMp4SegmentIndexBox, sidx);
6378+
if (true) {
6379+
sidx->version = 1;
6380+
sidx->reference_id = 1;
6381+
sidx->timescale = 1000;
6382+
sidx->earliest_presentation_time = uint64_t(decode_basetime / sidx->timescale);
6383+
6384+
uint64_t duration = 0;
6385+
if (samples && !samples->samples.empty()) {
6386+
SrsMp4Sample* first = samples->samples[0];
6387+
SrsMp4Sample* last = samples->samples[samples->samples.size() - 1];
6388+
duration = srs_max(0, last->dts - first->dts);
6389+
}
6390+
6391+
SrsMp4SegmentIndexEntry entry;
6392+
memset(&entry, 0, sizeof(entry));
6393+
entry.subsegment_duration = duration;
6394+
entry.starts_with_SAP = 1;
6395+
sidx->entries.push_back(entry);
6396+
}
6397+
63696398
// Create a mdat box.
63706399
// its payload will be writen by samples,
63716400
// and we will update its header(size) when flush.
63726401
SrsMp4MediaDataBox* mdat = new SrsMp4MediaDataBox();
63736402
SrsAutoFree(SrsMp4MediaDataBox, mdat);
63746403

6375-
// Although the sidx is not required to start play DASH, but it's required for AV sync.
6376-
// TODO: FIXME: Insert a sidx box.
6377-
63786404
// Write moof.
63796405
if (true) {
63806406
SrsMp4MovieFragmentBox* moof = new SrsMp4MovieFragmentBox();
@@ -6407,30 +6433,25 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts)
64076433
return srs_error_wrap(err, "write samples");
64086434
}
64096435

6410-
int nb_data = moof->nb_bytes();
64116436
// @remark Remember the data_offset of turn is size(moof)+header(mdat), not including styp or sidx.
6412-
trun->data_offset = (int32_t)(nb_data + mdat->sz_header());
6413-
6414-
uint8_t* data = new uint8_t[nb_data];
6415-
SrsAutoFreeA(uint8_t, data);
6416-
6417-
SrsBuffer* buffer = new SrsBuffer((char*)data, nb_data);
6418-
SrsAutoFree(SrsBuffer, buffer);
6419-
6420-
if ((err = moof->encode(buffer)) != srs_success) {
6421-
return srs_error_wrap(err, "encode moof");
6437+
int moof_bytes = moof->nb_bytes();
6438+
trun->data_offset = (int32_t)(moof_bytes + mdat->sz_header());
6439+
mdat->nb_data = (int)mdat_bytes;
6440+
6441+
// Update the size of sidx.
6442+
SrsMp4SegmentIndexEntry* entry = &sidx->entries[0];
6443+
entry->referenced_size = moof_bytes + mdat->nb_bytes();
6444+
if ((err = srs_mp4_write_box(writer, sidx)) != srs_success) {
6445+
return srs_error_wrap(err, "write sidx");
64226446
}
6423-
6424-
// TODO: FIXME: Ensure all bytes are writen.
6425-
if ((err = writer->write(data, nb_data, NULL)) != srs_success) {
6447+
6448+
if ((err = srs_mp4_write_box(writer, moof)) != srs_success) {
64266449
return srs_error_wrap(err, "write moof");
64276450
}
64286451
}
64296452

64306453
// Write mdat.
64316454
if (true) {
6432-
mdat->nb_data = (int)mdat_bytes;
6433-
64346455
int nb_data = mdat->sz_header();
64356456
uint8_t* data = new uint8_t[nb_data];
64366457
SrsAutoFreeA(uint8_t, data);

trunk/src/kernel/srs_kernel_mp4.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,7 @@ class SrsMp4SegmentIndexBox : public SrsMp4Box
18351835
uint32_t reference_id;
18361836
uint32_t timescale;
18371837
uint64_t earliest_presentation_time;
1838-
uint32_t first_offset;
1838+
uint64_t first_offset;
18391839
// TODO: FIXME: Should double check buffer.
18401840
std::vector<SrsMp4SegmentIndexEntry> entries;
18411841
public:
@@ -2115,6 +2115,7 @@ class SrsMp4M2tsSegmentEncoder
21152115
private:
21162116
uint32_t nb_audios;
21172117
uint32_t nb_videos;
2118+
uint32_t styp_bytes;
21182119
uint64_t mdat_bytes;
21192120
SrsMp4SampleManager* samples;
21202121
public:

0 commit comments

Comments
 (0)