refactor screen recording

Summary:
moving logic for screen recordings to the respective devices, instead of having it in the button component.
This is part of my wider effort to unify our use of adb/adbkit and upgrade to the latest version of adbkit.

Reviewed By: passy

Differential Revision: D17318702

fbshipit-source-id: cff4459047d7a197ed6cb8ee8c290b4eaab41479
This commit is contained in:
Daniel Büchele
2019-09-13 05:25:36 -07:00
committed by Facebook Github Bot
parent b7ad035742
commit 01be3dc5d1
4 changed files with 127 additions and 146 deletions

View File

@@ -6,13 +6,15 @@
*/
import BaseDevice, {DeviceType, DeviceShell, LogLevel} from './BaseDevice';
import adb from 'adbkit-fb';
import {Priority} from 'adbkit-logcat-fb';
import child_process from 'child_process';
import {spawn} from 'promisify-child-process';
import ArchivedDevice from './ArchivedDevice';
import {ReadStream} from 'fs';
import {createWriteStream} from 'fs';
type ADBClient = any;
const DEVICE_RECORDING_DIR = '/sdcard/flipper_recorder';
export default class AndroidDevice extends BaseDevice {
constructor(
@@ -61,6 +63,7 @@ export default class AndroidDevice extends BaseDevice {
adb: ADBClient;
pidAppMapping: {[key: number]: string} = {};
logReader: any;
private recordingDestination?: string;
supportedColumns(): Array<string> {
return ['date', 'pid', 'tid', 'tag', 'message', 'type', 'time'];
@@ -102,7 +105,7 @@ export default class AndroidDevice extends BaseDevice {
screenshot(): Promise<Buffer> {
return new Promise((resolve, reject) => {
this.adb.screencap(this.serial).then((stream: ReadStream) => {
this.adb.screencap(this.serial).then((stream: NodeJS.WriteStream) => {
const chunks: Array<Buffer> = [];
stream
.on('data', (chunk: Buffer) => chunks.push(chunk))
@@ -113,4 +116,57 @@ export default class AndroidDevice extends BaseDevice {
});
});
}
async screenCaptureAvailable(): Promise<boolean> {
try {
await this.executeShell(
`[ ! -f /system/bin/screenrecord ] && echo "File does not exist"`,
);
return true;
} catch (_e) {
return false;
}
}
private async executeShell(command: string): Promise<void> {
const output = await this.adb
.shell(this.serial, command)
.then(adb.util.readAll)
.then((output: Buffer) => output.toString().trim());
if (output) {
throw new Error(output);
}
}
async startScreenCapture(destination: string) {
await this.executeShell(
`mkdir -p "${DEVICE_RECORDING_DIR}" && echo -n > "${DEVICE_RECORDING_DIR}/.nomedia"`,
);
this.recordingDestination = destination;
const recordingLocation = `${DEVICE_RECORDING_DIR}/video.mp4`;
this.adb
.shell(this.serial, `screenrecord --bugreport "${recordingLocation}"`)
.then(
() =>
new Promise((resolve, reject) =>
this.adb
.pull(this.serial, recordingLocation)
.then((stream: NodeJS.WriteStream) => {
stream.on('end', resolve);
stream.on('error', reject);
stream.pipe(createWriteStream(destination));
}),
),
);
}
async stopScreenCapture(): Promise<string> {
const {recordingDestination} = this;
if (!recordingDestination) {
return Promise.reject(new Error('Recording was not properly started'));
}
this.recordingDestination = undefined;
await this.adb.shell(this.serial, `pgrep 'screenrecord' -L 2`);
return recordingDestination;
}
}