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

Support live streams (ie big timestamps), replace float with double #1910

Merged
merged 1 commit into from
Nov 22, 2023
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
72 changes: 35 additions & 37 deletions cobalt/dom/html_media_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
ready_state_(WebMediaPlayer::kReadyStateHaveNothing),
ready_state_maximum_(WebMediaPlayer::kReadyStateHaveNothing),
volume_(1.0f),
last_seek_time_(0),
last_seek_time_(0.0),
previous_progress_time_(std::numeric_limits<double>::max()),
duration_(std::numeric_limits<double>::quiet_NaN()),
playing_(false),
Expand All @@ -149,7 +149,7 @@
resume_frozen_flag_(false),
seeking_(false),
controls_(false),
last_time_update_event_movie_time_(std::numeric_limits<float>::max()),
last_time_update_event_movie_time_(std::numeric_limits<double>::max()),
processing_media_player_callback_(0),
media_source_url_(std::string(kMediaSourceUrlProtocol) + ':' +
base::GenerateGUID()),
Expand Down Expand Up @@ -222,7 +222,7 @@
}

player_->UpdateBufferedTimeRanges(
[&](float start, float end) { buffered->Add(start, end); });
[&](double start, double end) { buffered->Add(start, end); });

Check warning on line 225 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L225

Added line #L225 was not covered by tests
return buffered;
}

Expand Down Expand Up @@ -344,27 +344,26 @@
return seeking_;
}

