Skip to content

Commit

Permalink
Feat: Tracker events (#398)
Browse files Browse the repository at this point in the history
* Add streamType parameter to peer events

* Add event handlers for tracker errors and warnings

The code changes introduce event handlers for tracker errors and warnings. This allows for better monitoring and control of the tracker request process.

* Add streamType parameter to segment events

* Refactor P2PVideoDemo component to filter out non-main streamType in onPeerConnect and onPeerClose event handlers (#399)

* Improve onSegmentLoaded event

* Improved naming
  • Loading branch information
DimaDemchenko authored Jul 19, 2024
1 parent 8101d30 commit 0972c00
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
11 changes: 10 additions & 1 deletion packages/p2p-media-loader-core/src/p2p/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
RequestError,
RequestAbortErrorType,
SegmentWithStream,
StreamType,
} from "../types.js";
import * as Utils from "../utils/utils.js";
import * as Command from "./commands/index.js";
Expand Down Expand Up @@ -46,7 +47,8 @@ export class Peer {
private readonly connection: PeerConnection,
private readonly eventHandlers: PeerEventHandlers,
private readonly peerConfig: PeerConfig,
eventTarget: EventTarget<CoreEventMap>,
private readonly streamType: StreamType,
private readonly eventTarget: EventTarget<CoreEventMap>,
) {
this.onPeerClosed = eventTarget.getEventDispatcher("onPeerClose");

Expand All @@ -63,6 +65,7 @@ export class Peer {
);
eventTarget.getEventDispatcher("onPeerConnect")({
peerId: this.id,
streamType,
});

connection.on("error", this.onConnectionError);
Expand Down Expand Up @@ -342,6 +345,11 @@ export class Peer {

private onConnectionError = (error: Error) => {
this.logger(`peer connection error ${this.id} %O`, error);
this.eventTarget.getEventDispatcher("onPeerError")({
peerId: this.id,
streamType: this.streamType,
error,
});

const code = (error as { code?: string }).code;

Expand All @@ -358,6 +366,7 @@ export class Peer {
this.eventHandlers.onPeerClosed(this);
this.onPeerClosed({
peerId: this.id,
streamType: this.streamType,
});
this.logger(`peer closed ${this.id}`);
};
Expand Down
11 changes: 10 additions & 1 deletion packages/p2p-media-loader-core/src/p2p/tracker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class P2PTrackerClient {

constructor(
streamSwarmId: string,
stream: StreamWithSegments,
private readonly stream: StreamWithSegments,
private readonly eventHandlers: P2PTrackerClientEventHandlers,
private readonly config: StreamConfig,
private readonly eventTarget: EventTarget<CoreEventMap>,
Expand Down Expand Up @@ -110,6 +110,7 @@ export class P2PTrackerClient {
onSegmentsAnnouncement: this.eventHandlers.onSegmentsAnnouncement,
},
this.config,
this.stream.type,
this.eventTarget,
);
this.logger(
Expand All @@ -124,11 +125,19 @@ export class P2PTrackerClient {
) => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
this.logger(`tracker warning (${this.streamShortId}: ${warning})`);
this.eventTarget.getEventDispatcher("onTrackerWarning")({
streamType: this.stream.type,
warning,
});
};

private onTrackerClientError: TrackerClientEvents["error"] = (error) => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
this.logger(`tracker error (${this.streamShortId}: ${error})`);
this.eventTarget.getEventDispatcher("onTrackerError")({
streamType: this.stream.type,
error,
});
};

*peers() {
Expand Down
5 changes: 5 additions & 0 deletions packages/p2p-media-loader-core/src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class Request {
this.currentAttempt?.downloadSource === "p2p"
? this.currentAttempt.peerId
: undefined,
streamType: this.segment.stream.type,
});
this._abortRequestCallback = undefined;
this.manageBandwidthCalculatorsState("stop");
Expand Down Expand Up @@ -243,6 +244,7 @@ export class Request {
this.currentAttempt.downloadSource === "p2p"
? this.currentAttempt.peerId
: undefined,
streamType: this.segment.stream.type,
});
this.notReceivingBytesTimeout.clear();
this.manageBandwidthCalculatorsState("stop");
Expand All @@ -269,6 +271,7 @@ export class Request {
this.currentAttempt.downloadSource === "p2p"
? this.currentAttempt.peerId
: undefined,
streamType: this.segment.stream.type,
});
this.notReceivingBytesTimeout.clear();
this.manageBandwidthCalculatorsState("stop");
Expand All @@ -285,12 +288,14 @@ export class Request {
this.setStatus("succeed");
this._totalBytes = this._loadedBytes;
this.onSegmentLoaded({
segmentUrl: this.segment.url,
bytesLength: this.finalData.byteLength,
downloadSource: this.currentAttempt.downloadSource,
peerId:
this.currentAttempt.downloadSource === "p2p"
? this.currentAttempt.peerId
: undefined,
streamType: this.segment.stream.type,
});

this.logger(
Expand Down
64 changes: 62 additions & 2 deletions packages/p2p-media-loader-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ export type SegmentErrorDetails = {

/** The peer ID, if the segment was downloaded from a peer. */
peerId: string | undefined;

/** The type of stream that the segment is associated with. */
streamType: StreamType;
};

/** Represents details about a segment abort event. */
Expand All @@ -448,10 +451,16 @@ export type SegmentAbortDetails = {

/** The peer ID, if the segment was downloaded from a peer. */
peerId: string | undefined;

/** The type of stream that the segment is associated with. */
streamType: StreamType;
};

/** Represents the details about a loaded segment. */
export type SegmentLoadDetails = {
/** The URL of the loaded segment */
segmentUrl: string;

/** The length of the segment in bytes. */
bytesLength: number;

Expand All @@ -460,12 +469,42 @@ export type SegmentLoadDetails = {

/** The peer ID, if the segment was downloaded from a peer. */
peerId: string | undefined;

/** The segment that the event is about. */
streamType: StreamType;
};

/** Represents the details of a peer in a peer-to-peer network. */
export type PeerDetails = {
/** The unique identifier for a peer in the network. */
peerId: string;
/** The type of stream that the peer is connected to. */
streamType: StreamType;
};

/** Represents the details of a peer error event. */
export type PeerErrorDetails = {
/** The unique identifier for a peer in the network. */
peerId: string;
/** The type of stream that the peer is connected to. */
streamType: StreamType;
/** The error that occurred during the peer-to-peer connection. */
error: Error;
};

/** Represents the details of a tracker error event. */
export type TrackerErrorDetails = {
/** The type of stream that the tracker is for. */
streamType: StreamType;
/** The error that occurred during the tracker request. */
error: unknown;
};

export type TrackerWarningDetails = {
/** The type of stream that the tracker is for. */
streamType: StreamType;
/** The warning that occurred during the tracker request. */
warning: unknown;
};

/**
Expand Down Expand Up @@ -504,17 +543,24 @@ export type CoreEventMap = {
/**
* Occurs when a new peer-to-peer connection is established.
*
* @param peerId - The unique identifier of the peer that has just connected.
* @param params - Contains details about the peer that the event is about.
*/
onPeerConnect: (params: PeerDetails) => void;

/**
* Triggered when an existing peer-to-peer connection is closed.
*
* @param peerId - The unique identifier of the peer whose connection has been closed.
* @param params - Contains details about the peer that the event is about.
*/
onPeerClose: (params: PeerDetails) => void;

/**
* Triggered when an error occurs during a peer-to-peer connection.
*
* @param params - Contains details about the error and the peer that the event is about.
*/
onPeerError: (params: PeerErrorDetails) => void;

/**
* Invoked after a chunk of data from a segment has been successfully downloaded.
*
Expand All @@ -535,6 +581,20 @@ export type CoreEventMap = {
* @param peerId - The peer ID, if the segment was downloaded from a peer
*/
onChunkUploaded: (bytesLength: number, peerId: string) => void;

/**
* Called when an error occurs during the tracker request process.
*
* @param params - Contains information about the tracker error.
*/
onTrackerError: (params: TrackerErrorDetails) => void;

/**
* Called when a warning occurs during the tracker request process.
*
* @param params - Contains information about the tracker warning.
*/
onTrackerWarning: (params: TrackerWarningDetails) => void;
};

/** Defines the types of errors that can occur during a request abortion process. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ export const P2PVideoDemo = ({ debugToolsEnabled = false }: DemoProps) => {
}, []);

const onPeerConnect = useCallback((params: PeerDetails) => {
if (params.streamType !== "main") return;

setPeers((peers) => {
return [...peers, params.peerId];
});
}, []);

const onPeerClose = useCallback((params: PeerDetails) => {
if (params.streamType !== "main") return;

setPeers((peers) => {
return peers.filter((peer) => peer !== params.peerId);
});
Expand Down

0 comments on commit 0972c00

Please sign in to comment.