[Mac] HLS audio is not correctly selected according to system language
authorjer.noble@apple.com <jer.noble@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Feb 2015 17:27:55 +0000 (17:27 +0000)
committerjer.noble@apple.com <jer.noble@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Feb 2015 17:27:55 +0000 (17:27 +0000)
https://bugs.webkit.org/show_bug.cgi?id=140398
rdar://problem/19218487

Reviewed by Darin Adler.

Source/WebCore:

Test: http/tests/media/hls/hls-audio-tracks-locale-selection.html

When AVMediaSelectionOptions come and go and no explicit track selection choice has
been made, automatically pick the most appropriate track according to the user's
current preferred locale settings.

* platform/graphics/avfoundation/MediaSelectionGroupAVFObjC.h:
* platform/graphics/avfoundation/MediaSelectionGroupAVFObjC.mm:
(WebCore::MediaSelectionGroupAVFObjC::MediaSelectionGroupAVFObjC): Set m_shouldSelectOptionAutomatically
    to true by default.
(WebCore::MediaSelectionGroupAVFObjC::updateOptions): If m_shouldSelectOptionAutomatically is set
    pick the most appropriate media selection option.
(WebCore::MediaSelectionGroupAVFObjC::setSelectedOption): Set m_shouldSelectOptionAutomatically to false.
* platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:
(WebCore::MediaPlayerPrivateAVFoundationObjC::createAVPlayerItem): Remove these automatic selection
    requests as they are ineffective when -appliesMediaSelectionCriteriaAutomatically is NO.

LayoutTests:

* http/tests/media/hls/hls-audio-tracks-locale-selection-expected.txt: Added.
* http/tests/media/hls/hls-audio-tracks-locale-selection.html: Added.
* http/tests/media/resources/hls/audio-tracks.m3u8:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@179549 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/http/tests/media/hls/hls-audio-tracks-locale-selection-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/media/hls/hls-audio-tracks-locale-selection.html [new file with mode: 0644]
LayoutTests/http/tests/media/resources/hls/audio-tracks.m3u8
Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/avfoundation/MediaSelectionGroupAVFObjC.h
Source/WebCore/platform/graphics/avfoundation/MediaSelectionGroupAVFObjC.mm
Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm

index 5689ce9..c69e57c 100644 (file)
@@ -1,3 +1,15 @@
+2015-02-03  Jer Noble  <jer.noble@apple.com>
+
+        [Mac] HLS audio is not correctly selected according to system language
+        https://bugs.webkit.org/show_bug.cgi?id=140398
+        rdar://problem/19218487
+
+        Reviewed by Darin Adler.
+
+        * http/tests/media/hls/hls-audio-tracks-locale-selection-expected.txt: Added.
+        * http/tests/media/hls/hls-audio-tracks-locale-selection.html: Added.
+        * http/tests/media/resources/hls/audio-tracks.m3u8:
+
 2015-02-03  Piotr Krysiewicz  <p.krysiewicz@samsung.com>
 
         [EFL] fast/borders/mixed-border-style2.html fails due to missing -expected.txt
diff --git a/LayoutTests/http/tests/media/hls/hls-audio-tracks-locale-selection-expected.txt b/LayoutTests/http/tests/media/hls/hls-audio-tracks-locale-selection-expected.txt
new file mode 100644 (file)
index 0000000..38a76c4
--- /dev/null
@@ -0,0 +1,9 @@
+
+RUN(internals.setUserPreferredLanguages(["jp", "fr", "es-EN", "en"]))
+EVENT(canplaythrough)
+EXPECTED (video.audioTracks.length == '3') OK
+EXPECTED (video.audioTracks[0].enabled == 'false') OK
+EXPECTED (video.audioTracks[1].enabled == 'true') OK
+EXPECTED (video.audioTracks[2].enabled == 'false') OK
+END OF TEST
+
diff --git a/LayoutTests/http/tests/media/hls/hls-audio-tracks-locale-selection.html b/LayoutTests/http/tests/media/hls/hls-audio-tracks-locale-selection.html
new file mode 100644 (file)
index 0000000..1a40940
--- /dev/null
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <script src=../../media-resources/video-test.js></script>
+        <script src=../../media-resources/media-controls.js></script>
+        <script>
+            if (window.testRunner) {
+                testRunner.dumpAsText();
+                testRunner.waitUntilDone();
+            }
+
+            function start() {
+                if (window.internals)
+                    run('internals.setUserPreferredLanguages(["jp", "fr", "es-EN", "en"])');
+                else
+                    consoleWrite('Change your system language to French, and check that the french audio track was selected by default.');
+
+                video = document.getElementById('video');
+                waitForEvent('canplaythrough', canplaythrough);
+                video.src = "../resources/hls/audio-tracks.m3u8";
+            }
+
+            function canplaythrough() {
+                testExpected("video.audioTracks.length", 3);
+                testExpected("video.audioTracks[0].enabled", false);
+                testExpected("video.audioTracks[1].enabled", true);
+                testExpected("video.audioTracks[2].enabled", false);
+                endTest();
+            }
+        </script>
+    </head>
+    <body onload="start()">
+        <video id="video"></video>
+    </body>
+</html>
index 11ab1da..564cb74 100644 (file)
@@ -1,8 +1,8 @@
 #EXTM3U
 
-#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="bipbop_audio",LANGUAGE="eng",NAME="English Sound",AUTOSELECT=YES,DEFAULT=YES
-#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="bipbop_audio",LANGUAGE="fre",NAME="French Sound",AUTOSELECT=NO,DEFAULT=NO,URI="french/prog_index.m3u8"
-#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="bipbop_audio",LANGUAGE="spa",NAME="Spanish Sound",AUTOSELECT=NO,DEFAULT=NO,URI="spanish/prog_index.m3u8"
+#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="bipbop_audio",LANGUAGE="en-US",NAME="English Sound",AUTOSELECT=YES,DEFAULT=YES
+#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="bipbop_audio",LANGUAGE="fr-FR",NAME="French Sound",AUTOSELECT=NO,DEFAULT=NO,URI="french/prog_index.m3u8"
+#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="bipbop_audio",LANGUAGE="es-US",NAME="Spanish Sound",AUTOSELECT=NO,DEFAULT=NO,URI="spanish/prog_index.m3u8"
 
 #EXT-X-STREAM-INF:BANDWIDTH=634451,CODECS="mp4a.40.2, avc1.4d401e",RESOLUTION=640x480,AUDIO="bipbop_audio"
 bipbop/prog_index.m3u8
index 5cf6333..e3dc506 100644 (file)
@@ -1,3 +1,28 @@
+2015-02-03  Jer Noble  <jer.noble@apple.com>
+
+        [Mac] HLS audio is not correctly selected according to system language
+        https://bugs.webkit.org/show_bug.cgi?id=140398
+        rdar://problem/19218487
+
+        Reviewed by Darin Adler.
+
+        Test: http/tests/media/hls/hls-audio-tracks-locale-selection.html
+
+        When AVMediaSelectionOptions come and go and no explicit track selection choice has
+        been made, automatically pick the most appropriate track according to the user's
+        current preferred locale settings.
+
+        * platform/graphics/avfoundation/MediaSelectionGroupAVFObjC.h:
+        * platform/graphics/avfoundation/MediaSelectionGroupAVFObjC.mm:
+        (WebCore::MediaSelectionGroupAVFObjC::MediaSelectionGroupAVFObjC): Set m_shouldSelectOptionAutomatically
+            to true by default.
+        (WebCore::MediaSelectionGroupAVFObjC::updateOptions): If m_shouldSelectOptionAutomatically is set
+            pick the most appropriate media selection option.
+        (WebCore::MediaSelectionGroupAVFObjC::setSelectedOption): Set m_shouldSelectOptionAutomatically to false.
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:
+        (WebCore::MediaPlayerPrivateAVFoundationObjC::createAVPlayerItem): Remove these automatic selection
+            requests as they are ineffective when -appliesMediaSelectionCriteriaAutomatically is NO.
+
 2015-02-03  Darin Adler  <darin@apple.com>
 
         Move InstanceInvalidationGuard/UpdateBlocker to SVGElement from SVGElementInstance
