summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/encrypted-media/scripts/reset-src-after-setmediakeys.js
blob: 0ccce3275ecb7077bd600bb1ded1290ac76be71d (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
function runTest(config)
{
    async_test(function(test) {
        var mediaKeys;
        var mediaSource;
        var encryptedEventIndex = 0;
        var video = config.video;
        var keysystem = config.keysystem;
        var configuration = {
            initDataTypes: [config.initDataType],
            audioCapabilities: [{
                contentType: config.audioType
            }],
            videoCapabilities: [{
                contentType: config.videoType
            }],
            sessionTypes: ['temporary']
        };

        assert_not_equals(video, null);

        var onEncrypted = function(event) {
            ++encryptedEventIndex;
            assert_equals(video.mediaKeys, mediaKeys);

            // This event is fired once for the audio stream and once
            // for the video stream each time .src is set.
            if (encryptedEventIndex === 2) {
                // Finished first video; Create new media source and wait for two more encrypted events
                return testmediasource(config).then(function (source) {
                    video.src = URL.createObjectURL(source);
                }).catch(function (error) {
                    forceTestFailureFromPromise(test, error)
                });
            } else if (encryptedEventIndex === 4) {
                // Finished second video.
                test.done();
            }
        };

        // Create a MediaKeys object and assign it to video.
        navigator.requestMediaKeySystemAccess(keysystem, [configuration]).then(test.step_func(function (access) {
            assert_equals(access.keySystem, keysystem);
            return access.createMediaKeys();
        })).then(test.step_func(function (result) {
            mediaKeys = result;
            assert_not_equals(mediaKeys, null);
            return video.setMediaKeys(mediaKeys);
        })).then(test.step_func(function () {
            assert_equals(video.mediaKeys, mediaKeys);
            return testmediasource(config);
        })).then(function (source) {
            waitForEventAndRunStep('encrypted', video, onEncrypted, test);
            mediaSource = source;
            video.src = URL.createObjectURL(mediaSource);
        }).catch(function (error) {
            forceTestFailureFromPromise(test, error);
        });

    }, 'Reset src after setMediaKeys().');
}