Skip to content

Commit

Permalink
Multiply to 2 to get segment id from start time.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Dec 20, 2023
1 parent 3509712 commit 081557d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 1 addition & 3 deletions packages/p2p-media-loader-core/src/p2p/tracker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ export class P2PTrackerClient {
this.settings
);
this.logger(
`connected with peer: ${peerItem.peer.id} ${LoggerUtils.getStreamString(
this.stream
)}`
`connected with peer: ${peerItem.peer.id} ${this.streamShortId}`
);
this.eventHandlers.onPeerConnected(peerItem.peer);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/p2p-media-loader-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type Stream = {

export type ReadonlyLinkedMap<K, V extends object> = Pick<
LinkedMap<K, V>,
"has" | "keys" | "values" | "valuesBackwards" | "size"
"has" | "keys" | "values" | "size"
>;

export type StreamWithSegments<
Expand Down
16 changes: 12 additions & 4 deletions packages/p2p-media-loader-core/src/utils/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,25 @@ export function getSegmentPlaybackStatuses(
} = timeWindowsSettings;

return {
isHighDemand: isInTimeWindow(segment, playback, highDemandTimeWindow),
isHttpDownloadable: isInTimeWindow(
isHighDemand: isSegmentInTimeWindow(
segment,
playback,
highDemandTimeWindow
),
isHttpDownloadable: isSegmentInTimeWindow(
segment,
playback,
httpDownloadTimeWindow
),
isP2PDownloadable: isInTimeWindow(segment, playback, p2pDownloadTimeWindow),
isP2PDownloadable: isSegmentInTimeWindow(
segment,
playback,
p2pDownloadTimeWindow
),
};
}

function isInTimeWindow(
function isSegmentInTimeWindow(
segment: Segment,
playback: Playback,
timeWindowLength: number
Expand Down
10 changes: 7 additions & 3 deletions packages/p2p-media-loader-shaka/src/segment-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
ReadonlyLinkedMap,
} from "p2p-media-loader-core";

const SEGMENT_ID_SCALING_DURATION = 0.5;

export class SegmentManager {
private readonly core: Core<Stream>;
private streamInfo: Readonly<StreamInfo>;
Expand Down Expand Up @@ -59,7 +61,9 @@ export class SegmentManager {
const staleSegmentsIds = new Set(managerStream.segments.keys());
const newSegments: SegmentBase[] = [];
for (const reference of segmentReferences) {
const externalId = Math.trunc(reference.getStartTime() * 2);
const externalId = Math.trunc(
reference.getStartTime() / SEGMENT_ID_SCALING_DURATION
);

const segmentLocalId = Utils.getSegmentLocalIdFromReference(reference);
if (!managerStream.segments.has(segmentLocalId)) {
Expand Down Expand Up @@ -140,9 +144,9 @@ function* nSegmentsBackwards(
amount: number
) {
let i = 0;
for (const segment of segments.valuesBackwards()) {
for (const segment of segments.values()) {
if (i >= amount) break;
yield segment;
i--;
i++;
}
}

0 comments on commit 081557d

Please sign in to comment.