Skip to content

Instantly share code, notes, and snippets.

@drew-thompson
Created May 22, 2019 21:45
Show Gist options
  • Save drew-thompson/4298bce6d83112763442d439896670b4 to your computer and use it in GitHub Desktop.
Save drew-thompson/4298bce6d83112763442d439896670b4 to your computer and use it in GitHub Desktop.
Angular Video - Initialize Client with Handlers
import { Component, OnInit } from '@angular/core';
import { NgxAgoraService, Stream, AgoraClient, ClientEvent } from 'ngx-agora';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
title = 'angular-video';
remoteCalls: string[] = [];
private client: AgoraClient;
private localStream: Stream;
constructor(private ngxAgoraService: NgxAgoraService) {}
ngOnInit() {
this.client = this.ngxAgoraService.createClient({ mode: 'rtc', codec: 'h264' });
this.assignClientHandlers();
}
private assignClientHandlers(): void {
this.client.on(ClientEvent.LocalStreamPublished, evt => {
console.log('Publish local stream successfully');
});
this.client.on(ClientEvent.Error, error => {
console.log('Got error msg:', error.reason);
if (error.reason === 'DYNAMIC_KEY_TIMEOUT') {
this.client.renewChannelKey(
'',
() => console.log('Renewed the channel key successfully.'),
renewError => console.error('Renew channel key failed: ', renewError)
);
}
});
this.client.on(ClientEvent.RemoteStreamAdded, evt => {
const stream = evt.stream as Stream;
this.client.subscribe(stream, { audio: true, video: true }, err => {
console.log('Subscribe stream failed', err);
});
});
this.client.on(ClientEvent.RemoteStreamSubscribed, evt => {
const stream = evt.stream as Stream;
const id = this.getRemoteId(stream);
if (!this.remoteCalls.length) {
this.remoteCalls.push(id);
setTimeout(() => stream.play(id), 1000);
}
});
this.client.on(ClientEvent.RemoteStreamRemoved, evt => {
const stream = evt.stream as Stream;
if (stream) {
stream.stop();
this.remoteCalls = [];
console.log(`Remote stream is removed ${stream.getId()}`);
}
});
this.client.on(ClientEvent.PeerLeave, evt => {
const stream = evt.stream as Stream;
if (stream) {
stream.stop();
this.remoteCalls = this.remoteCalls.filter(call => call !== `${this.getRemoteId(stream)}`);
console.log(`${evt.uid} left from this channel`);
}
});
}
private getRemoteId(stream: Stream): string {
return `agora_remote-${stream.getId()}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment