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