Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Tracker events #398

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -34,7 +34,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 @@ -104,6 +104,7 @@ export class P2PTrackerClient {
onSegmentsAnnouncement: this.eventHandlers.onSegmentsAnnouncement,
},
this.config,
this.stream.type,
this.eventTarget,
);
this.logger(
Expand All @@ -118,11 +119,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 @@ -433,6 +433,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 @@ -445,10 +448,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 @@ -457,12 +466,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 @@ -501,17 +540,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 @@ -532,6 +578,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