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

feat: support electron 25+ #1122

Closed
wants to merge 4 commits into from
Closed
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
13 changes: 7 additions & 6 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,22 @@
]
},
"dependencies": {
"@lwahonen/ffi-napi": "^4.0.12",
"@lwahonen/ref-napi": "^4.0.8",
"agora-electron-sdk": "4.2.6",
"antd": "^4.20.3",
"download": "^8.0.0",
"ffi-napi": "^4.0.3",
"react": "^18.1.0",
"react-color": "^2.19.3",
"react-dom": "^18.1.0",
"react-router-dom": "^5.2.0",
"recharts": "^2.1.13",
"ref-napi": "^3.0.3",
"semver": "^7.5.4",
"source-map-support": "^0.5.16"
},
"devDependencies": {
"@babel/preset-react": "^7.16.7",
"@electron/rebuild": "^3.2.10",
"@electron/rebuild": "^3.3.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.6",
"@teamsupercell/typings-for-css-modules-loader": "^2.5.1",
"@types/download": "^8.0.2",
Expand All @@ -94,8 +95,8 @@
"@types/react-dom": "^18.0.3",
"@types/react-router-dom": "^5.1.6",
"@types/ref-napi": "^3.0.7",
"electron": "18.2.3",
"electron-builder": "^23.1.0",
"electron": "26.4.0",
"electron-builder": "^24.8.0",
"electron-webpack": "^2.8.2",
"fork-ts-checker-webpack-plugin": "^4.1.2",
"react-refresh": "^0.13.0",
Expand All @@ -104,6 +105,6 @@
"style-loader": "^2.0.0",
"ts-loader": "^8.4.0",
"typescript": "^4.5.2",
"webpack": "^4.43.0"
"webpack": "^4.47.0"
}
}
63 changes: 63 additions & 0 deletions example/src/main/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import path from 'path';
import { format as formatUrl } from 'url';

import createAgoraRtcEngine, {
ScreenCaptureSourceType,
} from 'agora-electron-sdk';
import { BrowserWindow, app, ipcMain, systemPreferences } from 'electron';

const isDevelopment = process.env.NODE_ENV !== 'production';
Expand Down Expand Up @@ -50,6 +53,66 @@ function createMainWindow() {
return await systemPreferences.askForMediaAccess(arg.type);
}
});
// Fix for https://bugs.chromium.org/p/chromium/issues/detail?id=306348
ipcMain.handle('IPC_AGORA_RTC_INITIALIZE', (_, { appId }) => {
const engine = createAgoraRtcEngine({ webEnvReady: false });
const result = engine.initialize({ appId });
engine.enableVideo();
engine.addListener(
'onVideoDeviceStateChanged',
(deviceId, deviceType, deviceState) => {
window.webContents.send(
'IPC_AGORA_RTC_VIDEO_DEVICE_STATE_CHANGED',
deviceId,
deviceType,
deviceState
);
}
);
return result;
});
ipcMain.handle('IPC_AGORA_RTC_RELEASE', () => {
const engine = createAgoraRtcEngine();
return engine.release();
});
ipcMain.handle('IPC_AGORA_RTC_ENUMERATE_VIDEO_DEVICES', () => {
const engine = createAgoraRtcEngine();
return engine.getVideoDeviceManager().enumerateVideoDevices();
});
ipcMain.handle(
'IPC_AGORA_RTC_START_SCREEN_CAPTURE',
(_, { source, captureParams }) => {
const engine = createAgoraRtcEngine();
let result;
if (
source.type === ScreenCaptureSourceType.ScreencapturesourcetypeScreen
) {
result = engine.startScreenCaptureByDisplayId(
source.sourceId,
{},
captureParams
);
} else {
result = engine.startScreenCaptureByWindowId(
source.sourceId,
{},
captureParams
);
}
return result;
}
);
ipcMain.handle(
'IPC_AGORA_RTC_UPDATE_SCREEN_CAPTURE_PARAMETERS',
(_, { captureParams }) => {
const engine = createAgoraRtcEngine();
return engine.updateScreenCaptureParameters(captureParams);
}
);
ipcMain.handle('IPC_AGORA_RTC_STOP_SCREEN_CAPTURE', () => {
const engine = createAgoraRtcEngine();
return engine.stopScreenCapture();
});
});

