diff --git a/src/chrome/ScreenCaptureButtons.tsx b/src/chrome/ScreenCaptureButtons.tsx index d16cb516f..fc6dfea13 100644 --- a/src/chrome/ScreenCaptureButtons.tsx +++ b/src/chrome/ScreenCaptureButtons.tsx @@ -102,10 +102,8 @@ class ScreenCaptureButtons extends Component { return; } const videoPath = path.join(CAPTURE_LOCATION, getFileName('mp4')); - await selectedDevice.startScreenCapture(videoPath); - - this.setState({ - recording: true, + await selectedDevice.startScreenCapture(videoPath).catch(e => { + console.error(e); }); }; @@ -114,18 +112,23 @@ class ScreenCaptureButtons extends Component { if (!selectedDevice) { return; } - const path = await selectedDevice.stopScreenCapture(); - this.setState({ - recording: false, + const path = await selectedDevice.stopScreenCapture().catch(e => { + console.error(e); }); - openFile(path); + path ? openFile(path) : 0; }; onRecordingClicked = () => { if (this.state.recording) { this.stopRecording(); + this.setState({ + recording: false, + }); } else { this.startRecording(); + this.setState({ + recording: true, + }); } }; diff --git a/src/devices/AndroidDevice.tsx b/src/devices/AndroidDevice.tsx index 693dfb8d8..cf4bc5fe1 100644 --- a/src/devices/AndroidDevice.tsx +++ b/src/devices/AndroidDevice.tsx @@ -132,6 +132,21 @@ export default class AndroidDevice extends BaseDevice { } } + private async isValidFile(filePath: string): Promise { + const fileSize = await this.adb + .shell(this.serial, `touch "${filePath}" && ls -l "${filePath}"`) + .then(adb.util.readAll) + .then((output: Buffer) => + output + .toString() + .trim() + .split(' '), + ) + .then(output => Number(output[5])); + + return fileSize > 0; + } + async startScreenCapture(destination: string) { await this.executeShell( `mkdir -p "${DEVICE_RECORDING_DIR}" && echo -n > "${DEVICE_RECORDING_DIR}/.nomedia"`, @@ -140,15 +155,29 @@ export default class AndroidDevice extends BaseDevice { this.recordingProcess = this.adb .shell(this.serial, `screenrecord --bugreport "${recordingLocation}"`) .then(adb.util.readAll) + .then( + output => + new Promise(async (resolve, _) => { + const isValid = await this.isValidFile(recordingLocation); + if (!isValid) { + const outputMessage = output.toString().trim(); + // throw error immediately + throw new Error( + 'Recording was not properly started: \n' + outputMessage, + ); + } + resolve(); + }), + ) .then( _ => - new Promise((resolve, reject) => + new Promise((resolve, reject) => { this.adb.pull(this.serial, recordingLocation).then(stream => { stream.on('end', resolve); stream.on('error', reject); stream.pipe(createWriteStream(destination)); - }), - ), + }); + }), ) .then(_ => destination); }