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 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
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
3 changes: 1 addition & 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,7 @@ export class Core<TStream extends Stream = Stream> {
highDemandTimeWindow: 15,
httpDownloadTimeWindow: 3000,
p2pDownloadTimeWindow: 6000,
cachedSegmentExpiration: 120 * 1000,
cachedSegmentsCount: 1000,
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: 30 additions & 15 deletions packages/p2p-media-loader-core/src/segments-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type StorageEventHandlers = {
[key in `onStorageUpdated-${string}`]: (steam: Stream) => void;
};

const DEFAULT_LIVE_CACHED_SEGMENT_EXPIRATION = 1200;

export class SegmentsMemoryStorage {
private cache = new Map<string, StorageItem>();
private _isInitialized = false;
Expand Down Expand Up @@ -61,7 +63,11 @@ export class SegmentsMemoryStorage {
}

// eslint-disable-next-line @typescript-eslint/require-await
async storeSegment(segment: SegmentWithStream, data: ArrayBuffer) {
async storeSegment(
segment: SegmentWithStream,
data: ArrayBuffer,
isLiveStream: boolean,
) {
const id = getStorageItemId(segment);
this.cache.set(id, {
segment,
Expand All @@ -70,7 +76,7 @@ export class SegmentsMemoryStorage {
});
this.logger(`add segment: ${id}`);
this.dispatchStorageUpdatedEvent(segment.stream);
void this.clear();
void this.clear(isLiveStream);
}

// eslint-disable-next-line @typescript-eslint/require-await
Expand Down Expand Up @@ -101,7 +107,13 @@ export class SegmentsMemoryStorage {
}

// eslint-disable-next-line @typescript-eslint/require-await
private async clear(): Promise<boolean> {
private async clear(isLiveStream: boolean): Promise<boolean> {
const cacheSegmentExpiration =
(this.storageConfig.cachedSegmentExpiration ??
(isLiveStream ? DEFAULT_LIVE_CACHED_SEGMENT_EXPIRATION : 0)) * 1000;

if (cacheSegmentExpiration === 0) return false;

const itemsToDelete: string[] = [];
const remainingItems: [string, StorageItem][] = [];
const streamsOfChangedItems = new Set<Stream>();
Expand All @@ -112,7 +124,8 @@ export class SegmentsMemoryStorage {
for (const entry of this.cache.entries()) {
const [itemId, item] = entry;
const { lastAccessed, segment } = item;
if (now - lastAccessed > this.storageConfig.cachedSegmentExpiration) {

if (now - lastAccessed > cacheSegmentExpiration) {
if (!this.isSegmentLocked(segment)) {
itemsToDelete.push(itemId);
streamsOfChangedItems.add(segment.stream);
Expand All @@ -123,17 +136,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