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.
7 * Class implement the video frame recorder extension client.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
17 * @param {remoting.ClientPlugin} plugin
19 remoting.VideoFrameRecorder = function(plugin) {
20 this.fileWriter_ = null;
21 this.isRecording_ = false;
22 this.plugin_ = plugin;
26 * Starts or stops video recording.
28 remoting.VideoFrameRecorder.prototype.startStopRecording = function() {
30 if (this.isRecording_) {
31 this.isRecording_ = false;
32 data = { type: 'stop' }
34 chrome.fileSystem.chooseEntry(
35 {type: 'saveFile', suggestedName: 'videoRecording.pb'},
36 this.onFileChosen_.bind(this));
38 this.isRecording_ = true;
39 data = { type: 'start' }
41 this.plugin_.sendClientMessage('video-recorder', JSON.stringify(data));
45 * Returns true if the session is currently being recorded.
48 remoting.VideoFrameRecorder.prototype.isRecording = function() {
49 return this.isRecording_;
53 * Handles 'video-recorder' extension messages and returns true. Returns
54 * false for all other message types.
56 * @param {string} type Type of extension message.
57 * @param {Object} message The parsed extension message data.
60 remoting.VideoFrameRecorder.prototype.handleMessage =
61 function(type, message) {
62 if (type != 'video-recorder') {
66 var messageType = getStringAttr(message, 'type');
67 var messageData = getStringAttr(message, 'data');
69 if (messageType == 'next-frame-reply') {
70 if (!this.fileWriter_) {
71 console.log("Received frame but have no writer");
75 console.log("Finished receiving frames");
76 this.fileWriter_ = null;
80 console.log("Received frame");
81 var videoPacketString = window.atob(messageData);
83 console.log("Converted from Base64 - length:" + videoPacketString.length);
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;
99 * @param {Array} parts
102 var makeBlob = function(parts) {
103 return new Blob(parts);
105 var videoPacketBlob = makeBlob(byteArrays);
108 this.fileWriter_.write(videoPacketBlob);
113 console.log("Unrecognized message: " + messageType);
118 * @param {Entry} entry The single file entry if multiple files are not allowed.
119 * @param {Array<FileEntry>} fileEntries List of file entries if multiple files
122 remoting.VideoFrameRecorder.prototype.onFileChosen_ = function(
123 entry, fileEntries) {
125 console.log("Cancelled save of video frames.");
127 chrome.fileSystem.getDisplayPath(entry, function(path) {
128 console.log("Saving video frames to:" + path);
130 entry.createWriter(this.onFileWriter_.bind(this));
134 /** @param {FileWriter} fileWriter */
135 remoting.VideoFrameRecorder.prototype.onFileWriter_ = function(fileWriter) {
136 console.log("Obtained FileWriter for video frame write");
137 fileWriter.onwriteend = this.onWriteComplete_.bind(this);
138 this.fileWriter_ = fileWriter;
139 this.fetchNextFrame_();
142 remoting.VideoFrameRecorder.prototype.onWriteComplete_ = function(e) {
143 console.log("Video frame write complete");
144 this.fetchNextFrame_();
147 remoting.VideoFrameRecorder.prototype.fetchNextFrame_ = function() {
148 console.log("Request next video frame");
149 var data = { type: 'next-frame' }
150 this.plugin_.sendClientMessage('video-recorder', JSON.stringify(data));