ba0dfdea3e4ab45d71ab790e931f4764a31ba427
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / InbandTextTrackPrivateLegacyAVFObjC.mm
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "config.h"
27
28 #if ENABLE(VIDEO) && USE(AVFOUNDATION) && !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT) && !PLATFORM(IOS_FAMILY)
29
30 #import "InbandTextTrackPrivateLegacyAVFObjC.h"
31
32 #import "InbandTextTrackPrivateAVF.h"
33 #import "Logging.h"
34 #import "MediaPlayerPrivateAVFoundationObjC.h"
35 #import <objc/runtime.h>
36
37 #import <pal/cocoa/AVFoundationSoftLink.h>
38
39 namespace WebCore {
40
41 InbandTextTrackPrivateLegacyAVFObjC::InbandTextTrackPrivateLegacyAVFObjC(MediaPlayerPrivateAVFoundationObjC* player, AVPlayerItemTrack* track)
42     : InbandTextTrackPrivateAVF(player, InbandTextTrackPrivate::Generic)
43     , m_playerItemTrack(track)
44 {
45 }
46
47 void InbandTextTrackPrivateLegacyAVFObjC::disconnect()
48 {
49     m_playerItemTrack = 0;
50     InbandTextTrackPrivateAVF::disconnect();
51 }
52
53 InbandTextTrackPrivate::Kind InbandTextTrackPrivateLegacyAVFObjC::kind() const
54 {
55     if (!m_playerItemTrack)
56         return InbandTextTrackPrivate::None;
57
58     return InbandTextTrackPrivate::Captions;
59 }
60
61 bool InbandTextTrackPrivateLegacyAVFObjC::isClosedCaptions() const
62 {
63     return m_playerItemTrack;
64 }
65
66 bool InbandTextTrackPrivateLegacyAVFObjC::containsOnlyForcedSubtitles() const
67 {
68     return false;
69 }
70
71 bool InbandTextTrackPrivateLegacyAVFObjC::isMainProgramContent() const
72 {
73     return m_playerItemTrack;
74 }
75
76 bool InbandTextTrackPrivateLegacyAVFObjC::isEasyToRead() const
77 {
78     return false;
79 }
80
81 AtomicString InbandTextTrackPrivateLegacyAVFObjC::label() const
82 {
83     if (!m_playerItemTrack)
84         return emptyAtom();
85
86     NSString *title = 0;
87
88     NSArray *titles = [PAL::getAVMetadataItemClass() metadataItemsFromArray:[[m_playerItemTrack assetTrack] commonMetadata] withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
89     if ([titles count]) {
90         // If possible, return a title in one of the user's preferred languages.
91         NSArray *titlesForPreferredLanguages = [PAL::getAVMetadataItemClass() metadataItemsFromArray:titles filteredAndSortedAccordingToPreferredLanguages:[NSLocale preferredLanguages]];
92         if ([titlesForPreferredLanguages count])
93             title = [[titlesForPreferredLanguages objectAtIndex:0] stringValue];
94
95         if (!title)
96             title = [[titles objectAtIndex:0] stringValue];
97     }
98
99     return title ? AtomicString(title) : emptyAtom();
100 }
101
102 AtomicString InbandTextTrackPrivateLegacyAVFObjC::language() const
103 {
104     if (!m_playerItemTrack)
105         return emptyAtom();
106
107     NSString *languageCode = [[m_playerItemTrack assetTrack] languageCode];
108     RetainPtr<NSLocale> locale = adoptNS([[NSLocale alloc] initWithLocaleIdentifier:languageCode]);
109     return [locale localeIdentifier];
110 }
111
112 } // namespace WebCore
113
114 #endif