window.webContents.on('devtools-opened', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
VideoSourceType,
createAgoraRtcEngine,
} from 'agora-electron-sdk';
import { ipcRenderer } from 'electron';
import React, { ReactElement } from 'react';
import semver from 'semver';

import {
BaseComponent,
Expand Down Expand Up @@ -131,16 +133,36 @@ export default class DeviceManager
/**
* Step 3-1: enumerateDevices
*/
enumerateDevices = () => {
enumerateDevices = async () => {
const playbackDevices = this.engine
?.getAudioDeviceManager()
.enumeratePlaybackDevices();
const recordingDevices = this.engine
?.getAudioDeviceManager()
.enumerateRecordingDevices();
const videoDevices = this.engine
?.getVideoDeviceManager()
.enumerateVideoDevices();
let videoDevices;
if (
semver.gt(process.versions.electron, '25.0.0') &&
process.platform === 'darwin'
) {
ipcRenderer.on(
'IPC_AGORA_RTC_VIDEO_DEVICE_STATE_CHANGED',
(_, deviceId: string, deviceType: number, deviceState: number) => {
this.onVideoDeviceStateChanged(deviceId, deviceType, deviceState);
}
);

await ipcRenderer.invoke('IPC_AGORA_RTC_INITIALIZE', {
appId: Config.appId,
});
videoDevices = await ipcRenderer.invoke(
'IPC_AGORA_RTC_ENUMERATE_VIDEO_DEVICES'
);
} else {
videoDevices = this.engine
?.getVideoDeviceManager()
.enumerateVideoDevices();
}

this.setState({
playbackDevices,
Expand Down Expand Up @@ -233,6 +255,7 @@ export default class DeviceManager
* Step 5: releaseRtcEngine
*/
protected releaseRtcEngine() {
ipcRenderer.invoke('IPC_AGORA_RTC_RELEASE');
this.engine?.unregisterEventHandler(this);
this.engine?.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import os from 'os';
import path from 'path';

import ffi, {
LibraryObject,
LibraryObjectDefinitionToLibraryDefinition,
} from '@lwahonen/ffi-napi';

Check failure on line 8 in example/src/renderer/examples/advanced/ProcessVideoRawData/ProcessVideoRawData.tsx

View workflow job for this annotation

GitHub Actions / lint

Could not find a declaration file for module '@lwahonen/ffi-napi'. '/home/runner/work/Electron-SDK/Electron-SDK/example/node_modules/@lwahonen/ffi-napi/lib/ffi.js' implicitly has an 'any' type.
import {
ChannelProfileType,
ClientRoleType,
IRtcEngineEventHandler,
createAgoraRtcEngine,
} from 'agora-electron-sdk';
import download from 'download';
import ffi, {
LibraryObject,
LibraryObjectDefinitionToLibraryDefinition,
} from 'ffi-napi';
import React, { ReactElement } from 'react';

import {
Expand Down
104 changes: 79 additions & 25 deletions example/src/renderer/examples/advanced/ScreenShare/ScreenShare.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
RenderModeType,
RtcConnection,
RtcStats,
ScreenCaptureParameters,
ScreenCaptureSourceInfo,
ScreenCaptureSourceType,
UserOfflineReasonType,
VideoSourceType,
createAgoraRtcEngine,
} from 'agora-electron-sdk';
import { ipcRenderer } from 'electron';
import React, { ReactElement } from 'react';
import { SketchPicker } from 'react-color';
import semver from 'semver';

import {
BaseComponent,
Expand Down Expand Up @@ -167,7 +170,7 @@
/**
* Step 3-2: startScreenCapture
*/
startScreenCapture = () => {
startScreenCapture = async () => {
const {
targetSource,
width,
Expand All @@ -187,47 +190,72 @@
return;
}

let captureParams: ScreenCaptureParameters = {};
if (
targetSource.type ===
ScreenCaptureSourceType.ScreencapturesourcetypeScreen
) {
captureParams = {
dimensions: { width, height },
frameRate,
bitrate,
captureMouseCursor,
excludeWindowList,
excludeWindowCount: excludeWindowList.length,
highLightWidth,
highLightColor,
enableHighLight,
};
} else {
captureParams = {
dimensions: { width, height },
frameRate,
bitrate,
windowFocus,
highLightWidth,
highLightColor,
enableHighLight,
};
}

if (
semver.gt(process.versions.electron, '25.0.0') &&
process.platform === 'darwin'
) {
await ipcRenderer.invoke('IPC_AGORA_RTC_INITIALIZE', {
appId: Config.appId,
});
await ipcRenderer.invoke('IPC_AGORA_RTC_START_SCREEN_CAPTURE', {
source: targetSource,
captureParams,
});
}

// Keep screen capture in the renderer process
if (
targetSource.type ===
ScreenCaptureSourceType.ScreencapturesourcetypeScreen
) {
this.engine?.startScreenCaptureByDisplayId(
targetSource.sourceId,
{},
{
dimensions: { width, height },
frameRate,
bitrate,
captureMouseCursor,
excludeWindowList,
excludeWindowCount: excludeWindowList.length,
highLightWidth,
highLightColor,
enableHighLight,
}
captureParams
);
} else {
this.engine?.startScreenCaptureByWindowId(
targetSource.sourceId,
{},
{
dimensions: { width, height },
frameRate,
bitrate,
windowFocus,
highLightWidth,
highLightColor,
enableHighLight,
}
captureParams
);
}

this.setState({ startScreenCapture: true });
};

/**
* Step 3-2 (Optional): updateScreenCaptureParameters
*/
updateScreenCaptureParameters = () => {
updateScreenCaptureParameters = async () => {
const {
width,
height,
Expand All @@ -240,7 +268,8 @@
highLightColor,
enableHighLight,
} = this.state;
this.engine?.updateScreenCaptureParameters({

const captureParams: ScreenCaptureParameters = {
dimensions: { width, height },
frameRate,
bitrate,
Expand All @@ -251,7 +280,22 @@
highLightWidth,
highLightColor,
enableHighLight,
});
};

if (
semver.gt(process.versions.electron, '25.0.0') &&
process.platform === 'darwin'
) {
await ipcRenderer.invoke(
'IPC_AGORA_RTC_UPDATE_SCREEN_CAPTURE_PARAMETERS',
{
captureParams,
}
);
}

// Keep screen capture in the renderer process
this.engine?.updateScreenCaptureParameters(captureParams);
};

/**
Expand Down Expand Up @@ -286,8 +330,18 @@
/**
* Step 3-5: stopScreenCapture
*/
stopScreenCapture = () => {
stopScreenCapture = async () => {
if (
semver.gt(process.versions.electron, '25.0.0') &&
process.platform === 'darwin'
) {
await ipcRenderer.invoke('IPC_AGORA_RTC_STOP_SCREEN_CAPTURE');
await ipcRenderer.invoke('IPC_AGORA_RTC_RELEASE');
}

// Keep screen capture in the renderer process
this.engine?.stopScreenCapture();

this.setState({ startScreenCapture: false });
};

Expand Down Expand Up @@ -542,7 +596,7 @@
};
})}
value={excludeWindowList}
onValueChange={(value, index) => {

Check warning on line 599 in example/src/renderer/examples/advanced/ScreenShare/ScreenShare.tsx

View workflow job for this annotation

GitHub Actions / lint

'index' is defined but never used. Allowed unused args must match /^_/u
if (excludeWindowList.indexOf(value) === -1) {
this.setState((preState) => {
return {
Expand Down
4 changes: 2 additions & 2 deletions example/webpack.renderer.additions.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ module.exports = function (config) {
// ...config.externals,
'webpack',
'agora-electron-sdk',
'ffi-napi',
'ref-napi',
'@lwahonen/ffi-napi',
'@lwahonen/ref-napi',
];
console.log('config', config.module.rules);
return config;
Expand Down
Loading
Loading