1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345

content / test / gpu / gpu_tests / webcodecs_integration_test.py [blame]

# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import sys
import json
import itertools
from typing import Any, List, Set
import unittest

import gpu_path_util
from gpu_tests import common_browser_args as cba
from gpu_tests import common_typing as ct
from gpu_tests import gpu_integration_test
from gpu_tests.util import host_information

html_path = os.path.join(gpu_path_util.CHROMIUM_SRC_DIR, 'content', 'test',
                         'data', 'gpu', 'webcodecs')
data_path = os.path.join(gpu_path_util.CHROMIUM_SRC_DIR, 'media', 'test',
                         'data')
four_colors_img_path = os.path.join(data_path, 'four-colors.y4m')

frame_sources = [
    'camera', 'capture', 'offscreen', 'arraybuffer', 'hw_decoder', 'sw_decoder'
]
hbd_frame_sources = ['hbd_arraybuffer']
video_codecs = [
    'avc1.42001E', 'hvc1.1.6.L123.00', 'vp8', 'vp09.00.10.08', 'av01.0.04M.08'
]
accelerations = ['prefer-hardware', 'prefer-software']


class WebCodecsIntegrationTest(gpu_integration_test.GpuIntegrationTest):
  @classmethod
  def Name(cls) -> str:
    return 'webcodecs'

  @classmethod
  def _SuiteSupportsParallelTests(cls) -> bool:
    return True

  def _GetSerialGlobs(self) -> Set[str]:
    serial_globs = set()
    if host_information.IsWindows() and host_information.IsNvidiaGpu():
      serial_globs |= {
          # crbug.com/1473480. Windows + NVIDIA has a maximum parallel encode
          # limit of 2, so serialize hardware encoding tests on Windows.
          'WebCodecs_*prefer-hardware*',
      }
    return serial_globs

  def _GetSerialTests(self) -> Set[str]:
    serial_tests = set()
    if host_information.IsWindows() and host_information.IsArmCpu():
      serial_tests |= {
          # crbug.com/323824490. Seems to flakily lose the D3D11 device when
          # run in parallel.
          'WebCodecs_FrameSizeChange_vp09.00.10.08_hw_decoder',
      }
    return serial_tests

