Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(470)

Side by Side Diff: native_client_sdk/src/examples/api/video_encode/example.js

Issue 1037793003: nacl sdk: add PPB_VideoEncoder example (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update after binji's review Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 var dataArray;
6 const MIME_TYPE = "application/octet-stream";
7
8 function $(id) {
9 return document.getElementById(id);
10 }
11
12
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();
19
20 var list = $('profileList');
21 var profile = (list.length < 1) ? 'vp8'
22 : list.options[list.selectedIndex].value;
23
24 common.naclModule.postMessage({
25 command: 'start',
26 track: track,
27 profile: profile,
28 width: 640,
29 height: 480
30 });
31 }
32
33 function failure(e) {
34 common.logMessage("Error: " + e);
35 }
36
37 function cleanupDownload() {
38 var download = $('download');
39 if (!download)
40 return;
41 download.parentNode.removeChild(download);
42 }
43
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);
52 }
53
54 function startRecord() {
55 $('length').innerHTML = ' Size: ' + dataArray.byteLength + ' bytes';
56 navigator.webkitGetUserMedia({audio: false, video: true},
57 success, failure);
58
59 $('start').disabled = true;
60 $('stop').disabled = false;
61 cleanupDownload();
62 }
63
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;
72
73 $('start').disabled = false;
74 $('stop').disabled = true;
75 appendDownload($('download-box'),
76 new Blob([dataArray], { type: MIME_TYPE }),
77 'Capture.video');
78 }
79
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 }
97 }
98
99 function resetData() {
100 dataArray = new ArrayBuffer(0);
101 }
102
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';
109 }
110
111 function attachListeners() {
112 $('start').addEventListener('click', function (e) {
113 resetData();
114 startRecord();
115 });
116 $('stop').addEventListener('click', function (e) {
117 stopRecord();
118 });
119 }
120
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();
126 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698