tor-browser

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

videoFrame-orientation.any.js (3116B)


      1 // META: global=window,dedicatedworker
      2 
      3 function make4x2VideoFrame(rotation, flip) {
      4  // y y r r
      5  // b b g g
      6  let data = new Uint8Array([
      7    255, 255, 0, 255,
      8    255, 255, 0, 255,
      9    255, 0, 0, 255,
     10    255, 0, 0, 255,
     11    0, 0, 255, 255,
     12    0, 0, 255, 255,
     13    0, 255, 0, 255,
     14    0, 255, 0, 255,
     15  ]);
     16  return new VideoFrame(data, {
     17    format: "RGBX",
     18    codedWidth: 4,
     19    codedHeight: 2,
     20    timestamp: 0,
     21    rotation,
     22    flip
     23  });
     24 }
     25 
     26 // Verifies various properties about a frame created frommake4x2VideoFrame()
     27 // with flip=true and a 90 degree rotation.
     28 function validateOrientedFrame(frame) {
     29  assert_equals(frame.visibleRect.width, 4, 'visibleRect.width');
     30  assert_equals(frame.visibleRect.height, 2, 'visibleRect.height');
     31  assert_equals(frame.rotation, 90, 'rotation');
     32  assert_equals(frame.flip, true, 'flip');
     33  assert_equals(frame.displayWidth, 2, 'displayWidth');
     34  assert_equals(frame.displayHeight, 4, 'displayHeight');
     35 
     36  let width = frame.displayWidth;
     37  let height = frame.displayHeight;
     38  let canvas = new OffscreenCanvas(width, height);
     39  let ctx = canvas.getContext('2d');
     40  ctx.drawImage(frame, 0, 0);
     41  frame.close();
     42  let data = ctx.getImageData(0, 0, width, height).data;
     43 
     44  function check_pixel(x, y, expected_color) {
     45    let tolerance = 2;
     46    assert_approx_equals(data[4*(width*y+x)], expected_color[0], tolerance);
     47    assert_approx_equals(data[4*(width*y+x)+1], expected_color[1], tolerance);
     48    assert_approx_equals(data[4*(width*y+x)+2], expected_color[2], tolerance);
     49  }
     50 
     51  // Expected rendering:
     52  //   y b
     53  //   y b
     54  //   r g
     55  //   r g
     56  check_pixel(0, 0, [255, 255, 0]);
     57  check_pixel(1, 0, [0, 0, 255]);
     58  check_pixel(0, 3, [255, 0, 0]);
     59  check_pixel(1, 3, [0, 255, 0]);
     60 }
     61 
     62 test(_ => {
     63  let frame = make4x2VideoFrame(-315, true);
     64  validateOrientedFrame(frame);
     65 }, 'Test oriented VideoFrame from ArrayBuffer');
     66 
     67 test(_ => {
     68  for (let baseRotation = 0; baseRotation < 360; baseRotation += 90) {
     69    for (let baseFlip = 0; baseFlip < 2; ++baseFlip) {
     70      let baseFrame = make4x2VideoFrame(baseRotation, !!baseFlip);
     71      for (let deltaRotation = 0; deltaRotation < 360; deltaRotation += 90) {
     72        for (let deltaFlip = 0; deltaFlip < 2; ++deltaFlip) {
     73          let deltaFrame = new VideoFrame(baseFrame, {
     74              rotation: deltaRotation,
     75              flip: !!deltaFlip
     76          });
     77          let appliedRotation = !!baseFlip ? 360 - deltaRotation
     78                                           : deltaRotation;
     79          let expectedRotation = (baseRotation + appliedRotation) % 360;
     80          let expectedFlip = !!(baseFlip ^ deltaFlip);
     81          assert_equals(deltaFrame.rotation, expectedRotation, 'rotation');
     82          assert_equals(deltaFrame.flip, expectedFlip, 'flip');
     83          deltaFrame.close();
     84        }
     85      }
     86      baseFrame.close();
     87    }
     88  }
     89 }, 'Test combinations of rotation and flip');
     90 
     91 test(_ => {
     92  let orig_frame = make4x2VideoFrame(0, false);
     93  let frame = new VideoFrame(orig_frame, {rotation: -315, flip: true});
     94  orig_frame.close();
     95  validateOrientedFrame(frame);
     96 }, 'Test orientation of wrapped VideoFrame');