OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 /** | |
6 * @fileoverview | |
7 * Class implement the video frame recorder extension client. | |
Jamie
2014/08/19 00:48:56
s/implement/implementing/
| |
8 */ | |
9 | |
10 'use strict'; | |
11 | |
12 /** @suppress {duplicate} */ | |
13 var remoting = remoting || {}; | |
14 | |
15 /** | |
16 * @constructor | |
17 */ | |
18 remoting.VideoFrameRecorder = function(plugin) { | |
19 this.fileWriter_ = null; | |
20 this.isRecording_ = false; | |
21 this.plugin_ = plugin; | |
22 }; | |
23 | |
24 /** | |
25 * | |
26 */ | |
27 remoting.VideoFrameRecorder.prototype.startStopRecording = function() { | |
28 var data = {}; | |
29 if (this.isRecording_) { | |
30 this.isRecording_ = false; | |
31 data = { type: 'stop' } | |
32 | |
33 chrome.fileSystem.chooseEntry( | |
34 {type: 'saveFile', suggestedName: "videoRecording.pb"}, | |
Jamie
2014/08/19 00:48:56
You've used padding spaces after '{' and before '}
| |
35 this.onFileChosen_.bind(this)); | |
36 } else { | |
37 this.isRecording_ = true; | |
38 data = { type: 'start' } | |
39 } | |
40 this.plugin_.sendClientMessage('video-recorder', JSON.stringify(data)); | |
41 } | |
42 | |
43 /** | |
44 * | |
45 */ | |
46 remoting.VideoFrameRecorder.prototype.onFileChosen_ = function(fileEntry) { | |
47 if (!fileEntry) { | |
48 console.log("Cancelled save of video frames."); | |
49 } else { | |
50 chrome.fileSystem.getDisplayPath(fileEntry, function(path) { | |
51 console.log("Saving video frames to:" + path); | |
52 }); | |
53 fileEntry.createWriter(this.onFileWriter_.bind(this)); | |
54 } | |
55 } | |
56 | |
57 /** | |
58 * | |
59 */ | |
60 remoting.VideoFrameRecorder.prototype.onFileWriter_ = function(fileWriter) { | |
61 console.log("Obtained FileWriter for video frame write"); | |
62 fileWriter.onwriteend = this.onWriteComplete_.bind(this); | |
Jamie
2014/08/19 00:48:56
Do you need an onerror handler?
| |
63 this.fileWriter_ = fileWriter; | |
64 this.fetchNextFrame_(); | |
65 } | |
66 | |
67 /* | |
68 * | |
69 */ | |
70 remoting.VideoFrameRecorder.prototype.onWriteComplete_ = function(e) { | |
Jamie
2014/08/19 00:48:56
In JS, there's no need to declare parameters if yo
| |
71 console.log("Video frame write complete"); | |
72 this.fetchNextFrame_(); | |
73 } | |
74 | |
75 /* | |
76 * | |
77 */ | |
78 remoting.VideoFrameRecorder.prototype.fetchNextFrame_ = function() { | |
79 console.log("Request next video frame"); | |
80 var data = { type: 'next-frame' } | |
81 this.plugin_.sendClientMessage('video-recorder', JSON.stringify(data)); | |
82 } | |
83 | |
84 /** | |
85 * | |
86 */ | |
87 remoting.VideoFrameRecorder.prototype.handleMessage = | |
88 function(type, data) { | |
89 if (type != 'video-recorder') { | |
90 return false; | |
91 } | |
92 | |
93 var message = getJsonObjectFromString(data); | |
94 | |
95 if (message.type == 'next-frame-reply') { | |
96 if (!this.fileWriter_) { | |
97 console.log("Received frame but have no writer"); | |
98 return true; | |
99 } | |
100 if (!message.data) { | |
101 console.log("Finished receiving frames"); | |
102 this.fileWriter_ = null; | |
103 return true; | |
104 } | |
105 | |
106 console.log("Received frame"); | |
107 var videoPacketString = atob(message.data); | |
108 | |
109 console.log("Converted from Base64 - length:" + videoPacketString.length); | |
110 var byteArrays = []; | |
111 | |
112 for (var offset = 0; offset < videoPacketString.length; offset += 512) { | |
113 var slice = videoPacketString.slice(offset, offset + 512); | |
Jamie
2014/08/19 00:48:56
Indentation.
| |
114 var byteNumbers = new Array(slice.length); | |
115 for (var i = 0; i < slice.length; i++) { | |
116 byteNumbers[i] = slice.charCodeAt(i); | |
117 } | |
Jamie
2014/08/19 00:48:56
Can you do this more succinctly with Array.map?
| |
118 var byteArray = new Uint8Array(byteNumbers); | |
119 byteArrays.push(byteArray); | |
120 } | |
121 | |
122 console.log("Writing frame"); | |
123 videoPacketString = null; | |
124 var videoPacketBlob = new Blob(byteArrays); | |
125 byteArrays = null; | |
126 | |
127 this.fileWriter_.write(videoPacketBlob); | |
128 | |
129 return true; | |
130 } | |
131 | |
132 console.log("Unrecognized message: " + message.type); | |
133 return true; | |
134 } | |
OLD | NEW |