Home | History | Annotate | Download | only in media
      1 <html>
      2   <head>
      3     <title>Test media source config changes.</title>
      4   </head>
      5   <body onload="runTest();">
      6     <video controls></video>
      7     <script src="media_utils.js" type="text/javascript"></script>
      8     <script src="media_source_utils.js" type="text/javascript"></script>
      9     <script src="encrypted_media_utils.js" type="text/javascript"></script>
     10     <script type="text/javascript">
     11       var runEncrypted = QueryString.runencrypted == 1;
     12       var video = document.querySelector('video');
     13       var mediaType = 'video/webm; codecs="vorbis, vp8"';
     14 
     15       var MEDIA_1 = 'bear-320x240.webm';
     16       var MEDIA_2 = 'bear-640x360.webm';
     17       if (runEncrypted) {
     18         MEDIA_1 = 'bear-320x240-av-enc_av.webm';
     19         MEDIA_2 = 'bear-640x360-av-enc_av.webm';
     20       }
     21 
     22       var MEDIA_1_WIDTH = 320;
     23       var MEDIA_1_HEIGHT = 240;
     24 
     25       var MEDIA_2_WIDTH = 640;
     26       var MEDIA_2_HEIGHT = 360;
     27       var MEDIA_2_LENGTH = 2.75;
     28 
     29       // The time in secs to append the second media source.
     30       var APPEND_TIME = 1;
     31       // DELTA is the time after APPEND_TIME where the second video dimensions
     32       // are guaranteed to take effect.
     33       var DELTA = 0.1;
     34       // Append MEDIA_2 source at APPEND_TIME, so expected total duration is:
     35       var TOTAL_DURATION = APPEND_TIME + MEDIA_2_LENGTH;
     36 
     37       function appendNextSource(mediaSource) {
     38         console.log('Appending next media source at ' + APPEND_TIME + 'sec.');
     39         var xhr = new XMLHttpRequest();
     40         xhr.open("GET", MEDIA_2);
     41         xhr.responseType = 'arraybuffer';
     42         xhr.addEventListener('load', function(e) {
     43           var srcBuffer = mediaSource.sourceBuffers[0];
     44           srcBuffer.timestampOffset = APPEND_TIME;
     45           srcBuffer.append(new Uint8Array(e.target.response));
     46           mediaSource.endOfStream();
     47           if (!mediaSource.duration ||
     48               Math.abs(mediaSource.duration - TOTAL_DURATION) > DELTA) {
     49             failTest('Unexpected mediaSource.duration = ' +
     50                      mediaSource.duration + ', expected duration = ' +
     51                      TOTAL_DURATION);
     52             return;
     53           }
     54           video.play();
     55         });
     56         xhr.send();
     57       }
     58 
     59       function onTimeUpdate() {
     60         // crbug.com/246308
     61         //checkVideoProperties();
     62 
     63         // Seek to APPEND_TIME because after a seek a timeUpdate event is fired
     64         // before video width and height properties get updated.
     65         if (video.currentTime < APPEND_TIME - DELTA) {
     66           // Seek to save test execution time (about 1 secs) and to test seek
     67           // on the first buffer.
     68           video.currentTime = APPEND_TIME - DELTA;
     69         } else if (video.currentTime > APPEND_TIME + DELTA) {
     70           // Check video duration here to guarantee that second segment has been
     71           // appended and video total duration is updated.
     72           // Video duration is a float value so we check it within a range.
     73           if (!video.duration ||
     74               Math.abs(video.duration - TOTAL_DURATION) > DELTA) {
     75             failTest('Unexpected video.duration = ' + video.duration +
     76                      ', expected duration = ' + TOTAL_DURATION);
     77             return;
     78           }
     79 
     80           video.removeEventListener('timeupdate', onTimeUpdate);
     81           video.removeEventListener('ended', failTest);
     82           installTitleEventHandler(video, 'ended');
     83           // Seek to save test execution time and to test seek on second buffer.
     84           video.currentTime = APPEND_TIME + MEDIA_2_LENGTH * 0.9;
     85         }
     86       }
     87 
     88       function checkVideoProperties() {
     89         if (video.currentTime <= APPEND_TIME) {
     90           if (video.videoWidth != MEDIA_1_WIDTH ||
     91               video.videoHeight != MEDIA_1_HEIGHT) {
     92             logVideoDimensions();
     93             failTest('Unexpected dimensions for first video segment.');
     94             return;
     95           }
     96         } else if (video.currentTime >= APPEND_TIME + DELTA) {
     97           if (video.videoWidth != MEDIA_2_WIDTH ||
     98               video.videoHeight != MEDIA_2_HEIGHT) {
     99             logVideoDimensions();
    100             failTest('Unexpected dimensions for second video segment.');
    101             return;
    102           }
    103         }
    104       }
    105 
    106       function logVideoDimensions() {
    107         console.log('video.currentTime = ' + video.currentTime +
    108                     ', video dimensions = ' + video.videoWidth + 'x' +
    109                     video.videoHeight + '.');
    110       }
    111 
    112       function runTest() {
    113         video.addEventListener('timeupdate', onTimeUpdate);
    114         video.addEventListener('ended', failTest);
    115         if (runEncrypted) {
    116           loadEncryptedMedia(video, MEDIA_1, keySystem, KEY, true,
    117                              appendNextSource);
    118         } else {
    119           var mediaSource = loadMediaSource(MEDIA_1, mediaType,
    120                                             appendNextSource);
    121           video.src = window.URL.createObjectURL(mediaSource);
    122         }
    123       }
    124       </script>
    125   </body>
    126 </html>
    127