[Mac] process raw VTT in-band captions
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / AVTrackPrivateAVFObjCImpl.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 #include "config.h"
27 #include "AVTrackPrivateAVFObjCImpl.h"
28
29 #if ENABLE(VIDEO_TRACK)
30
31 #import "SoftLinking.h"
32 #import <objc/runtime.h>
33 #import <AVFoundation/AVAssetTrack.h>
34 #import <AVFoundation/AVPlayerItemTrack.h>
35 #import <AVFoundation/AVMetadataItem.h>
36
37 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
38
39 SOFT_LINK_CLASS(AVFoundation, AVAssetTrack)
40 SOFT_LINK_CLASS(AVFoundation, AVPlayerItemTrack)
41 SOFT_LINK_CLASS(AVFoundation, AVMetadataItem)
42
43 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaCharacteristicIsMainProgramContent, NSString *)
44 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaCharacteristicDescribesVideoForAccessibility, NSString *)
45 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaCharacteristicIsAuxiliaryContent, NSString *)
46 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, NSString *)
47 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMetadataCommonKeyTitle, NSString *)
48 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMetadataKeySpaceCommon, NSString *)
49
50 #define AVMetadataItem getAVMetadataItemClass()
51
52 #define AVMediaCharacteristicIsMainProgramContent getAVMediaCharacteristicIsMainProgramContent()
53 #define AVMediaCharacteristicDescribesVideoForAccessibility getAVMediaCharacteristicDescribesVideoForAccessibility()
54 #define AVMediaCharacteristicIsAuxiliaryContent getAVMediaCharacteristicIsAuxiliaryContent()
55 #define AVMediaCharacteristicTranscribesSpokenDialogForAccessibility getAVMediaCharacteristicTranscribesSpokenDialogForAccessibility()
56 #define AVMetadataCommonKeyTitle getAVMetadataCommonKeyTitle()
57 #define AVMetadataKeySpaceCommon getAVMetadataKeySpaceCommon()
58
59 namespace WebCore {
60
61 AVTrackPrivateAVFObjCImpl::AVTrackPrivateAVFObjCImpl(AVPlayerItemTrack* track)
62     : m_playerItemTrack(track)
63     , m_assetTrack([track assetTrack])
64 {
65 }
66
67 AVTrackPrivateAVFObjCImpl::AVTrackPrivateAVFObjCImpl(AVAssetTrack* track)
68     : m_assetTrack(track)
69 {
70 }
71
72 bool AVTrackPrivateAVFObjCImpl::enabled() const
73 {
74     ASSERT(m_playerItemTrack);
75     return [m_playerItemTrack isEnabled];
76 }
77
78 void AVTrackPrivateAVFObjCImpl::setEnabled(bool enabled)
79 {
80     ASSERT(m_playerItemTrack);
81     [m_playerItemTrack setEnabled:enabled];
82 }
83
84 AudioTrackPrivate::Kind AVTrackPrivateAVFObjCImpl::audioKind() const
85 {
86     if ([m_assetTrack hasMediaCharacteristic:AVMediaCharacteristicIsAuxiliaryContent])
87         return AudioTrackPrivate::Alternative;
88     else if ([m_assetTrack hasMediaCharacteristic:AVMediaCharacteristicIsMainProgramContent])
89         return AudioTrackPrivate::Main;
90     return AudioTrackPrivate::None;
91 }
92
93 VideoTrackPrivate::Kind AVTrackPrivateAVFObjCImpl::videoKind() const
94 {
95     if ([m_assetTrack hasMediaCharacteristic:AVMediaCharacteristicDescribesVideoForAccessibility])
96         return VideoTrackPrivate::Sign;
97     else if ([m_assetTrack hasMediaCharacteristic:AVMediaCharacteristicTranscribesSpokenDialogForAccessibility])
98         return VideoTrackPrivate::Captions;
99     else if ([m_assetTrack hasMediaCharacteristic:AVMediaCharacteristicIsAuxiliaryContent])
100         return VideoTrackPrivate::Alternative;
101     else if ([m_assetTrack hasMediaCharacteristic:AVMediaCharacteristicIsMainProgramContent])
102         return VideoTrackPrivate::Main;
103     return VideoTrackPrivate::None;
104 }
105
106 AtomicString AVTrackPrivateAVFObjCImpl::id() const
107 {
108     return String::format("%d", [m_assetTrack trackID]);
109 }
110
111 AtomicString AVTrackPrivateAVFObjCImpl::label() const
112 {
113     NSArray *titles = [AVMetadataItem metadataItemsFromArray:[m_assetTrack commonMetadata] withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
114     if (![titles count])
115         return emptyAtom;
116
117     // If possible, return a title in one of the user's preferred languages.
118     NSArray *titlesForPreferredLanguages = [AVMetadataItem metadataItemsFromArray:titles filteredAndSortedAccordingToPreferredLanguages:[NSLocale preferredLanguages]];
119     if ([titlesForPreferredLanguages count])
120         return [[titlesForPreferredLanguages objectAtIndex:0] stringValue];
121     return [[titles objectAtIndex:0] stringValue];
122 }
123
124 AtomicString AVTrackPrivateAVFObjCImpl::language() const
125 {
126     return languageForAVAssetTrack(m_assetTrack.get());
127 }
128
129 String AVTrackPrivateAVFObjCImpl::languageForAVAssetTrack(AVAssetTrack* track)
130 {
131     NSString *language = [track extendedLanguageTag];
132
133     // If the language code is stored as a QuickTime 5-bit packed code there aren't enough bits for a full
134     // RFC 4646 language tag so extendedLanguageTag returns NULL. In this case languageCode will return the
135     // ISO 639-2/T language code so check it.
136     if (!language)
137         language = [track languageCode];
138
139     // Some legacy tracks have "und" as a language, treat that the same as no language at all.
140     if (!language || [language isEqualToString:@"und"])
141         return emptyString();
142
143     return language;
144 }
145
146 int AVTrackPrivateAVFObjCImpl::trackID() const
147 {
148     return [m_assetTrack trackID];
149 }
150
151 }
152
153 #endif // ENABLE(VIDEO_TRACK)