summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/mediacapture-streams/MediaStreamTrack-getSettings.https.html
blob: 3bae50c346dd3c83d15f37eca598b58838250c46 (plain)
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
<!doctype html>
<title>MediaStreamTrack GetSettings</title>
<p class="instructions">When prompted, accept to share your video stream.</p>
<meta name=timeout content=long>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src=permission-helper.js></script>
<script>
  'use strict'

  // https://w3c.github.io/mediacapture-main/archives/20170605/getusermedia.html

  async function createTrackAndGetSettings(t, kind) {
    const constraints = {};
    constraints[kind] = true;
    const stream = await navigator.mediaDevices.getUserMedia(constraints);
    assert_equals(stream.getTracks().length, 1);
    t.add_cleanup(() => stream.getTracks()[0].stop());
    return stream.getTracks()[0].getSettings();
  }

  promise_test(async t => {
    await setMediaPermission("granted", ["camera"]);
    const mediaStream1 = await navigator.mediaDevices.getUserMedia({
      video: true,
      audio: false,
    });
    t.add_cleanup(() => mediaStream1.getVideoTracks()[0].stop());
    const settings1 = mediaStream1.getVideoTracks()[0].getSettings();

    const mediaStream2 = await navigator.mediaDevices.getUserMedia({
      video: {
        deviceId: {exact: settings1.deviceId},
      },
      audio: false
    });
    t.add_cleanup(() => mediaStream2.getVideoTracks()[0].stop());
    const settings2 = mediaStream2.getVideoTracks()[0].getSettings();

    assert_equals(settings1.deviceId, settings2.deviceId);
  }, 'A device can be opened twice and have the same device ID');

  promise_test(async t => {
    const mediaStream1 = await navigator.mediaDevices.getUserMedia({
      video: true,
      audio: false,
    });
    t.add_cleanup(() => mediaStream1.getVideoTracks()[0].stop());
    const settings1 = mediaStream1.getVideoTracks()[0].getSettings();

    const mediaStream2 = await navigator.mediaDevices.getUserMedia({
      video: {
        deviceId: {exact: settings1.deviceId},
        width: {
          ideal: settings1.width / 2,
        },
      },
      audio: false
    });
    t.add_cleanup(() => mediaStream2.getVideoTracks()[0].stop());
    const settings2 = mediaStream2.getVideoTracks()[0].getSettings();

    assert_equals(settings1.deviceId, settings2.deviceId);
    assert_between_inclusive(settings2.width, settings1.width / 2, settings1.width);
  }, 'A device can be opened twice with different resolutions requested');

  promise_test(async t => {
    // getUserMedia needs to be called before deviceIds and groupIds are exposed
    const afterGum = await navigator.mediaDevices.getUserMedia({
      video: true, audio: true
    });
    afterGum.getTracks().forEach(track => track.stop());

    const devices = await navigator.mediaDevices.enumerateDevices();
    const inputDevices = devices.filter(({kind}) => kind != "audiooutput");
    assert_greater_than(inputDevices.length, 1, "have at least 2 test devices");
    for (const {kind, deviceId, groupId} of inputDevices) {
      const type = {videoinput: "video", audioinput: "audio"}[kind];
      const stream = await navigator.mediaDevices.getUserMedia({
        [type]: {deviceId: {exact: deviceId}}
      });
      const [track] = stream.getTracks();
      const settings = track.getSettings();
      track.stop();
      assert_true(settings.groupId == groupId, "device groupId");
      assert_greater_than(settings.groupId.length, 0, "groupId is not empty");
    }
  }, 'groupId is correctly reported by getSettings() for all input devices');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.deviceId), "string",
                  "deviceId should exist and it should be a string.");
  }, 'deviceId is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.groupId), "string",
                  "groupId should exist and it should be a string.");
  }, 'groupId is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.sampleRate), "number",
                  "sampleRate should exist and it should be a number.");
    assert_greater_than(settings.sampleRate, 0);
  }, 'sampleRate is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.sampleSize), "number",
                  "sampleSize should exist and it should be a number.");
    assert_greater_than(settings.sampleSize, 0);
  }, 'sampleSize is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.echoCancellation), "boolean",
                  "echoCancellation should exist and it should be a boolean.");
  }, 'echoCancellation is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.autoGainControl), "boolean",
                  "autoGainControl should exist and it should be a boolean.");
  }, 'autoGainControl is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.noiseSuppression), "boolean",
                  "noiseSuppression should exist and it should be a boolean.");
  }, 'noiseSuppression is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.voiceIsolation), "boolean",
                  "voiceIsolation should exist and it should be a boolean.");
  }, 'voiceIsolation is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.latency), "number",
                  "latency should exist and it should be a number.");
    assert_greater_than_equal(settings.latency,0);
  }, 'latency is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "audio");
    assert_equals(typeof(settings.channelCount), "number",
                  "channelCount should exist and it should be a number.");
    assert_greater_than(settings.channelCount, 0);
  }, 'channelCount is reported by getSettings() for getUserMedia() audio tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "video");
    assert_equals(typeof(settings.deviceId), "string",
                  "deviceId should exist and it should be a string.");
  }, 'deviceId is reported by getSettings() for getUserMedia() video tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "video");
    assert_equals(typeof(settings.groupId), "string",
                  "groupId should exist and it should be a string.");
  }, 'groupId is reported by getSettings() for getUserMedia() video tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "video");
    assert_equals(typeof(settings.width), "number",
                  "width should exist and it should be a number.");
    assert_true(Number.isInteger(settings.width), "width should be an integer.");
    assert_greater_than_equal(settings.width, 0);;
  }, 'width is reported by getSettings() for getUserMedia() video tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "video");
    assert_equals(typeof(settings.height), "number",
                  "height should exist and it should be a number.");
    assert_true(Number.isInteger(settings.height), "height should be an integer.");
    assert_greater_than_equal(settings.height, 0);
  }, 'height is reported by getSettings() for getUserMedia() video tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "video");
    assert_equals(typeof(settings.aspectRatio), "number",
                  "aspectRatio should exist and it should be a number.");
    assert_greater_than_equal(settings.aspectRatio, 0);
  }, 'aspectRatio is reported by getSettings() for getUserMedia() video tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "video");
    assert_equals(typeof(settings.frameRate), "number",
                  "frameRate should exist and it should be a number.");
    assert_greater_than_equal(settings.frameRate, 0);
  }, 'frameRate is reported by getSettings() for getUserMedia() video tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "video");
    // facingMode not treated as mandatory because not all platforms provide
    // this information.
    if (settings.facingMode) {
      assert_equals(typeof(settings.facingMode), "string",
                  "If facingMode is provided it should be a string.");
      assert_in_array(settings.facingMode,
                  ['user', 'environment', 'left', 'right']);
    }
  }, 'facingMode is reported by getSettings() for getUserMedia() video tracks');

  promise_test(async t => {
    const settings = await createTrackAndGetSettings(t, "video");
    assert_equals(typeof(settings.resizeMode), "string",
                "resizeMode should exist and it should be a string.");
    assert_in_array(settings.resizeMode, ['none', 'crop-and-scale']);
  }, 'resizeMode is reported by getSettings() for getUserMedia() video tracks');

  promise_test(async t => {
    const stream = await navigator.mediaDevices.getUserMedia({ audio: true, video : true});
    const audioTrack = stream.getAudioTracks()[0];
    const videoTrack = stream.getVideoTracks()[0];

    const audioDeviceId = audioTrack.getSettings().deviceId;
    const videoDeviceId = videoTrack.getSettings().deviceId;
    const audioGroupId = audioTrack.getSettings().groupId;
    const videoGroupId = videoTrack.getSettings().groupId;

    audioTrack.stop();
    videoTrack.stop();

    assert_equals(audioTrack.getSettings().deviceId, audioDeviceId, "audio track deviceId");
    assert_equals(videoTrack.getSettings().deviceId, videoDeviceId, "video track deviceId");
    assert_equals(audioTrack.getSettings().groupId, audioGroupId, "audio track groupId");
    assert_equals(videoTrack.getSettings().groupId, videoGroupId, "video track groupId");
  }, 'Stopped tracks should expose deviceId/groupId');
</script>