Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / remoting / webapp / crd / js / video_frame_recorder.js
blob001224271ed8e34b84652bd81f10f7a4c90088fc
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}
18  */
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.
38  */
39 remoting.VideoFrameRecorder.prototype.startExtension =
40     function(sendMessageToHost) {
41   this.sendMessageToHostCallback_ = sendMessageToHost;
44 /**
45  * @param {Object} data The data to send.
46  * @private
47  */
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.
56  *
57  * @param {string} type Type of extension message.
58  * @param {Object} message The parsed extension message data.
59  * @return {boolean}
60  */
61 remoting.VideoFrameRecorder.prototype.onExtensionMessage =
62     function(type, message) {
63   base.debug.assert(type == remoting.VideoFrameRecorder.EXTENSION_TYPE);
65   var messageType = base.getStringAttr(message, 'type');
66   var messageData = base.getStringAttr(message, 'data');
68   if (messageType == 'next-frame-reply') {
69     if (!this.fileWriter_) {
70       console.log("Received frame but have no writer");
71       return true;
72     }
73     if (!messageData) {
74       console.log("Finished receiving frames");
75       this.fileWriter_ = null;
76       return true;
77     }
79     console.log("Received frame");
80     var videoPacketString = window.atob(messageData);
82     console.log("Converted from Base64 - length:" + videoPacketString.length);
83     var byteArrays = [];
85     for (var offset = 0; offset < videoPacketString.length; offset += 512) {
86         var slice = videoPacketString.slice(offset, offset + 512);
87         var byteNumbers = new Array(slice.length);
88         for (var i = 0; i < slice.length; i++) {
89             byteNumbers[i] = slice.charCodeAt(i);
90         }
91         var byteArray = new Uint8Array(byteNumbers);
92         byteArrays.push(byteArray);
93     }
95     console.log("Writing frame");
96     videoPacketString = null;
97     /**
98      * @param {Array} parts
99      * @return {Blob}
100      */
101     var makeBlob = function(parts) {
102       return new Blob(parts);
103     }
104     var videoPacketBlob = makeBlob(byteArrays);
105     byteArrays = null;
107     this.fileWriter_.write(videoPacketBlob);
109     return true;
110   }
112   console.log("Unrecognized message: " + messageType);
113   return true;
117  * Starts or stops video recording.
118  */
119 remoting.VideoFrameRecorder.prototype.startStopRecording = function() {
120   var data = {};
121   if (this.isRecording_) {
122     this.isRecording_ = false;
123     data = { type: 'stop' }
125     chrome.fileSystem.chooseEntry(
126         {type: 'saveFile', suggestedName: 'videoRecording.pb'},
127         this.onFileChosen_.bind(this));
128   } else {
129     this.isRecording_ = true;
130     data = { type: 'start' }
131   }
132   this.sendMessageToHost_(data);
136  * Returns true if the session is currently being recorded.
137  * @return {boolean}
138  */
139 remoting.VideoFrameRecorder.prototype.isRecording = function() {
140   return this.isRecording_;
144  * @param {Entry=} entry The single file entry if multiple files are not
145  *     allowed.
146  * @param {Array<!FileEntry>=} fileEntries List of file entries if multiple
147  *     files are allowed.
148  */
149 remoting.VideoFrameRecorder.prototype.onFileChosen_ = function(
150     entry, fileEntries) {
151   if (!entry) {
152     console.log("Cancelled save of video frames.");
153   } else {
154     chrome.fileSystem.getDisplayPath(entry, function(path) {
155       console.log("Saving video frames to:" + path);
156     });
157     entry.createWriter(this.onFileWriter_.bind(this));
158   }
161 /** @param {FileWriter} fileWriter */
162 remoting.VideoFrameRecorder.prototype.onFileWriter_ = function(fileWriter) {
163   console.log("Obtained FileWriter for video frame write");
164   fileWriter.onwriteend = this.onWriteComplete_.bind(this);
165   this.fileWriter_ = fileWriter;
166   this.fetchNextFrame_();
169 remoting.VideoFrameRecorder.prototype.onWriteComplete_ = function(e) {
170   console.log("Video frame write complete");
171   this.fetchNextFrame_();
174 remoting.VideoFrameRecorder.prototype.fetchNextFrame_ = function() {
175   console.log("Request next video frame");
176   var data = { type: 'next-frame' }
177   this.sendMessageToHost_(data);