|
| 1 | +// Copyright 2026 The Cobalt Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +const v1 = document.getElementById('video1'); |
| 16 | +const v2 = document.getElementById('video2'); |
| 17 | +const v1Wrapper = document.getElementById('v1-wrap'); |
| 18 | +const v2Wrapper = document.getElementById('v2-wrap'); |
| 19 | + |
| 20 | + |
| 21 | +function log(msg) { |
| 22 | + console.log('JS: ' + msg); |
| 23 | +} |
| 24 | + |
| 25 | +const url1 = 'vp9-720p.webm'; |
| 26 | +const url2 = 'https://storage.googleapis.com/ytlr-cert.appspot.com/test-materials/media/big-buck-bunny-vp9-480p-30fps.webm'; |
| 27 | + |
| 28 | +function bootstrapVideo(videoObj, url, useTexture = false) { |
| 29 | + return new Promise((resolve) => { |
| 30 | + if (typeof MediaSource === 'undefined') { log('MSE not supported'); return resolve(); } |
| 31 | + const mediaSource = new MediaSource(); |
| 32 | + mediaSource.addEventListener('sourceopen', async () => { |
| 33 | + try { |
| 34 | + const isMp4 = url.toLowerCase().includes('.mp4'); |
| 35 | + const mimeType = isMp4 ? 'video/mp4; codecs="avc1.640028"' : 'video/webm; codecs="vp9"'; |
| 36 | + const codecString = mimeType + (useTexture ? '; decode-to-texture=true' : ''); |
| 37 | + const sourceBuffer = mediaSource.addSourceBuffer(codecString); |
| 38 | + // Safely fetch only the first 5MB of media to prevent JS Heap OOM |
| 39 | + // crashes when testing massive external videos on constrained TV memory |
| 40 | + const response = await fetch(url, { headers: { 'Range': 'bytes=0-5000000' } }); |
| 41 | + if (!response.ok) { |
| 42 | + throw new Error(`Failed to fetch video: ${response.status} ${response.statusText}`); |
| 43 | + } |
| 44 | + const data = await response.arrayBuffer(); |
| 45 | + sourceBuffer.addEventListener('updateend', () => { |
| 46 | + if (!sourceBuffer.updating && mediaSource.readyState === 'open') { |
| 47 | + mediaSource.endOfStream(); |
| 48 | + videoObj.play().catch(e => log('Play prevented: ' + e)); |
| 49 | + resolve(); |
| 50 | + } |
| 51 | + }, { once: true }); |
| 52 | + sourceBuffer.appendBuffer(data); |
| 53 | + } catch (error) { log('Error loading MSE: ' + error); resolve(); } |
| 54 | + }, { once: true }); |
| 55 | + |
| 56 | + videoObj.src = URL.createObjectURL(mediaSource); |
| 57 | + videoObj.loop = true; |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +let isStage3 = false; |
| 62 | +let isTransitioning = false; |
| 63 | + |
| 64 | +document.addEventListener('keydown', async (e) => { |
| 65 | + // Bind the down button to enter and exit pip |
| 66 | + if (e.key !== 'ArrowDown') return; |
| 67 | + if (isTransitioning) return; |
| 68 | + isTransitioning = true; |
| 69 | + |
| 70 | + try { |
| 71 | + if (!isStage3) { |
| 72 | + log('Stage 3: Native PiP for V1, Fullscreen for V2'); |
| 73 | + |
| 74 | + try { |
| 75 | + if (typeof v1.requestPictureInPicture !== 'function') { |
| 76 | + throw new Error('Picture-in-Picture API is not supported on this device'); |
| 77 | + } |
| 78 | + await v1.requestPictureInPicture(); |
| 79 | + |
| 80 | + setTimeout(() => { |
| 81 | + v2Wrapper.style.transition = 'none'; |
| 82 | + v2Wrapper.classList.add('fullscreen'); |
| 83 | + |
| 84 | + // Restore transition shortly after jump is completed |
| 85 | + setTimeout(() => { |
| 86 | + v2Wrapper.style.transition = ''; |
| 87 | + }, 100); |
| 88 | + }, 300); |
| 89 | + |
| 90 | + isStage3 = true; |
| 91 | + } catch (err) { log('Native PiP Error: ' + err.message); } |
| 92 | + |
| 93 | + isStage3 = true; |
| 94 | + } else { |
| 95 | + log('Stage 2: Side-by-Side (Exiting PiP)'); |
| 96 | + |
| 97 | + v2Wrapper.classList.remove('fullscreen'); |
| 98 | + try { await document.exitPictureInPicture(); } catch (err) {} |
| 99 | + |
| 100 | + isStage3 = false; |
| 101 | + } |
| 102 | + } catch(e) { log(e.message); } |
| 103 | + |
| 104 | + // Wait for the 500ms CSS transition to mathematically conclude before allowing another toggle |
| 105 | + setTimeout(() => { isTransitioning = false; }, 500); |
| 106 | +}); |
| 107 | + |
| 108 | +log('Bootstrapping decoders...'); |
| 109 | +Promise.all([ |
| 110 | + bootstrapVideo(v1, url1, true), // V1 Texture |
| 111 | + bootstrapVideo(v2, url2, false) // V2 Overlay |
| 112 | +]).then(() => { |
| 113 | + // Both decoders are now initialized. |
| 114 | + // Wait until V1 (the later one) actually fires its first frame. |
| 115 | + // When V1 starts rendering to its WebGL texture, Cobalt's compositor |
| 116 | + // does a deep structural redraw that historically wipes V2's overlay hole |
| 117 | + const fixV2Hole = () => { |
| 118 | + // Force a minor width change to recalculate Cobalt's compositor safely. |
| 119 | + // (Do NOT use `transform`, as it breaks `position: fixed` inside WebKit/Cobalt) |
| 120 | + v2.style.width = '105%'; |
| 121 | + setTimeout(() => { |
| 122 | + v2.style.width = ''; |
| 123 | + log('Ready!'); |
| 124 | + }, 50); |
| 125 | + }; |
| 126 | + |
| 127 | + if (v1.readyState >= 3) { |
| 128 | + fixV2Hole(); |
| 129 | + } else { |
| 130 | + v1.addEventListener('playing', fixV2Hole, { once: true }); |
| 131 | + } |
| 132 | +}); |
0 commit comments