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

Refactor: docs #366

Merged
merged 19 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
4 changes: 0 additions & 4 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ But they support a limited number of peers and can reject connections or even go

That is why they can't be used in production environments. Consider running your personal tracker or buy resources from a tracker provider to go stable.

## How to achieve better P2P ratio for live streams & VOD streams?

Our current default configuration efficiently supports both Live and Video On Demand (VOD) streams. This setup is optimized to provide a robust P2P sharing ratio, enhancing performance without the need for custom settings.

## What are the requirements to share a stream over P2P?

The requirements to share a stream over P2P are:
Expand Down
3 changes: 1 addition & 2 deletions packages/p2p-media-loader-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
"dependencies": {
"bittorrent-tracker": "^11.0.2",
"debug": "^4.3.4",
"nano-md5": "^1.0.5",
"ts-essentials": "^9.4.1"
"nano-md5": "^1.0.5"
},
"devDependencies": {
"vite-plugin-node-polyfills": "^0.21.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class Core<TStream extends Stream = Stream> {
highDemandTimeWindow: 15,
httpDownloadTimeWindow: 3000,
p2pDownloadTimeWindow: 6000,
cachedSegmentExpiration: 120 * 1000,
cachedSegmentsCount: 1000,
cachedSegmentExpiration: 0,
mrlika marked this conversation as resolved.
Show resolved Hide resolved
cachedSegmentsCount: 0,
webRtcMaxMessageSize: 64 * 1024 - 1,
p2pNotReceivingBytesTimeoutMs: 1000,
p2pInactiveLoaderDestroyTimeoutMs: 30 * 1000,
Expand Down
6 changes: 5 additions & 1 deletion packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ export class HybridLoader {
this.engineRequest = undefined;
}
this.requests.remove(request);
void this.segmentStorage.storeSegment(request.segment, request.data);
void this.segmentStorage.storeSegment(
request.segment,
request.data,
this.streamDetails.isLive,
);
break;

case "failed":
Expand Down
45 changes: 32 additions & 13 deletions packages/p2p-media-loader-core/src/segments-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type StorageItem = {
segment: SegmentWithStream;
data: ArrayBuffer;
lastAccessed: number;
isSegmentLive: boolean;
};

type StorageEventHandlers = {
Expand Down Expand Up @@ -61,12 +62,17 @@ export class SegmentsMemoryStorage {
}

// eslint-disable-next-line @typescript-eslint/require-await
async storeSegment(segment: SegmentWithStream, data: ArrayBuffer) {
async storeSegment(
segment: SegmentWithStream,
data: ArrayBuffer,
isStreamLive: boolean,
) {
const id = getStorageItemId(segment);
this.cache.set(id, {
segment,
data,
lastAccessed: performance.now(),
isSegmentLive: isStreamLive,
});
this.logger(`add segment: ${id}`);
this.dispatchStorageUpdatedEvent(segment.stream);
Expand Down Expand Up @@ -108,11 +114,22 @@ export class SegmentsMemoryStorage {

// Delete old segments
const now = performance.now();
const defaultLiveExpiration = 1000 * 60 * 20;

for (const entry of this.cache.entries()) {
const [itemId, item] = entry;
const { lastAccessed, segment } = item;
if (now - lastAccessed > this.storageConfig.cachedSegmentExpiration) {

let expirationThreshold;
if (this.storageConfig.cachedSegmentExpiration > 0) {
expirationThreshold = this.storageConfig.cachedSegmentExpiration * 1000;
} else if (item.isSegmentLive) {
expirationThreshold = defaultLiveExpiration;
} else {
continue;
}

if (now - lastAccessed > expirationThreshold) {
if (!this.isSegmentLocked(segment)) {
itemsToDelete.push(itemId);
streamsOfChangedItems.add(segment.stream);
Expand All @@ -123,17 +140,19 @@ export class SegmentsMemoryStorage {
}

// Delete segments over cached count
let countOverhead =
remainingItems.length - this.storageConfig.cachedSegmentsCount;
if (countOverhead > 0) {
remainingItems.sort(([, a], [, b]) => a.lastAccessed - b.lastAccessed);

for (const [itemId, { segment }] of remainingItems) {
if (!this.isSegmentLocked(segment)) {
itemsToDelete.push(itemId);
streamsOfChangedItems.add(segment.stream);
countOverhead--;
if (countOverhead === 0) break;
if (this.storageConfig.cachedSegmentsCount > 0) {
let countOverhead =
remainingItems.length - this.storageConfig.cachedSegmentsCount;
if (countOverhead > 0) {
remainingItems.sort(([, a], [, b]) => a.lastAccessed - b.lastAccessed);

for (const [itemId, { segment }] of remainingItems) {
if (!this.isSegmentLocked(segment)) {
itemsToDelete.push(itemId);
streamsOfChangedItems.add(segment.stream);
countOverhead--;
if (countOverhead === 0) break;
}
}
}
}
Expand Down
Loading