LibMedia+LibWeb: Implement media volume/muting

This commit is contained in:
Zaggy1024 2025-09-27 16:02:48 -05:00 committed by Jelle Raaijmakers
parent e176249db8
commit 8d9a493b1b
Notes: github-actions[bot] 2025-10-28 00:34:20 +00:00
6 changed files with 34 additions and 2 deletions

View file

@ -221,4 +221,10 @@ void PlaybackManager::pause()
m_time_provider->pause();
}
void PlaybackManager::set_volume(double volume)
{
if (m_audio_sink)
m_audio_sink->set_volume(volume);
}
}

View file

@ -63,6 +63,8 @@ public:
void play();
void pause();
void set_volume(double);
Function<void(DecoderError&&)> on_error;
private:

View file

@ -212,6 +212,8 @@ void AudioMixingSink::create_playback_stream(u32 sample_rate, u32 channel_count)
if (m_playing)
resume();
set_volume(m_volume);
}
AK::Duration AudioMixingSink::current_time() const
@ -320,4 +322,16 @@ void AudioMixingSink::set_time(AK::Duration time)
});
}
void AudioMixingSink::set_volume(double volume)
{
m_volume = volume;
if (m_playback_stream) {
m_playback_stream->set_volume(m_volume)
->when_rejected([](Error&&) {
// FIXME: Do we even need this function to return a promise?
});
}
}
}

View file

@ -40,6 +40,8 @@ public:
void pause();
void set_time(AK::Duration);
void set_volume(double);
private:
static constexpr size_t MAX_BLOCK_COUNT = 16;
@ -86,6 +88,7 @@ private:
u32 m_playback_stream_sample_rate { 0 };
u32 m_playback_stream_channel_count { 0 };
bool m_playing { false };
double m_volume { 1 };
HashMap<Track, TrackMixingData> m_track_mixing_datas;
Atomic<i64, MemoryOrder::memory_order_relaxed> m_next_sample_to_write { 0 };

View file

@ -454,12 +454,12 @@ void HTMLMediaElement::volume_or_muted_attribute_changed()
if (auto* paintable = this->paintable())
paintable->set_needs_display();
// FIXME: Set the volume on the PlaybackManager.
update_volume();
}
void HTMLMediaElement::page_mute_state_changed(Badge<Page>)
{
// FIXME: Set the volume on the PlaybackManager.
update_volume();
}
// https://html.spec.whatwg.org/multipage/media.html#effective-media-volume
@ -484,6 +484,12 @@ double HTMLMediaElement::effective_media_volume() const
return volume;
}
void HTMLMediaElement::update_volume()
{
if (m_playback_manager)
m_playback_manager->set_volume(effective_media_volume());
}
// https://html.spec.whatwg.org/multipage/media.html#dom-media-addtexttrack
GC::Ref<TextTrack> HTMLMediaElement::add_text_track(Bindings::TextTrackKind kind, String const& label, String const& language)
{

View file

@ -211,6 +211,7 @@ private:
void set_duration(double);
void volume_or_muted_attribute_changed();
void update_volume();
bool is_eligible_for_autoplay() const;
bool has_ended_playback() const;