Eyevinn / webrtc-player

WebRTC (recvonly) player
MIT License
87 stars 18 forks source link

feat: emit an event on initial connection failed (#45) #46

Closed nigelsim closed 1 year ago

nigelsim commented 1 year ago

This addresses the issue raised in #45 by emitting an initial-connection-failed event if the WHEP request fails. It also properly cleans up the peer in this situation.

This allows you to do something like the following in your client code, giving good control over when to retry, and when to alert the user:

class SomeAngularComponent {
  connectionAttemptsRemaining = 3;

  private whepUrl?: string;

  async ngOnInit() {
    this.client = new WebRTCPlayer({
      video: this.video.nativeElement,
      type: 'whep',
    });

    this.client.on('initial-connection-failed', () => this.maybeRetryConnection());
    await this.client.load(new URL(this.whepUrl));
  }

  private maybeRetryConnection() {
    if (this.connectionAttemptsRemaining > 0) {
      this.client!.load(new URL(this.whepUrl!));
      this.connectionAttemptsRemaining--;
    } else {
      // Alert the user
    }
  }
}