Unreviewed, rolling out r244627.
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / InbandTextTrackPrivateAVFObjC.mm
1 /*
2  * Copyright (C) 2012, 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_MEDIA_SELECTION_GROUP)
29
30 #import "InbandTextTrackPrivateAVFObjC.h"
31
32 #import "FloatConversion.h"
33 #import "InbandTextTrackPrivate.h"
34 #import "InbandTextTrackPrivateAVF.h"
35 #import "Logging.h"
36 #import <AVFoundation/AVMediaSelectionGroup.h>
37 #import <AVFoundation/AVMetadataItem.h>
38 #import <AVFoundation/AVPlayer.h>
39 #import <AVFoundation/AVPlayerItem.h>
40 #import <AVFoundation/AVPlayerItemOutput.h>
41 #import <objc/runtime.h>
42 #import <wtf/SoftLinking.h>
43
44 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
45
46 SOFT_LINK_CLASS(AVFoundation, AVPlayer)
47 SOFT_LINK_CLASS(AVFoundation, AVPlayerItem)
48 SOFT_LINK_CLASS(AVFoundation, AVMetadataItem)
49 SOFT_LINK_CLASS(AVFoundation, AVPlayerItemLegibleOutput)
50 #define AVMediaCharacteristicVisual getAVMediaCharacteristicVisual()
51 #define AVMediaCharacteristicAudible getAVMediaCharacteristicAudible()
52 #define AVMediaTypeClosedCaption getAVMediaTypeClosedCaption()
53 #define AVMediaCharacteristicContainsOnlyForcedSubtitles getAVMediaCharacteristicContainsOnlyForcedSubtitles()
54 #define AVMediaCharacteristicIsMainProgramContent getAVMediaCharacteristicIsMainProgramContent()
55 #define AVMediaCharacteristicEasyToRead getAVMediaCharacteristicEasyToRead()
56
57 SOFT_LINK_CONSTANT(AVFoundation, AVMediaTypeClosedCaption, NSString *)
58 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicLegible, NSString *)
59 SOFT_LINK_CONSTANT(AVFoundation, AVMetadataCommonKeyTitle, NSString *)
60 SOFT_LINK_CONSTANT(AVFoundation, AVMetadataKeySpaceCommon, NSString *)
61 SOFT_LINK_CONSTANT(AVFoundation, AVMediaTypeSubtitle, NSString *)
62 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, NSString *)
63 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicDescribesMusicAndSoundForAccessibility, NSString *)
64 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicContainsOnlyForcedSubtitles, NSString *)
65 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicIsMainProgramContent, NSString *)
66 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicEasyToRead, NSString *)
67
68 #define AVPlayer getAVPlayerClass()
69 #define AVPlayerItem getAVPlayerItemClass()
70 #define AVMetadataItem getAVMetadataItemClass()
71 #define AVPlayerItemLegibleOutput getAVPlayerItemLegibleOutputClass()
72 #define AVMediaCharacteristicLegible getAVMediaCharacteristicLegible()
73 #define AVMetadataCommonKeyTitle getAVMetadataCommonKeyTitle()
74 #define AVMetadataKeySpaceCommon getAVMetadataKeySpaceCommon()
75 #define AVMediaTypeSubtitle getAVMediaTypeSubtitle()
76 #define AVMediaCharacteristicTranscribesSpokenDialogForAccessibility getAVMediaCharacteristicTranscribesSpokenDialogForAccessibility()
77 #define AVMediaCharacteristicDescribesMusicAndSoundForAccessibility getAVMediaCharacteristicDescribesMusicAndSoundForAccessibility()
78
79 namespace WebCore {
80
81 InbandTextTrackPrivateAVFObjC::InbandTextTrackPrivateAVFObjC(AVFInbandTrackParent* player, AVMediaSelectionOption *selection, InbandTextTrackPrivate::CueFormat format)
82     : InbandTextTrackPrivateAVF(player, format)
83     , m_mediaSelectionOption(selection)
84 {
85 }
86
87 void InbandTextTrackPrivateAVFObjC::disconnect()
88 {
89     m_mediaSelectionOption = 0;
90     InbandTextTrackPrivateAVF::disconnect();
91 }
92
93 InbandTextTrackPrivate::Kind InbandTextTrackPrivateAVFObjC::kind() const
94 {
95     if (!m_mediaSelectionOption)
96         return InbandTextTrackPrivate::None;
97
98     NSString *mediaType = [m_mediaSelectionOption mediaType];
99     
100     if ([mediaType isEqualToString:AVMediaTypeClosedCaption])
101         return InbandTextTrackPrivate::Captions;
102     if ([mediaType isEqualToString:AVMediaTypeSubtitle]) {
103
104         if ([m_mediaSelectionOption hasMediaCharacteristic:AVMediaCharacteristicContainsOnlyForcedSubtitles])
105             return InbandTextTrackPrivate::Forced;
106
107         // An "SDH" track is a subtitle track created for the deaf or hard-of-hearing. "captions" in WebVTT are
108         // "labeled as appropriate for the hard-of-hearing", so tag SDH sutitles as "captions".
109         if ([m_mediaSelectionOption hasMediaCharacteristic:AVMediaCharacteristicTranscribesSpokenDialogForAccessibility])
110             return InbandTextTrackPrivate::Captions;
111         if ([m_mediaSelectionOption hasMediaCharacteristic:AVMediaCharacteristicDescribesMusicAndSoundForAccessibility])
112             return InbandTextTrackPrivate::Captions;
113         
114         return InbandTextTrackPrivate::Subtitles;
115     }
116
117     return InbandTextTrackPrivate::Captions;
118 }
119
120 bool InbandTextTrackPrivateAVFObjC::isClosedCaptions() const
121 {
122     if (!m_mediaSelectionOption)
123         return false;
124     
125     return [[m_mediaSelectionOption mediaType] isEqualToString:AVMediaTypeClosedCaption];
126 }
127
128 bool InbandTextTrackPrivateAVFObjC::isSDH() const
129 {
130     if (!m_mediaSelectionOption)
131         return false;
132     
133     if (![[m_mediaSelectionOption mediaType] isEqualToString:AVMediaTypeSubtitle])
134         return false;
135
136     if ([m_mediaSelectionOption hasMediaCharacteristic:AVMediaCharacteristicTranscribesSpokenDialogForAccessibility] && [m_mediaSelectionOption hasMediaCharacteristic:AVMediaCharacteristicDescribesMusicAndSoundForAccessibility])
137         return true;
138
139     return false;
140 }
141     
142 bool InbandTextTrackPrivateAVFObjC::containsOnlyForcedSubtitles() const
143 {
144     if (!m_mediaSelectionOption)
145         return false;
146     
147     return [m_mediaSelectionOption hasMediaCharacteristic:AVMediaCharacteristicContainsOnlyForcedSubtitles];
148 }
149
150 bool InbandTextTrackPrivateAVFObjC::isMainProgramContent() const
151 {
152     if (!m_mediaSelectionOption)
153         return false;
154     
155     return [m_mediaSelectionOption hasMediaCharacteristic:AVMediaCharacteristicIsMainProgramContent];
156 }
157
158 bool InbandTextTrackPrivateAVFObjC::isEasyToRead() const
159 {
160     if (!m_mediaSelectionOption)
161         return false;
162
163     return [m_mediaSelectionOption hasMediaCharacteristic:AVMediaCharacteristicEasyToRead];
164 }
165
166 AtomicString InbandTextTrackPrivateAVFObjC::label() const
167 {
168     if (!m_mediaSelectionOption)
169         return emptyAtom();
170
171     NSString *title = 0;
172
173     NSArray *titles = [AVMetadataItem metadataItemsFromArray:[m_mediaSelectionOption.get() commonMetadata] withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
174     if ([titles count]) {
175         // If possible, return a title in one of the user's preferred languages.
176         NSArray *titlesForPreferredLanguages = [AVMetadataItem metadataItemsFromArray:titles filteredAndSortedAccordingToPreferredLanguages:[NSLocale preferredLanguages]];
177         if ([titlesForPreferredLanguages count])
178             title = [[titlesForPreferredLanguages objectAtIndex:0] stringValue];
179
180         if (!title)
181             title = [[titles objectAtIndex:0] stringValue];
182     }
183
184     return title ? AtomicString(title) : emptyAtom();
185 }
186
187 AtomicString InbandTextTrackPrivateAVFObjC::language() const
188 {
189     if (!m_mediaSelectionOption)
190         return emptyAtom();
191
192     return [[m_mediaSelectionOption.get() locale] localeIdentifier];
193 }
194
195 bool InbandTextTrackPrivateAVFObjC::isDefault() const
196 {
197     return false;
198 }
199
200 } // namespace WebCore
201
202 #endif