Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<title>Test MediaRecorder Record Timeslice</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="text/javascript" src="manifest.js"></script>
<pre id="test">
<script class="testbody" type="text/javascript">
var manager = new MediaTestManager;
* Starts a test on every media recorder file included to check that a stream
* derived from the file can be recorded with a timeslice provided
function startTest(test, token) {
var element = document.createElement('audio');
var expectedMimeType = test.type;
element.token = token;
element.src =;
element.test = test;
element.preload = "auto";
// Set up MediaRecorder once loadedmetadata fires and tracks are available.
element.onloadedmetadata = function() {
element.onloadedmetadata = null;
const stream = element.mozCaptureStream();
const mediaRecorder =
new MediaRecorder(stream, {mimeType: expectedMimeType});
mediaRecorder.onerror = function () {
ok(false, 'Unexpected onerror callback fired');
mediaRecorder.onwarning = function () {
ok(false, 'Unexpected onwarning callback fired');
mediaRecorder.onstop = function () {
ok(false, 'Unexpected onstop callback fired');
var dataAvailableCount = 0;
var onDataAvailableFirst = false;
// This handler fires every 250ms to generate a blob.
mediaRecorder.ondataavailable = function (evt) {
info('ondataavailable fired');
ok(evt instanceof BlobEvent,
'Events fired from ondataavailable should be BlobEvent');
is(evt.type, 'dataavailable',
'Event type should dataavailable');
ok( >= 0,
'Blob data size received is greater than or equal to zero');
is(, expectedMimeType,
'Blob data received should have type = ' + expectedMimeType);
is(mediaRecorder.mimeType, expectedMimeType,
'Mime type in ondataavailable = ' + mediaRecorder.mimeType);
// We'll stop recording upon the 1st blob being received
if (dataAvailableCount === 1) {
mediaRecorder.onstop = function () {
info('onstop fired');
if (!onDataAvailableFirst) {
ok(false, 'onstop unexpectedly fired before ondataavailable');
is(mediaRecorder.state, 'inactive',
'Media recorder is inactive after being stopped');
is(, stream,
'Media recorder stream = element stream post recording');
} else if (dataAvailableCount === 2) {
// Ensure we've received at least two ondataavailable events before onstop
onDataAvailableFirst = true;
is(mediaRecorder.state, 'recording', 'Media recorder should be recording');
is(, stream,
'Media recorder stream = element stream at the start of recording');
manager.runTests(gMediaRecorderTests, startTest);