@jxstjh/jhvideo
Version:
HTML5 jhvideo base on MPEG2-TS Stream Player
203 lines • 8.08 kB
JavaScript
/*
* Copyright (C) 2016 Bilibili. All Rights Reserved.
*
* @author zheng qian <xqq@xqq.im>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import Log from '../utils/logger.js';
import { BaseLoader, LoaderStatus, LoaderErrors } from './loader.js';
import { RuntimeException } from '../utils/exception.js';
// For FireFox browser which supports `xhr.responseType = 'moz-chunked-arraybuffer'`
var MozChunkedLoader = /** @class */ (function (_super) {
__extends(MozChunkedLoader, _super);
function MozChunkedLoader(seekHandler, config) {
var _this = _super.call(this, 'xhr-moz-chunked-loader') || this;
_this.TAG = 'MozChunkedLoader';
_this._seekHandler = seekHandler;
_this._config = config;
_this._needStash = true;
_this._xhr = null;
_this._requestAbort = false;
_this._contentLength = null;
_this._receivedLength = 0;
return _this;
}
MozChunkedLoader.isSupported = function () {
try {
var xhr = new XMLHttpRequest();
// Firefox 37- requires .open() to be called before setting responseType
xhr.open('GET', 'https://example.com', true);
xhr.responseType = 'moz-chunked-arraybuffer';
return (xhr.responseType === 'moz-chunked-arraybuffer');
}
catch (e) {
Log.w('MozChunkedLoader', e.message);
return false;
}
};
MozChunkedLoader.prototype.destroy = function () {
if (this.isWorking()) {
this.abort();
}
if (this._xhr) {
this._xhr.onreadystatechange = null;
this._xhr.onprogress = null;
this._xhr.onloadend = null;
this._xhr.onerror = null;
this._xhr = null;
}
_super.prototype.destroy.call(this);
};
MozChunkedLoader.prototype.open = function (dataSource, range) {
this._dataSource = dataSource;
this._range = range;
var sourceURL = dataSource.url;
if (this._config.reuseRedirectedURL && dataSource.redirectedURL != undefined) {
sourceURL = dataSource.redirectedURL;
}
var seekConfig = this._seekHandler.getConfig(sourceURL, range);
this._requestURL = seekConfig.url;
var xhr = this._xhr = new XMLHttpRequest();
xhr.open('GET', seekConfig.url, true);
xhr.responseType = 'moz-chunked-arraybuffer';
xhr.onreadystatechange = this._onReadyStateChange.bind(this);
xhr.onprogress = this._onProgress.bind(this);
xhr.onloadend = this._onLoadEnd.bind(this);
xhr.onerror = this._onXhrError.bind(this);
// cors is auto detected and enabled by xhr
// withCredentials is disabled by default
if (dataSource.withCredentials) {
xhr.withCredentials = true;
}
if (typeof seekConfig.headers === 'object') {
var headers = seekConfig.headers;
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
xhr.setRequestHeader(key, headers[key]);
}
}
}
// add additional headers
if (typeof this._config.headers === 'object') {
var headers = this._config.headers;
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
xhr.setRequestHeader(key, headers[key]);
}
}
}
this._status = LoaderStatus.kConnecting;
xhr.send();
};
MozChunkedLoader.prototype.abort = function () {
this._requestAbort = true;
if (this._xhr) {
this._xhr.abort();
}
this._status = LoaderStatus.kComplete;
};
MozChunkedLoader.prototype._onReadyStateChange = function (e) {
var xhr = e.target;
if (xhr.readyState === 2) { // HEADERS_RECEIVED
if (xhr.responseURL != undefined && xhr.responseURL !== this._requestURL) {
if (this._onURLRedirect) {
var redirectedURL = this._seekHandler.removeURLParameters(xhr.responseURL);
this._onURLRedirect(redirectedURL);
}
}
if (xhr.status !== 0 && (xhr.status < 200 || xhr.status > 299)) {
this._status = LoaderStatus.kError;
if (this._onError) {
this._onError(LoaderErrors.HTTP_STATUS_CODE_INVALID, { code: xhr.status, msg: xhr.statusText });
}
else {
throw new RuntimeException('MozChunkedLoader: Http code invalid, ' + xhr.status + ' ' + xhr.statusText);
}
}
else {
this._status = LoaderStatus.kBuffering;
}
}
};
MozChunkedLoader.prototype._onProgress = function (e) {
if (this._status === LoaderStatus.kError) {
// Ignore error response
return;
}
if (this._contentLength === null) {
if (e.total !== null && e.total !== 0) {
this._contentLength = e.total;
if (this._onContentLengthKnown) {
this._onContentLengthKnown(this._contentLength);
}
}
}
var chunk = e.target.response;
var byteStart = this._range.from + this._receivedLength;
this._receivedLength += chunk.byteLength;
if (this._onDataArrival) {
this._onDataArrival(chunk, byteStart, this._receivedLength);
}
};
MozChunkedLoader.prototype._onLoadEnd = function (e) {
if (this._requestAbort === true) {
this._requestAbort = false;
return;
}
else if (this._status === LoaderStatus.kError) {
return;
}
this._status = LoaderStatus.kComplete;
if (this._onComplete) {
this._onComplete(this._range.from, this._range.from + this._receivedLength - 1);
}
};
MozChunkedLoader.prototype._onXhrError = function (e) {
this._status = LoaderStatus.kError;
var type = 0;
var info = null;
if (this._contentLength && e.loaded < this._contentLength) {
type = LoaderErrors.EARLY_EOF;
info = { code: -1, msg: 'Moz-Chunked stream meet Early-Eof' };
}
else {
type = LoaderErrors.EXCEPTION;
info = { code: -1, msg: e.constructor.name + ' ' + e.type };
}
if (this._onError) {
this._onError(type, info);
}
else {
throw new RuntimeException(info.msg);
}
};
return MozChunkedLoader;
}(BaseLoader));
export default MozChunkedLoader;
//# sourceMappingURL=xhr-moz-chunked-loader.js.map