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