Skip to content

Commit 6f69e59

Browse files
Editorial: modernize example (#339)
Co-authored-by: Chris Needham <chris.needham@bbc.co.uk>
1 parent 6da1bef commit 6f69e59

File tree

1 file changed

+92
-68
lines changed

1 file changed

+92
-68
lines changed

media-source-respec.html

Lines changed: 92 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,22 +3042,31 @@ <h2><dfn data-export="">Byte Stream Formats</dfn></h2>
30423042

30433043
<section id="examples">
30443044
<h2>Examples</h2>
3045-
<p>Example use of the Media Source Extensions</p>
3046-
<div class="block">
3047-
<div class="blockContent">
3048-
<pre class="code">&lt;script&gt;
3049-
function onSourceOpen(videoTag, e) {
3050-
var mediaSource = e.target;
3045+
<p>Example use of the Media Source Extensions.</p>
3046+
<pre class="example html">
3047+
&lt;video id="v" autoplay&gt;&lt;/video&gt;
3048+
&lt;script&gt;
3049+
const video = document.getElementById("v");
3050+
const mediaSource = new MediaSource();
3051+
mediaSource.addEventListener("sourceopen", onSourceOpen);
3052+
video.src = window.URL.createObjectURL(mediaSource);
3053+
3054+
async function onSourceOpen(e) {
3055+
const mediaSource = e.target;
30513056

3052-
if (mediaSource.sourceBuffers.length &gt; 0)
3053-
return;
3057+
if (mediaSource.sourceBuffers.length > 0) return;
30543058

3055-
var sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
3059+
const sourceBuffer = mediaSource.addSourceBuffer(
3060+
'video/webm; codecs="vorbis,vp8"',
3061+
);
30563062

3057-
videoTag.addEventListener('seeking', onSeeking.bind(videoTag, mediaSource));
3058-
videoTag.addEventListener('progress', onProgress.bind(videoTag, mediaSource));
3063+
video.addEventListener("seeking", (e) => onSeeking(mediaSource, e.target));
3064+
video.addEventListener("progress", () =>
3065+
appendNextMediaSegment(mediaSource),
3066+
);
30593067

3060-
var initSegment = GetInitializationSegment();
3068+
try {
3069+
const initSegment = await getInitializationSegment();
30613070

30623071
if (initSegment == null) {
30633072
// Error fetching the initialization segment. Signal end of stream with an error.
@@ -3066,77 +3075,92 @@ <h2>Examples</h2>
30663075
}
30673076

30683077
// Append the initialization segment.
3069-
var firstAppendHandler = function(e) {
3070-
var sourceBuffer = e.target;
3071-
sourceBuffer.removeEventListener('updateend', firstAppendHandler);
3078+
sourceBuffer.addEventListener("updateend", function firstAppendHandler() {
3079+
sourceBuffer.removeEventListener("updateend", firstAppendHandler);
30723080

30733081
// Append some initial media data.
30743082
appendNextMediaSegment(mediaSource);
3075-
};
3076-
sourceBuffer.addEventListener('updateend', firstAppendHandler);
3083+
});
3084+
30773085
sourceBuffer.appendBuffer(initSegment);
3086+
} catch (error) {
3087+
// Handle errors that might occur during initialization segment fetching.
3088+
console.error("Error fetching initialization segment:", error);
3089+
mediaSource.endOfStream("network");
3090+
}
3091+
}
3092+
3093+
async function appendNextMediaSegment(mediaSource) {
3094+
if (
3095+
mediaSource.readyState === "closed" ||
3096+
mediaSource.sourceBuffers[0].updating
3097+
)
3098+
return;
3099+
3100+
// If we have run out of stream data, then signal end of stream.
3101+
if (!haveMoreMediaSegments()) {
3102+
mediaSource.endOfStream();
3103+
return;
30783104
}
30793105

3080-
function appendNextMediaSegment(mediaSource) {
3081-
if (mediaSource.readyState == "closed")
3082-
return;
3083-
3084-
// If we have run out of stream data, then signal end of stream.
3085-
if (!HaveMoreMediaSegments()) {
3086-
mediaSource.endOfStream();
3087-
return;
3088-
}
3089-
3090-
// Make sure the previous append is not still pending.
3091-
if (mediaSource.sourceBuffers[0].updating)
3092-
return;
3093-
3094-
var mediaSegment = GetNextMediaSegment();
3106+
try {
3107+
const mediaSegment = await getNextMediaSegment();
30953108

3096-
if (!mediaSegment) {
3097-
// Error fetching the next media segment.
3098-
mediaSource.endOfStream("network");
3099-
return;
3100-
}
3101-
3102-
// NOTE: If mediaSource.readyState == “ended”, this appendBuffer() call will
3109+
// NOTE: If mediaSource.readyState == "ended", this appendBuffer() call will
31033110
// cause mediaSource.readyState to transition to "open". The web application
3104-
// should be prepared to handle multiple sourceopen events.
3111+
// should be prepared to handle multiple "sourceopen" events.
31053112
mediaSource.sourceBuffers[0].appendBuffer(mediaSegment);
31063113
}
3107-
3108-
function onSeeking(mediaSource, e) {
3109-
var video = e.target;
3110-
3111-
if (mediaSource.readyState == "open") {
3112-
// Abort current segment append.
3113-
mediaSource.sourceBuffers[0].abort();
3114-
}
3115-
3116-
// Notify the media segment loading code to start fetching data at the
3117-
// new playback position.
3118-
SeekToMediaSegmentAt(video.currentTime);
3119-
3120-
// Append a media segment from the new playback position.
3121-
appendNextMediaSegment(mediaSource);
3114+
catch (error) {
3115+
// Handle errors that might occur during media segment fetching.
3116+
console.error("Error fetching media segment:", error);
3117+
mediaSource.endOfStream("network");
31223118
}
3119+
}
31233120

3124-
function onProgress(mediaSource, e) {
3125-
appendNextMediaSegment(mediaSource);
3121+
function onSeeking(mediaSource, video) {
3122+
if (mediaSource.readyState === "open") {
3123+
// Abort current segment append.
3124+
mediaSource.sourceBuffers[0].abort();
31263125
}
3127-
&lt;/script&gt;
3128-
3129-
&lt;video id="v" autoplay&gt; &lt;/video&gt;
31303126

3131-
&lt;script&gt;
3132-
var video = document.getElementById('v');
3133-
var mediaSource = new MediaSource();
3134-
mediaSource.addEventListener('sourceopen', onSourceOpen.bind(this, video));
3135-
video.src = window.URL.createObjectURL(mediaSource);
3127+
// Notify the media segment loading code to start fetching data at the
3128+
// new playback position.
3129+
seekToMediaSegmentAt(video.currentTime);
3130+
3131+
// Append a media segment from the new playback position.
3132+
appendNextMediaSegment(mediaSource);
3133+
}
3134+
3135+
function onProgress(mediaSource, e) {
3136+
appendNextMediaSegment(mediaSource);
3137+
}
3138+
3139+
// Example of async function for getting initialization segment
3140+
async function getInitializationSegment() {
3141+
// Implement fetching of the initialization segment
3142+
// This is just a placeholder function
3143+
}
3144+
3145+
// Example function for checking if there are more media segments
3146+
function haveMoreMediaSegments() {
3147+
// Implement logic to determine if there are more media segments
3148+
// This is just a placeholder function
3149+
}
3150+
3151+
// Example function for getting the next media segment
3152+
async function getNextMediaSegment() {
3153+
// Implement fetching of the next media segment
3154+
// This is just a placeholder function
3155+
}
3156+
3157+
// Example function for seeking to a specific media segment
3158+
function seekToMediaSegmentAt(currentTime) {
3159+
// Implement seeking logic
3160+
// This is just a placeholder function
3161+
}
31363162
&lt;/script&gt;
3137-
</pre>
3138-
</div>
3139-
</div>
3163+
</pre>
31403164
</section>
31413165

31423166

0 commit comments

Comments
 (0)