UNPKG

ngx-agora-sdk-ng

Version:

<div style="display:flex"> <img src="docs/images/angular-logo.svg" width="50px" height="50px" alt="Angular"/> <img src="docs/images/agora-logo.png" width="50px" height="50px" alt="Agora"/> </div>

417 lines (402 loc) 16.6 kB
import { __awaiter } from 'tslib'; import { EventEmitter, ɵɵdefineInjectable, ɵɵinject, Injectable, Inject, Component, NgModule } from '@angular/core'; import AgoraRTC from 'agora-rtc-sdk-ng'; class AudioTrack { constructor(microphoneAudioTrack) { this.microphoneAudioTrack = microphoneAudioTrack; this._microphoneAudioTrack = this.microphoneAudioTrack; } stop() { this._microphoneAudioTrack.stop(); this._microphoneAudioTrack.close(); } microphoneMute() { this._microphoneAudioTrack.setEnabled(false); } microphoneUnMute() { this._microphoneAudioTrack.setEnabled(true); } setVolume(volume) { this._microphoneAudioTrack.setVolume(volume); } } class JoinAudioChannel { constructor(client, config, channelName, token, uid) { this.client = client; this.config = config; this.channelName = channelName; this.token = token; this.uid = uid; } WithMediaStream(audioMediaStream) { this.localAudioTrack = AgoraRTC.createCustomAudioTrack({ mediaStreamTrack: audioMediaStream }); return this; } WithMicrophone(microphoneId) { this.requestInWait = AgoraRTC.createMicrophoneAudioTrack({ microphoneId: microphoneId }); return this; } Apply() { return __awaiter(this, void 0, void 0, function* () { yield this.client.join(this.config.AppID, this.channelName, this.token, this.uid); let localTrack; if (this.requestInWait) { localTrack = yield this.requestInWait; } else if (this.localAudioTrack) { localTrack = this.localAudioTrack; } else { localTrack = yield AgoraRTC.createMicrophoneAudioTrack(); } yield this.client.publish([localTrack]); let audioTrack = new AudioTrack(localTrack); return new Promise((resolve, reject) => { resolve(audioTrack); reject(); }); }); } } class MediaTrack { constructor(track) { this.track = track; this._microphoneAudioTrack = this.track[0]; this._cameraVideoTrack = this.track[1]; } stop() { this._cameraVideoTrack.stop(); this._cameraVideoTrack.close(); this._microphoneAudioTrack.stop(); this._microphoneAudioTrack.close(); } playVideo(element, config) { this._cameraVideoTrack.play(element, config); } microphoneMute() { this._microphoneAudioTrack.setEnabled(false); } microphoneUnMute() { this._microphoneAudioTrack.setEnabled(true); } cameraOff() { this._cameraVideoTrack.setEnabled(false); } cameraOn() { this._cameraVideoTrack.setEnabled(true); } setVolume(volume) { this._microphoneAudioTrack.setVolume(volume); } } class JoinChannel { constructor(client, config, channelName, token, uid) { this.client = client; this.config = config; this.channelName = channelName; this.token = token; this.uid = uid; this.tracks = new Array(); this._onLocalUserJoinedEvent = new EventEmitter(); } Apply() { return __awaiter(this, void 0, void 0, function* () { yield this.client.join(this.config.AppID, this.channelName, this.token, this.uid); let localTrack; if (this.requestInWait) { localTrack = yield this.requestInWait; } else if (this.tracks.length > 0) { localTrack = this.tracks; } else { localTrack = yield AgoraRTC.createMicrophoneAndCameraTracks(); } yield this.client.publish(localTrack); let mediaTrack = new MediaTrack(localTrack); this._onLocalUserJoinedEvent.emit({ track: mediaTrack }); return new Promise((resolve, reject) => { resolve(mediaTrack); reject(); }); }); } WithMediaStream(videoMediaStream, audioMediaStream) { this.tracks.push(AgoraRTC.createCustomAudioTrack({ mediaStreamTrack: audioMediaStream })); this.tracks.push(AgoraRTC.createCustomVideoTrack({ mediaStreamTrack: videoMediaStream })); return this; } WithCameraAndMicrophone(microphoneId, cameraId) { this.requestInWait = AgoraRTC.createMicrophoneAndCameraTracks({ microphoneId: microphoneId }, { cameraId: cameraId }); return this; } registerUserJoinedEvent(event) { this._onLocalUserJoinedEvent = event; } } class VideoTrack { constructor(cameraVideoTrack) { this.cameraVideoTrack = cameraVideoTrack; this._cameraVideoTrack = this.cameraVideoTrack; } stop() { this._cameraVideoTrack.stop(); this._cameraVideoTrack.close(); } playVideo(element, config) { this._cameraVideoTrack.play(element, config); } cameraOff() { this._cameraVideoTrack.setEnabled(false); } cameraOn() { this._cameraVideoTrack.setEnabled(true); } } class JoinVideoChannel { constructor(client, config, channelName, token, uid) { this.client = client; this.config = config; this.channelName = channelName; this.token = token; this.uid = uid; } WithMediaStream(mediaStream) { this.localVideoTrack = AgoraRTC.createCustomVideoTrack({ mediaStreamTrack: mediaStream }); return this; } WithCamera(cameraId) { this.requestInWait = AgoraRTC.createCameraVideoTrack({ cameraId: cameraId }); return this; } Apply() { return __awaiter(this, void 0, void 0, function* () { yield this.client.join(this.config.AppID, this.channelName, this.token, this.uid); let localTrack; if (this.requestInWait) { localTrack = yield this.requestInWait; } else if (this.localVideoTrack) { localTrack = this.localVideoTrack; } else { localTrack = yield AgoraRTC.createCameraVideoTrack(); } yield this.client.publish(localTrack); let videTrack = new VideoTrack(localTrack); return new Promise((resolve, reject) => { resolve(videTrack); reject(); }); }); } } class AgoraConfig { } class NgxAgoraSdkNgService { constructor(config) { var _a, _b, _c; this.config = config; this._onRemoteUserStateEvent = new EventEmitter(); this._onRemoteUserJoinedEvent = new EventEmitter(); this._onRemoteUserLeftEvent = new EventEmitter(); this._onRemoteVolumeIndicatorEvent = new EventEmitter(); this._onLocalUserJoinedEvent = new EventEmitter(); this._onLocalUserLeftEvent = new EventEmitter(); this._onLocalNetworkQualityChangeEvent = new EventEmitter(); this.agoraClient = AgoraRTC.createClient({ codec: config.Video ? (_a = config.Video) === null || _a === void 0 ? void 0 : _a.codec : 'h264', mode: config.Video ? (_b = config.Video) === null || _b === void 0 ? void 0 : _b.mode : 'rtc', role: config.Video ? (_c = config.Video) === null || _c === void 0 ? void 0 : _c.role : 'audience' }); this.agoraClient.on('user-published', (user, mediaType) => __awaiter(this, void 0, void 0, function* () { yield this.agoraClient.subscribe(user, mediaType); this._onRemoteUserStateEvent.emit({ mediaType: mediaType, connectionState: 'CONNECTED', user: { uid: user.uid, hasVideo: user.hasVideo, hasAudio: user.hasAudio, audioTrack: { setVolume: (volume) => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.setVolume(volume); }, getVolumeLevel: () => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.getVolumeLevel(); }, play: () => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.play(); }, getMediaStream: () => { var _a; return new MediaStream([(_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.getMediaStreamTrack()]); } }, videoTrack: { play: (element) => { var _a; return (_a = user.videoTrack) === null || _a === void 0 ? void 0 : _a.play(element); }, getMediaStream: () => { var _a; return new MediaStream([(_a = user.videoTrack) === null || _a === void 0 ? void 0 : _a.getMediaStreamTrack()]); } } } }); })); this.agoraClient.on('user-unpublished', user => { this._onRemoteUserStateEvent.emit({ connectionState: 'DISCONNECTED', user: { uid: user.uid, hasVideo: user.hasVideo, hasAudio: user.hasAudio, audioTrack: { setVolume: (volume) => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.setVolume(volume); }, getVolumeLevel: () => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.getVolumeLevel(); }, play: () => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.play(); }, getMediaStream: () => { var _a; return new MediaStream([(_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.getMediaStreamTrack()]); } }, videoTrack: { play: (element) => { var _a; return (_a = user.videoTrack) === null || _a === void 0 ? void 0 : _a.play(element); }, getMediaStream: () => { var _a; return new MediaStream([(_a = user.videoTrack) === null || _a === void 0 ? void 0 : _a.getMediaStreamTrack()]); } } } }); }); this.agoraClient.on('user-joined', (user) => { this._onRemoteUserJoinedEvent.emit({ uid: user.uid, hasVideo: user.hasVideo, hasAudio: user.hasAudio, audioTrack: { setVolume: (volume) => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.setVolume(volume); }, getVolumeLevel: () => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.getVolumeLevel(); }, play: () => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.play(); }, getMediaStream: () => { var _a; return new MediaStream([(_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.getMediaStreamTrack()]); } }, videoTrack: { play: (element) => { var _a; return (_a = user.videoTrack) === null || _a === void 0 ? void 0 : _a.play(element); }, getMediaStream: () => { var _a; return new MediaStream([(_a = user.videoTrack) === null || _a === void 0 ? void 0 : _a.getMediaStreamTrack()]); } } }); }); this.agoraClient.on('user-left', (user, reason) => { this._onRemoteUserLeftEvent.emit({ user: { uid: user.uid, hasVideo: user.hasVideo, hasAudio: user.hasAudio, audioTrack: { setVolume: (volume) => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.setVolume(volume); }, getVolumeLevel: () => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.getVolumeLevel(); }, play: () => { var _a; return (_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.play(); }, getMediaStream: () => { var _a; return new MediaStream([(_a = user.audioTrack) === null || _a === void 0 ? void 0 : _a.getMediaStreamTrack()]); } }, videoTrack: { play: (element) => { var _a; return (_a = user.videoTrack) === null || _a === void 0 ? void 0 : _a.play(element); }, getMediaStream: () => { var _a; return new MediaStream([(_a = user.videoTrack) === null || _a === void 0 ? void 0 : _a.getMediaStreamTrack()]); } } }, reason: reason }); }); this.agoraClient.enableAudioVolumeIndicator(); this.agoraClient.on('volume-indicator', (result) => { this._onRemoteVolumeIndicatorEvent.emit(result); }); this.agoraClient.on('network-quality', (stats) => { this._onLocalNetworkQualityChangeEvent.emit({ download: stats.downlinkNetworkQuality, upload: stats.uplinkNetworkQuality }); }); } join(channelName, token, uid) { let joinChannel = new JoinChannel(this.agoraClient, this.config, channelName, token, uid); joinChannel.registerUserJoinedEvent(this._onLocalUserJoinedEvent); return joinChannel; } joinVideo(channelName, token, uid) { return new JoinVideoChannel(this.agoraClient, this.config, channelName, token, uid); } joinAudio(channelName, token, uid) { return new JoinAudioChannel(this.agoraClient, this.config, channelName, token, uid); } // remark: review this code. leave() { return __awaiter(this, void 0, void 0, function* () { yield this.agoraClient.leave(); this._onLocalUserLeftEvent.emit(); }); } getCameras() { return AgoraRTC.getCameras(); } getMicrophones() { return AgoraRTC.getMicrophones(); } getDevices() { return AgoraRTC.getDevices(); } onRemoteUsersStatusChange() { return this._onRemoteUserStateEvent.asObservable(); } onRemoteUserJoined() { return this._onRemoteUserJoinedEvent.asObservable(); } onRemoteUserLeft() { return this._onRemoteUserLeftEvent.asObservable(); } onRemoteVolumeIndicator() { return this._onRemoteVolumeIndicatorEvent.asObservable(); } onLocalUserJoined() { return this._onLocalUserJoinedEvent.asObservable(); } onLocalUserLeft() { return this._onLocalUserLeftEvent.asObservable(); } onLocalNetworkQualityChange() { return this._onLocalNetworkQualityChangeEvent.asObservable(); } } NgxAgoraSdkNgService.ɵprov = ɵɵdefineInjectable({ factory: function NgxAgoraSdkNgService_Factory() { return new NgxAgoraSdkNgService(ɵɵinject("config")); }, token: NgxAgoraSdkNgService, providedIn: "root" }); NgxAgoraSdkNgService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; NgxAgoraSdkNgService.ctorParameters = () => [ { type: AgoraConfig, decorators: [{ type: Inject, args: ['config',] }] } ]; class NgxAgoraSdkNgComponent { constructor() { } ngOnInit() { } } NgxAgoraSdkNgComponent.decorators = [ { type: Component, args: [{ selector: 'ngx-agora-sdk-ng', template: ` <p> ngx-agora-sdk-ng works! </p> ` },] } ]; NgxAgoraSdkNgComponent.ctorParameters = () => []; class NgxAgoraSdkNgModule { static forRoot(config) { return { ngModule: NgxAgoraSdkNgModule, providers: [NgxAgoraSdkNgService, { provide: 'config', useValue: config }] }; } } NgxAgoraSdkNgModule.decorators = [ { type: NgModule, args: [{ declarations: [NgxAgoraSdkNgComponent], imports: [], exports: [NgxAgoraSdkNgComponent] },] } ]; class UserState { } class IChannelConfig { } /* * Public API Surface of ngx-agora-sdk-ng */ /** * Generated bundle index. Do not edit. */ export { AgoraConfig, IChannelConfig, NgxAgoraSdkNgComponent, NgxAgoraSdkNgModule, NgxAgoraSdkNgService, UserState }; //# sourceMappingURL=ngx-agora-sdk-ng.js.map