index be3dac7..b3412ff 100644 (file)
@@ -89,8 +89,9 @@ private:
     RetainPtr<AVPlayerItem> m_playerItem;
     RetainPtr<AVMediaSelectionGroup> m_mediaSelectionGroup;
     OptionContainer m_options;
-    MediaSelectionOptionAVFObjC* m_selectedOption;
+    MediaSelectionOptionAVFObjC* m_selectedOption { nullptr };
     Timer m_selectionTimer;
+    bool m_shouldSelectOptionAutomatically { true };
 };
 
 }
index 010b14e..3fc35ca 100644 (file)
@@ -28,6 +28,7 @@
 
 #if ENABLE(VIDEO_TRACK)
 
+#import "Language.h"
 #import "SoftLinking.h"
 #import <AVFoundation/AVAsset.h>
 #import <AVFoundation/AVMediaSelectionGroup.h>
@@ -35,6 +36,7 @@
 #import <objc/runtime.h>
 #import <wtf/HashMap.h>
 #import <wtf/HashSet.h>
+#import <wtf/text/WTFString.h>
 
 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
 
@@ -88,7 +90,6 @@ PassRefPtr<MediaSelectionGroupAVFObjC> MediaSelectionGroupAVFObjC::create(AVPlay
 MediaSelectionGroupAVFObjC::MediaSelectionGroupAVFObjC(AVPlayerItem *item, AVMediaSelectionGroup *group)
     : m_playerItem(item)
     , m_mediaSelectionGroup(group)
-    , m_selectedOption(nullptr)
     , m_selectionTimer(*this, &MediaSelectionGroupAVFObjC::selectionTimerFired)
 {
     updateOptions();
@@ -128,6 +129,25 @@ void MediaSelectionGroupAVFObjC::updateOptions()
             m_selectedOption = addedOption.get();
         m_options.set(addedAVOption, addedOption.release());
     }
+
+    if (!m_shouldSelectOptionAutomatically)
+        return;
+
+    RetainPtr<NSMutableArray> nsLanguages = adoptNS([[NSMutableArray alloc] initWithCapacity:userPreferredLanguages().size()]);
+    for (auto& language : userPreferredLanguages())
+        [nsLanguages addObject:(NSString*)language];
+    NSArray* filteredOptions = [getAVMediaSelectionGroupClass() mediaSelectionOptionsFromArray:[m_mediaSelectionGroup options] filteredAndSortedAccordingToPreferredLanguages:nsLanguages.get()];
+
+    if (![filteredOptions count])
+        return;
+
+    AVMediaSelectionOption* preferredOption = [filteredOptions objectAtIndex:0];
+    if (m_selectedOption && m_selectedOption->avMediaSelectionOption() == preferredOption)
+        return;
+
+    ASSERT(m_options.contains(preferredOption));
+    m_selectedOption = m_options.get(preferredOption);
+    m_selectionTimer.startOneShot(0);
 }
 
 void MediaSelectionGroupAVFObjC::setSelectedOption(MediaSelectionOptionAVFObjC* option)
@@ -135,6 +155,7 @@ void MediaSelectionGroupAVFObjC::setSelectedOption(MediaSelectionOptionAVFObjC*
     if (m_selectedOption == option)
         return;
 
+    m_shouldSelectOptionAutomatically = false;
     m_selectedOption = option;
     if (m_selectionTimer.isActive())
         m_selectionTimer.stop();
index 4ed1a7b..3ddc794 100644 (file)
@@ -935,12 +935,6 @@ void MediaPlayerPrivateAVFoundationObjC::createAVPlayerItem()
     [m_avPlayerItem.get() addOutput:m_legibleOutput.get()];
 #endif
 
-#if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
-        [m_avPlayerItem selectMediaOptionAutomaticallyInMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
-        [m_avPlayerItem selectMediaOptionAutomaticallyInMediaSelectionGroup:safeMediaSelectionGroupForAudibleMedia()];
-        [m_avPlayerItem selectMediaOptionAutomaticallyInMediaSelectionGroup:safeMediaSelectionGroupForVisualMedia()];
-#endif
-
 #if ENABLE(WEB_AUDIO) && USE(MEDIATOOLBOX)
     if (m_provider)
         m_provider->setPlayerItem(m_avPlayerItem.get());