Have you read the FAQ and checked for duplicate open issues?
Yes, I have. This specific issue of getPlayheadTimeAsDate inaccuracy across discontinuities with a proposal for a corresponding event does not appear to be covered in existing open issues.
Is your feature request related to a problem? Please describe.
Yes. The player.getPlayheadTimeAsDate() method currently returns an incorrect wall-clock time for HLS streams where the #EXT-X-PROGRAM-DATE-TIME (PDT) tag is not contiguous across an #EXT-X-DISCONTINUITY boundary.
The current implementation calculates the date by simply adding the playhead's elapsed time (in the presentation timeline) to the initial PDT found at the start of the manifest. This assumption fails in common real-world scenarios:
- Live Stream Gaps: When a live stream stops and restarts, a discontinuity tag is often inserted, and the new segments start with a new PDT that reflects the new wall-clock time. The player, however, continues to calculate from the original PDT, causing the reported time to drift significantly from reality.
- Server-Side Ad Insertion (SSAI): Ads are often stitched into the main content with discontinuity tags. If an ad's duration does not perfectly match the content it replaces, the PDT after the ad break will jump forward or backward to align with the live timeline. The current
getPlayheadTimeAsDate() does not account for this jump, leading to inaccurate time reporting during and after ad breaks.
This makes it impossible for applications to build reliable features based on wall-clock time, such as displaying an accurate program clock or synchronizing secondary events with the stream.
Describe the solution you'd like
I propose a two-part solution to make Shaka Player's time reporting robust and transparent:
-
Make getPlayheadTimeAsDate() Discontinuity-Aware:
The core logic of getPlayheadTimeAsDate() should be enhanced. Instead of relying on a single initial PDT, the player should:
- During HLS manifest parsing, build a map of "timeline regions", where each region (demarcated by a discontinuity) is associated with its starting presentation time and its starting PDT.
- When
getPlayheadTimeAsDate() is called, it should first identify which timeline region the current playhead position falls into.
- It should then calculate the date by adding the playhead's offset within that specific region to that region's starting PDT. This will ensure the returned date is always relative to the currently active timeline.
-
Introduce a timelinechange Event:
To allow applications to react to these timeline shifts, I propose adding a new event, timelinechange, to the shaka.Player instance.
- This event would be dispatched whenever the playhead crosses a discontinuity boundary that has a new
#EXT-X-PROGRAM-DATE-TIME.
- The event payload should include essential information about the change:
programDateTime: The new Date object that is now the basis for the timeline.
timestamp: The presentation time (in seconds) at which this change occurred.
An example of how an application would use this:
player.addEventListener('timelinechange', (event) => {
console.log(`Timeline changed at ${event.timestamp}s.`);
console.log(`New base Program Date Time is ${event.programDateTime.toISOString()}`);
// Application can now update its UI clock or other time-sensitive elements.
});
This dual approach not only fixes the immediate bug but also empowers developers with the tools to build more robust applications on top of Shaka Player.
Describe alternatives you've considered
-
Only Fix getPlayheadTimeAsDate(): We could just fix the method without adding a new event. This would solve the accuracy problem, but it would be a "silent" fix. Applications would have no way of knowing when the underlying time base has changed, which can be critical for UI updates or logging. It is less transparent.
-
Only Add an Event: We could add the timelinechange event and leave it to the application developer to calculate the correct time. This is not ideal as it would shift a complex burden from the player (which has all the necessary context from parsing the manifest) to every application developer, leading to duplicated and potentially incorrect implementations.
The proposed solution of fixing the core method and adding a notification event is the most comprehensive and developer-friendly approach.
Additional context
This feature would be immensely valuable not only for HLS but could also establish a pattern for DASH Multi-Period streams. A Period transition in DASH is conceptually similar to a discontinuity in HLS. The timelinechange event could be generalized to fire on Period changes as well, providing a unified API for developers to handle timeline shifts regardless of the streaming format.
Are you planning to send a PR to add it?
N/A
Have you read the FAQ and checked for duplicate open issues?
Yes, I have. This specific issue of
getPlayheadTimeAsDateinaccuracy across discontinuities with a proposal for a corresponding event does not appear to be covered in existing open issues.Is your feature request related to a problem? Please describe.
Yes. The
player.getPlayheadTimeAsDate()method currently returns an incorrect wall-clock time for HLS streams where the#EXT-X-PROGRAM-DATE-TIME(PDT) tag is not contiguous across an#EXT-X-DISCONTINUITYboundary.The current implementation calculates the date by simply adding the playhead's elapsed time (in the presentation timeline) to the initial PDT found at the start of the manifest. This assumption fails in common real-world scenarios:
getPlayheadTimeAsDate()does not account for this jump, leading to inaccurate time reporting during and after ad breaks.This makes it impossible for applications to build reliable features based on wall-clock time, such as displaying an accurate program clock or synchronizing secondary events with the stream.
Describe the solution you'd like
I propose a two-part solution to make Shaka Player's time reporting robust and transparent:
Make
getPlayheadTimeAsDate()Discontinuity-Aware:The core logic of
getPlayheadTimeAsDate()should be enhanced. Instead of relying on a single initial PDT, the player should:getPlayheadTimeAsDate()is called, it should first identify which timeline region the current playhead position falls into.Introduce a
timelinechangeEvent:To allow applications to react to these timeline shifts, I propose adding a new event,
timelinechange, to theshaka.Playerinstance.#EXT-X-PROGRAM-DATE-TIME.programDateTime: The newDateobject that is now the basis for the timeline.timestamp: The presentation time (in seconds) at which this change occurred.An example of how an application would use this:
This dual approach not only fixes the immediate bug but also empowers developers with the tools to build more robust applications on top of Shaka Player.
Describe alternatives you've considered
Only Fix
getPlayheadTimeAsDate(): We could just fix the method without adding a new event. This would solve the accuracy problem, but it would be a "silent" fix. Applications would have no way of knowing when the underlying time base has changed, which can be critical for UI updates or logging. It is less transparent.Only Add an Event: We could add the
timelinechangeevent and leave it to the application developer to calculate the correct time. This is not ideal as it would shift a complex burden from the player (which has all the necessary context from parsing the manifest) to every application developer, leading to duplicated and potentially incorrect implementations.The proposed solution of fixing the core method and adding a notification event is the most comprehensive and developer-friendly approach.
Additional context
This feature would be immensely valuable not only for HLS but could also establish a pattern for DASH Multi-Period streams. A Period transition in DASH is conceptually similar to a discontinuity in HLS. The
timelinechangeevent could be generalized to fire on Period changes as well, providing a unified API for developers to handle timeline shifts regardless of the streaming format.Are you planning to send a PR to add it?
N/A