|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>MediaRecorder MIMEType</title> |
| 5 | + <link rel="help" href="https://w3c.github.io/mediacapture-record/MediaRecorder.html#dom-mediarecorder-mimetype"> |
| 6 | + <script src="/resources/testharness.js"></script> |
| 7 | + <script src="/resources/testharnessreport.js"></script> |
| 8 | +</head> |
| 9 | +<body> |
| 10 | +<canvas id="canvas" width="200" height="200"> |
| 11 | +</canvas> |
| 12 | +<script> |
| 13 | +function createAudioStream(t) { |
| 14 | + const ac = new AudioContext(); |
| 15 | + const {stream} = ac.createMediaStreamDestination(); |
| 16 | + const tracks = stream.getTracks(); |
| 17 | + t.add_cleanup(() => tracks.forEach(tr => tr.stop())); |
| 18 | + return stream; |
| 19 | +} |
| 20 | + |
| 21 | +function createVideoStream(t) { |
| 22 | + const canvas = document.getElementById("canvas"); |
| 23 | + canvas.getContext('2d'); |
| 24 | + const stream = canvas.captureStream(); |
| 25 | + const tracks = stream.getTracks(); |
| 26 | + t.add_cleanup(() => tracks.forEach(tr => tr.stop())); |
| 27 | + return stream; |
| 28 | +} |
| 29 | + |
| 30 | +function createAudioVideoStream(t) { |
| 31 | + return new MediaStream([ |
| 32 | + ...createAudioStream(t).getTracks(), |
| 33 | + ...createVideoStream(t).getTracks(), |
| 34 | + ]); |
| 35 | +} |
| 36 | + |
| 37 | +test(t => { |
| 38 | + const recorder = new MediaRecorder(createAudioStream(t)); |
| 39 | + assert_equals(recorder.mimeType, "", |
| 40 | + "MediaRecorder has no default MIMEtype"); |
| 41 | +}, "MediaRecorder sets no default MIMEType in the constructor for audio"); |
| 42 | + |
| 43 | +test(t => { |
| 44 | + const recorder = new MediaRecorder(createVideoStream(t)); |
| 45 | + assert_equals(recorder.mimeType, "", |
| 46 | + "MediaRecorder has no default MIMEtype"); |
| 47 | +}, "MediaRecorder sets no default MIMEType in the constructor for video"); |
| 48 | + |
| 49 | +test(t => { |
| 50 | + const stream = createAudioVideoStream(t); |
| 51 | + const recorder = new MediaRecorder(stream); |
| 52 | + assert_equals(recorder.mimeType, "", |
| 53 | + "MediaRecorder has no default MIMEtype"); |
| 54 | +}, "MediaRecorder sets no default MIMEType in the constructor for audio/video"); |
| 55 | + |
| 56 | +test(t => { |
| 57 | + assert_throws("NotSupportedError", |
| 58 | + () => new MediaRecorder(new MediaStream(), {mimeType: "audio/banana"})); |
| 59 | +}, "MediaRecorder invalid audio MIMEType throws"); |
| 60 | + |
| 61 | +test(t => { |
| 62 | + assert_false(MediaRecorder.isTypeSupported("audio/banana")); |
| 63 | +}, "MediaRecorder invalid audio MIMEType is unsupported"); |
| 64 | + |
| 65 | +test(t => { |
| 66 | + assert_throws("NotSupportedError", |
| 67 | + () => new MediaRecorder(new MediaStream(), {mimeType: "video/pineapple"})); |
| 68 | +}, "MediaRecorder invalid video MIMEType throws"); |
| 69 | + |
| 70 | +test(t => { |
| 71 | + assert_false(MediaRecorder.isTypeSupported("video/pineapple")); |
| 72 | +}, "MediaRecorder invalid video MIMEType is unsupported"); |
| 73 | + |
| 74 | +// New MIME types could be added to this list as needed. |
| 75 | +for (const mimeType of [ |
| 76 | + 'audio/mp4', |
| 77 | + 'video/mp4', |
| 78 | + 'audio/ogg', |
| 79 | + 'audio/ogg; codecs="vorbis"', |
| 80 | + 'audio/ogg; codecs="opus"', |
| 81 | + 'audio/webm', |
| 82 | + 'audio/webm; codecs="vorbis"', |
| 83 | + 'audio/webm; codecs="opus"', |
| 84 | + 'video/webm', |
| 85 | + 'video/webm; codecs="vp8"', |
| 86 | + 'video/webm; codecs="vp8, vorbis"', |
| 87 | + 'video/webm; codecs="vp8, opus"', |
| 88 | + 'video/webm; codecs="vp9"', |
| 89 | + 'video/webm; codecs="vp9, vorbis"', |
| 90 | + 'video/webm; codecs="vp9, opus"', |
| 91 | + 'video/webm; codecs="av1"', |
| 92 | + 'video/webm; codecs="av1, opus"', |
| 93 | +]) { |
| 94 | + if (MediaRecorder.isTypeSupported(mimeType)) { |
| 95 | + test(t => { |
| 96 | + const recorder = new MediaRecorder(new MediaStream(), {mimeType}); |
| 97 | + assert_equals(recorder.mimeType, mimeType, "Supported MIMEType is set"); |
| 98 | + }, `Supported MIMEType ${mimeType} is set immediately after constructing`); |
| 99 | + } else { |
| 100 | + test(t => { |
| 101 | + assert_throws("NotSupportedError", |
| 102 | + () => new MediaRecorder(new MediaStream(), {mimeType})); |
| 103 | + }, `Unsupported MIMEType ${mimeType} throws`); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +test(t => { |
| 108 | + const recorder = new MediaRecorder(createAudioStream(t)); |
| 109 | + recorder.start(); |
| 110 | + assert_not_equals(recorder.mimeType, "", |
| 111 | + "MediaRecorder has a MIMEtype after start() for audio"); |
| 112 | + assert_regexp_match(recorder.mimeType, /^audio\//, |
| 113 | + "MIMEtype has an expected media type"); |
| 114 | + assert_regexp_match(recorder.mimeType, /^[a-z]+\/[a-z]+/, |
| 115 | + "MIMEtype has a container subtype"); |
| 116 | + assert_regexp_match(recorder.mimeType, /^[a-z]+\/[a-z]+; codecs=[^,]+$/, |
| 117 | + "MIMEtype has one codec"); |
| 118 | +}, "MediaRecorder sets a MIMEType after start() for audio"); |
| 119 | + |
| 120 | +test(t => { |
| 121 | + const recorder = new MediaRecorder(createVideoStream(t)); |
| 122 | + recorder.start(); |
| 123 | + assert_not_equals(recorder.mimeType, "", |
| 124 | + "MediaRecorder has a MIMEtype after start() for video"); |
| 125 | + assert_regexp_match(recorder.mimeType, /^video\//, |
| 126 | + "MIMEtype has an expected media type"); |
| 127 | + assert_regexp_match(recorder.mimeType, /^[a-z]+\/[a-z]+/, |
| 128 | + "MIMEtype has a container subtype"); |
| 129 | + assert_regexp_match(recorder.mimeType, /^[a-z]+\/[a-z]+; codecs=[^,]+$/, |
| 130 | + "MIMEtype has one codec"); |
| 131 | +}, "MediaRecorder sets a MIMEType after start() for video"); |
| 132 | + |
| 133 | +test(t => { |
| 134 | + const recorder = new MediaRecorder(createAudioVideoStream(t)); |
| 135 | + recorder.start(); |
| 136 | + assert_not_equals(recorder.mimeType, "", |
| 137 | + "MediaRecorder has a MIMEtype after start() for audio/video"); |
| 138 | + assert_regexp_match(recorder.mimeType, /^video\//, |
| 139 | + "MIMEtype has an expected media type"); |
| 140 | + assert_regexp_match(recorder.mimeType, /^[a-z]+\/[a-z]+/, |
| 141 | + "MIMEtype has a container subtype"); |
| 142 | + assert_regexp_match(recorder.mimeType, /^[a-z]+\/[a-z]+; codecs=[^,]+,[^,]+$/, |
| 143 | + "MIMEtype has two codecs"); |
| 144 | +}, "MediaRecorder sets a MIMEType after start() for audio/video"); |
| 145 | +</script> |
| 146 | +</body> |
| 147 | +</html> |
0 commit comments