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;
}
}

View File

@@ -152,4 +152,16 @@ export default class BaseDevice {
new Error('No screenshot support for current device'),
);
}
async screenCaptureAvailable(): Promise<boolean> {
return false;
}
async startScreenCapture(destination: string) {
throw new Error('startScreenCapture not implemented on BaseDevice ');
}
async stopScreenCapture(): Promise<string | null> {
return null;
}
}

View File

@@ -6,7 +6,7 @@
*/
import {DeviceType, LogLevel, DeviceLogEntry} from './BaseDevice';
import child_process from 'child_process';
import child_process, {ChildProcess} from 'child_process';
import BaseDevice from './BaseDevice';
import JSONStream from 'JSONStream';
import {Transform} from 'stream';
@@ -39,6 +39,8 @@ type RawLogEntry = {
export default class IOSDevice extends BaseDevice {
log: any;
buffer: string;
private recordingProcess?: ChildProcess;
private recordingLocation?: string;
constructor(serial: string, deviceType: DeviceType, title: string) {
super(serial, deviceType, title, 'iOS');
@@ -138,11 +140,11 @@ export default class IOSDevice extends BaseDevice {
static parseLogEntry(entry: RawLogEntry): DeviceLogEntry {
const LOG_MAPPING: Map<IOSLogLevel, LogLevel> = new Map([
['Default', 'debug'],
['Info', 'info'],
['Debug', 'debug'],
['Error', 'error'],
['Fault', 'fatal'],
['Default' as IOSLogLevel, 'debug' as LogLevel],
['Info' as IOSLogLevel, 'info' as LogLevel],
['Debug' as IOSLogLevel, 'debug' as LogLevel],
['Error' as IOSLogLevel, 'error' as LogLevel],
['Fault' as IOSLogLevel, 'fatal' as LogLevel],
]);
let type: LogLevel = LOG_MAPPING.get(entry.messageType) || 'unknown';
@@ -174,6 +176,27 @@ export default class IOSDevice extends BaseDevice {
type,
};
}
async screenCaptureAvailable() {
return this.deviceType === 'emulator';
}
async startScreenCapture(destination: string) {
this.recordingProcess = exec(
`xcrun simctl io booted recordVideo "${destination}"`,
);
this.recordingLocation = destination;
}
async stopScreenCaputre(): Promise<string | null> {
if (this.recordingProcess && this.recordingLocation) {
this.recordingProcess.kill('SIGINT');
const {recordingLocation} = this;
this.recordingLocation = undefined;
return recordingLocation;
}
return null;
}
}
// Used to strip the initial output of the logging utility where it prints out settings.