我正在尝试将视频流式化为html5视频标记。
我现在有点卡住了,SourceBuffer确实拿走了我的所有数据,我得到了那部分工作。
视频没有播放。我真的无法弄清楚为什么。它似乎也在某种程度上被打破了。
这是控制台中的相应日志
new stream from server
webkitsourceopen
Event {clipboardData: undefined, cancelBubble: false, returnValue: true, srcElement: WebKitMediaSource, defaultPrevented: false…}
data append
WebKitMediaSource {readyState: "open", duration: NaN, activeSourceBuffers: WebKitSourceBufferList, sourceBuffers: WebKitSourceBufferList, addSourceBuffer: function…}
play
data append
WebKitMediaSource {readyState: "open", duration: 32.48, activeSourceBuffers: WebKitSourceBufferList, sourceBuffers: WebKitSourceBufferList, addSourceBuffer: function…}
data append
WebKitMediaSource {readyState: "open", duration: 32.48, activeSourceBuffers: WebKitSourceBufferList, sourceBuffers: WebKitSourceBufferList, addSourceBuffer: function…}
...
data append
WebKitMediaSource {readyState: "open", duration: 32.48, activeSourceBuffers: WebKitSourceBufferList, sourceBuffers: WebKitSourceBufferList, addSourceBuffer: function…}
stream 100% received on client client.js:39
webkitsourceclose
最后这是我在浏览器中的代码:
jQuery(function () {
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
if ( !URL ) {
URL = {};
}
if ( !URL.createObjectURL ) {
URL.createObjectURL = function (obj) {
return obj;
}
}
console.log("new stream from server")
// Create media source
var mediaSource = new MediaSource();
var streamClient = document.getElementById('streamClient');
streamClient.src = URL.createObjectURL(mediaSource);
document.getElementById('app').appendChild(streamClient);
mediaSource.addEventListener('webkitsourceopen', function (e) {
var sourceBuffer = e.target.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
console.log('webkitsourceopen', e);
var client = new BinaryClient('ws://' + window.location.hostname + ':11345');
client.on('open', function (stream) {
// Stream received from server
client.on('stream', function (stream) {
// Wait for streamed data from server
stream.on('data', function (data) {
// Append data to client buffer
console.log("data append", e.target);
e.target.activeSourceBuffers[0].append(new Uint8Array(data));
if ( streamClient.paused ) {
console.log('play');
streamClient.play();
}
});
stream.on('end', function () {
console.log("stream 100% received on client");
stream.destroy();
});
});
});
client.on('close', function () {
console.log('client closed')
});
}, false);
mediaSource.addEventListener('webkitsourceclose', function (e) {
console.log('webkitsourceclose');
});
});