float HTMLMediaElement::current_time(
double HTMLMediaElement::current_time(
script::ExceptionState* exception_state) const {
if (!player_) {
LOG(INFO) << 0 << " (because player is NULL)";
return 0;
return 0.0;

Check warning on line 351 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L351

Added line #L351 was not covered by tests
}

if (seeking_) {
MLOG() << last_seek_time_ << " (seeking)";
return last_seek_time_;
}

float time = player_->GetCurrentTime();
const double time = player_->GetCurrentTime();

Check warning on line 359 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L359

Added line #L359 was not covered by tests
MLOG() << time << " (from player)";
return time;
}

void HTMLMediaElement::set_current_time(
float time, script::ExceptionState* exception_state) {
double time, script::ExceptionState* exception_state) {

Check warning on line 365 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L365

Added line #L365 was not covered by tests
// 4.8.9.9 Seeking

// 1 - If the media element's readyState is
// WebMediaPlayer::kReadyStateHaveNothing, then raise an INVALID_STATE_ERR
// exception.
Expand All @@ -377,10 +376,9 @@
Seek(time);
}

float HTMLMediaElement::duration() const {
double HTMLMediaElement::duration() const {

Check warning on line 379 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L379

Added line #L379 was not covered by tests
MLOG() << duration_;
// TODO: Turn duration into double.
return static_cast<float>(duration_);
return duration_;

Check warning on line 381 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L381

Added line #L381 was not covered by tests
}

base::Time HTMLMediaElement::GetStartDate() const {
Expand Down Expand Up @@ -433,7 +431,7 @@
const scoped_refptr<TimeRanges>& HTMLMediaElement::played() {
MLOG();
if (playing_) {
float time = current_time(NULL);
const double time = current_time(NULL);

Check warning on line 434 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L434

Added line #L434 was not covered by tests
if (time > last_seek_time_) {
AddPlayedRange(last_seek_time_, time);
}
Expand All @@ -447,8 +445,8 @@
}

scoped_refptr<TimeRanges> HTMLMediaElement::seekable() const {
if (player_ && player_->GetMaxTimeSeekable() != 0) {
double max_time_seekable = player_->GetMaxTimeSeekable();
if (player_ && player_->GetMaxTimeSeekable() != 0.0) {
const double max_time_seekable = player_->GetMaxTimeSeekable();

Check warning on line 449 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L448-L449

Added lines #L448 - L449 were not covered by tests
MLOG() << "(0, " << max_time_seekable << ")";
return new TimeRanges(0, max_time_seekable);
}
Expand Down Expand Up @@ -632,7 +630,7 @@
ScheduleOwnEvent(base::Tokens::durationchange());

if (request_seek) {
Seek(static_cast<float>(duration));
Seek(duration);

Check warning on line 633 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L633

Added line #L633 was not covered by tests
}
}

Expand Down Expand Up @@ -775,7 +773,7 @@

// 2 - Asynchronously await a stable state.
played_time_ranges_ = new TimeRanges;
last_seek_time_ = 0;
last_seek_time_ = 0.0;

Check warning on line 776 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L776

Added line #L776 was not covered by tests

ConfigureMediaControls();
}
Expand Down Expand Up @@ -1019,8 +1017,8 @@
return;
}

double time = base::Time::Now().ToDoubleT();
double time_delta = time - previous_progress_time_;
const double time = base::Time::Now().ToDoubleT();
const double time_delta = time - previous_progress_time_;

Check warning on line 1021 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1020-L1021

Added lines #L1020 - L1021 were not covered by tests

if (player_->DidLoadingProgress()) {
ScheduleOwnEvent(base::Tokens::progress());
Expand Down Expand Up @@ -1082,7 +1080,7 @@
void HTMLMediaElement::ScheduleTimeupdateEvent(bool periodic_event) {
// Some media engines make multiple "time changed" callbacks at the same time,
// but we only want one event at a given time so filter here
float movie_time = current_time(NULL);
const double movie_time = current_time(NULL);

Check warning on line 1083 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1083

Added line #L1083 was not covered by tests
if (movie_time != last_time_update_event_movie_time_) {
if (!periodic_event && playback_progress_timer_.IsRunning()) {
playback_progress_timer_.Reset();
Expand Down Expand Up @@ -1268,7 +1266,7 @@
network_state_ = kNetworkIdle;
}

void HTMLMediaElement::Seek(float time) {
void HTMLMediaElement::Seek(double time) {

Check warning on line 1269 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1269

Added line #L1269 was not covered by tests
LOG(INFO) << "Seek to " << time << ".";
// 4.8.9.9 Seeking - continued from set_current_time().

Expand All @@ -1279,7 +1277,7 @@

// Get the current time before setting seeking_, last_seek_time_ is returned
// once it is set.
float now = current_time(NULL);
const double now = current_time(NULL);

Check warning on line 1280 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1280

Added line #L1280 was not covered by tests

// 2 - If the element's seeking IDL attribute is true, then another instance
// of this algorithm is already running. Abort that other instance of the
Expand All @@ -1297,7 +1295,7 @@

// 6 - If the new playback position is less than the earliest possible
// position, let it be that position instead.
time = std::max(time, 0.f);
time = std::max(time, 0.0);

Check warning on line 1298 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1298

Added line #L1298 was not covered by tests

// 7 - If the (possibly now changed) new playback position is not in one of
// the ranges given in the seekable attribute, then let it be the position in
Expand Down Expand Up @@ -1329,7 +1327,7 @@
seeking_ = false;
return;
}
time = static_cast<float>(seekable_ranges->Nearest(time));
time = seekable_ranges->Nearest(time);

Check warning on line 1330 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1330

Added line #L1330 was not covered by tests

if (playing_) {
if (last_seek_time_ < now) {
Expand Down Expand Up @@ -1361,7 +1359,7 @@
ScheduleOwnEvent(base::Tokens::seeked());
}

void HTMLMediaElement::AddPlayedRange(float start, float end) {
void HTMLMediaElement::AddPlayedRange(double start, double end) {

Check warning on line 1362 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1362

Added line #L1362 was not covered by tests
if (!played_time_ranges_) {
played_time_ranges_ = new TimeRanges;
}
Expand Down Expand Up @@ -1411,7 +1409,7 @@

playback_progress_timer_.Stop();
playing_ = false;
float time = current_time(NULL);
const double time = current_time(NULL);

Check warning on line 1412 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1412

Added line #L1412 was not covered by tests
if (time > last_seek_time_) {
AddPlayedRange(last_seek_time_, time);
}
Expand All @@ -1431,7 +1429,7 @@
}

bool HTMLMediaElement::EndedPlayback() const {
float dur = duration();
const double dur = duration();

Check warning on line 1432 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1432

Added line #L1432 was not covered by tests
if (!player_ || std::isnan(dur)) {
return false;
}
Expand All @@ -1448,15 +1446,15 @@
// direction of playback is forwards, Either the media element does not have a
// loop attribute specified, or the media element has a current media
// controller.
float now = current_time(NULL);
if (playback_rate_ > 0) {
return dur > 0 && now >= dur && !loop();
const double now = current_time(NULL);
if (playback_rate_ > 0.f) {
return dur > 0.0 && now >= dur && !loop();

Check warning on line 1451 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1449-L1451

Added lines #L1449 - L1451 were not covered by tests
}

// or the current playback position is the earliest possible position and the
// direction of playback is backwards
if (playback_rate_ < 0) {
return now <= 0;
if (playback_rate_ < 0.f) {
return now <= 0.0;

Check warning on line 1457 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1456-L1457

Added lines #L1456 - L1457 were not covered by tests
}

return false;
Expand Down Expand Up @@ -1557,14 +1555,14 @@
// already posted one at the current movie time.
ScheduleTimeupdateEvent(false);

float now = current_time(NULL);
float dur = duration();
const double now = current_time(NULL);
const double dur = duration();

Check warning on line 1559 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1558-L1559

Added lines #L1558 - L1559 were not covered by tests

// When the current playback position reaches the end of the media resource
// when the direction of playback is forwards, then the user agent must follow
// these steps:
eos_played |=
!std::isnan(dur) && (0.0f != dur) && now >= dur && playback_rate_ > 0;
!std::isnan(dur) && (0.0 != dur) && now >= dur && playback_rate_ > 0.f;

Check warning on line 1565 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1565

Added line #L1565 was not covered by tests
sideb0ard marked this conversation as resolved.
Show resolved Hide resolved
if (eos_played) {
LOG(INFO) << "End of stream is played.";
// If the media element has a loop attribute specified and does not have a
Expand All @@ -1573,7 +1571,7 @@
sent_end_event_ = false;
// then seek to the earliest possible position of the media resource and
// abort these steps.
Seek(0);
Seek(0.0);

Check warning on line 1574 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1574

Added line #L1574 was not covered by tests
} else {
// If the media element does not have a current media controller, and the
// media element has still ended playback, and the direction of playback
Expand Down Expand Up @@ -1603,15 +1601,15 @@

ScheduleOwnEvent(base::Tokens::durationchange());

double now = current_time(NULL);
const double now = current_time(NULL);

Check warning on line 1604 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1604

Added line #L1604 was not covered by tests
// Reset and update |duration_|.
duration_ = std::numeric_limits<double>::quiet_NaN();
if (player_ && ready_state_ >= WebMediaPlayer::kReadyStateHaveMetadata) {
duration_ = player_->GetDuration();
}

if (now > duration_) {
Seek(static_cast<float>(duration_));
Seek(duration_);

Check warning on line 1612 in cobalt/dom/html_media_element.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/dom/html_media_element.cc#L1612

Added line #L1612 was not covered by tests
}

EndProcessingMediaPlayerCallback();
Expand Down
12 changes: 6 additions & 6 deletions cobalt/dom/html_media_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ class HTMLMediaElement : public HTMLElement,
bool seeking() const;

// Playback state
float current_time(script::ExceptionState* exception_state) const;
void set_current_time(float time, script::ExceptionState* exception_state);
float duration() const;
double current_time(script::ExceptionState* exception_state) const;
void set_current_time(double time, script::ExceptionState* exception_state);
double duration() const;
base::Time GetStartDate() const;
bool paused() const;
bool resume_frozen_flag() const;
Expand Down Expand Up @@ -210,10 +210,10 @@ class HTMLMediaElement : public HTMLElement,
void ChangeNetworkStateFromLoadingToIdle();

// Playback
void Seek(float time);
void Seek(double time);
void FinishSeek();

void AddPlayedRange(float start, float end);
void AddPlayedRange(double start, double end);

void UpdateVolume();
void UpdatePlayState();
Expand Down Expand Up @@ -269,7 +269,7 @@ class HTMLMediaElement : public HTMLElement,
WebMediaPlayer::ReadyState ready_state_maximum_;

float volume_;
float last_seek_time_;
double last_seek_time_;
double previous_progress_time_;

double duration_;
Expand Down
12 changes: 6 additions & 6 deletions cobalt/media/player/web_media_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class WebMediaPlayer {
// Return true if the punch through box should be rendered. Return false if
// no punch through box should be rendered.
typedef base::Callback<bool(int x, int y, int width, int height)> SetBoundsCB;
typedef std::function<void(float start, float end)> AddRangeCB;
typedef std::function<void(double start, double end)> AddRangeCB;

enum NetworkState {
kNetworkStateEmpty,
Expand Down Expand Up @@ -109,12 +109,12 @@ class WebMediaPlayer {
// Playback controls.
virtual void Play() = 0;
virtual void Pause() = 0;
virtual void Seek(float seconds) = 0;
virtual void Seek(double seconds) = 0;
virtual void SetRate(float rate) = 0;
virtual void SetVolume(float volume) = 0;
virtual void SetVisible(bool visible) = 0;
virtual void UpdateBufferedTimeRanges(const AddRangeCB& add_range_cb) = 0;
virtual float GetMaxTimeSeekable() const = 0;
virtual double GetMaxTimeSeekable() const = 0;

// Suspend/Resume
virtual void Suspend() = 0;
Expand All @@ -136,11 +136,11 @@ class WebMediaPlayer {
// Getters of playback state.
virtual bool IsPaused() const = 0;
virtual bool IsSeeking() const = 0;
virtual float GetDuration() const = 0;
virtual double GetDuration() const = 0;
#if SB_HAS(PLAYER_WITH_URL)
virtual base::Time GetStartDate() const = 0;
#endif // SB_HAS(PLAYER_WITH_URL)
virtual float GetCurrentTime() const = 0;
virtual double GetCurrentTime() const = 0;
virtual float GetPlaybackRate() const = 0;

// Get rate of loading the resource.
Expand All @@ -152,7 +152,7 @@ class WebMediaPlayer {

virtual bool DidLoadingProgress() const = 0;

virtual float MediaTimeForTimeValue(float timeValue) const = 0;
virtual double MediaTimeForTimeValue(double timeValue) const = 0;

virtual PlayerStatistics GetStatistics() const = 0;

Expand Down
Loading
Loading