Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / crd / js / video_frame_recorder.js
blob1b53b5cbbc16cd4ad4bdc557514082e846879f8d
1 // Copyright 2014 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 /**
6 * @fileoverview
7 * Class implement the video frame recorder extension client.
8 */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16 * @constructor
17 * @implements {remoting.ProtocolExtension}
19 remoting.VideoFrameRecorder = function() {
20 /** @private {?function(string,string)} */
21 this.sendMessageToHostCallback_ = null;
23 this.fileWriter_ = null;
24 this.isRecording_ = false;
27 /** @private {string} */
28 remoting.VideoFrameRecorder.EXTENSION_TYPE = 'video-recorder';
30 /** @return {Array<string>} */
31 remoting.VideoFrameRecorder.prototype.getExtensionTypes = function() {
32 return [remoting.VideoFrameRecorder.EXTENSION_TYPE];
35 /**
36 * @param {function(string,string)} sendMessageToHost Callback to send a message
37 * to the host.
39 remoting.VideoFrameRecorder.prototype.startExtension =
40 function(sendMessageToHost) {
41 this.sendMessageToHostCallback_ = sendMessageToHost;
44 /**
45 * @param {Object} data The data to send.
46 * @private
48 remoting.VideoFrameRecorder.prototype.sendMessageToHost_ = function(data) {
49 this.sendMessageToHostCallback_(remoting.VideoFrameRecorder.EXTENSION_TYPE,
50 JSON.stringify(data));
53 /**
54 * Handles 'video-recorder' extension messages and returns true. Returns
55 * false for all other message types.
57 * @param {string} type Type of extension message.
58 * @param {Object} message The parsed extension message data.
59 * @return {boolean}
61 remoting.VideoFrameRecorder.prototype.onExtensionMessage =
62 function(type, message) {
63 console.assert(type == remoting.VideoFrameRecorder.EXTENSION_TYPE,
64 'Unexpected extension message type: ' + type + '.');
66 var messageType = base.getStringAttr(message, 'type');
67 var messageData = base.getStringAttr(message, 'data');
69 if (messageType == 'next-frame-reply') {
70 if (!this.fileWriter_) {
71 console.log("Received frame but have no writer");
72 return true;
74 if (!messageData) {
75 console.log("Finished receiving frames");
76 this.fileWriter_ = null;
77 return true;
80 console.log("Received frame");
81 var videoPacketString = window.atob(messageData);
83 console.log("Converted from Base64 - length:" + videoPacketString.length);
84 var byteArrays = [];
86 for (var offset = 0; offset < videoPacketString.length; offset += 512) {
87 var slice = videoPacketString.slice(offset, offset + 512);
88 var byteNumbers = new Array(slice.length);
89 for (var i = 0; i < slice.length; i++) {
90 byteNumbers[i] = slice.charCodeAt(i);
92 var byteArray = new Uint8Array(byteNumbers);
93 byteArrays.push(byteArray);
96 console.log("Writing frame");
97 videoPacketString = null;
98 /**
99 * @param {Array} parts
100 * @return {Blob}
102 var makeBlob = function(parts) {
103 return new Blob(parts);
105 var videoPacketBlob = makeBlob(byteArrays);
106 byteArrays = null;
108 this.fileWriter_.write(videoPacketBlob);
110 return true;
113 console.log("Unrecognized message: " + messageType);
114 return true;
118 * Starts or stops video recording.
120 remoting.VideoFrameRecorder.prototype.startStopRecording = function() {
121 var data = {};
122 if (this.isRecording_) {
123 this.isRecording_ = false;
124 data = { type: 'stop' }
126 chrome.fileSystem.chooseEntry(
127 {type: 'saveFile', suggestedName: 'videoRecording.pb'},
128 this.onFileChosen_.bind(this));
129 } else {
130 this.isRecording_ = true;
131 data = { type: 'start' }
133 this.sendMessageToHost_(data);
137 * Returns true if the session is currently being recorded.
138 * @return {boolean}
140 remoting.VideoFrameRecorder.prototype.isRecording = function() {
141 return this.isRecording_;
145 * @param {Entry=} entry The single file entry if multiple files are not
146 * allowed.
147 * @param {Array<!FileEntry>=} fileEntries List of file entries if multiple
148 * files are allowed.
150 remoting.VideoFrameRecorder.prototype.onFileChosen_ = function(
151 entry, fileEntries) {
152 if (!entry) {
153 console.log("Cancelled save of video frames.");
154 } else {
155 chrome.fileSystem.getDisplayPath(entry, function(path) {
156 console.log("Saving video frames to:" + path);
158 entry.createWriter(this.onFileWriter_.bind(this));
162 /** @param {FileWriter} fileWriter */
163 remoting.VideoFrameRecorder.prototype.onFileWriter_ = function(fileWriter) {
164 console.log("Obtained FileWriter for video frame write");
165 fileWriter.onwriteend = this.onWriteComplete_.bind(this);
166 this.fileWriter_ = fileWriter;
167 this.fetchNextFrame_();
170 remoting.VideoFrameRecorder.prototype.onWriteComplete_ = function(e) {
171 console.log("Video frame write complete");
172 this.fetchNextFrame_();
175 remoting.VideoFrameRecorder.prototype.fetchNextFrame_ = function() {
176 console.log("Request next video frame");
177 var data = { type: 'next-frame' }
178 this.sendMessageToHost_(data);