Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/linux desktop capture #478

Merged
merged 6 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import {
DefaultDeviceSystemInfo,
DeviceSystemInfo,
DeviceWindowInfo,
ErrorResult,
FilledRuntimeInfo,
Platform,
PrivateProtocol,
ProfileMethod,
RuntimeInfo,
ScreenRecordOption,
Serial,
StreamingAnswer,
} from '@dogu-private/types';
import { Closable, Printable, PromiseOrValue, stringify } from '@dogu-tech/common';
import { BrowserInstallation, StreamingOfferDto } from '@dogu-tech/device-client-common';
import { ChildProcess, isFreePort } from '@dogu-tech/node';
import { Observable } from 'rxjs';
import systeminformation from 'systeminformation';
import { AppiumContext, AppiumContextKey } from '../../appium/appium.context';
import { DeviceWebDriverHandler } from '../../device-webdriver/device-webdriver.common';
import { SeleniumDeviceWebDriverHandler } from '../../device-webdriver/selenium.device-webdriver.handler';
import { GamiumContext } from '../../gamium/gamium.context';
import { logger } from '../../logger/logger.instance';
import { DesktopCapturer } from '../externals/index';
import { DeviceChannel, DeviceChannelOpenParam, DeviceHealthStatus, DeviceServerService, LogHandler } from '../public/device-channel';
import { DeviceAgentService } from '../services/device-agent/device-agent-service';
import { NullDeviceAgentService } from '../services/device-agent/null-device-agent-service';
import { DesktopProfileService } from '../services/profile/desktop-profiler';
import { ProfileService } from '../services/profile/profile-service';
import { StreamingService } from '../services/streaming/streaming-service';
import { checkTime } from '../util/check-time';

type DeviceControl = PrivateProtocol.DeviceControl;

