For #299, write MPD file.

pull/790/head
winlin 8 years ago
parent e754ab7073
commit 8ab727f3c5

@ -948,8 +948,27 @@ vhost dash.srs.com {
dash {
# Whether DASH is enabled.
# Transmux RTMP to DASH if on.
# default: off
enabled on;
# Default: off
enabled on;
# The duration of segment in seconds.
# Default: 10
dash_fragment 10;
# The period to update the MPD in seconds.
# Default: 30
dash_update_period 30;
# The depth of timeshift buffer in seconds.
# Default: 60
dash_timeshift 60;
# The base/home dir/path for dash.
# All init and segment files will write under this dir.
dash_path ./objs/nginx/html;
# The DASH MPD file path.
# We supports some variables to generate the filename.
# [vhost], the vhost of stream.
# [app], the app of stream.
# [stream], the stream name of stream.
# Default: [app]/[stream].mpd
dash_mpd_file [app]/[stream].mpd;
}
}

@ -3917,7 +3917,8 @@ int SrsConfig::check_config()
} else if (n == "dash") {
for (int j = 0; j < (int)conf->directives.size(); j++) {
string m = conf->at(j)->name;
if (m != "enabled") {
if (m != "enabled" && m != "dash_fragment" && m != "dash_update_period" && m != "dash_timeshift" && m != "dash_path"
&& m != "dash_mpd_file") {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("Illegal directive %s in vhost.dash, ret=%d", m.c_str(), ret);
return ret;
@ -5988,6 +5989,91 @@ bool SrsConfig::get_dash_enabled(string vhost)
return SRS_CONF_PERFER_FALSE(conf->arg0());
}
int SrsConfig::get_dash_fragment(string vhost)
{
static int DEFAULT = 10 * 1000;
SrsConfDirective* conf = get_dash(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("dash_fragment");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return (int)(1000 * ::atof(conf->arg0().c_str()));
}
int SrsConfig::get_dash_update_period(string vhost)
{
static int DEFAULT = 30 * 1000;
SrsConfDirective* conf = get_dash(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("dash_update_period");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return (int)(1000 * ::atof(conf->arg0().c_str()));
}
int SrsConfig::get_dash_timeshift(string vhost)
{
static int DEFAULT = 60 * 1000;
SrsConfDirective* conf = get_dash(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("dash_timeshift");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return (int)(1000 * ::atof(conf->arg0().c_str()));
}
string SrsConfig::get_dash_path(string vhost)
{
static string DEFAULT = "./objs/nginx/html";
SrsConfDirective* conf = get_dash(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("dash_path");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return conf->arg0();
}
string SrsConfig::get_dash_mpd_file(string vhost)
{
static string DEFAULT = "[app]/[stream].mpd";
SrsConfDirective* conf = get_dash(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("dash_mpd_file");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return conf->arg0();
}
SrsConfDirective* SrsConfig::get_hls(string vhost)
{
SrsConfDirective* conf = get_vhost(vhost);

@ -1140,6 +1140,16 @@ private:
public:
// Whether DASH is enabled.
virtual bool get_dash_enabled(std::string vhost);
// Get the duration of segment in milliseconds.
virtual int get_dash_fragment(std::string vhost);
// Get the period to update MPD in milliseconds.
virtual int get_dash_update_period(std::string vhost);
// Get the depth of timeshift buffer in milliseconds.
virtual int get_dash_timeshift(std::string vhost);
// Get the base/home dir/path for dash, into which write files.
virtual std::string get_dash_path(std::string vhost);
// Get the path for DASH MPD, to generate the MPD file.
virtual std::string get_dash_mpd_file(std::string vhost);
// hls section
private:
/**

@ -27,6 +27,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_source.hpp>
#include <srs_app_config.hpp>
#include <srs_rtmp_stack.hpp>
#include <srs_kernel_codec.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_app_utility.hpp>
#include <srs_kernel_file.hpp>
#include <sstream>
using namespace std;
SrsFragmentedMp4::SrsFragmentedMp4()
{
@ -38,18 +45,150 @@ SrsFragmentedMp4::~SrsFragmentedMp4()
SrsMpdWriter::SrsMpdWriter()
{
req = NULL;
timeshit = update_period = fragment = 0;
last_update_mpd = -1;
}
SrsMpdWriter::~SrsMpdWriter()
{
}
int SrsMpdWriter::initialize(SrsRequest* r)
{
int ret = ERROR_SUCCESS;
req = r;
fragment = _srs_config->get_dash_fragment(r->vhost);
update_period = _srs_config->get_dash_update_period(r->vhost);
timeshit = _srs_config->get_dash_timeshift(r->vhost);
home = _srs_config->get_dash_path(r->vhost);
mpd_file = _srs_config->get_dash_mpd_file(r->vhost);
return ret;
}
int SrsMpdWriter::write(SrsFormat* format)
{
int ret = ERROR_SUCCESS;
// MPD is not expired?
if (last_update_mpd != -1 && srs_get_system_time_ms() - last_update_mpd < update_period) {
return ret;
}
last_update_mpd = srs_get_system_time_ms();
string mpd_path = srs_path_build_stream(mpd_file, req->vhost, req->app, req->stream);
string full_path = home + "/" + mpd_path;
string full_home = srs_path_dirname(full_path);
if ((ret = srs_create_dir_recursively(full_home)) != ERROR_SUCCESS) {
srs_error("DASH: create MPD home failed, home=%s, ret=%d", full_home.c_str(), ret);
return ret;
}
stringstream ss;
ss << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl
<< "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " << endl
<< " xmlns=\"urn:mpeg:DASH:schema:MPD:2011\" " << endl
<< " xsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 DASH-MPD.xsd\" " << endl
<< " type=\"dynamic\" minimumUpdatePeriod=\"PT" << update_period / 1000 << "S\" " << endl
<< " timeShiftBufferDepth=\"PT" << timeshit / 1000 << "S\" availabilityStartTime=\"1970-01-01T00:00:00Z\" " << endl
<< " maxSegmentDuration=\"PT" << fragment / 1000 << "S\" minBufferTime=\"PT1S\">" << endl
<< " <Period>" << endl;
if (format->acodec) {
ss << " <AdaptationSet mimeType=\"audio/mp4\" codecs=\"mp4a.40.2\" segmentAlignment=\"true\" startWithSAP=\"1\">" << endl;
ss << " <SegmentTemplate initialization=\"$RepresentationID$/init.mp4\" media=\"$RepresentationID$/$Number$.m4s\" />" << endl;
ss << " <Representation id=\"audio\" bandwidth=\"48000\"/>" << endl;
ss << " </AdaptationSet>" << endl;
}
if (format->vcodec) {
int w = format->vcodec->width;
int h = format->vcodec->height;
ss << " <AdaptationSet mimeType=\"video/mp4\" codecs=\"avc1.64001e\" segmentAlignment=\"true\" startWithSAP=\"1\">" << endl;
ss << " <SegmentTemplate initialization=\"$RepresentationID$/init.mp4\" media=\"$RepresentationID$/$Number$.m4s\" />" << endl;
ss << " <Representation id=\"video\" bandwidth=\"800000\" width=\"" << w << "\" height=\"" << h << "\"/>" << endl;
ss << " </AdaptationSet>" << endl;
}
ss << " </Period>" << endl
<< "</MPD>" << endl;
SrsFileWriter fw;
if ((ret = fw.open(full_path)) != ERROR_SUCCESS) {
srs_error("DASH: open MPD file=%s failed, ret=%d", full_path.c_str(), ret);
return ret;
}
string content = ss.str();
if ((ret = fw.write((void*)content.data(), content.length(), NULL)) != ERROR_SUCCESS) {
srs_error("DASH: write MPD file=%s failed, ret=%d", full_path.c_str(), ret);
return ret;
}
srs_trace("DASH: refresh MPD successed, size=%dB, file=%s", content.length(), full_path.c_str());
return ret;
}
SrsDashController::SrsDashController()
{
mpd = new SrsMpdWriter();
}
SrsDashController::~SrsDashController()
{
srs_freep(mpd);
}
int SrsDashController::initialize(SrsRequest* r)
{
int ret = ERROR_SUCCESS;
if ((ret = mpd->initialize(r)) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
int SrsDashController::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format)
{
int ret = ERROR_SUCCESS;
if ((ret = refresh_mpd(format)) != ERROR_SUCCESS) {
srs_error("DASH: refresh the MPD failed. ret=%d", ret);
return ret;
}
return ret;
}
int SrsDashController::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format)
{
int ret = ERROR_SUCCESS;
if ((ret = refresh_mpd(format)) != ERROR_SUCCESS) {
srs_error("DASH: refresh the MPD failed. ret=%d", ret);
return ret;
}
return ret;
}
int SrsDashController::refresh_mpd(SrsFormat* format)
{
int ret = ERROR_SUCCESS;
// TODO: FIXME: Support pure audio streaming.
if (!format->acodec || !format->vcodec) {
return ret;
}
if ((ret = mpd->write(format)) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
SrsDash::SrsDash()
@ -73,6 +212,11 @@ int SrsDash::initialize(SrsOriginHub* h, SrsRequest* r)
hub = h;
req = r;
if ((ret = controller->initialize(req)) != ERROR_SUCCESS) {
srs_error("DASH: initialize controller failed. ret=%d", ret);
return ret;
}
return ret;
}
@ -88,7 +232,6 @@ int SrsDash::on_publish()
if (!_srs_config->get_dash_enabled(req->vhost)) {
return ret;
}
enabled = true;
return ret;
@ -102,6 +245,11 @@ int SrsDash::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format)
return ret;
}
if ((ret = controller->on_audio(shared_audio, format)) != ERROR_SUCCESS) {
srs_error("DASH: consume audio failed. ret=%d", ret);
return ret;
}
return ret;
}
@ -113,6 +261,11 @@ int SrsDash::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format)
return ret;
}
if ((ret = controller->on_video(shared_video, format)) != ERROR_SUCCESS) {
srs_error("DASH: consume video failed. ret=%d", ret);
return ret;
}
return ret;
}

@ -29,6 +29,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_core.hpp>
#include <string>
class SrsRequest;
class SrsOriginHub;
class SrsSharedPtrMessage;
@ -49,9 +51,27 @@ public:
*/
class SrsMpdWriter
{
private:
SrsRequest* req;
int64_t last_update_mpd;
private:
// The duration of fragment in ms.
int fragment;
// The period to update the mpd in ms.
int update_period;
// The timeshift buffer depth.
int timeshit;
// The base or home dir for dash to write files.
std::string home;
// The MPD path template, from which to build the file path.
std::string mpd_file;
public:
SrsMpdWriter();
virtual ~SrsMpdWriter();
public:
virtual int initialize(SrsRequest* r);
// Write MPD according to parsed format of stream.
virtual int write(SrsFormat* format);
};
/**
@ -59,9 +79,17 @@ public:
*/
class SrsDashController
{
private:
SrsMpdWriter* mpd;
public:
SrsDashController();
virtual ~SrsDashController();
public:
virtual int initialize(SrsRequest* r);
virtual int on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format);
virtual int on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format);
private:
virtual int refresh_mpd(SrsFormat* format);
};
/**

@ -652,13 +652,13 @@ int SrsFormat::on_aac_sequence_header(char* data, int size)
bool SrsFormat::is_aac_sequence_header()
{
return acodec && acodec->id == SrsAudioCodecIdAAC
&& audio && audio->aac_packet_type == SrsAudioAacFrameTraitSequenceHeader;
&& audio && audio->aac_packet_type == SrsAudioAacFrameTraitSequenceHeader;
}
bool SrsFormat::is_avc_sequence_header()
{
return vcodec && vcodec->id == SrsVideoCodecIdAVC
&& video && video->avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader;
&& video && video->avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader;
}
int SrsFormat::video_avc_demux(SrsBuffer* stream, int64_t timestamp)

@ -374,6 +374,11 @@ int SrsHttpFileServer::serve_file(ISrsHttpResponseWriter* w, ISrsHttpMessage* r,
_mime[".jpeg"] = "image/jpeg";
_mime[".jpg"] = "image/jpeg";
_mime[".gif"] = "image/gif";
// For MPEG-DASH.
//_mime[".mpd"] = "application/dash+xml";
_mime[".mpd"] = "text/xml";
_mime[".m4s"] = "video/iso.segment";
_mime[".mp4v"] = "video/mp4";
}
if (true) {

Loading…
Cancel
Save