nacl sdk: add PPB_VideoEncoder example
[chromium-blink-merge.git] / native_client_sdk / src / examples / api / video_encode / example.js
blob9cf5e626a0183f3addc133306a8e9f8d8fc3d752
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var dataArray;
6 const MIME_TYPE = "application/octet-stream";
8 function $(id) {
9   return document.getElementById(id);
13 function success(stream) {
14   track = stream.getVideoTracks()[0];
15   var video = $('video')
16   video.src = URL.createObjectURL(stream);
17   video.track = track;
18   video.play();
20   var list = $('profileList');
21   var profile = (list.length < 1) ? 'vp8'
22     : list.options[list.selectedIndex].value;
24   common.naclModule.postMessage({
25     command: 'start',
26     track: track,
27     profile: profile,
28     width: 640,
29     height: 480
30   });
33 function failure(e) {
34   common.logMessage("Error: " + e);
37 function cleanupDownload() {
38   var download = $('download');
39   if (!download)
40     return;
41   download.parentNode.removeChild(download);
44 function appendDownload(parent, blob, filename) {
45   var a = document.createElement('a');
46   a.id = "download";
47   a.download = filename;
48   a.href = window.URL.createObjectURL(blob);
49   a.textContent = 'Download';
50   a.dataset.downloadurl = [MIME_TYPE, a.download, a.href].join(':');
51   parent.appendChild(a);
54 function startRecord() {
55   $('length').innerHTML = ' Size: ' + dataArray.byteLength + ' bytes';
56   navigator.webkitGetUserMedia({audio: false, video: true},
57                                success, failure);
59   $('start').disabled = true;
60   $('stop').disabled = false;
61   cleanupDownload();
64 function stopRecord() {
65   common.naclModule.postMessage({
66     command: "stop"
67   });
68   var video = $('video');
69   video.pause();
70   video.track.stop();
71   video.track = null;
73   $('start').disabled = false;
74   $('stop').disabled = true;
75   appendDownload($('download-box'),
76                  new Blob([dataArray], { type: MIME_TYPE }),
77                  'Capture.video');
80 function handleMessage(msg) {
81   if (msg.data.name == 'data') {
82     appendData(msg.data.data);
83   } else if (msg.data.name == 'supportedProfiles') {
84     common.logMessage('profiles: ' + JSON.stringify(msg.data.profiles));
85     var profileList = $('profileList');
86     for (var node in profileList.childNodes)
87       profileList.remove(node);
88     for (var i = 0; i < msg.data.profiles.length; i++) {
89       var item = document.createElement('option');
90       item.label = item.value = msg.data.profiles[i];
91       profileList.appendChild(item);
92     }
93     $('start').disabled = !(msg.data.profiles.length > 0);
94   } else if (msg.data.name == 'log') {
95     common.logMessage(msg.data.message);
96   }
99 function resetData() {
100   dataArray = new ArrayBuffer(0);
103 function appendData(data) {
104   var tmp = new Uint8Array(dataArray.byteLength + data.byteLength);
105   tmp.set(new Uint8Array(dataArray), 0 );
106   tmp.set(new Uint8Array(data), dataArray.byteLength);
107   dataArray = tmp.buffer;
108   $('length').textContent = ' Size: ' + dataArray.byteLength + ' bytes';
111 function attachListeners() {
112   $('start').addEventListener('click', function (e) {
113     resetData();
114     startRecord();
115   });
116   $('stop').addEventListener('click', function (e) {
117     stopRecord();
118   });
121 // Called by the common.js module.
122 function moduleDidLoad() {
123   // The module is not hidden by default so we can easily see if the plugin
124   // failed to load.
125   common.hideModule();