Skip to content

Commit

Permalink
feat: add mew config param isP2PUploadDisabled
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Nov 18, 2024
1 parent ace3b9b commit cdde125
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Core<TStream extends Stream = Stream> {

/** Default configuration for stream settings. */
static readonly DEFAULT_STREAM_CONFIG: StreamConfig = {
isP2PUploadDisabled: false,
isP2PDisabled: false,
simultaneousHttpDownloads: 2,
simultaneousP2PDownloads: 3,
Expand Down Expand Up @@ -162,6 +163,9 @@ export class Core<TStream extends Stream = Stream> {
if (this.secondaryStreamConfig.isP2PDisabled) {
this.destroyStreamLoader("secondary");
}

this.mainStreamLoader?.sendBroadcastAnnouncement();
this.secondaryStreamLoader?.sendBroadcastAnnouncement();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,10 @@ export class HybridLoader {
this.levelChangedTimestamp = performance.now();
}

sendBroadcastAnnouncement() {
this.p2pLoaders.currentLoader.broadcastAnnouncement();
}

updatePlayback(position: number, rate: number) {
const isRateChanged = this.playback.rate !== rate;
const isPositionChanged = this.playback.position !== position;
Expand Down
10 changes: 9 additions & 1 deletion packages/p2p-media-loader-core/src/p2p/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,16 @@ export class P2PLoader {
}

private onPeerConnected = (peer: Peer) => {
if (this.config.isP2PUploadDisabled) return;

const { httpLoading, loaded } = this.getSegmentsAnnouncement();
peer.sendSegmentsAnnouncementCommand(loaded, httpLoading);
};

broadcastAnnouncement = () => {
if (this.isAnnounceMicrotaskCreated) return;
if (this.isAnnounceMicrotaskCreated || this.config.isP2PUploadDisabled) {
return;
}

this.isAnnounceMicrotaskCreated = true;
queueMicrotask(() => {
Expand All @@ -142,6 +146,10 @@ export class P2PLoader {
segmentExternalId,
);
if (!segment) return;
if (this.config.isP2PUploadDisabled) {
peer.sendSegmentAbsentCommand(segmentExternalId, requestId);
return;
}

const swarmId = this.config.swarmId ?? this.streamManifestUrl;
const streamSwarmId = StreamUtils.getStreamSwarmId(swarmId, this.stream);
Expand Down
19 changes: 17 additions & 2 deletions packages/p2p-media-loader-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export type DynamicStreamProperties =
| "p2pErrorRetries"
| "validateP2PSegment"
| "httpRequestSetup"
| "isP2PDisabled";
| "isP2PDisabled"
| "isP2PUploadDisabled";

/**
* Represents a dynamically modifiable configuration, allowing updates to selected CoreConfig properties at runtime.
Expand Down Expand Up @@ -186,6 +187,16 @@ export type CoreConfig = Partial<StreamConfig> &

/** Configuration options for the Core functionality, including network and processing parameters. */
export type StreamConfig = {
/**
* Indicates if Peer-to-Peer (P2P) upload is disabled for the stream.
* If `true`, the stream only downloads segments without uploading to peers.
*
* @default
* ```typescript
* isP2PUploadDisabled: false
* ```
*/
isP2PUploadDisabled: boolean;
/**
* Indicates whether Peer-to-Peer (P2P) functionality is disabled for the stream.
* If set to true, P2P functionality is disabled for the stream.
Expand Down Expand Up @@ -387,7 +398,11 @@ export type StreamConfig = {
* validateP2PSegment: undefined
* ```
*/
validateP2PSegment?: (url: string, byteRange: ByteRange | undefined, data: ArrayBuffer) => Promise<boolean>;
validateP2PSegment?: (
url: string,
byteRange: ByteRange | undefined,
data: ArrayBuffer,
) => Promise<boolean>;

/**
* Optional function to customize the setup of HTTP requests for segment downloads.
Expand Down

0 comments on commit cdde125

Please sign in to comment.