@@ -1126,6 +1126,14 @@ bool HTMLMediaElement::verify_response(GC::Ref<Fetch::Infrastructure::Response>
11261126 TODO ();
11271127}
11281128
1129+ void HTMLMediaElement::set_audio_track_enabled (Badge<AudioTrack>, GC::Ptr<HTML::AudioTrack> audio_track, bool enabled)
1130+ {
1131+ if (enabled)
1132+ m_playback_manager->enable_an_audio_track (audio_track->track_in_playback_manager ());
1133+ else
1134+ m_playback_manager->disable_an_audio_track (audio_track->track_in_playback_manager ());
1135+ }
1136+
11291137void HTMLMediaElement::set_selected_video_track (Badge<VideoTrack>, GC::Ptr<HTML::VideoTrack> video_track)
11301138{
11311139 set_needs_style_update (true );
@@ -1194,17 +1202,51 @@ WebIDL::ExceptionOr<void> HTMLMediaElement::process_media_data(Function<void(Str
11941202
11951203 m_playback_manager = playback_manager_result.release_value ();
11961204
1197- // FIXME: -> If the media resource is found to have an audio track
1198- // 1. Create an AudioTrack object to represent the audio track.
1199- // 2. Update the media element's audioTracks attribute's AudioTrackList object with the new AudioTrack object.
1200- // 3. Let enable be unknown.
1201- // 4. If either the media resource or the URL of the current media resource indicate a particular set of audio tracks to enable, or if
1202- // the user agent has information that would facilitate the selection of specific audio tracks to improve the user's experience, then:
1203- // if this audio track is one of the ones to enable, then set enable to true, otherwise, set enable to false.
1204- // 5. If enable is still unknown, then, if the media element does not yet have an enabled audio track, then set enable to true, otherwise,
1205- // set enable to false.
1206- // 6. If enable is true, then enable this audio track, otherwise, do not enable this audio track.
1207- // 7. Fire an event named addtrack at this AudioTrackList object, using TrackEvent, with the track attribute initialized to the new AudioTrack object.
1205+ // -> If the media resource is found to have an audio track
1206+ auto preferred_audio_track = m_playback_manager->preferred_audio_track ();
1207+ auto has_enabled_preferred_audio_track = false ;
1208+
1209+ for (auto const & track : m_playback_manager->audio_tracks ()) {
1210+ // 1. Create an AudioTrack object to represent the audio track.
1211+ auto audio_track = realm.create <AudioTrack>(realm, *this , track);
1212+
1213+ // 2. Update the media element's audioTracks attribute's AudioTrackList object with the new AudioTrack object.
1214+ m_audio_tracks->add_track ({}, audio_track);
1215+
1216+ // 3. Let enable be unknown.
1217+ auto enable = TriState::Unknown;
1218+
1219+ // 4. If either the media resource or the URL of the current media resource indicate a particular set of audio tracks to enable, or if
1220+ // the user agent has information that would facilitate the selection of specific audio tracks to improve the user's experience, then:
1221+ // if this audio track is one of the ones to enable, then set enable to true, otherwise, set enable to false.
1222+ if (preferred_audio_track.has_value ()) {
1223+ if (track == preferred_audio_track && !has_enabled_preferred_audio_track) {
1224+ enable = TriState::True;
1225+ has_enabled_preferred_audio_track = true ;
1226+ } else {
1227+ enable = TriState::False;
1228+ }
1229+ }
1230+
1231+ // 5. If enable is still unknown, then, if the media element does not yet have an enabled audio track, then set enable to true, otherwise,
1232+ // set enable to false.
1233+ if (enable == TriState::Unknown)
1234+ enable = !m_audio_tracks->has_enabled_track () ? TriState::True : TriState::False;
1235+
1236+ // 6. If enable is true, then enable this audio track, otherwise, do not enable this audio track.
1237+ if (enable == TriState::True)
1238+ audio_track->set_enabled (true );
1239+
1240+ // 7. Fire an event named addtrack at this AudioTrackList object, using TrackEvent, with the track attribute initialized to the new AudioTrack object.
1241+ TrackEventInit event_init {};
1242+ event_init.track = GC::make_root (audio_track);
1243+
1244+ auto event = TrackEvent::create (realm, HTML::EventNames::addtrack, move (event_init));
1245+ m_audio_tracks->dispatch_event (event);
1246+ }
1247+
1248+ if (preferred_audio_track.has_value ())
1249+ VERIFY (has_enabled_preferred_audio_track);
12081250
12091251 // -> If the media resource is found to have a video track
12101252 auto preferred_video_track = m_playback_manager->preferred_video_track ();
@@ -1301,7 +1343,13 @@ WebIDL::ExceptionOr<void> HTMLMediaElement::process_media_data(Function<void(Str
13011343 // FIXME: 11. If either the media resource or the URL of the current media resource indicate a particular start time, then set the initial playback
13021344 // position to that time and, if jumped is still false, seek to that time.
13031345
1304- // FIXME: 12. If there is no enabled audio track, then enable an audio track. This will cause a change event to be fired.
1346+ // 12. If there is no enabled audio track, then enable an audio track. This will cause a change event to be fired.
1347+ if (!m_audio_tracks->has_enabled_track ()) {
1348+ m_audio_tracks->for_each_track ([](auto & track) {
1349+ track.set_enabled (true );
1350+ return IterationDecision::Break;
1351+ });
1352+ }
13051353
13061354 // 13. If there is no selected video track, then select a video track. This will cause a change event to be fired.
13071355 if (m_video_tracks->selected_index () == -1 ) {
0 commit comments