summaryrefslogtreecommitdiffstats
path: root/dom/media/mediacontrol/tests/browser/browser_media_control_captured_audio.js
blob: 0da8acd62b0d3447d983dad7c4f82e1309e5f9c5 (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
const PAGE_NON_AUTOPLAY_MEDIA =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_non_autoplay.html";

const testVideoId = "video";

add_task(async function setupTestingPref() {
  await SpecialPowers.pushPrefEnv({
    set: [["media.mediacontrol.testingevents.enabled", true]],
  });
});

/**
 * When we capture audio from an media element to the web audio, if the media
 * is audible, it should be controlled by media keys as well.
 */
add_task(async function testAudibleCapturedMedia() {
  info(`open new non autoplay media page`);
  const tab = await createLoadedTabWrapper(PAGE_NON_AUTOPLAY_MEDIA);

  info(`capture audio and start playing`);
  await captureAudio(tab, testVideoId);
  await playMedia(tab, testVideoId);

  info(`pressing 'pause' key, captured media should be paused`);
  await generateMediaControlKeyEvent("pause");
  await checkOrWaitUntilMediaStoppedPlaying(tab, testVideoId);

  info(`remove tab`);
  await tab.close();
});

/**
 * The following are helper functions.
 */
function captureAudio(tab, elementId) {
  return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => {
    const video = content.document.getElementById(Id);
    if (!video) {
      ok(false, `can't get the media element!`);
    }
    const context = new content.AudioContext();
    // Capture audio from the media element to a MediaElementAudioSourceNode.
    context.createMediaElementSource(video);
  });
}