trtc-electron-sdk
Version:
trtc electron sdk
306 lines (305 loc) • 14 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from 'events';
import { TRTCVideoStreamType, TRTCScreenCaptureSourceInfo, Rect, TRTCVideoPixelFormat, TRTCCameraCaptureParams, TRTCScreenCaptureProperty } from '../trtc_define';
import { TRTCLocalMediaTranscodingParams, ITRTCLocalMediaTranscoder } from './types';
export { TRTCLocalMediaTranscodingError, TRTCLocalMediaTranscodingSourceType, TRTCLocalMediaTranscodingSource, TRTCLocalMediaTranscodingParams } from './types';
/**
* @description 本地媒体流混流转码(目前只支持 Windows)
*
* @extends EventEmitter
*
* @example
* // 运行示例
* import TRTCCloud, {
* TRTCCameraCaptureMode,
* TRTCVideoRotation,
* TRTCVideoFillMode,
* TRTCVideoMirrorType,
* TRTCVideoStreamType,
* TRTCLocalMediaTranscodingSourceType,
* TRTCVideoResolution,
* TRTCVideoResolutionMode,
* TRTCVideoPixelFormat
* } from 'trtc-electron-sdk';
*
* const trtcCloud = TRTCCloud.getTRTCShareInstance();
* const cameraList =trtcCloud.getCameraDevicesList();
*
* // 获取本地合图转码推流对象
* const localMediaTranscoder =trtcCloud.createLocalMediaTranscoder({
* pixelFormat: TRTCVideoPixelFormat.TRTCVideoPixelFormat_I420,
* });
*
* // 注册事件监听
* // 监听合图开始、结束事件
* localMediaTranscoder.on('onTranscodingStarted', (errCode: number, errMsg: string) => {
* console.log(`onTranscodingStarted { errCode:${errCode} errMsg:${errMsg}`);
* });
* localMediaTranscoder.on('onTranscodingStopped', (reason: number, reasonMsg: string) => {
* console.log(`onTranscodingStopped { reason:${reason} reasonMsg:${reasonMsg} }`);
* });
* // 监听摄像头事件
* localMediaTranscoder.on('onCameraSourceStarted', (deviceId: string, errCode: number, errMsg: string) => {
* console.log(`onCameraSourceStarted { deviceId:${deviceId} errCode:${errCode} errMsg:${errMsg} }`);
* });
* localMediaTranscoder.on('onCameraSourceStopped', (deviceId: string, reasonMsg: string) => {
* console.log(`onCameraSourceStopped { deviceId:${deviceId} reasonMsg:${reasonMsg}}`);
* });
* // 监听屏幕、窗口分享事件
* localMediaTranscoder.on('onScreenSourceStarted', (sourceId: string, errCode: number, errMsg: string) => {
* console.log(`onScreenSourceStarted { sourceId:${sourceId} errCode:${errCode} errMsg:${errMsg} }`);
* });
* localMediaTranscoder.on('onScreenSourceStopped', (sourceId: string, reason: number) => {
* console.log(`onScreenSourceStopped { sourceId:${sourceId}, reason:${reason} }`);
* });
* // 监听图片事件
* localMediaTranscoder.on('onImageSourceStarted', (imagePath: string, errCode: number, errMsg: string) => {
* console.log(`onImageSourceStarted { imagePath:${imagePath} errCode:${errCode} errMsg:${errMsg} }`);
* });
* localMediaTranscoder.on('onImageSourceStopped', (imagePath: string) => {
* console.log(`onImageSourceStopped { imagePath:${imagePath} }`);
* });
*
* // 开启摄像头一
* localMediaTranscoder.startCameraSource(cameraList[0].deviceId);
* localMediaTranscoder.setCameraCaptureParams(
* cameraList[0].deviceId,
* {
* mode: TRTCCameraCaptureMode.TRTCCameraResolutionStrategyAuto,
* width: 480,
* height: 480,
* }
* );
*
* // 开启摄像头二
* localMediaTranscoder.startCameraSource(cameraList[1].deviceId);
* localMediaTranscoder.setCameraCaptureParams(
* cameraList[1].deviceId,
* {
* mode: TRTCCameraCaptureMode.TRTCCameraResolutionStrategyAuto,
* width: 960,
* height: 540
* }
* );
*
*
* // 开启屏幕一
* localMediaTranscoder.startScreenSource(screenSourceList[0], {top:10, left:10, right:1200, bottom:800});
*
* // 开启屏幕二
* localMediaTranscoder.startScreenSource(screenSourceList[1], {top:0, left:0, right:0, bottom:0});
*
* // 加载图片一
* localMediaTranscoder.addImageSource('<please set valid image path>', 15);
*
* // 加载图片二
* localMediaTranscoder.addImageSource('<please set valid image path>', 15);
*
* // 启动合图转码并推流
* localMediaTranscoder.startTranscoding(
* TRTCVideoStreamType.TRTCVideoStreamTypeBig,
* {
* inputSourceList: [
* { // camera 1
* sourceType: TRTCLocalMediaTranscodingSourceType.MediaSourceCamera,
* cameraDeviceId: cameraList[0].deviceId,
* rect: { left: 0, top: 0, right: 320, bottom: 180 },
* zOrder: 1,
* renderParams: {
* rotation: TRTCVideoRotation.TRTCVideoRotation0,
* fillMode: TRTCVideoFillMode.TRTCVideoFillMode_Fit,
* mirrorType: TRTCVideoMirrorType.TRTCVideoMirrorType_Disable
* },
* },
* { // camera 2
* sourceType: TRTCLocalMediaTranscodingSourceType.MediaSourceCamera,
* cameraDeviceId: cameraList[1].deviceId,
* rect: {left: 320 + 20, top: 180 + 20, right: 640 + 20, bottom: 360 + 20},
* zOrder: 2,
* renderParams: {
* rotation: TRTCVideoRotation.TRTCVideoRotation90,
* fillMode: TRTCVideoFillMode.TRTCVideoFillMode_Fill,
* mirrorType: TRTCVideoMirrorType.TRTCVideoMirrorType_Enable
* },
* },
* {
* // screen 1
* sourceType: TRTCLocalMediaTranscodingSourceType.MediaSourceScreen,
* screenSourceId: screenSourceList[0].sourceId,
* rect: {left: 320 + 20, top: 0, right: 640 + 20, bottom: 180},
* zOrder: 3,
* renderParams: {
* rotation: TRTCVideoRotation.TRTCVideoRotation180,
* fillMode: TRTCVideoFillMode.TRTCVideoFillMode_Fit,
* mirrorType: TRTCVideoMirrorType.TRTCVideoMirrorType_Enable
* },
* },
* {
* // screen 2
* sourceType: TRTCLocalMediaTranscodingSourceType.MediaSourceScreen,
* screenSourceId: screenSourceList[1].sourceId,
* rect: {left: 0, top: 180 + 20, right: 320, bottom: 360 + 20},
* zOrder: 4,
* renderParams: {
* rotation: TRTCVideoRotation.TRTCVideoRotation270,
* fillMode: TRTCVideoFillMode.TRTCVideoFillMode_Fill,
* mirrorType: TRTCVideoMirrorType.TRTCVideoMirrorType_Auto
* },
* },
* { // image 1
* sourceType: TRTCLocalMediaTranscodingSourceType.MediaSourceImage,
* imagePath: '<please set valid image path>',
* rect: {left: 0, top: 360 + 40, right: 320, bottom: 540 + 40},
* zOrder: 5,
* renderParams: {
* rotation: TRTCVideoRotation.TRTCVideoRotation90, // 不会生效,参数设置将被忽略
* fillMode: TRTCVideoFillMode.TRTCVideoFillMode_Fit,
* mirrorType: TRTCVideoMirrorType.TRTCVideoMirrorType_Enable // 不会生效,参数设置将被忽略
* },
* },
* { // image 2
* sourceType: TRTCLocalMediaTranscodingSourceType.MediaSourceImage,
* imagePath: '<please set valid image path>',
* rect: {left: 320 + 20, top: 360 + 40, right: 640, bottom: 540 + 40},
* zOrder: 5,
* renderParams: {
* rotation: TRTCVideoRotation.TRTCVideoRotation0, // 不会生效,参数设置将被忽略
* fillMode: TRTCVideoFillMode.TRTCVideoFillMode_Fill,
* mirrorType: TRTCVideoMirrorType.TRTCVideoMirrorType_Disable // 不会生效,参数设置将被忽略
* },
* }
* ],
* videoEncoderParams: {
* videoResolution: TRTCVideoResolution.TRTCVideoResolution_960_720,
* resMode: TRTCVideoResolutionMode.TRTCVideoResolutionModeLandscape,
* videoFps: 30,
* videoBitrate: 800,
* minVideoBitrate: 0,
* enableAdjustRes: false,
* },
* canvasColor: 0xff0000, // 0x61b9f1,
* }
* );
*
* // 设置本地预览
* const view = document.getElementById("preview-view");
* localMediaTranscoder.setMixedVideoRenderView(view as HTMLElement);
*/
declare class TRTCLocalMediaTranscoder extends EventEmitter implements ITRTCLocalMediaTranscoder {
private logPrefix;
private nativeLocalMediaTranscoder;
private renderer;
private container;
private pixelFormat;
private pixelLength;
private streamType;
private videoRenderBuffer;
constructor(nativeLocalMediaTranscoder: any, pixelFormat?: TRTCVideoPixelFormat);
destroy(): void;
private validateTranscodingConfig;
/**
* 启动本地混流转码
*
* @param streamType {TRTCVideoStreamType} - 指定使用 TRTC 的 TRTCVideoStreamTypeBig 来推流,还是 TRTCVideoStreamTypeSub 来推流。
* @param params {TRTCLocalMediaTranscodingParams} - 指定本地混流转码的参数
* @param params.inputSourceList {TRTCLocalMediaTranscodingSource} - 【字段含义】指定转码流中的每一路输入媒体源配置的信息。
* -【推荐取值】该字段是一个 TRTCLocalMediaTranscodingSource 类型的数组,数组中的每一个元素都用来代表每一路输入媒体源的信息。
* -【特别说明】媒体源信息不支持留空,否则 TRTCLocalMediaTranscoder 的 onTranscodingStarted 会报错。
* @param params.videoEncoderParams {TRTCVideoEncParam} -【字段含义】转码流的视频编码参数
* @param params.canvasColor {Number} - 【字段含义】指定混合画面的底色。
* -【推荐取值】默认值:0x000000 代表黑色。格式为十六进制数字,比如:“0x61B9F1” 代表 RGB 分别为(97、158、241)。
*
* @fires [onTranscodingStarted]{@link TRTCLocalMediaTranscodingEvent#onTranscodingStarted}
*/
startTranscoding(streamType: TRTCVideoStreamType, params: TRTCLocalMediaTranscodingParams): void;
/**
* 更新本地混流转码参数
*
* @param params {TRTCLocalMediaTranscodingParams} - 指定本地混流转码的参数
*/
updateTranscodingParams(params: TRTCLocalMediaTranscodingParams): void;
/**
* 停止本地混流转码
*
* @fires [onTranscodingStopped]{@link TRTCLocalMediaTranscodingEvent#onTranscodingStopped}
*/
stopTranscoding(): void;
/**
* 启动摄像头采集
*
* @param deviceId {String} - 摄像头设备 Id
*
* @fires [onCameraSourceStarted]{@link TRTCLocalMediaTranscodingEvent#onCameraSourceStarted}
*/
startCameraSource(deviceId: string): void;
/**
* 设置摄像头采集参数
*
* @param deviceId {String} - 摄像头设备 Id
* @param cameraCaptureParams {TRTCCameraCaptureParams} - 指定摄像头采集分辨率和帧率
*/
setCameraCaptureParams(deviceId: string, cameraCaptureParams: TRTCCameraCaptureParams): void;
/**
* 停止摄像头采集
* @param deviceId {String} - 摄像头设备 Id
*
* @fires [onCameraSourceStopped]{@link TRTCLocalMediaTranscodingEvent#onCameraSourceStopped}
*/
stopCameraSource(deviceId: string): void;
/**
* 启用摄像头采集画面绿幕抠图
*
* @param cameraDeviceId {String} - 摄像头设备 ID。
* @param enable {Boolean} - 是否启用摄像头采集画面绿幕抠图。
* @param color {Number} - 指定画面中需要抠除的颜色值。默认值:0x00FF00 代表绿色。格式为十六进制数字,比如:“0x61B9F1” 代表 RGB 分别为(97、185、241)。
*/
enableCameraGreenScreen(deviceId: string, enable: boolean): void;
/**
* 启动屏幕/窗口采集
*
* @param source {TRTCScreenCaptureSourceInfo} - 屏幕/窗口采集的详细参数
* @param rect {Rect} - 指定捕获的区域
*
* @fires [onScreenSourceStarted]{@link TRTCLocalMediaTranscodingEvent#onScreenSourceStarted}
*/
startScreenSource(source: TRTCScreenCaptureSourceInfo, rect: Rect): void;
/**
* 更新窗口/屏幕采集属性
*
* @param {String} sourceId 屏幕/窗口的 Id
* @param {TRTCScreenCaptureProperty} property 屏幕/窗口采集参数
*/
updateScreenCaptureProperty(sourceId: string, property: TRTCScreenCaptureProperty): void;
/**
* 停止屏幕/窗口采集
* @param sourceId {String} - 屏幕/窗口的 Id
*
* @fires [onScreenSourceStopped]{@link TRTCLocalMediaTranscodingEvent#onScreenSourceStopped}
*/
stopScreenSource(sourceId: string): void;
/**
* 启动图片采集
* @param path {String} - 图片路径 ,目前只支持 BMP、JPG、PNG、GIF 四种格式
* @param fps {Number} - 采集输出帧率,可以不设置由 SDK 做最佳决策
*
* @fires [onImageSourceStarted]{@link TRTCLocalMediaTranscodingEvent#onImageSourceStarted}
*/
addImageSource(path: string, fps: number): void;
/**
* 停止图片采集
* @param path {String} - 图片路径
*
* @fires [onImageSourceStopped]{@link TRTCLocalMediaTranscodingEvent#onImageSourceStopped}
*/
removeImageSource(path: string): void;
/**
* 设置本地混流转码视频预览节点
* @param view {HTMLElement | null} - 如果传入的是 HTML 元素,必须是块元素;传入 null 时,会停止视频预览
*/
setMixedVideoRenderView(view: HTMLElement | null): void;
private setMixedVideoFrameRenderCallback;
private unsetMixedVideoFrameRenderCallback;
private setEventCallback;
private renderMixedVideoFrame;
}
export default TRTCLocalMediaTranscoder;