Skip to content
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
@@ -1,15 +1,8 @@
import { PropertiesManager } from './properties-manager';
import { Inject } from 'services/core/injector';
import { WebsocketService } from 'services/websocket';
import * as obs from '../../../../obs-api';
import { Subscription } from 'rxjs';
import { VisionService } from 'services/vision';
import uuid from 'uuid/v4';

interface ISourceMessage {
sourceName: string;
message: any;
}

export class SmartBrowserSourceManager extends PropertiesManager {
@Inject() private websocketService: WebsocketService;
Expand All @@ -18,27 +11,6 @@ export class SmartBrowserSourceManager extends PropertiesManager {
private sseSub!: Subscription;

init() {
obs.NodeObs.RegisterSourceMessageCallback(async (evt: ISourceMessage[]) => {
console.log('SmartBrowserSourceManager: Received source message', evt);
for (const { sourceName, message } of evt) {
if (sourceName !== this.obsSource.name) {
continue;
}
const keys = JSON.parse(message).keys;
const tree = this.convertDotNotationToTree(keys);
const res = await this.visionService.requestState({ query: tree });
const payload = JSON.stringify({
type: 'state.update',
message: res,
key: keys?.join(','),
event_id: uuid(),
});
console.log('SmartBrowserSourceManager: Sending message to source', sourceName, payload);
this.obsSource.sendMessage({
message: payload,
});
}
});
this.socketSub = this.websocketService.socketEvent.subscribe(e => {
console.log('WS event', e);

Expand All @@ -55,20 +27,4 @@ export class SmartBrowserSourceManager extends PropertiesManager {
this.socketSub?.unsubscribe();
this.sseSub?.unsubscribe();
}

private convertDotNotationToTree(states: string[] | string): any {
const tree: any = {};
const stateArray = Array.isArray(states) ? states : [states];
stateArray.forEach(state => {
const parts = state.split('.');
let current = tree;
parts.forEach((part, index) => {
if (!current[part]) {
current[part] = index === parts.length - 1 ? true : {};
}
current = current[part];
});
});
return tree;
}
}
37 changes: 35 additions & 2 deletions app/services/vision/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChildProcess, spawn } from 'child_process';
import { Inject, Service } from 'services';
import { InitAfter, Inject, Service } from 'services';
import * as remote from '@electron/remote';
import path from 'path';
import { authorizedHeaders, downloadFile, IDownloadProgress, jfetch } from 'util/requests';
Expand All @@ -8,12 +8,14 @@ import { OutputStreamHandler } from 'services/platform-apps/api/modules/native-c
import crypto from 'crypto';
import { importExtractZip } from 'util/slow-imports';
import { pipeline } from 'stream/promises';
import { HostsService, SettingsService, UserService } from 'app-services';
import { HostsService, SourcesService, SettingsService, UserService } from 'app-services';
import { RealmObject } from 'services/realm';
import { ObjectSchema } from 'realm';
import http from 'http';
import { AddressInfo } from 'net';
import uuid from 'uuid/v4';
import * as obs from '../../../obs-api';
import { convertDotNotationToTree } from 'util/dot-tree';

interface IVisionManifest {
version: string;
Expand Down Expand Up @@ -51,6 +53,7 @@ export class VisionState extends RealmObject {

VisionState.register();

@InitAfter('UserService')
export class VisionService extends Service {
public basepath: string;

Expand All @@ -62,6 +65,7 @@ export class VisionService extends Service {

@Inject() userService: UserService;
@Inject() hostsService: HostsService;
@Inject() private sourcesService: SourcesService;
@Inject() settingsService: SettingsService;

state = VisionState.inject();
Expand All @@ -71,6 +75,35 @@ export class VisionService extends Service {
init() {
this.basepath = path.join(remote.app.getPath('userData'), '..', 'streamlabs-vision');
this.manifestPath = path.resolve(this.basepath, 'manifest.json');


obs.NodeObs.RegisterSourceMessageCallback(async (evt: { sourceName: string; message: any; }[]) => {
console.log("SmartBrowserSourceManager: Received source message", evt);

for (const { sourceName, message } of evt) {
const source = this.sourcesService.views.getSource(sourceName)?.getObsInput();

if (!source) {
continue;
}

const keys = JSON.parse(message).keys;
const tree = convertDotNotationToTree(keys);
const res = await this.requestState({ query: tree });
const payload = JSON.stringify({
type: 'state.update',
message: res,
key: keys?.join(","),
event_id: uuid(),
});

console.log("SmartBrowserSourceManager: Sending message to source", sourceName, payload);

source.sendMessage({
message: payload
});
}
});
}

/**
Expand Down
15 changes: 15 additions & 0 deletions app/util/dot-tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function convertDotNotationToTree(states: string[] | string): any {
const tree: any = {};
const stateArray = Array.isArray(states) ? states : [states];
stateArray.forEach(state => {
const parts = state.split('.');
let current = tree;
parts.forEach((part, index) => {
if (!current[part]) {
current[part] = index === parts.length - 1 ? true : {};
}
current = current[part];
});
});
return tree;
}
Loading