# pylint: disable=too-many-branches

  @classmethod
  def GenerateGpuTests(cls, options: ct.ParsedCmdArgs) -> ct.TestGenerator:
    tests = itertools.chain(cls.GenerateFrameTests(), cls.GenerateVideoTests(),
                            cls.GenerateAudioTests(), cls.BitrateTests())
    for test in tests:
      yield test

  @classmethod
  def GenerateFrameTests(cls) -> ct.TestGenerator:
    for source_type in frame_sources:
      yield ('WebCodecs_DrawImage_' + source_type, 'draw-image.html', [{
          'source_type':
          source_type
      }])
      yield ('WebCodecs_TexImage2d_' + source_type, 'tex-image-2d.html', [{
          'source_type':
          source_type
      }])
      yield ('WebCodecs_copyTo_' + source_type, 'copyTo.html', [{
          'source_type':
          source_type
      }])
      yield ('WebCodecs_convertToRGB_' + source_type, 'convert-to-rgb.html', [{
          'source_type':
          source_type
      }])
    for source_type in hbd_frame_sources:
      yield ('WebCodecs_DrawImage_' + source_type, 'draw-image.html', [{
          'source_type':
          source_type
      }])

  @classmethod
  def GenerateAudioTests(cls) -> ct.TestGenerator:
    yield ('WebCodecs_AudioEncoding_AAC_LC', 'audio-encode-decode.html', [{
        'codec':
        'mp4a.67',
        'sample_rate':
        48000,
        'channels':
        2,
        'aac_format':
        'aac'
    }])
    yield ('WebCodecs_AudioEncoding_AAC_LC_ADTS', 'audio-encode-decode.html', [{
        'codec':
        'mp4a.67',
        'sample_rate':
        48000,
        'channels':
        2,
        'aac_format':
        'adts'
    }])

  @classmethod
  def BitrateTests(cls) -> ct.TestGenerator:
    high_res_codecs = [
        'avc1.420034', 'hvc1.1.6.L123.00', 'vp8', 'vp09.00.10.08',
        'av01.0.04M.08'
    ]
    for codec in high_res_codecs:
      for acc in accelerations:
        for bitrate_mode in ['constant', 'variable']:
          for bitrate in [1500000, 2000000, 3000000]:
            args = (codec, acc, bitrate_mode, bitrate)
            yield ('WebCodecs_EncodingRateControl_%s_%s_%s_%s' % args,
                   'encoding-rate-control.html', [{
                       'codec': codec,
                       'acceleration': acc,
                       'bitrate_mode': bitrate_mode,
                       'bitrate': bitrate
                   }])

  @classmethod
  def GenerateVideoTests(cls) -> ct.TestGenerator:
    yield ('WebCodecs_WebRTCPeerConnection_Window',
           'webrtc-peer-connection.html', [{
               'use_worker': False
           }])
    yield ('WebCodecs_WebRTCPeerConnection_Worker',
           'webrtc-peer-connection.html', [{
               'use_worker': True
           }])
    yield ('WebCodecs_Terminate_Worker', 'terminate-worker.html', [{
        'source_type':
        'offscreen',
    }])

    source_type = 'offscreen'
    codec = 'avc1.42001E'
    acc = 'prefer-hardware'
    args = (source_type, codec, acc)
    yield ('WebCodecs_PerFrameQpEncoding_%s_%s_%s' % args,
           'frame-qp-encoding.html', [{
               'source_type': source_type,
               'codec': codec,
               'acceleration': acc
           }])

    codec = 'av01.0.04M.08'
    acc = 'prefer-software'
    for layers in range(4):
      args = (codec, acc, layers)
      yield ('WebCodecs_ManualSVC_%s_%s_layers_%d' % args, 'manual-svc.html', [{
          'codec':
          codec,
          'acceleration':
          acc,
          'layers':
          layers
      }])

    for source_type in frame_sources:
      for codec in video_codecs:
        for acc in accelerations:
          args = (source_type, codec, acc)
          yield ('WebCodecs_EncodeDecode_%s_%s_%s' % args, 'encode-decode.html',
                 [{
                     'source_type': source_type,
                     'codec': codec,
                     'acceleration': acc
                 }])

    for source_type in frame_sources:
      # Also verify we can deal with the encoder's frame delay that we can
      # encounter for the AVC High profile (avc1.64).
      for codec in video_codecs + ['avc1.64001E']:
        for acc in accelerations:
          args = (source_type, codec, acc)
          yield ('WebCodecs_Encode_%s_%s_%s' % args, 'encode.html', [{
              'source_type':
              source_type,
              'codec':
              codec,
              'acceleration':
              acc
          }])

    for resolution in ['1920x1080', '3840x2160', '7680x3840']:
      for framerate in [30, 60, 120, 240]:
        # Use at least level 6.2 (H.264/H.265/VP9), or level 6.3 (AV1) mimetypes
        # to test 8k 120fps support.
        for codec in [
            'avc1.64003E', 'hvc1.1.6.L186.B0', 'vp09.00.62.08', 'av01.1.19M.08'
        ]:
          acc = 'prefer-hardware'
          latency_mode = 'quality'
          args = (resolution, framerate, codec, acc, latency_mode)
          yield ('WebCodecs_EncodingFramerateResolutions_%s_%s_%s_%s_%s' % args,
                 'encoding-framerate-resolutions.html', [{
                     'resolution':
                     resolution,
                     'framerate':
                     framerate,
                     'codec':
                     codec,
                     'acceleration':
                     acc,
                     'latency_mode':
                     latency_mode,
                 }])

    for codec in video_codecs:
      for acc in accelerations:
        for bitrate_mode in ['constant', 'variable']:
          for latency_mode in ['realtime', 'quality']:
            source_type = 'offscreen'
            content_hint = 'motion'
            args = (source_type, codec, acc, bitrate_mode, latency_mode)
            yield ('WebCodecs_EncodingModes_%s_%s_%s_%s_%s' % args,
                   'encoding-modes.html', [{
                       'source_type': source_type,
                       'codec': codec,
                       'acceleration': acc,
                       'bitrate_mode': bitrate_mode,
                       'latency_mode': latency_mode,
                       'content_hint': content_hint
                   }])

    for codec in video_codecs:
      for content_hint in ['detail', 'text', 'motion']:
        source_type = 'offscreen'
        acc = 'prefer-hardware'
        bitrate_mode = 'constant'
        latency_mode = 'realtime'
        yield ('WebCodecs_ContentHint_%s_%s' % (codec, content_hint),
               'encoding-modes.html', [{
                   'source_type': source_type,
                   'codec': codec,
                   'acceleration': acc,
                   'bitrate_mode': bitrate_mode,
                   'latency_mode': latency_mode,
                   'content_hint': content_hint
               }])

    for codec in video_codecs:
      for acc in accelerations:
        for layers in [2, 3]:
          args = (codec, acc, layers)
          yield ('WebCodecs_SVC_%s_%s_layers_%d' % args, 'svc.html', [{
              'codec':
              codec,
              'acceleration':
              acc,
              'layers':
              layers
          }])

    for codec in video_codecs:
      for acc in accelerations:
        args = (codec, acc)
        yield ('WebCodecs_EncodeColorSpace_%s_%s' % args,
               'encode-color-space.html', [{
                   'codec': codec,
                   'acceleration': acc
               }])

    for codec in video_codecs:
      for source_type in frame_sources:
        args = (codec, source_type)
        yield ('WebCodecs_FrameSizeChange_%s_%s' % args,
               'frame-size-change.html', [{
                   'codec': codec,
                   'source_type': source_type
               }])