export class LinuxChannel implements DeviceChannel {
constructor(
private readonly _serial: Serial,
private readonly _info: DeviceSystemInfo,
private readonly _profile: ProfileService,
private readonly _streaming: StreamingService,
private readonly _deviceAgent: DeviceAgentService,
private readonly _seleniumDeviceWebDriverHandler: SeleniumDeviceWebDriverHandler,
readonly browserInstallations: BrowserInstallation[],
) {}

get serial(): Serial {
return this._serial;
}

get serialUnique(): string {
return this._serial;
}

get platform(): Platform {
return Platform.PLATFORM_LINUX;
}

get info(): DeviceSystemInfo {
return this._info;
}

get isVirtual(): boolean {
return this._info.isVirtual;
}

static async create(param: DeviceChannelOpenParam, streaming: StreamingService, deviceServerService: DeviceServerService): Promise<DeviceChannel> {
const platform = Platform.PLATFORM_LINUX;
const deviceAgent = new NullDeviceAgentService();

const osInfo = await checkTime('os', systeminformation.osInfo());
const info: DeviceSystemInfo = {
...DefaultDeviceSystemInfo(),
nickname: osInfo.hostname,
version: osInfo.release,
system: await checkTime('system', systeminformation.system()),
os: { ...osInfo, platform },
uuid: await checkTime('uuid', systeminformation.uuid()),
cpu: await checkTime('cpu', systeminformation.cpu()),
};
await streaming.deviceConnected(param.serial, {
serial: param.serial,
platform,
screenUrl: deviceAgent.screenUrl,
inputUrl: deviceAgent.inputUrl,
screenWidth: 0 < info.graphics.displays.length ? info.graphics.displays[0].resolutionX : 0,
screenHeight: 0 < info.graphics.displays.length ? info.graphics.displays[0].resolutionY : 0,
});
const seleniumDeviceWebDriverHandler = new SeleniumDeviceWebDriverHandler(
platform,
param.serial,
deviceServerService.seleniumService,
deviceServerService.httpRequestRelayService,
deviceServerService.seleniumEndpointHandlerService,
deviceServerService.browserManagerService,
deviceServerService.doguLogger,
);

const deviceChannel = new LinuxChannel(param.serial, info, new DesktopProfileService(), streaming, deviceAgent, seleniumDeviceWebDriverHandler, []);
return Promise.resolve(deviceChannel);
}

async queryProfile(methods: ProfileMethod[] | ProfileMethod): Promise<FilledRuntimeInfo> {
const methodList = Array.isArray(methods) ? methods : [methods];
const result = await this._profile.profile(this.serial, methodList);
return {
...RuntimeInfo.fromPartial(result),
platform: Platform.PLATFORM_LINUX,
localTimeStamp: new Date(),
};
}

async startStreamingWebRtcWithTrickle(offer: StreamingOfferDto): Promise<Observable<StreamingAnswer>> {
return Promise.resolve(this._streaming.startStreamingWithTrickle(this.serial, offer));
}

async startRecord(option: ScreenRecordOption): Promise<ErrorResult> {
return Promise.resolve(this._streaming.startRecord(this.serial, option));
}

async stopRecord(filePath: string): Promise<ErrorResult> {
return Promise.resolve(this._streaming.stopRecord(this.serial, filePath));
}

control(control: DeviceControl): void {
throw new Error('Method not implemented.');
}

turnScreen(isOn: boolean): void {
throw new Error('Method not implemented.');
}

reboot(): void {
throw new Error('Method not implemented.');
}

checkHealth(): DeviceHealthStatus {
return { isHealthy: true, message: '' };
}

async killOnPort(port: number): Promise<void> {
await ChildProcess.exec(`lsof -i tcp:${port} | grep LISTEN | awk '{print $2}' | xargs kill -9`, {}, logger);
}

forward(hostPort: number, devicePort: number, printable?: Printable): void {
// noop
}

unforward(hostPort: number): void {
// noop
}

async isPortListening(port: number): Promise<boolean> {
const isFree = await isFreePort(port);
return !isFree;
}

async getWindows(): Promise<DeviceWindowInfo[]> {
return await DesktopCapturer.getWindows(logger);
}

uninstallApp(appPath: string): void {
throw new Error('Method not implemented.');
}

installApp(appPath: string): void {
throw new Error('Method not implemented.');
}

runApp(appPath: string): void {
ChildProcess.spawn(appPath, [], {}, logger).catch((err) => {
logger.error(`failed to start app`, { error: stringify(err) });
});
}

subscribeLog(args: string[], handler: LogHandler, printable?: Printable | undefined): PromiseOrValue<Closable> {
throw new Error('Method not implemented.');
}

reset(): PromiseOrValue<void> {
throw new Error('Method not implemented.');
}

joinWifi(ssid: string, password: string): PromiseOrValue<void> {
throw new Error('Method not implemented.');
}

getAppiumContext(): null {
return null;
}

switchAppiumContext(key: AppiumContextKey): PromiseOrValue<AppiumContext> {
throw new Error('Method not implemented.');
}

getAppiumCapabilities(): null {
return null;
}

set gamiumContext(context: GamiumContext | null) {
throw new Error('Method not implemented.');
}

get gamiumContext(): GamiumContext | null {
return null;
}

getWebDriverHandler(): DeviceWebDriverHandler | null {
return this._seleniumDeviceWebDriverHandler;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Platform, Serial } from '@dogu-private/types';
import systeminformation from 'systeminformation';
import { env } from '../../env';
import { createGdcLogger, idcLogger } from '../../logger/logger.instance';
import { LinuxChannel } from '../channel/linux-channel';
import { DeviceChannel, DeviceChannelOpenParam, DeviceServerService } from '../public/device-channel';
import { DeviceDriver, DeviceScanResult } from '../public/device-driver';
import { PionStreamingService } from '../services/streaming/pion-streaming-service';
import { StreamingService } from '../services/streaming/streaming-service';

export class LinuxDriver implements DeviceDriver {
private constructor(private readonly streamingService: StreamingService, private readonly deviceServerService: DeviceServerService) {}

static async create(deviceServerService: DeviceServerService): Promise<LinuxDriver> {
const streaming = await PionStreamingService.create(Platform.PLATFORM_LINUX, env.DOGU_DEVICE_SERVER_PORT, createGdcLogger(Platform.PLATFORM_LINUX));
return new LinuxDriver(streaming, deviceServerService);
}

get platform(): Platform {
return Platform.PLATFORM_LINUX;
}

async scanSerials(): Promise<DeviceScanResult[]> {
const hostname = (await systeminformation.osInfo()).hostname;
const uuid = await systeminformation.uuid();
return [{ serial: uuid.os, status: 'online', name: hostname }];
}

async openChannel(initParam: DeviceChannelOpenParam): Promise<DeviceChannel> {
return await LinuxChannel.create(initParam, this.streamingService, this.deviceServerService);
}

async closeChannel(seial: Serial): Promise<void> {
return await this.streamingService.deviceDisconnected(seial);
}

async reset(): Promise<void> {
idcLogger.warn('LinuxDriver.reset is not implemented');
return await Promise.resolve();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Platform, PlatformType } from '@dogu-private/types';
import { AndroidDriver } from '../driver/android-driver';
import { IosDriver } from '../driver/ios-driver';
import { LinuxDriver } from '../driver/linux-driver';
import { MacosDriver } from '../driver/macos-driver';
import { NullDeviceDriver } from '../driver/null-device-driver';
import { WindowsDriver } from '../driver/windows-driver';
Expand Down Expand Up @@ -38,6 +39,22 @@ export class MacOSDeviceDriverFactory implements DeviceDriverFactory {
}
}

export class LinuxDeviceDriverFactory implements DeviceDriverFactory {
constructor(private readonly enabledPlatforms: readonly PlatformType[], private readonly deviceServerService: DeviceServerService) {}

async create(): Promise<Map<Platform, DeviceDriver>> {
const map = new Map<Platform, DeviceDriver>();
if (this.enabledPlatforms.includes('linux')) {
map.set(Platform.PLATFORM_LINUX, await LinuxDriver.create(this.deviceServerService));
}
if (this.enabledPlatforms.includes('android')) {
map.set(Platform.PLATFORM_ANDROID, await AndroidDriver.create(this.deviceServerService));
}

return map;
}
}

export class WindowsDeviceDriverFactory implements DeviceDriverFactory {
constructor(private readonly enabledPlatforms: readonly PlatformType[], private readonly deviceServerService: DeviceServerService) {}

Expand All @@ -63,6 +80,8 @@ export function createDeviceDriverFactoryByHostPlatform(
return new MacOSDeviceDriverFactory(enabledPlatforms, deviceServerService);
case Platform.PLATFORM_WINDOWS:
return new WindowsDeviceDriverFactory(enabledPlatforms, deviceServerService);
case Platform.PLATFORM_LINUX:
return new LinuxDeviceDriverFactory(enabledPlatforms, deviceServerService);
default:
return new NullDeviceDriverFactory();
}
Expand All @@ -74,6 +93,8 @@ export async function createDeviceDriverByDevicePlatform(platform: Platform, dev
return await MacosDriver.create(deviceServerService);
case Platform.PLATFORM_WINDOWS:
return await WindowsDriver.create(deviceServerService);
case Platform.PLATFORM_LINUX:
return await LinuxDriver.create(deviceServerService);
case Platform.PLATFORM_ANDROID:
return await AndroidDriver.create(deviceServerService);
case Platform.PLATFORM_IOS:
Expand Down
4 changes: 3 additions & 1 deletion projects/go-device-controller/desktop-capturer/code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "args/args.hxx"
#include "myWindows.h"
#include "mywebrtc.h"
#include "webrtcUtil.h"

#include <iostream>

Expand Down Expand Up @@ -97,7 +98,8 @@ void WindowCommand(args::Subparser &parser)
if (info)
{
std::string json;
mywindows::getInfosString(json);
auto infos = webrtcUtil::getWindowInfos();
mywindows::getInfosString(infos, json);
std::cout << json << std::endl << std::flush;
}
}
Expand Down
Loading