1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #299, write legal MPD, fix CORS.

This commit is contained in:
winlin 2017-02-26 21:45:08 +08:00
parent 8ab727f3c5
commit 747d6f2f52
3 changed files with 42 additions and 20 deletions

View file

@ -89,33 +89,40 @@ int SrsMpdWriter::write(SrsFormat* format)
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
<< "<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
<< " xmlns=\"urn:mpeg:dash:schema:mpd:2011\" xmlns:ns1=\"http://www.w3.org/2001/XMLSchema-instance\" " << 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;
<< " maxSegmentDuration=\"PT" << fragment / 1000 << "S\" minBufferTime=\"PT" << fragment / 1000 << "S\" >" << endl
<< " <BaseURL>" << req->stream << "/" << "</BaseURL>" << endl
<< " <Period start=\"PT0S\">" << 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 mimeType=\"audio/mp4\" segmentAlignment=\"true\" startWithSAP=\"1\">" << endl;
ss << " <SegmentTemplate duration=\"" << fragment / 1000 << "\" "
<< "initialization=\"$RepresentationID$-init.mp4\" "
<< "media=\"$RepresentationID$-$Number$.m4s\" />" << endl;
ss << " <Representation id=\"audio\" bandwidth=\"48000\" codecs=\"mp4a.40.2\" />" << 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 mimeType=\"video/mp4\" segmentAlignment=\"true\" startWithSAP=\"1\">" << endl;
ss << " <SegmentTemplate duration=\"" << fragment / 1000 << "\" "
<< "initialization=\"$RepresentationID$-init.mp4\" "
<< "media=\"$RepresentationID$-$Number$.m4s\" />" << endl;
ss << " <Representation id=\"video\" bandwidth=\"800000\" codecs=\"avc1.64001e\" "
<< "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);
string full_path_tmp = full_path + ".tmp";
if ((ret = fw.open(full_path_tmp)) != ERROR_SUCCESS) {
srs_error("DASH: open MPD file=%s failed, ret=%d", full_path_tmp.c_str(), ret);
return ret;
}
@ -125,6 +132,12 @@ int SrsMpdWriter::write(SrsFormat* format)
return ret;
}
if (::rename(full_path_tmp.c_str(), full_path.c_str()) < 0) {
ret = ERROR_DASH_WRITE_FAILED;
srs_error("DASH: rename %s to %s failed, ret=%d", full_path_tmp.c_str(), 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;

View file

@ -261,6 +261,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define ERROR_FLV_REQUIRE_SPACE 3084
#define ERROR_MP4_AVCC_CHANGE 3085
#define ERROR_MP4_ASC_CHANGE 3086
#define ERROR_DASH_WRITE_FAILED 3087
///////////////////////////////////////////////////////
// HTTP/StreamCaster/KAFKA protocol error.

View file

@ -787,16 +787,24 @@ int SrsHttpCorsMux::initialize(ISrsHttpServeMux* worker, bool cros_enabled)
int SrsHttpCorsMux::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
// method is OPTIONS and enable crossdomain, required crossdomain header.
if (r->is_http_options() && enabled) {
required = true;
// If CORS enabled, and there is a "Origin" header, it's CORS.
if (enabled) {
for (int i = 0; i < r->request_header_count(); i++) {
string k = r->request_header_key_at(i);
if (k == "Origin" || k == "origin") {
required = true;
break;
}
}
}
// whenever crossdomain required, set crossdomain header.
// When CORS required, set the CORS headers.
if (required) {
w->header()->set("Access-Control-Allow-Origin", "*");
w->header()->set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE");
w->header()->set("Access-Control-Allow-Headers", "Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type");
SrsHttpHeader* h = w->header();
h->set("Access-Control-Allow-Origin", "*");
h->set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE, OPTIONS");
h->set("Access-Control-Expose-Headers", "Server,range,Content-Length,Content-Range");
h->set("Access-Control-Allow-Headers", "origin,range,accept-encoding,referer,Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type");
}
// handle the http options.