OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * Class implement the video frame recorder extension client. | 7 * Class implement the video frame recorder extension client. |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
14 | 14 |
15 /** | 15 /** |
16 * @constructor | 16 * @constructor |
17 * @param {remoting.ClientPlugin} plugin | 17 * @implements {remoting.ProtocolExtension} |
18 */ | 18 */ |
19 remoting.VideoFrameRecorder = function(plugin) { | 19 remoting.VideoFrameRecorder = function() { |
20 /** @private {?function(string,string)} */ | |
21 this.sendMessageToHostCallback_ = null; | |
22 | |
20 this.fileWriter_ = null; | 23 this.fileWriter_ = null; |
21 this.isRecording_ = false; | 24 this.isRecording_ = false; |
22 this.plugin_ = plugin; | 25 }; |
26 | |
27 /** @private {string} */ | |
28 remoting.VideoFrameRecorder.EXTENSION_TYPE = 'video-recorder'; | |
29 | |
30 /** @return {Array<string>} */ | |
31 remoting.VideoFrameRecorder.prototype.getExtensionTypes = function() { | |
32 return [remoting.VideoFrameRecorder.EXTENSION_TYPE]; | |
23 }; | 33 }; |
24 | 34 |
25 /** | 35 /** |
26 * Starts or stops video recording. | 36 * @param {function(string,string)} sendMessageToHost Callback to send a message |
37 * to the host. | |
27 */ | 38 */ |
28 remoting.VideoFrameRecorder.prototype.startStopRecording = function() { | 39 remoting.VideoFrameRecorder.prototype.startExtension = |
29 var data = {}; | 40 function(sendMessageToHost) { |
30 if (this.isRecording_) { | 41 this.sendMessageToHostCallback_ = sendMessageToHost; |
31 this.isRecording_ = false; | 42 }; |
32 data = { type: 'stop' } | |
33 | 43 |
34 chrome.fileSystem.chooseEntry( | 44 /** |
35 {type: 'saveFile', suggestedName: 'videoRecording.pb'}, | 45 * @param {Object} data The data to send. |
36 this.onFileChosen_.bind(this)); | 46 * @private |
37 } else { | 47 */ |
38 this.isRecording_ = true; | 48 remoting.VideoFrameRecorder.prototype.sendMessageToHost_ = function(data) { |
39 data = { type: 'start' } | 49 this.sendMessageToHostCallback_(remoting.VideoFrameRecorder.EXTENSION_TYPE, |
40 } | 50 JSON.stringify(data)); |
41 this.plugin_.sendClientMessage('video-recorder', JSON.stringify(data)); | |
42 } | 51 } |
43 | 52 |
44 /** | 53 /** |
45 * Returns true if the session is currently being recorded. | |
46 * @return {boolean} | |
47 */ | |
48 remoting.VideoFrameRecorder.prototype.isRecording = function() { | |
49 return this.isRecording_; | |
50 } | |
51 | |
52 /** | |
53 * Handles 'video-recorder' extension messages and returns true. Returns | 54 * Handles 'video-recorder' extension messages and returns true. Returns |
54 * false for all other message types. | 55 * false for all other message types. |
55 * | 56 * |
56 * @param {string} type Type of extension message. | 57 * @param {string} type Type of extension message. |
57 * @param {Object} message The parsed extension message data. | 58 * @param {Object} message The parsed extension message data. |
58 * @return {boolean} | 59 * @return {boolean} |
59 */ | 60 */ |
60 remoting.VideoFrameRecorder.prototype.handleMessage = | 61 remoting.VideoFrameRecorder.prototype.onExtensionMessage = |
61 function(type, message) { | 62 function(type, message) { |
62 if (type != 'video-recorder') { | 63 if (type != remoting.VideoFrameRecorder.EXTENSION_TYPE) { |
kelvinp
2015/03/21 17:36:09
Nit: Assert instead.
garykac
2015/03/23 22:51:04
Done.
| |
63 return false; | 64 return false; |
64 } | 65 } |
65 | 66 |
66 var messageType = base.getStringAttr(message, 'type'); | 67 var messageType = base.getStringAttr(message, 'type'); |
67 var messageData = base.getStringAttr(message, 'data'); | 68 var messageData = base.getStringAttr(message, 'data'); |
68 | 69 |
69 if (messageType == 'next-frame-reply') { | 70 if (messageType == 'next-frame-reply') { |
70 if (!this.fileWriter_) { | 71 if (!this.fileWriter_) { |
71 console.log("Received frame but have no writer"); | 72 console.log("Received frame but have no writer"); |
72 return true; | 73 return true; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 this.fileWriter_.write(videoPacketBlob); | 109 this.fileWriter_.write(videoPacketBlob); |
109 | 110 |
110 return true; | 111 return true; |
111 } | 112 } |
112 | 113 |
113 console.log("Unrecognized message: " + messageType); | 114 console.log("Unrecognized message: " + messageType); |
114 return true; | 115 return true; |
115 } | 116 } |
116 | 117 |
117 /** | 118 /** |
119 * Starts or stops video recording. | |
120 */ | |
121 remoting.VideoFrameRecorder.prototype.startStopRecording = function() { | |
122 var data = {}; | |
123 if (this.isRecording_) { | |
124 this.isRecording_ = false; | |
125 data = { type: 'stop' } | |
126 | |
127 chrome.fileSystem.chooseEntry( | |
128 {type: 'saveFile', suggestedName: 'videoRecording.pb'}, | |
129 this.onFileChosen_.bind(this)); | |
130 } else { | |
131 this.isRecording_ = true; | |
132 data = { type: 'start' } | |
133 } | |
134 this.sendMessageToHost_(data); | |
135 } | |
136 | |
137 /** | |
138 * Returns true if the session is currently being recorded. | |
139 * @return {boolean} | |
140 */ | |
141 remoting.VideoFrameRecorder.prototype.isRecording = function() { | |
142 return this.isRecording_; | |
143 } | |
144 | |
145 /** | |
118 * @param {Entry} entry The single file entry if multiple files are not allowed. | 146 * @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 | 147 * @param {Array<FileEntry>} fileEntries List of file entries if multiple files |
120 * are allowed. | 148 * are allowed. |
121 */ | 149 */ |
122 remoting.VideoFrameRecorder.prototype.onFileChosen_ = function( | 150 remoting.VideoFrameRecorder.prototype.onFileChosen_ = function( |
123 entry, fileEntries) { | 151 entry, fileEntries) { |
124 if (!entry) { | 152 if (!entry) { |
125 console.log("Cancelled save of video frames."); | 153 console.log("Cancelled save of video frames."); |
126 } else { | 154 } else { |
127 chrome.fileSystem.getDisplayPath(entry, function(path) { | 155 chrome.fileSystem.getDisplayPath(entry, function(path) { |
(...skipping 12 matching lines...) Expand all Loading... | |
140 } | 168 } |
141 | 169 |
142 remoting.VideoFrameRecorder.prototype.onWriteComplete_ = function(e) { | 170 remoting.VideoFrameRecorder.prototype.onWriteComplete_ = function(e) { |
143 console.log("Video frame write complete"); | 171 console.log("Video frame write complete"); |
144 this.fetchNextFrame_(); | 172 this.fetchNextFrame_(); |
145 } | 173 } |
146 | 174 |
147 remoting.VideoFrameRecorder.prototype.fetchNextFrame_ = function() { | 175 remoting.VideoFrameRecorder.prototype.fetchNextFrame_ = function() { |
148 console.log("Request next video frame"); | 176 console.log("Request next video frame"); |
149 var data = { type: 'next-frame' } | 177 var data = { type: 'next-frame' } |
150 this.plugin_.sendClientMessage('video-recorder', JSON.stringify(data)); | 178 this.sendMessageToHost_(data); |
151 } | 179 } |
OLD | NEW |