Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
LichKing-2234 committed Dec 2, 2023
1 parent f32389a commit 21852f1
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 184 deletions.
21 changes: 14 additions & 7 deletions example/src/renderer/components/RtcSurfaceView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
IMediaPlayer,
IRtcEngineEx,
RenderModeType,
RtcConnection,
VideoCanvas,
VideoMirrorModeType,
VideoSourceType,
VideoViewSetupMode,
createAgoraRtcEngine,
} from 'agora-electron-sdk';
import React, { Component } from 'react';
Expand All @@ -23,6 +25,12 @@ interface State {
uniqueId: number;
}

type SetupVideoFunc =
| typeof IRtcEngineEx.prototype.setupRemoteVideoEx
| typeof IRtcEngineEx.prototype.setupRemoteVideo
| typeof IRtcEngineEx.prototype.setupLocalVideo
| typeof IMediaPlayer.prototype.setView;

export class RtcSurfaceView extends Component<Props, State> {
constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -69,21 +77,18 @@ export class RtcSurfaceView extends Component<Props, State> {
this,
{
...canvas,
view: null,
setupMode: VideoViewSetupMode.VideoViewSetupRemove,
view: this.getHTMLElement(),
},
connection!
);
}

getSetupVideoFunc = () => {
getSetupVideoFunc = (): SetupVideoFunc => {
const { canvas, connection } = this.props;
const engine = createAgoraRtcEngine();

let func:
| typeof IRtcEngineEx.prototype.setupRemoteVideoEx
| typeof IRtcEngineEx.prototype.setupRemoteVideo
| typeof IRtcEngineEx.prototype.setupLocalVideo
| typeof IMediaPlayer.prototype.setView;
let func: SetupVideoFunc;

if (canvas.sourceType === undefined) {
if (canvas.uid) {
Expand Down Expand Up @@ -113,6 +118,8 @@ export class RtcSurfaceView extends Component<Props, State> {
this,
{
...canvas,
setupMode: VideoViewSetupMode.VideoViewSetupReplace,
renderMode: RenderModeType.RenderModeHidden,
mirrorMode: isMirror
? VideoMirrorModeType.VideoMirrorModeEnabled
: VideoMirrorModeType.VideoMirrorModeDisabled,
Expand Down
23 changes: 12 additions & 11 deletions ts/Private/internal/MediaPlayerInternal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createCheckers } from 'ts-interface-checker';

import { AgoraEnv } from '../../Utils';

import { ErrorCodeType } from '../AgoraBase';
import {
IAudioPcmFrameSink,
Expand Down Expand Up @@ -303,22 +302,24 @@ export class MediaPlayerInternal extends IMediaPlayerImpl {
}

override setView(view: HTMLElement): number {
const renderer = AgoraEnv.AgoraRendererManager?.addRendererToCache({
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const renderer = AgoraEnv.AgoraRendererManager.addOrRemoveRenderer({
sourceType: VideoSourceType.VideoSourceMediaPlayer,
uid: this._mediaPlayerId,
mediaPlayerId: this._mediaPlayerId,
view,
});
if (!renderer) return -ErrorCodeType.ErrNotInitialized;
if (!renderer) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}

override setRenderMode(renderMode: RenderModeType): number {
return (
AgoraEnv.AgoraRendererManager?.setRendererContext({
sourceType: VideoSourceType.VideoSourceMediaPlayer,
uid: this._mediaPlayerId,
renderMode,
}) ?? -ErrorCodeType.ErrNotInitialized
);
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const renderer = AgoraEnv.AgoraRendererManager.setRendererContext({
sourceType: VideoSourceType.VideoSourceMediaPlayer,
mediaPlayerId: this._mediaPlayerId,
renderMode,
});
if (!renderer) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}
}
143 changes: 47 additions & 96 deletions ts/Private/internal/RtcEngineExInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
SimulcastStreamMode,
VideoCanvas,
VideoMirrorModeType,
VideoViewSetupMode,
WatermarkOptions,
} from '../AgoraBase';
import {
Expand Down Expand Up @@ -189,7 +188,7 @@ export class RtcEngineExInternal extends IRtcEngineExImpl {
addListener<EventType extends keyof IRtcEngineEvent>(
eventType: EventType,
listener: IRtcEngineEvent[EventType]
): void {
) {
this._addListenerPreCheck(eventType);
const callback = (eventProcessor: EventProcessor<any>, data: any) => {
if (eventProcessor.type(data) !== EVENT_TYPE.IRtcEngine) {
Expand Down Expand Up @@ -578,107 +577,60 @@ export class RtcEngineExInternal extends IRtcEngineExImpl {
}

override setupLocalVideo(canvas: VideoCanvas): number {
let {
sourceType = VideoSourceType.VideoSourceCamera,
uid,
mediaPlayerId,
view,
renderMode,
mirrorMode,
setupMode = VideoViewSetupMode.VideoViewSetupAdd,
} = canvas;
if (
sourceType === VideoSourceType.VideoSourceMediaPlayer &&
mediaPlayerId !== undefined
) {
uid = mediaPlayerId;
}
const renderer = AgoraEnv.AgoraRendererManager?.addRendererToCache({
sourceType,
channelId: '',
uid,
view,
renderMode,
mirrorMode,
setupMode,
});
if (!renderer) return -ErrorCodeType.ErrNotInitialized;
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const renderer = AgoraEnv.AgoraRendererManager.addOrRemoveRenderer(canvas);
if (!renderer) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}

override setupRemoteVideo(canvas: VideoCanvas): number {
const {
sourceType = VideoSourceType.VideoSourceRemote,
uid,
view,
renderMode,
mirrorMode,
} = canvas;
const renderer = AgoraEnv.AgoraRendererManager?.addRendererToCache({
sourceType,
channelId: AgoraEnv.AgoraRendererManager?.defaultChannelId,
uid,
view,
renderMode,
mirrorMode,
});
if (!renderer) return -ErrorCodeType.ErrNotInitialized;
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const renderer = AgoraEnv.AgoraRendererManager.addOrRemoveRenderer(canvas);
if (!renderer) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}

override setupRemoteVideoEx(
canvas: VideoCanvas,
connection: RtcConnection
): number {
const {
sourceType = VideoSourceType.VideoSourceRemote,
uid,
view,
renderMode,
mirrorMode,
} = canvas;
const { channelId } = connection;
const renderer = AgoraEnv.AgoraRendererManager?.addRendererToCache({
sourceType,
channelId,
uid,
view,
renderMode,
mirrorMode,
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const renderer = AgoraEnv.AgoraRendererManager.addOrRemoveRenderer({
...canvas,
...connection,
});
if (!renderer) return -ErrorCodeType.ErrNotInitialized;
if (!renderer) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}

override setLocalRenderMode(
renderMode: RenderModeType,
mirrorMode: VideoMirrorModeType = VideoMirrorModeType.VideoMirrorModeAuto
): number {
return (
AgoraEnv.AgoraRendererManager?.setRendererContext({
sourceType: VideoSourceType.VideoSourceCamera,
channelId: '',
uid: 0,
renderMode,
mirrorMode,
}) ?? -ErrorCodeType.ErrNotInitialized
);
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const result = AgoraEnv.AgoraRendererManager.setRendererContext({
sourceType: VideoSourceType.VideoSourceCamera,
renderMode,
mirrorMode,
});
if (!result) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}

override setRemoteRenderMode(
uid: number,
renderMode: RenderModeType,
mirrorMode: VideoMirrorModeType
): number {
return (
AgoraEnv.AgoraRendererManager?.setRendererContext({
sourceType: VideoSourceType.VideoSourceRemote,
channelId: AgoraEnv.AgoraRendererManager?.defaultChannelId,
uid,
renderMode,
mirrorMode,
}) ?? -ErrorCodeType.ErrNotInitialized
);
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const result = AgoraEnv.AgoraRendererManager.setRendererContext({
sourceType: VideoSourceType.VideoSourceRemote,
uid,
renderMode,
mirrorMode,
});
if (!result) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}

override setRemoteRenderModeEx(
Expand All @@ -687,30 +639,29 @@ export class RtcEngineExInternal extends IRtcEngineExImpl {
mirrorMode: VideoMirrorModeType,
connection: RtcConnection
): number {
const { channelId } = connection;
return (
AgoraEnv.AgoraRendererManager?.setRendererContext({
sourceType: VideoSourceType.VideoSourceRemote,
channelId,
uid,
renderMode,
mirrorMode,
}) ?? -ErrorCodeType.ErrNotInitialized
);
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const result = AgoraEnv.AgoraRendererManager.setRendererContext({
sourceType: VideoSourceType.VideoSourceRemote,
...connection,
uid,
renderMode,
mirrorMode,
});
if (!result) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}

override setLocalVideoMirrorMode(mirrorMode: VideoMirrorModeType): number {
return (
AgoraEnv.AgoraRendererManager?.setRendererContext({
sourceType: VideoSourceType.VideoSourceCamera,
channelId: '',
uid: 0,
mirrorMode,
}) ?? -ErrorCodeType.ErrNotInitialized
);
if (!AgoraEnv.AgoraRendererManager) return -ErrorCodeType.ErrNotInitialized;
const result = AgoraEnv.AgoraRendererManager.setRendererContext({
sourceType: VideoSourceType.VideoSourceCamera,
mirrorMode,
});
if (!result) return -ErrorCodeType.ErrFailed;
return ErrorCodeType.ErrOk;
}

override destroyRendererByView(view: any) {
override destroyRendererByView(view: Element) {
AgoraEnv.AgoraRendererManager?.removeRendererFromCache({ view });
}

Expand Down
5 changes: 3 additions & 2 deletions ts/Renderer/AgoraView.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VideoMirrorModeType } from '../Private/AgoraBase';
import { VideoMirrorModeType, VideoViewSetupMode } from '../Private/AgoraBase';
import { RenderModeType, VideoSourceType } from '../Private/AgoraMediaBase';
import { AgoraEnv } from '../Utils';

Expand Down Expand Up @@ -143,7 +143,7 @@ export default class AgoraView extends HTMLElement {

initializeRender = () => {
const { channelId, uid, sourceType, renderMode, renderMirror } = this;
AgoraEnv.AgoraRendererManager?.addRendererToCache({
AgoraEnv.AgoraRendererManager?.addOrRemoveRenderer({
sourceType,
view: this,
uid,
Expand All @@ -152,6 +152,7 @@ export default class AgoraView extends HTMLElement {
mirrorMode: renderMirror
? VideoMirrorModeType.VideoMirrorModeEnabled
: VideoMirrorModeType.VideoMirrorModeDisabled,
setupMode: VideoViewSetupMode.VideoViewSetupReplace,
});
};

Expand Down
33 changes: 22 additions & 11 deletions ts/Renderer/IRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { VideoMirrorModeType } from '../Private/AgoraBase';
import { RenderModeType, VideoFrame } from '../Private/AgoraMediaBase';
import { RendererContext } from '../Types';

export type _RendererContext = Pick<
RendererContext,
'renderMode' | 'mirrorMode'
>;
type Context = Pick<RendererContext, 'renderMode' | 'mirrorMode'>;

export abstract class IRenderer {
parentElement?: HTMLElement;
container?: HTMLElement;
canvas?: HTMLCanvasElement;
context: _RendererContext = {};
_context: Context = {};

public bind(element: HTMLElement) {
this.parentElement = element;
Expand Down Expand Up @@ -47,7 +44,7 @@ export abstract class IRenderer {

public abstract drawFrame(videoFrame: VideoFrame): void;

public set rendererContext({ renderMode, mirrorMode }: RendererContext) {
public set context({ renderMode, mirrorMode }: Context) {
if (this.context.renderMode !== renderMode) {
this.context.renderMode = renderMode;
this.updateRenderMode();
Expand All @@ -59,6 +56,10 @@ export abstract class IRenderer {
}
}

public get context(): Context {
return this._context;
}

protected updateRenderMode() {
if (!this.canvas || !this.container) return;

Expand Down Expand Up @@ -98,9 +99,19 @@ export abstract class IRenderer {
});
}

protected abstract rotateCanvas({
width,
height,
rotation,
}: VideoFrame): void;
protected rotateCanvas({ width, height, rotation }: VideoFrame): void {
if (!this.canvas) return;

if (rotation === 0 || rotation === 180) {
this.canvas.width = width!;
this.canvas.height = height!;
} else if (rotation === 90 || rotation === 270) {
this.canvas.height = width!;
this.canvas.width = height!;
} else {
throw new Error(
`Invalid rotation: ${rotation}, only 0, 90, 180, 270 are supported`
);
}
}
}
Loading

0 comments on commit 21852f1

Please sign in to comment.