如果Blob URL是不可变的,那么Media Source Extension API如何使用它们来流式传输视频? [英] If Blob URLs are immutable, how does Media Source Extension API use them to stream videos?

查看:314
本文介绍了如果Blob URL是不可变的,那么Media Source Extension API如何使用它们来流式传输视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们从一个示例开始:

  1. 您访问youtube.com,该网站在某些设备上使用带有HTML5的媒体源扩展(MSE).
  2. MSE注入了< video>带有Blob URL的代码.看起来像这样:blob: https://www.youtube.com/blahblahblah "
  3. 在流式传输整个视频过程中,您的浏览器会进行多个网络调用,以下载各个视频块,并将它们附加到MSE的SourceBuffer中.
  4. 因此,在整个视频流中,Meda Source对象作为一个整体进行了更新
  5. 但是,最初附加到< video>的Blob URL假定代表媒体源对象的元素保持不变.

在我看来,这似乎没有多大意义.假设Blob URL代表了永不改变的不可变数据块.但是,似乎MSE能够使它们代表可变的内存缓冲区.

这是如何工作的?而且,如果我们还想使Blob URL表示一些可变的内存缓冲区,那么我们如何使用javascript自己做到这一点?

解决方案

您需要了解BlobURI 不代表任何数据.它们只是链接,指向内存中的某些资源,就像字符串https://stackoverflow.com/questions/54613972本身不包含您正在读取的任何内容一样,它仅指向服务器指令,然后该服务器指令将生成页面.

他们的链接可以说是不可变的,一旦使用URL.createObjectURL(target)生成了链接,就无法更改其target,就像使用const关键字一样.

以实例const foo = {}为例,现在foo不能设置为该对象以外的其他值. 但是foo地址指向的对象仍然可变. foo.bar = 'baz'仍然可以完成.

 const foo = {};
try{
  foo = 'fails';
}
catch(e) {
  console.error(e);
}
foo.mutable = true;

console.log(foo); 

对于blobURIs来说都是一样的. blobURI指向targe对象,无法更改此链接,但是target仍然可变.对于MediaSource对象也是如此,对于其他对象也是如此.

如果您还记得几年前,我们仍然能够对MediaStreams使用blobURI(这是一个坏主意),那是相同的过程,blobURI以不可更改的方式指向MediaStream对象,但是媒体数据处于不断变化的状态(流).

即使对于文件,您也可以在硬盘驱动器上拥有一个指向文件的blobURI,即使现在blobURI不再指向任何地方,这也不会阻止您将其从硬盘中删除./p>

与此相关的一种特殊情况是Blob的情况,它是由内存中的数据(即不仅仅是指向磁盘上文件的指针)生成的.这里Blob所保存的数据是不可变的,因此在这种情况下,blobURI确实指向了确实保存不可变数据的对象.

对于您要求有一个blobURI指向存储在内存中的某些数据,但仍然能够修改此数据,则无法完成...
这是因为这种情况暗示您是使用内存中的数据从Blob对象创建blobURI的,该内存再次将数据保持为不可变状态.

Let's start with an example:

  1. You visit youtube.com, which uses Media Source Extension (MSE) with HTML5 for certain devices.
  2. MSE injects the <video> tag with a blob URL. It looks something like this: blob:https://www.youtube.com/blahblahblah"
  3. Throughout streaming the entire video, your browser makes multiple network calls to download the various chunks of video, and appends them to the MSE's SourceBuffer
  4. Therefore, the Meda Source object as a whole is updated throughout the video stream
  5. However, the blob URL that was initially attached to the <video> element, which is suppose to represent the Media Source object, remains constant.

To me, this doesn't seem to make much sense. Blob URLs are suppose to represent chunks of immutable data that never changes. But it seems like MSE is able to make them represent a mutable buffer of memory.

How does this work under the hood? And if we also want to make blob URLs represent some mutable buffer of memory, how can we do this ourselves with javascript?

解决方案

You need to understand that BlobURIs do not represent any data. They are just links, pointing to some resource in memory, just like the string https://stackoverflow.com/questions/54613972 doesn't contain any of what you are reading per se, it just points to a server instruction which will then generate the page.

Their link can be said to be immutable, once you generated it using URL.createObjectURL(target), you can not change its target, just like if you used the const keyword.

Take for instance const foo = {} now foo can't be set to something else than this object. But the object that is pointed by foo address is still mutable. foo.bar = 'baz' can still be done.

const foo = {};
try{
  foo = 'fails';
}
catch(e) {
  console.error(e);
}
foo.mutable = true;

console.log(foo);

Well for blobURIs it's just the same. The blobURI points to an targe object, this link can't be changed, but the target is still mutable. This is true for MediaSource objects but also others.

If you remember a few years ago, we were still able to use blobURIs for MediaStreams (was a bad idea), it was the same process, the blobURI was pointing to the MediaStream object, in an un-mutable manner, but the media-data was in constant mutation (a stream).

And even for Files, you can very well have a blobURI that points to a File on your hard-drive, this won't block you from removing it from your HDD, even though the blobURI now points to nowhere anymore.

The one particular case with this regard is the case of a Blob, generated from data in memory (i.e not just a pointer to a file on disk). Here the data held by the Blob is immutable, so in this case, the blobURI indeed points to an object that does hold immutable data.

And for you request to have a blobURI pointing to some data stored in memory, but still be able to modify this data, this can't be done...
That is because this scenario implies that you created your blobURI from a Blob object using data in memory, which once again does hold data in an immutable state.

这篇关于如果Blob URL是不可变的,那么Media Source Extension API如何使用它们来流式传输视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