Start cleaning up iOS upstreaming #ifs in platform/graphics
[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 COMPUTER, 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 COMPUTER, 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)
29
30 #import "InbandTextTrackPrivateLegacyAVFObjC.h"
31
32 #import "InbandTextTrackPrivateAVF.h"
33 #import "Logging.h"
34 #import "MediaPlayerPrivateAVFoundationObjC.h"
35 #import "SoftLinking.h"
36 #import <AVFoundation/AVFoundation.h>
37 #import <objc/runtime.h>
38
39 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
40 #define AVPlayerItem getAVPlayerItemClass()
41
42 SOFT_LINK_CLASS(AVFoundation, AVPlayerItem)
43 SOFT_LINK_CLASS(AVFoundation, AVMetadataItem)
44 #define AVMediaTypeClosedCaption getAVMediaTypeClosedCaption()
45
46 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeClosedCaption, NSString *)
47 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicLegible, NSString *)
48 SOFT_LINK_POINTER(AVFoundation, AVMetadataCommonKeyTitle, NSString *)
49 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceCommon, NSString *)
50
51 #define AVMetadataItem getAVMetadataItemClass()
52 #define AVMediaCharacteristicLegible getAVMediaCharacteristicLegible()
53 #define AVMetadataCommonKeyTitle getAVMetadataCommonKeyTitle()
54 #define AVMetadataKeySpaceCommon getAVMetadataKeySpaceCommon()
55
56 namespace WebCore {
57
58 InbandTextTrackPrivateLegacyAVFObjC::InbandTextTrackPrivateLegacyAVFObjC(MediaPlayerPrivateAVFoundationObjC* player, AVPlayerItemTrack* track)
59     : InbandTextTrackPrivateAVF(player)
60     , m_playerItemTrack(track)
61 {
62 }
63
64 void InbandTextTrackPrivateLegacyAVFObjC::disconnect()
65 {
66     m_playerItemTrack = 0;
67     InbandTextTrackPrivateAVF::disconnect();
68 }
69
70 InbandTextTrackPrivate::Kind InbandTextTrackPrivateLegacyAVFObjC::kind() const
71 {
72     if (!m_playerItemTrack)
73         return InbandTextTrackPrivate::None;
74
75     return InbandTextTrackPrivate::Captions;
76 }
77
78 bool InbandTextTrackPrivateLegacyAVFObjC::isClosedCaptions() const
79 {
80     return m_playerItemTrack;
81 }
82
83 bool InbandTextTrackPrivateLegacyAVFObjC::containsOnlyForcedSubtitles() const
84 {
85     return false;
86 }
87
88 bool InbandTextTrackPrivateLegacyAVFObjC::isMainProgramContent() const
89 {
90     return m_playerItemTrack;
91 }
92
93 bool InbandTextTrackPrivateLegacyAVFObjC::isEasyToRead() const
94 {
95     return false;
96 }
97
98 AtomicString InbandTextTrackPrivateLegacyAVFObjC::label() const
99 {
100     if (!m_playerItemTrack)
101         return emptyAtom;
102
103     NSString *title = 0;
104
105     NSArray *titles = [AVMetadataItem metadataItemsFromArray:[[m_playerItemTrack assetTrack] commonMetadata] withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
106     if ([titles count]) {
107         // If possible, return a title in one of the user's preferred languages.
108         NSArray *titlesForPreferredLanguages = [AVMetadataItem metadataItemsFromArray:titles filteredAndSortedAccordingToPreferredLanguages:[NSLocale preferredLanguages]];
109         if ([titlesForPreferredLanguages count])
110             title = [[titlesForPreferredLanguages objectAtIndex:0] stringValue];
111
112         if (!title)
113             title = [[titles objectAtIndex:0] stringValue];
114     }
115
116     return title ? AtomicString(title) : emptyAtom;
117 }
118
119 AtomicString InbandTextTrackPrivateLegacyAVFObjC::language() const
120 {
121     if (!m_playerItemTrack)
122         return emptyAtom;
123
124     NSString *languageCode = [[m_playerItemTrack assetTrack] languageCode];
125     RetainPtr<NSLocale> locale = adoptNS([[NSLocale alloc] initWithLocaleIdentifier:languageCode]);
126     return [locale localeIdentifier];
127 }
128
129 } // namespace WebCore
130
131 #endif