tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

reconfiguring-encoder.https.any.js (3256B)


      1 // META: timeout=long
      2 // META: global=window,dedicatedworker
      3 // META: script=/webcodecs/video-encoder-utils.js
      4 // META: variant=?av1
      5 // META: variant=?vp8
      6 // META: variant=?vp9_p0
      7 // META: variant=?vp9_p2
      8 // META: variant=?h264_avc
      9 // META: variant=?h264_annexb
     10 
     11 var ENCODER_CONFIG = null;
     12 promise_setup(async () => {
     13  const config = {
     14    '?av1': {codec: 'av01.0.04M.08'},
     15    '?vp8': {codec: 'vp8'},
     16    '?vp9_p0': {codec: 'vp09.00.10.08'},
     17    '?vp9_p2': {codec: 'vp09.02.10.10'},
     18    '?h264_avc': {codec: 'avc1.42001F', avc: {format: 'avc'}},
     19    '?h264_annexb': {codec: 'avc1.42001F', avc: {format: 'annexb'}}
     20  }[location.search];
     21  config.hardwareAcceleration = 'prefer-software';
     22  config.bitrateMode = "constant";
     23  config.framerate = 30;
     24  ENCODER_CONFIG = config;
     25 });
     26 
     27 promise_test(async t => {
     28  let original_w = 800;
     29  let original_h = 600;
     30  let original_bitrate = 3_000_000;
     31 
     32  let new_w = 640;
     33  let new_h = 480;
     34  let new_bitrate = 2_000_000;
     35 
     36  let next_ts = 0
     37  let reconf_ts = 0;
     38  let frames_to_encode = 16;
     39  let before_reconf_frames = 0;
     40  let after_reconf_frames = 0;
     41 
     42  let process_video_chunk = function (chunk, metadata) {
     43    let config = metadata.decoderConfig;
     44    var data = new Uint8Array(chunk.data);
     45    assert_greater_than_equal(data.length, 0);
     46    let after_reconf = (reconf_ts != 0) && (chunk.timestamp >= reconf_ts);
     47    if (after_reconf) {
     48      after_reconf_frames++;
     49      if (config) {
     50        assert_equals(config.codedWidth, new_w);
     51        assert_equals(config.codedHeight, new_h);
     52      }
     53    } else {
     54      before_reconf_frames++;
     55      if (config) {
     56        assert_equals(config.codedWidth, original_w);
     57        assert_equals(config.codedHeight, original_h);
     58      }
     59    }
     60  };
     61 
     62  const init = {
     63    output: (chunk, md) => {
     64      try {
     65        process_video_chunk(chunk, md);
     66      } catch (e) {
     67        assert_unreached(e.message);
     68      }
     69    },
     70    error: (e) => {
     71      assert_unreached(e.message);
     72    },
     73  };
     74  const params = {
     75    ...ENCODER_CONFIG,
     76    width: original_w,
     77    height: original_h,
     78    bitrate: original_bitrate,
     79  };
     80  await checkEncoderSupport(t, params);
     81 
     82  let encoder = new VideoEncoder(init);
     83  encoder.configure(params);
     84 
     85  // Remove this flush after crbug.com/1275789 is fixed
     86  await encoder.flush();
     87 
     88  // Encode |frames_to_encode| frames with original settings
     89  for (let i = 0; i < frames_to_encode; i++) {
     90    var frame = createFrame(original_w, original_h, next_ts++);
     91    encoder.encode(frame, {});
     92    frame.close();
     93  }
     94 
     95  params.width = new_w;
     96  params.height = new_h;
     97  params.bitrate = new_bitrate;
     98 
     99  // Reconfigure encoder and run |frames_to_encode| frames with new settings
    100  encoder.configure(params);
    101  reconf_ts = next_ts;
    102 
    103  for (let i = 0; i < frames_to_encode; i++) {
    104    var frame = createFrame(new_w, new_h, next_ts++);
    105    encoder.encode(frame, {});
    106    frame.close();
    107  }
    108 
    109  await encoder.flush();
    110 
    111  // Configure back to original config
    112  params.width = original_w;
    113  params.height = original_h;
    114  params.bitrate = original_bitrate;
    115  encoder.configure(params);
    116  await encoder.flush();
    117 
    118  encoder.close();
    119  assert_equals(before_reconf_frames, frames_to_encode);
    120  assert_equals(after_reconf_frames, frames_to_encode);
    121 }, "Reconfiguring encoder");