# pylint: enable=too-many-branches

  def RunActualGpuTest(self, test_path: str, args: ct.TestArgs) -> None:
    url = self.UrlOfStaticFilePath(html_path + '/' + test_path)
    tab = self.tab
    arg_obj = args[0]
    os_name = self.platform.GetOSName()
    arg_obj['validate_camera_frames'] = self.CameraCanShowFourColors(os_name)
    tab.Navigate(url)
    tab.action_runner.WaitForJavaScriptCondition(
        'document.readyState == "complete"')
    tab.EvaluateJavaScript('TEST.run(' + json.dumps(arg_obj) + ')')
    tab.action_runner.WaitForJavaScriptCondition('TEST.finished', timeout=60)
    if tab.EvaluateJavaScript('TEST.skipped'):
      self.skipTest('Skipping test:' + tab.EvaluateJavaScript('TEST.summary()'))
    if not tab.EvaluateJavaScript('TEST.success'):
      self.fail('Test failure:' + tab.EvaluateJavaScript('TEST.summary()'))

  @staticmethod
  def CameraCanShowFourColors(os_name: str) -> bool:
    return os_name not in ('android', 'chromeos')

  @classmethod
  def SetUpProcess(cls) -> None:
    super(WebCodecsIntegrationTest, cls).SetUpProcess()
    args = [
        '--use-fake-device-for-media-stream',
        '--use-fake-ui-for-media-stream',
        '--enable-blink-features=SharedArrayBuffer',
        cba.ENABLE_PLATFORM_HEVC_ENCODER_SUPPORT,
        cba.ENABLE_EXPERIMENTAL_WEB_PLATFORM_FEATURES,
    ] + cba.ENABLE_WEBGPU_FOR_TESTING

    # If we don't call CustomizeBrowserArgs cls.platform is None
    cls.CustomizeBrowserArgs(args)

    if cls.CameraCanShowFourColors(cls.platform.GetOSName()):
      args.append('--use-file-for-fake-video-capture=' + four_colors_img_path)
      cls.CustomizeBrowserArgs(args)

    cls.StartBrowser()
    cls.SetStaticServerDirs([html_path, data_path])

  @classmethod
  def ExpectationsFiles(cls) -> List[str]:
    return [
        os.path.join(os.path.dirname(os.path.abspath(__file__)),
                     'test_expectations', 'webcodecs_expectations.txt')
    ]


def load_tests(loader: unittest.TestLoader, tests: Any,
               pattern: Any) -> unittest.TestSuite:
  del loader, tests, pattern  # Unused.
  return gpu_integration_test.LoadAllTestsInModule(sys.modules[__name__])