e0bef21b6a4b8327c457cf9f029b3f6d72583c97
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / MediaPlayerPrivateAVFoundationObjC.mm
1 /*
2  * Copyright (C) 2011-2017 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 #import "MediaPlayerPrivateAVFoundationObjC.h"
28
29 #if ENABLE(VIDEO) && USE(AVFOUNDATION)
30
31 #import "AVAssetTrackUtilities.h"
32 #import "AVFoundationMIMETypeCache.h"
33 #import "AVTrackPrivateAVFObjCImpl.h"
34 #import "AudioSourceProviderAVFObjC.h"
35 #import "AudioTrackPrivateAVFObjC.h"
36 #import "AuthenticationChallenge.h"
37 #import "CDMInstanceFairPlayStreamingAVFObjC.h"
38 #import "CDMSessionAVFoundationObjC.h"
39 #import "Cookie.h"
40 #import "DeprecatedGlobalSettings.h"
41 #import "Extensions3D.h"
42 #import "FloatConversion.h"
43 #import "GraphicsContext.h"
44 #import "GraphicsContext3D.h"
45 #import "GraphicsContextCG.h"
46 #import "InbandMetadataTextTrackPrivateAVF.h"
47 #import "InbandTextTrackPrivateAVFObjC.h"
48 #import "InbandTextTrackPrivateLegacyAVFObjC.h"
49 #import "Logging.h"
50 #import "MediaPlaybackTargetMac.h"
51 #import "MediaPlaybackTargetMock.h"
52 #import "MediaSelectionGroupAVFObjC.h"
53 #import "OutOfBandTextTrackPrivateAVF.h"
54 #import "PixelBufferConformerCV.h"
55 #import "PlatformTimeRanges.h"
56 #import "SecurityOrigin.h"
57 #import "SerializedPlatformRepresentationMac.h"
58 #import "SharedBuffer.h"
59 #import "TextEncoding.h"
60 #import "TextTrackRepresentation.h"
61 #import "TextureCacheCV.h"
62 #import "URL.h"
63 #import "VideoFullscreenLayerManagerObjC.h"
64 #import "VideoTextureCopierCV.h"
65 #import "VideoTrackPrivateAVFObjC.h"
66 #import "WebCoreAVFResourceLoader.h"
67 #import "WebCoreCALayerExtras.h"
68 #import "WebCoreNSURLSession.h"
69 #import <JavaScriptCore/DataView.h>
70 #import <JavaScriptCore/JSCInlines.h>
71 #import <JavaScriptCore/TypedArrayInlines.h>
72 #import <JavaScriptCore/Uint16Array.h>
73 #import <JavaScriptCore/Uint32Array.h>
74 #import <JavaScriptCore/Uint8Array.h>
75 #import <functional>
76 #import <objc/runtime.h>
77 #import <pal/avfoundation/MediaTimeAVFoundation.h>
78 #import <pal/spi/cocoa/QuartzCoreSPI.h>
79 #import <pal/spi/mac/AVFoundationSPI.h>
80 #import <wtf/BlockObjCExceptions.h>
81 #import <wtf/ListHashSet.h>
82 #import <wtf/NeverDestroyed.h>
83 #import <wtf/OSObjectPtr.h>
84 #import <wtf/text/CString.h>
85
86 #if ENABLE(AVF_CAPTIONS)
87 #include "TextTrack.h"
88 #endif
89
90 #import <AVFoundation/AVAssetImageGenerator.h>
91 #import <AVFoundation/AVAssetTrack.h>
92 #import <AVFoundation/AVMediaSelectionGroup.h>
93 #import <AVFoundation/AVMetadataItem.h>
94 #import <AVFoundation/AVPlayer.h>
95 #import <AVFoundation/AVPlayerItem.h>
96 #import <AVFoundation/AVPlayerItemOutput.h>
97 #import <AVFoundation/AVPlayerItemTrack.h>
98 #import <AVFoundation/AVPlayerLayer.h>
99 #import <AVFoundation/AVTime.h>
100
101 #if PLATFORM(IOS)
102 #import "WAKAppKitStubs.h"
103 #import <CoreImage/CoreImage.h>
104 #import <UIKit/UIDevice.h>
105 #import <mach/mach_port.h>
106 #else
107 #import <Foundation/NSGeometry.h>
108 #import <QuartzCore/CoreImage.h>
109 #endif
110
111 #if USE(VIDEOTOOLBOX)
112 #import <CoreVideo/CoreVideo.h>
113 #import <VideoToolbox/VideoToolbox.h>
114 #endif
115
116 #import "CoreVideoSoftLink.h"
117 #import "MediaRemoteSoftLink.h"
118
119 namespace std {
120 template <> struct iterator_traits<HashSet<RefPtr<WebCore::MediaSelectionOptionAVFObjC>>::iterator> {
121     typedef RefPtr<WebCore::MediaSelectionOptionAVFObjC> value_type;
122 };
123 }
124
125 #if ENABLE(AVF_CAPTIONS)
126 // Note: This must be defined before our SOFT_LINK macros:
127 @class AVMediaSelectionOption;
128 @interface AVMediaSelectionOption (OutOfBandExtensions)
129 @property (nonatomic, readonly) NSString* outOfBandSource;
130 @property (nonatomic, readonly) NSString* outOfBandIdentifier;
131 @end
132 #endif
133
134 @interface AVURLAsset (WebKitExtensions)
135 @property (nonatomic, readonly) NSURL *resolvedURL;
136 @end
137
138 typedef AVPlayer AVPlayerType;
139 typedef AVPlayerItem AVPlayerItemType;
140 typedef AVPlayerItemLegibleOutput AVPlayerItemLegibleOutputType;
141 typedef AVPlayerItemVideoOutput AVPlayerItemVideoOutputType;
142 typedef AVMetadataItem AVMetadataItemType;
143 typedef AVMediaSelectionGroup AVMediaSelectionGroupType;
144 typedef AVMediaSelectionOption AVMediaSelectionOptionType;
145 typedef AVAssetCache AVAssetCacheType;
146
147 #pragma mark - Soft Linking
148
149 // Soft-linking headers must be included last since they #define functions, constants, etc.
150 #import <pal/cf/CoreMediaSoftLink.h>
151
152 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
153
154 SOFT_LINK_FRAMEWORK_OPTIONAL(CoreImage)
155
156 SOFT_LINK_CLASS_FOR_SOURCE(WebCore, AVFoundation, AVPlayer)
157 SOFT_LINK_CLASS_FOR_SOURCE(WebCore, AVFoundation, AVPlayerItem)
158 SOFT_LINK_CLASS_FOR_SOURCE(WebCore, AVFoundation, AVPlayerItemVideoOutput)
159 SOFT_LINK_CLASS_FOR_SOURCE(WebCore, AVFoundation, AVPlayerLayer)
160 SOFT_LINK_CLASS_FOR_SOURCE(WebCore, AVFoundation, AVURLAsset)
161 SOFT_LINK_CLASS_FOR_SOURCE(WebCore, AVFoundation, AVAssetImageGenerator)
162 SOFT_LINK_CLASS_FOR_SOURCE(WebCore, AVFoundation, AVMetadataItem)
163 SOFT_LINK_CLASS_FOR_SOURCE(WebCore, AVFoundation, AVAssetCache)
164
165 SOFT_LINK_CLASS(CoreImage, CIContext)
166 SOFT_LINK_CLASS(CoreImage, CIImage)
167
168 SOFT_LINK_POINTER(AVFoundation, AVAudioTimePitchAlgorithmSpectral, NSString*)
169 SOFT_LINK_POINTER(AVFoundation, AVAudioTimePitchAlgorithmVarispeed, NSString*)
170 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicVisual, NSString *)
171 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicAudible, NSString *)
172 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeClosedCaption, NSString *)
173 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeVideo, NSString *)
174 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeAudio, NSString *)
175 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeMetadata, NSString *)
176 SOFT_LINK_POINTER(AVFoundation, AVPlayerItemDidPlayToEndTimeNotification, NSString *)
177 SOFT_LINK_POINTER(AVFoundation, AVURLAssetInheritURIQueryComponentFromReferencingURIKey, NSString *)
178 SOFT_LINK_POINTER(AVFoundation, AVAssetImageGeneratorApertureModeCleanAperture, NSString *)
179 SOFT_LINK_POINTER(AVFoundation, AVURLAssetReferenceRestrictionsKey, NSString *)
180 SOFT_LINK_POINTER(AVFoundation, AVLayerVideoGravityResizeAspect, NSString *)
181 SOFT_LINK_POINTER(AVFoundation, AVLayerVideoGravityResizeAspectFill, NSString *)
182 SOFT_LINK_POINTER(AVFoundation, AVLayerVideoGravityResize, NSString *)
183
184 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVURLAssetClientBundleIdentifierKey, NSString *)
185 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVURLAssetRequiresCustomURLLoadingKey, NSString *)
186 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVURLAssetOutOfBandMIMETypeKey, NSString *)
187 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVURLAssetUseClientURLLoadingExclusively, NSString *)
188
189 #define AVPlayer initAVPlayer()
190 #define AVPlayerItem initAVPlayerItem()
191 #define AVPlayerLayer initAVPlayerLayer()
192 #define AVURLAsset initAVURLAsset()
193 #define AVAssetImageGenerator initAVAssetImageGenerator()
194 #define AVPlayerItemVideoOutput initAVPlayerItemVideoOutput()
195 #define AVMetadataItem initAVMetadataItem()
196 #define AVAssetCache initAVAssetCache()
197
198 #define AVAudioTimePitchAlgorithmSpectral getAVAudioTimePitchAlgorithmSpectral()
199 #define AVAudioTimePitchAlgorithmVarispeed getAVAudioTimePitchAlgorithmVarispeed()
200 #define AVMediaCharacteristicVisual getAVMediaCharacteristicVisual()
201 #define AVMediaCharacteristicAudible getAVMediaCharacteristicAudible()
202 #define AVMediaTypeClosedCaption getAVMediaTypeClosedCaption()
203 #define AVMediaTypeVideo getAVMediaTypeVideo()
204 #define AVMediaTypeAudio getAVMediaTypeAudio()
205 #define AVMediaTypeMetadata getAVMediaTypeMetadata()
206 #define AVPlayerItemDidPlayToEndTimeNotification getAVPlayerItemDidPlayToEndTimeNotification()
207 #define AVURLAssetInheritURIQueryComponentFromReferencingURIKey getAVURLAssetInheritURIQueryComponentFromReferencingURIKey()
208 #define AVURLAssetClientBundleIdentifierKey getAVURLAssetClientBundleIdentifierKey()
209 #define AVURLAssetRequiresCustomURLLoadingKey getAVURLAssetRequiresCustomURLLoadingKey()
210 #define AVURLAssetOutOfBandMIMETypeKey getAVURLAssetOutOfBandMIMETypeKey()
211 #define AVURLAssetUseClientURLLoadingExclusively getAVURLAssetUseClientURLLoadingExclusively()
212 #define AVAssetImageGeneratorApertureModeCleanAperture getAVAssetImageGeneratorApertureModeCleanAperture()
213 #define AVURLAssetReferenceRestrictionsKey getAVURLAssetReferenceRestrictionsKey()
214 #define AVLayerVideoGravityResizeAspect getAVLayerVideoGravityResizeAspect()
215 #define AVLayerVideoGravityResizeAspectFill getAVLayerVideoGravityResizeAspectFill()
216 #define AVLayerVideoGravityResize getAVLayerVideoGravityResize()
217
218 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
219 typedef AVMediaSelectionGroup AVMediaSelectionGroupType;
220 typedef AVMediaSelectionOption AVMediaSelectionOptionType;
221
222 SOFT_LINK_CLASS(AVFoundation, AVPlayerItemLegibleOutput)
223 SOFT_LINK_CLASS(AVFoundation, AVMediaSelectionGroup)
224 SOFT_LINK_CLASS(AVFoundation, AVMediaSelectionOption)
225
226 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicLegible, NSString *)
227 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeSubtitle, NSString *)
228 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicContainsOnlyForcedSubtitles, NSString *)
229 SOFT_LINK_POINTER(AVFoundation, AVPlayerItemLegibleOutputTextStylingResolutionSourceAndRulesOnly, NSString *)
230
231 #define AVPlayerItemLegibleOutput getAVPlayerItemLegibleOutputClass()
232 #define AVMediaSelectionGroup getAVMediaSelectionGroupClass()
233 #define AVMediaSelectionOption getAVMediaSelectionOptionClass()
234 #define AVMediaCharacteristicLegible getAVMediaCharacteristicLegible()
235 #define AVMediaTypeSubtitle getAVMediaTypeSubtitle()
236 #define AVMediaCharacteristicContainsOnlyForcedSubtitles getAVMediaCharacteristicContainsOnlyForcedSubtitles()
237 #define AVPlayerItemLegibleOutputTextStylingResolutionSourceAndRulesOnly getAVPlayerItemLegibleOutputTextStylingResolutionSourceAndRulesOnly()
238 #endif
239
240 #if ENABLE(AVF_CAPTIONS)
241 SOFT_LINK_POINTER(AVFoundation, AVURLAssetCacheKey, NSString*)
242 SOFT_LINK_POINTER(AVFoundation, AVURLAssetHTTPCookiesKey, NSString*)
243 SOFT_LINK_POINTER(AVFoundation, AVURLAssetOutOfBandAlternateTracksKey, NSString*)
244 SOFT_LINK_POINTER(AVFoundation, AVURLAssetUsesNoPersistentCacheKey, NSString*)
245 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackDisplayNameKey, NSString*)
246 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackExtendedLanguageTagKey, NSString*)
247 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackIsDefaultKey, NSString*)
248 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackMediaCharactersticsKey, NSString*)
249 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackIdentifierKey, NSString*)
250 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackSourceKey, NSString*)
251 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicDescribesMusicAndSoundForAccessibility, NSString*)
252 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, NSString*)
253 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicIsAuxiliaryContent, NSString*)
254
255 #define AVURLAssetHTTPCookiesKey getAVURLAssetHTTPCookiesKey()
256 #define AVURLAssetOutOfBandAlternateTracksKey getAVURLAssetOutOfBandAlternateTracksKey()
257 #define AVURLAssetCacheKey getAVURLAssetCacheKey()
258 #define AVURLAssetUsesNoPersistentCacheKey getAVURLAssetUsesNoPersistentCacheKey()
259 #define AVOutOfBandAlternateTrackDisplayNameKey getAVOutOfBandAlternateTrackDisplayNameKey()
260 #define AVOutOfBandAlternateTrackExtendedLanguageTagKey getAVOutOfBandAlternateTrackExtendedLanguageTagKey()
261 #define AVOutOfBandAlternateTrackIsDefaultKey getAVOutOfBandAlternateTrackIsDefaultKey()
262 #define AVOutOfBandAlternateTrackMediaCharactersticsKey getAVOutOfBandAlternateTrackMediaCharactersticsKey()
263 #define AVOutOfBandAlternateTrackIdentifierKey getAVOutOfBandAlternateTrackIdentifierKey()
264 #define AVOutOfBandAlternateTrackSourceKey getAVOutOfBandAlternateTrackSourceKey()
265 #define AVMediaCharacteristicDescribesMusicAndSoundForAccessibility getAVMediaCharacteristicDescribesMusicAndSoundForAccessibility()
266 #define AVMediaCharacteristicTranscribesSpokenDialogForAccessibility getAVMediaCharacteristicTranscribesSpokenDialogForAccessibility()
267 #define AVMediaCharacteristicIsAuxiliaryContent getAVMediaCharacteristicIsAuxiliaryContent()
268 #endif
269
270 #if ENABLE(DATACUE_VALUE)
271 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceQuickTimeUserData, NSString*)
272 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMetadataKeySpaceISOUserData, NSString*)
273 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceQuickTimeMetadata, NSString*)
274 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceiTunes, NSString*)
275 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceID3, NSString*)
276
277 #define AVMetadataKeySpaceQuickTimeUserData getAVMetadataKeySpaceQuickTimeUserData()
278 #define AVMetadataKeySpaceISOUserData getAVMetadataKeySpaceISOUserData()
279 #define AVMetadataKeySpaceQuickTimeMetadata getAVMetadataKeySpaceQuickTimeMetadata()
280 #define AVMetadataKeySpaceiTunes getAVMetadataKeySpaceiTunes()
281 #define AVMetadataKeySpaceID3 getAVMetadataKeySpaceID3()
282 #endif
283
284 #if PLATFORM(IOS)
285 SOFT_LINK_POINTER(AVFoundation, AVURLAssetBoundNetworkInterfaceName, NSString *)
286 #define AVURLAssetBoundNetworkInterfaceName getAVURLAssetBoundNetworkInterfaceName()
287 #endif
288
289 SOFT_LINK_FRAMEWORK(MediaToolbox)
290 SOFT_LINK_OPTIONAL(MediaToolbox, MTEnableCaption2015Behavior, Boolean, (), ())
291
292 #if PLATFORM(IOS)
293 #if HAVE(CELESTIAL)
294 SOFT_LINK_PRIVATE_FRAMEWORK(Celestial)
295 SOFT_LINK_POINTER(Celestial, AVController_RouteDescriptionKey_RouteCurrentlyPicked, NSString *)
296 SOFT_LINK_POINTER(Celestial, AVController_RouteDescriptionKey_RouteName, NSString *)
297 SOFT_LINK_POINTER(Celestial, AVController_RouteDescriptionKey_AVAudioRouteName, NSString *)
298 #define AVController_RouteDescriptionKey_RouteCurrentlyPicked getAVController_RouteDescriptionKey_RouteCurrentlyPicked()
299 #define AVController_RouteDescriptionKey_RouteName getAVController_RouteDescriptionKey_RouteName()
300 #define AVController_RouteDescriptionKey_AVAudioRouteName getAVController_RouteDescriptionKey_AVAudioRouteName()
301 #endif // HAVE(CELESTIAL)
302
303 SOFT_LINK_FRAMEWORK(UIKit)
304 SOFT_LINK_CLASS(UIKit, UIDevice)
305 #define UIDevice getUIDeviceClass()
306 #endif // PLATFORM(IOS)
307
308 using namespace WebCore;
309
310 enum MediaPlayerAVFoundationObservationContext {
311     MediaPlayerAVFoundationObservationContextPlayerItem,
312     MediaPlayerAVFoundationObservationContextPlayerItemTrack,
313     MediaPlayerAVFoundationObservationContextPlayer,
314     MediaPlayerAVFoundationObservationContextAVPlayerLayer,
315 };
316
317 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
318 @interface WebCoreAVFMovieObserver : NSObject <AVPlayerItemLegibleOutputPushDelegate>
319 #else
320 @interface WebCoreAVFMovieObserver : NSObject
321 #endif
322 {
323     MediaPlayerPrivateAVFoundationObjC* m_callback;
324     int m_delayCallbacks;
325 }
326 -(id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC*)callback;
327 -(void)disconnect;
328 -(void)metadataLoaded;
329 -(void)didEnd:(NSNotification *)notification;
330 -(void)observeValueForKeyPath:keyPath ofObject:(id)object change:(NSDictionary *)change context:(MediaPlayerAVFoundationObservationContext)context;
331 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
332 - (void)legibleOutput:(id)output didOutputAttributedStrings:(NSArray *)strings nativeSampleBuffers:(NSArray *)nativeSamples forItemTime:(CMTime)itemTime;
333 - (void)outputSequenceWasFlushed:(id)output;
334 #endif
335 @end
336
337 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
338 @interface WebCoreAVFLoaderDelegate : NSObject<AVAssetResourceLoaderDelegate> {
339     MediaPlayerPrivateAVFoundationObjC* m_callback;
340 }
341 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC*)callback;
342 - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest;
343 - (void)setCallback:(MediaPlayerPrivateAVFoundationObjC*)callback;
344 @end
345 #endif
346
347 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
348 @interface WebCoreAVFPullDelegate : NSObject<AVPlayerItemOutputPullDelegate> {
349     MediaPlayerPrivateAVFoundationObjC *m_callback;
350     dispatch_semaphore_t m_semaphore;
351 }
352 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC *)callback;
353 - (void)setCallback:(MediaPlayerPrivateAVFoundationObjC*)callback;
354 - (void)outputMediaDataWillChange:(AVPlayerItemOutput *)sender;
355 - (void)outputSequenceWasFlushed:(AVPlayerItemOutput *)output;
356 @end
357 #endif
358
359 namespace WebCore {
360 using namespace PAL;
361
362 static NSArray *assetMetadataKeyNames();
363 static NSArray *itemKVOProperties();
364 static NSArray *assetTrackMetadataKeyNames();
365 static NSArray *playerKVOProperties();
366 static AVAssetTrack* firstEnabledTrack(NSArray* tracks);
367
368 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
369 static dispatch_queue_t globalLoaderDelegateQueue()
370 {
371     static dispatch_queue_t globalQueue;
372     static dispatch_once_t onceToken;
373     dispatch_once(&onceToken, ^{
374         globalQueue = dispatch_queue_create("WebCoreAVFLoaderDelegate queue", DISPATCH_QUEUE_SERIAL);
375     });
376     return globalQueue;
377 }
378 #endif
379
380 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
381 static dispatch_queue_t globalPullDelegateQueue()
382 {
383     static dispatch_queue_t globalQueue;
384     static dispatch_once_t onceToken;
385     dispatch_once(&onceToken, ^{
386         globalQueue = dispatch_queue_create("WebCoreAVFPullDelegate queue", DISPATCH_QUEUE_SERIAL);
387     });
388     return globalQueue;
389 }
390 #endif
391
392 void MediaPlayerPrivateAVFoundationObjC::registerMediaEngine(MediaEngineRegistrar registrar)
393 {
394     if (!isAvailable())
395         return;
396
397     registrar([](MediaPlayer* player) { return std::make_unique<MediaPlayerPrivateAVFoundationObjC>(player); },
398             getSupportedTypes, supportsType, originsInMediaCache, clearMediaCache, clearMediaCacheForOrigins, supportsKeySystem);
399     AVFoundationMIMETypeCache::singleton().loadTypes();
400 }
401
402 static AVAssetCacheType *assetCacheForPath(const String& path)
403 {
404     NSURL *assetCacheURL;
405     
406     if (path.isEmpty())
407         assetCacheURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:@"MediaCache" isDirectory:YES];
408     else
409         assetCacheURL = [NSURL fileURLWithPath:path isDirectory:YES];
410
411     return [initAVAssetCache() assetCacheWithURL:assetCacheURL];
412 }
413
414 HashSet<RefPtr<SecurityOrigin>> MediaPlayerPrivateAVFoundationObjC::originsInMediaCache(const String& path)
415 {
416     HashSet<RefPtr<SecurityOrigin>> origins;
417     for (NSString *key in [assetCacheForPath(path) allKeys]) {
418         URL keyAsURL = URL(URL(), key);
419         if (keyAsURL.isValid())
420             origins.add(SecurityOrigin::create(keyAsURL));
421     }
422     return origins;
423 }
424
425 static WallTime toSystemClockTime(NSDate *date)
426 {
427     ASSERT(date);
428     return WallTime::fromRawSeconds(date.timeIntervalSince1970);
429 }
430
431 void MediaPlayerPrivateAVFoundationObjC::clearMediaCache(const String& path, WallTime modifiedSince)
432 {
433     AVAssetCacheType* assetCache = assetCacheForPath(path);
434     
435     for (NSString *key in [assetCache allKeys]) {
436         if (toSystemClockTime([assetCache lastModifiedDateOfEntryForKey:key]) > modifiedSince)
437             [assetCache removeEntryForKey:key];
438     }
439
440     NSFileManager *fileManager = [NSFileManager defaultManager];
441     NSURL *baseURL = [assetCache URL];
442
443     if (modifiedSince <= WallTime::fromRawSeconds(0)) {
444         [fileManager removeItemAtURL:baseURL error:nil];
445         return;
446     }
447     
448     NSArray *propertyKeys = @[NSURLNameKey, NSURLContentModificationDateKey, NSURLIsRegularFileKey];
449     NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:baseURL includingPropertiesForKeys:
450         propertyKeys options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
451         errorHandler:nil];
452     
453     RetainPtr<NSMutableArray> urlsToDelete = adoptNS([[NSMutableArray alloc] init]);
454     for (NSURL *fileURL : enumerator) {
455         NSDictionary *fileAttributes = [fileURL resourceValuesForKeys:propertyKeys error:nil];
456     
457         if (![fileAttributes[NSURLNameKey] hasPrefix:@"CachedMedia-"])
458             continue;
459         
460         if (![fileAttributes[NSURLIsRegularFileKey] boolValue])
461             continue;
462         
463         if (toSystemClockTime(fileAttributes[NSURLContentModificationDateKey]) <= modifiedSince)
464             continue;
465         
466         [urlsToDelete addObject:fileURL];
467     }
468     
469     for (NSURL *fileURL in urlsToDelete.get())
470         [fileManager removeItemAtURL:fileURL error:nil];
471 }
472
473 void MediaPlayerPrivateAVFoundationObjC::clearMediaCacheForOrigins(const String& path, const HashSet<RefPtr<SecurityOrigin>>& origins)
474 {
475     AVAssetCacheType* assetCache = assetCacheForPath(path);
476     for (NSString *key in [assetCache allKeys]) {
477         URL keyAsURL = URL(URL(), key);
478         if (keyAsURL.isValid()) {
479             if (origins.contains(SecurityOrigin::create(keyAsURL)))
480                 [assetCache removeEntryForKey:key];
481         }
482     }
483 }
484
485 MediaPlayerPrivateAVFoundationObjC::MediaPlayerPrivateAVFoundationObjC(MediaPlayer* player)
486     : MediaPlayerPrivateAVFoundation(player)
487     , m_videoFullscreenLayerManager(std::make_unique<VideoFullscreenLayerManagerObjC>())
488     , m_videoFullscreenGravity(MediaPlayer::VideoGravityResizeAspect)
489     , m_objcObserver(adoptNS([[WebCoreAVFMovieObserver alloc] initWithCallback:this]))
490     , m_videoFrameHasDrawn(false)
491     , m_haveCheckedPlayability(false)
492 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
493     , m_videoOutputDelegate(adoptNS([[WebCoreAVFPullDelegate alloc] initWithCallback:this]))
494     , m_videoOutputSemaphore(nullptr)
495 #endif
496 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
497     , m_loaderDelegate(adoptNS([[WebCoreAVFLoaderDelegate alloc] initWithCallback:this]))
498 #endif
499     , m_currentTextTrack(0)
500     , m_cachedRate(0)
501     , m_cachedTotalBytes(0)
502     , m_pendingStatusChanges(0)
503     , m_cachedItemStatus(MediaPlayerAVPlayerItemStatusDoesNotExist)
504     , m_cachedLikelyToKeepUp(false)
505     , m_cachedBufferEmpty(false)
506     , m_cachedBufferFull(false)
507     , m_cachedHasEnabledAudio(false)
508     , m_shouldBufferData(true)
509     , m_cachedIsReadyForDisplay(false)
510     , m_haveBeenAskedToCreateLayer(false)
511 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
512     , m_allowsWirelessVideoPlayback(true)
513 #endif
514 {
515 }
516
517 MediaPlayerPrivateAVFoundationObjC::~MediaPlayerPrivateAVFoundationObjC()
518 {
519 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
520     [m_loaderDelegate.get() setCallback:0];
521     [[m_avAsset.get() resourceLoader] setDelegate:nil queue:0];
522
523     for (auto& pair : m_resourceLoaderMap)
524         pair.value->invalidate();
525 #endif
526 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
527     [m_videoOutputDelegate setCallback:0];
528     [m_videoOutput setDelegate:nil queue:0];
529     if (m_videoOutputSemaphore)
530         dispatch_release(m_videoOutputSemaphore);
531 #endif
532
533     if (m_videoLayer)
534         destroyVideoLayer();
535
536     cancelLoad();
537 }
538
539 void MediaPlayerPrivateAVFoundationObjC::cancelLoad()
540 {
541     INFO_LOG(LOGIDENTIFIER);
542     tearDownVideoRendering();
543
544     [[NSNotificationCenter defaultCenter] removeObserver:m_objcObserver.get()];
545     [m_objcObserver.get() disconnect];
546
547     // Tell our observer to do nothing when our cancellation of pending loading calls its completion handler.
548     setIgnoreLoadStateChanges(true);
549     if (m_avAsset) {
550         [m_avAsset.get() cancelLoading];
551         m_avAsset = nil;
552     }
553
554     clearTextTracks();
555
556 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
557     if (m_legibleOutput) {
558         if (m_avPlayerItem)
559             [m_avPlayerItem.get() removeOutput:m_legibleOutput.get()];
560         m_legibleOutput = nil;
561     }
562 #endif
563
564     if (m_avPlayerItem) {
565         for (NSString *keyName in itemKVOProperties())
566             [m_avPlayerItem.get() removeObserver:m_objcObserver.get() forKeyPath:keyName];
567         
568         m_avPlayerItem = nil;
569     }
570     if (m_avPlayer) {
571         if (m_timeObserver)
572             [m_avPlayer.get() removeTimeObserver:m_timeObserver.get()];
573         m_timeObserver = nil;
574
575         for (NSString *keyName in playerKVOProperties())
576             [m_avPlayer.get() removeObserver:m_objcObserver.get() forKeyPath:keyName];
577
578         [m_avPlayer replaceCurrentItemWithPlayerItem:nil];
579         m_avPlayer = nil;
580     }
581
582     // Reset cached properties
583     m_pendingStatusChanges = 0;
584     m_cachedItemStatus = MediaPlayerAVPlayerItemStatusDoesNotExist;
585     m_cachedSeekableRanges = nullptr;
586     m_cachedLoadedRanges = nullptr;
587     m_cachedHasEnabledAudio = false;
588     m_cachedPresentationSize = FloatSize();
589     m_cachedDuration = MediaTime::zeroTime();
590
591     for (AVPlayerItemTrack *track in m_cachedTracks.get())
592         [track removeObserver:m_objcObserver.get() forKeyPath:@"enabled"];
593     m_cachedTracks = nullptr;
594
595 #if ENABLE(WEB_AUDIO) && USE(MEDIATOOLBOX)
596     if (m_provider) {
597         m_provider->setPlayerItem(nullptr);
598         m_provider->setAudioTrack(nullptr);
599     }
600 #endif
601
602     setIgnoreLoadStateChanges(false);
603 }
604
605 bool MediaPlayerPrivateAVFoundationObjC::hasLayerRenderer() const
606 {
607     return m_haveBeenAskedToCreateLayer;
608 }
609
610 bool MediaPlayerPrivateAVFoundationObjC::hasContextRenderer() const
611 {
612 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
613     if (m_videoOutput)
614         return true;
615 #endif
616     return m_imageGenerator;
617 }
618
619 void MediaPlayerPrivateAVFoundationObjC::createContextVideoRenderer()
620 {
621 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
622     createVideoOutput();
623 #else
624     createImageGenerator();
625 #endif
626 }
627
628 void MediaPlayerPrivateAVFoundationObjC::createImageGenerator()
629 {
630     using namespace PAL;
631     INFO_LOG(LOGIDENTIFIER);
632
633     if (!m_avAsset || m_imageGenerator)
634         return;
635
636     m_imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:m_avAsset.get()];
637
638     [m_imageGenerator.get() setApertureMode:AVAssetImageGeneratorApertureModeCleanAperture];
639     [m_imageGenerator.get() setAppliesPreferredTrackTransform:YES];
640     [m_imageGenerator.get() setRequestedTimeToleranceBefore:kCMTimeZero];
641     [m_imageGenerator.get() setRequestedTimeToleranceAfter:kCMTimeZero];
642 }
643
644 void MediaPlayerPrivateAVFoundationObjC::destroyContextVideoRenderer()
645 {
646 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
647     destroyVideoOutput();
648 #endif
649     destroyImageGenerator();
650 }
651
652 void MediaPlayerPrivateAVFoundationObjC::destroyImageGenerator()
653 {
654     if (!m_imageGenerator)
655         return;
656
657     INFO_LOG(LOGIDENTIFIER);
658
659     m_imageGenerator = 0;
660 }
661
662 void MediaPlayerPrivateAVFoundationObjC::createVideoLayer()
663 {
664     if (!m_avPlayer || m_haveBeenAskedToCreateLayer)
665         return;
666
667     callOnMainThread([this, weakThis = createWeakPtr()] {
668         if (!weakThis)
669             return;
670
671         if (!m_avPlayer || m_haveBeenAskedToCreateLayer)
672             return;
673         m_haveBeenAskedToCreateLayer = true;
674
675         if (!m_videoLayer)
676             createAVPlayerLayer();
677
678 #if USE(VIDEOTOOLBOX) && HAVE(AVFOUNDATION_VIDEO_OUTPUT)
679         if (!m_videoOutput)
680             createVideoOutput();
681 #endif
682
683         player()->client().mediaPlayerRenderingModeChanged(player());
684     });
685 }
686
687 void MediaPlayerPrivateAVFoundationObjC::createAVPlayerLayer()
688 {
689     if (!m_avPlayer)
690         return;
691
692     m_videoLayer = adoptNS([[AVPlayerLayer alloc] init]);
693     [m_videoLayer setPlayer:m_avPlayer.get()];
694
695 #ifndef NDEBUG
696     [m_videoLayer setName:@"MediaPlayerPrivate AVPlayerLayer"];
697 #endif
698     [m_videoLayer addObserver:m_objcObserver.get() forKeyPath:@"readyForDisplay" options:NSKeyValueObservingOptionNew context:(void *)MediaPlayerAVFoundationObservationContextAVPlayerLayer];
699     updateVideoLayerGravity();
700     [m_videoLayer setContentsScale:player()->client().mediaPlayerContentsScale()];
701     IntSize defaultSize = snappedIntRect(player()->client().mediaPlayerContentBoxRect()).size();
702     INFO_LOG(LOGIDENTIFIER);
703
704     m_videoFullscreenLayerManager->setVideoLayer(m_videoLayer.get(), defaultSize);
705
706 #if PLATFORM(IOS) && !ENABLE(EXTRA_ZOOM_MODE)
707     if ([m_videoLayer respondsToSelector:@selector(setPIPModeEnabled:)])
708         [m_videoLayer setPIPModeEnabled:(player()->fullscreenMode() & MediaPlayer::VideoFullscreenModePictureInPicture)];
709 #endif
710 }
711
712 void MediaPlayerPrivateAVFoundationObjC::destroyVideoLayer()
713 {
714     if (!m_videoLayer)
715         return;
716
717     INFO_LOG(LOGIDENTIFIER);
718
719     [m_videoLayer removeObserver:m_objcObserver.get() forKeyPath:@"readyForDisplay"];
720     [m_videoLayer setPlayer:nil];
721     m_videoFullscreenLayerManager->didDestroyVideoLayer();
722
723     m_videoLayer = nil;
724 }
725
726 MediaTime MediaPlayerPrivateAVFoundationObjC::getStartDate() const
727 {
728     // Date changes as the track's playback position changes. Must subtract currentTime (offset in seconds) from date offset to get date beginning
729     double date = [[m_avPlayerItem currentDate] timeIntervalSince1970] * 1000;
730
731     // No live streams were made during the epoch (1970). AVFoundation returns 0 if the media file doesn't have a start date
732     if (!date)
733         return MediaTime::invalidTime();
734
735     double currentTime = CMTimeGetSeconds([m_avPlayerItem currentTime]) * 1000;
736
737     // Rounding due to second offset error when subtracting.
738     return MediaTime::createWithDouble(round(date - currentTime));
739 }
740
741 bool MediaPlayerPrivateAVFoundationObjC::hasAvailableVideoFrame() const
742 {
743     if (currentRenderingMode() == MediaRenderingToLayer)
744         return m_cachedIsReadyForDisplay;
745
746 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
747     if (m_videoOutput && (m_lastPixelBuffer || [m_videoOutput hasNewPixelBufferForItemTime:[m_avPlayerItem currentTime]]))
748         return true;
749 #endif
750
751     return m_videoFrameHasDrawn;
752 }
753
754 #if ENABLE(AVF_CAPTIONS)
755 static const NSArray* mediaDescriptionForKind(PlatformTextTrack::TrackKind kind)
756 {
757     static bool manualSelectionMode = MTEnableCaption2015BehaviorPtr() && MTEnableCaption2015BehaviorPtr()();
758     if (manualSelectionMode)
759         return @[ AVMediaCharacteristicIsAuxiliaryContent ];
760
761     // FIXME: Match these to correct types:
762     if (kind == PlatformTextTrack::Caption)
763         return [NSArray arrayWithObjects: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, nil];
764
765     if (kind == PlatformTextTrack::Subtitle)
766         return [NSArray arrayWithObjects: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, nil];
767
768     if (kind == PlatformTextTrack::Description)
769         return [NSArray arrayWithObjects: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, AVMediaCharacteristicDescribesMusicAndSoundForAccessibility, nil];
770
771     if (kind == PlatformTextTrack::Forced)
772         return [NSArray arrayWithObjects: AVMediaCharacteristicContainsOnlyForcedSubtitles, nil];
773
774     return [NSArray arrayWithObjects: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, nil];
775 }
776     
777 void MediaPlayerPrivateAVFoundationObjC::notifyTrackModeChanged()
778 {
779     trackModeChanged();
780 }
781     
782 void MediaPlayerPrivateAVFoundationObjC::synchronizeTextTrackState()
783 {
784     const Vector<RefPtr<PlatformTextTrack>>& outOfBandTrackSources = player()->outOfBandTrackSources();
785     
786     for (auto& textTrack : m_textTracks) {
787         if (textTrack->textTrackCategory() != InbandTextTrackPrivateAVF::OutOfBand)
788             continue;
789         
790         RefPtr<OutOfBandTextTrackPrivateAVF> trackPrivate = static_cast<OutOfBandTextTrackPrivateAVF*>(textTrack.get());
791         RetainPtr<AVMediaSelectionOptionType> currentOption = trackPrivate->mediaSelectionOption();
792         
793         for (auto& track : outOfBandTrackSources) {
794             RetainPtr<CFStringRef> uniqueID = String::number(track->uniqueId()).createCFString();
795             
796             if (![[currentOption.get() outOfBandIdentifier] isEqual: reinterpret_cast<const NSString*>(uniqueID.get())])
797                 continue;
798             
799             InbandTextTrackPrivate::Mode mode = InbandTextTrackPrivate::Hidden;
800             if (track->mode() == PlatformTextTrack::Hidden)
801                 mode = InbandTextTrackPrivate::Hidden;
802             else if (track->mode() == PlatformTextTrack::Disabled)
803                 mode = InbandTextTrackPrivate::Disabled;
804             else if (track->mode() == PlatformTextTrack::Showing)
805                 mode = InbandTextTrackPrivate::Showing;
806             
807             textTrack->setMode(mode);
808             break;
809         }
810     }
811 }
812 #endif
813
814
815 static NSURL *canonicalURL(const URL& url)
816 {
817     NSURL *cocoaURL = url;
818     if (url.isEmpty())
819         return cocoaURL;
820
821     RetainPtr<NSURLRequest> request = adoptNS([[NSURLRequest alloc] initWithURL:cocoaURL]);
822     if (!request)
823         return cocoaURL;
824
825     NSURLRequest *canonicalRequest = [NSURLProtocol canonicalRequestForRequest:request.get()];
826     if (!canonicalRequest)
827         return cocoaURL;
828
829     return [canonicalRequest URL];
830 }
831
832 #if PLATFORM(IOS)
833 static NSHTTPCookie* toNSHTTPCookie(const Cookie& cookie)
834 {
835     RetainPtr<NSMutableDictionary> properties = adoptNS([[NSMutableDictionary alloc] init]);
836     [properties setDictionary:@{
837         NSHTTPCookieName: cookie.name,
838         NSHTTPCookieValue: cookie.value,
839         NSHTTPCookieDomain: cookie.domain,
840         NSHTTPCookiePath: cookie.path,
841         NSHTTPCookieExpires: [NSDate dateWithTimeIntervalSince1970:(cookie.expires / 1000)],
842     }];
843     if (cookie.secure)
844         [properties setObject:@YES forKey:NSHTTPCookieSecure];
845     if (cookie.session)
846         [properties setObject:@YES forKey:NSHTTPCookieDiscard];
847
848     return [NSHTTPCookie cookieWithProperties:properties.get()];
849 }
850 #endif
851
852 void MediaPlayerPrivateAVFoundationObjC::createAVAssetForURL(const URL& url)
853 {
854     if (m_avAsset)
855         return;
856
857     INFO_LOG(LOGIDENTIFIER);
858
859     setDelayCallbacks(true);
860
861     RetainPtr<NSMutableDictionary> options = adoptNS([[NSMutableDictionary alloc] init]);    
862
863     [options.get() setObject:[NSNumber numberWithInt:AVAssetReferenceRestrictionForbidRemoteReferenceToLocal | AVAssetReferenceRestrictionForbidLocalReferenceToRemote] forKey:AVURLAssetReferenceRestrictionsKey];
864
865     RetainPtr<NSMutableDictionary> headerFields = adoptNS([[NSMutableDictionary alloc] init]);
866
867     String referrer = player()->referrer();
868     if (!referrer.isEmpty())
869         [headerFields.get() setObject:referrer forKey:@"Referer"];
870
871     String userAgent = player()->userAgent();
872     if (!userAgent.isEmpty())
873         [headerFields.get() setObject:userAgent forKey:@"User-Agent"];
874
875     if ([headerFields.get() count])
876         [options.get() setObject:headerFields.get() forKey:@"AVURLAssetHTTPHeaderFieldsKey"];
877
878     if (player()->doesHaveAttribute("x-itunes-inherit-uri-query-component"))
879         [options.get() setObject:@YES forKey: AVURLAssetInheritURIQueryComponentFromReferencingURIKey];
880
881     if (AVURLAssetUseClientURLLoadingExclusively)
882         [options setObject:@YES forKey:AVURLAssetUseClientURLLoadingExclusively];
883 #if PLATFORM(IOS)
884     else if (AVURLAssetRequiresCustomURLLoadingKey)
885         [options setObject:@YES forKey:AVURLAssetRequiresCustomURLLoadingKey];
886     // FIXME: rdar://problem/20354688
887     String identifier = player()->sourceApplicationIdentifier();
888     if (!identifier.isEmpty() && AVURLAssetClientBundleIdentifierKey)
889         [options setObject:identifier forKey:AVURLAssetClientBundleIdentifierKey];
890 #endif
891
892     auto type = player()->contentMIMEType();
893     if (AVURLAssetOutOfBandMIMETypeKey && !type.isEmpty() && !player()->contentMIMETypeWasInferredFromExtension()) {
894         auto codecs = player()->contentTypeCodecs();
895         if (!codecs.isEmpty()) {
896             NSString *typeString = [NSString stringWithFormat:@"%@; codecs=\"%@\"", (NSString *)type, (NSString *)codecs];
897             [options setObject:typeString forKey:AVURLAssetOutOfBandMIMETypeKey];
898         } else
899             [options setObject:(NSString *)type forKey:AVURLAssetOutOfBandMIMETypeKey];
900     }
901
902 #if ENABLE(AVF_CAPTIONS)
903     const Vector<RefPtr<PlatformTextTrack>>& outOfBandTrackSources = player()->outOfBandTrackSources();
904     if (!outOfBandTrackSources.isEmpty()) {
905         RetainPtr<NSMutableArray> outOfBandTracks = adoptNS([[NSMutableArray alloc] init]);
906         for (auto& trackSource : outOfBandTrackSources) {
907             RetainPtr<CFStringRef> label = trackSource->label().createCFString();
908             RetainPtr<CFStringRef> language = trackSource->language().createCFString();
909             RetainPtr<CFStringRef> uniqueID = String::number(trackSource->uniqueId()).createCFString();
910             RetainPtr<CFStringRef> url = trackSource->url().createCFString();
911             [outOfBandTracks.get() addObject:@{
912                 AVOutOfBandAlternateTrackDisplayNameKey: reinterpret_cast<const NSString*>(label.get()),
913                 AVOutOfBandAlternateTrackExtendedLanguageTagKey: reinterpret_cast<const NSString*>(language.get()),
914                 AVOutOfBandAlternateTrackIsDefaultKey: trackSource->isDefault() ? @YES : @NO,
915                 AVOutOfBandAlternateTrackIdentifierKey: reinterpret_cast<const NSString*>(uniqueID.get()),
916                 AVOutOfBandAlternateTrackSourceKey: reinterpret_cast<const NSString*>(url.get()),
917                 AVOutOfBandAlternateTrackMediaCharactersticsKey: mediaDescriptionForKind(trackSource->kind()),
918             }];
919         }
920
921         [options.get() setObject:outOfBandTracks.get() forKey:AVURLAssetOutOfBandAlternateTracksKey];
922     }
923 #endif
924
925 #if PLATFORM(IOS)
926     String networkInterfaceName = player()->mediaPlayerNetworkInterfaceName();
927     if (!networkInterfaceName.isEmpty())
928         [options setObject:networkInterfaceName forKey:AVURLAssetBoundNetworkInterfaceName];
929 #endif
930
931 #if PLATFORM(IOS)
932     Vector<Cookie> cookies;
933     if (player()->getRawCookies(url, cookies)) {
934         RetainPtr<NSMutableArray> nsCookies = adoptNS([[NSMutableArray alloc] initWithCapacity:cookies.size()]);
935         for (auto& cookie : cookies)
936             [nsCookies addObject:toNSHTTPCookie(cookie)];
937
938         [options setObject:nsCookies.get() forKey:AVURLAssetHTTPCookiesKey];
939     }
940 #endif
941
942     bool usePersistentCache = player()->client().mediaPlayerShouldUsePersistentCache();
943     [options setObject:@(!usePersistentCache) forKey:AVURLAssetUsesNoPersistentCacheKey];
944     
945     if (usePersistentCache)
946         [options setObject:assetCacheForPath(player()->client().mediaPlayerMediaCacheDirectory()) forKey:AVURLAssetCacheKey];
947
948     NSURL *cocoaURL = canonicalURL(url);
949     m_avAsset = adoptNS([[AVURLAsset alloc] initWithURL:cocoaURL options:options.get()]);
950
951 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
952     AVAssetResourceLoader *resourceLoader = m_avAsset.get().resourceLoader;
953     [resourceLoader setDelegate:m_loaderDelegate.get() queue:globalLoaderDelegateQueue()];
954
955 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED > 101100
956     if (DeprecatedGlobalSettings::isAVFoundationNSURLSessionEnabled()
957         && [resourceLoader respondsToSelector:@selector(setURLSession:)]
958         && [resourceLoader respondsToSelector:@selector(URLSessionDataDelegate)]
959         && [resourceLoader respondsToSelector:@selector(URLSessionDataDelegateQueue)]) {
960         RefPtr<PlatformMediaResourceLoader> mediaResourceLoader = player()->createResourceLoader();
961         if (mediaResourceLoader)
962             resourceLoader.URLSession = (NSURLSession *)[[[WebCoreNSURLSession alloc] initWithResourceLoader:*mediaResourceLoader delegate:resourceLoader.URLSessionDataDelegate delegateQueue:resourceLoader.URLSessionDataDelegateQueue] autorelease];
963     }
964 #endif
965
966 #endif
967
968     m_haveCheckedPlayability = false;
969
970     setDelayCallbacks(false);
971 }
972
973 void MediaPlayerPrivateAVFoundationObjC::setAVPlayerItem(AVPlayerItemType *item)
974 {
975     if (!m_avPlayer)
976         return;
977
978     if (pthread_main_np()) {
979         [m_avPlayer replaceCurrentItemWithPlayerItem:item];
980         return;
981     }
982
983     RetainPtr<AVPlayerType> strongPlayer = m_avPlayer.get();
984     RetainPtr<AVPlayerItemType> strongItem = item;
985     dispatch_async(dispatch_get_main_queue(), [strongPlayer, strongItem] {
986         [strongPlayer replaceCurrentItemWithPlayerItem:strongItem.get()];
987     });
988 }
989
990 void MediaPlayerPrivateAVFoundationObjC::createAVPlayer()
991 {
992     if (m_avPlayer)
993         return;
994
995     INFO_LOG(LOGIDENTIFIER);
996
997     setDelayCallbacks(true);
998
999     m_avPlayer = adoptNS([[AVPlayer alloc] init]);
1000     for (NSString *keyName in playerKVOProperties())
1001         [m_avPlayer.get() addObserver:m_objcObserver.get() forKeyPath:keyName options:NSKeyValueObservingOptionNew context:(void *)MediaPlayerAVFoundationObservationContextPlayer];
1002
1003 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
1004     [m_avPlayer.get() setAppliesMediaSelectionCriteriaAutomatically:NO];
1005 #endif
1006
1007 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
1008     updateDisableExternalPlayback();
1009     [m_avPlayer.get() setAllowsExternalPlayback:m_allowsWirelessVideoPlayback];
1010 #endif
1011
1012 #if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS)
1013     if (m_shouldPlayToPlaybackTarget) {
1014         // Clear m_shouldPlayToPlaybackTarget so doesn't return without doing anything.
1015         m_shouldPlayToPlaybackTarget = false;
1016         setShouldPlayToPlaybackTarget(true);
1017     }
1018 #endif
1019
1020 #if PLATFORM(IOS) && !PLATFORM(IOS_SIMULATOR)
1021     setShouldDisableSleep(player()->shouldDisableSleep());
1022 #endif
1023
1024     if (m_muted) {
1025         // Clear m_muted so setMuted doesn't return without doing anything.
1026         m_muted = false;
1027         [m_avPlayer.get() setMuted:m_muted];
1028     }
1029
1030     if (player()->client().mediaPlayerIsVideo())
1031         createAVPlayerLayer();
1032
1033     if (m_avPlayerItem)
1034         setAVPlayerItem(m_avPlayerItem.get());
1035
1036     setDelayCallbacks(false);
1037 }
1038
1039 void MediaPlayerPrivateAVFoundationObjC::createAVPlayerItem()
1040 {
1041     if (m_avPlayerItem)
1042         return;
1043
1044     INFO_LOG(LOGIDENTIFIER);
1045
1046     setDelayCallbacks(true);
1047
1048     // Create the player item so we can load media data. 
1049     m_avPlayerItem = adoptNS([[AVPlayerItem alloc] initWithAsset:m_avAsset.get()]);
1050
1051     [[NSNotificationCenter defaultCenter] addObserver:m_objcObserver.get() selector:@selector(didEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:m_avPlayerItem.get()];
1052
1053     NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionPrior;
1054     for (NSString *keyName in itemKVOProperties())
1055         [m_avPlayerItem.get() addObserver:m_objcObserver.get() forKeyPath:keyName options:options context:(void *)MediaPlayerAVFoundationObservationContextPlayerItem];
1056
1057     [m_avPlayerItem setAudioTimePitchAlgorithm:(player()->preservesPitch() ? AVAudioTimePitchAlgorithmSpectral : AVAudioTimePitchAlgorithmVarispeed)];
1058
1059     if (m_avPlayer)
1060         setAVPlayerItem(m_avPlayerItem.get());
1061
1062 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
1063     const NSTimeInterval legibleOutputAdvanceInterval = 2;
1064
1065     RetainPtr<NSArray> subtypes = adoptNS([[NSArray alloc] initWithObjects:[NSNumber numberWithUnsignedInt:kCMSubtitleFormatType_WebVTT], nil]);
1066     m_legibleOutput = adoptNS([[AVPlayerItemLegibleOutput alloc] initWithMediaSubtypesForNativeRepresentation:subtypes.get()]);
1067     [m_legibleOutput.get() setSuppressesPlayerRendering:YES];
1068
1069     [m_legibleOutput.get() setDelegate:m_objcObserver.get() queue:dispatch_get_main_queue()];
1070     [m_legibleOutput.get() setAdvanceIntervalForDelegateInvocation:legibleOutputAdvanceInterval];
1071     [m_legibleOutput.get() setTextStylingResolution:AVPlayerItemLegibleOutputTextStylingResolutionSourceAndRulesOnly];
1072     [m_avPlayerItem.get() addOutput:m_legibleOutput.get()];
1073 #endif
1074
1075 #if ENABLE(WEB_AUDIO) && USE(MEDIATOOLBOX)
1076     if (m_provider) {
1077         m_provider->setPlayerItem(m_avPlayerItem.get());
1078         m_provider->setAudioTrack(firstEnabledTrack(safeAVAssetTracksForAudibleMedia()));
1079     }
1080 #endif
1081
1082 #if ENABLE(EXTRA_ZOOM_MODE)
1083     createVideoOutput();
1084 #endif
1085
1086     setDelayCallbacks(false);
1087 }
1088
1089 void MediaPlayerPrivateAVFoundationObjC::checkPlayability()
1090 {
1091     if (m_haveCheckedPlayability)
1092         return;
1093     m_haveCheckedPlayability = true;
1094
1095     INFO_LOG(LOGIDENTIFIER);
1096     auto weakThis = createWeakPtr();
1097
1098     [m_avAsset.get() loadValuesAsynchronouslyForKeys:[NSArray arrayWithObjects:@"playable", @"tracks", nil] completionHandler:^{
1099         callOnMainThread([weakThis] {
1100             if (weakThis)
1101                 weakThis->scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification::AssetPlayabilityKnown);
1102         });
1103     }];
1104 }
1105
1106 void MediaPlayerPrivateAVFoundationObjC::beginLoadingMetadata()
1107 {
1108     INFO_LOG(LOGIDENTIFIER);
1109
1110     OSObjectPtr<dispatch_group_t> metadataLoadingGroup = adoptOSObject(dispatch_group_create());
1111     dispatch_group_enter(metadataLoadingGroup.get());
1112     auto weakThis = createWeakPtr();
1113     [m_avAsset.get() loadValuesAsynchronouslyForKeys:assetMetadataKeyNames() completionHandler:^{
1114
1115         callOnMainThread([weakThis, metadataLoadingGroup] {
1116             if (weakThis && [weakThis->m_avAsset.get() statusOfValueForKey:@"tracks" error:nil] == AVKeyValueStatusLoaded) {
1117                 for (AVAssetTrack *track in [weakThis->m_avAsset.get() tracks]) {
1118                     dispatch_group_enter(metadataLoadingGroup.get());
1119                     [track loadValuesAsynchronouslyForKeys:assetTrackMetadataKeyNames() completionHandler:^{
1120                         dispatch_group_leave(metadataLoadingGroup.get());
1121                     }];
1122                 }
1123             }
1124             dispatch_group_leave(metadataLoadingGroup.get());
1125         });
1126     }];
1127
1128     dispatch_group_notify(metadataLoadingGroup.get(), dispatch_get_main_queue(), ^{
1129         callOnMainThread([weakThis] {
1130             if (weakThis)
1131                 [weakThis->m_objcObserver.get() metadataLoaded];
1132         });
1133     });
1134 }
1135
1136 MediaPlayerPrivateAVFoundation::ItemStatus MediaPlayerPrivateAVFoundationObjC::playerItemStatus() const
1137 {
1138     if (!m_avPlayerItem)
1139         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusDoesNotExist;
1140
1141     if (m_cachedItemStatus == AVPlayerItemStatusUnknown)
1142         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusUnknown;
1143     if (m_cachedItemStatus == AVPlayerItemStatusFailed)
1144         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusFailed;
1145     if (m_cachedLikelyToKeepUp)
1146         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusPlaybackLikelyToKeepUp;
1147     if (m_cachedBufferFull)
1148         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusPlaybackBufferFull;
1149     if (m_cachedBufferEmpty)
1150         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusPlaybackBufferEmpty;
1151
1152     return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusReadyToPlay;
1153 }
1154
1155 PlatformMedia MediaPlayerPrivateAVFoundationObjC::platformMedia() const
1156 {
1157     INFO_LOG(LOGIDENTIFIER);
1158     PlatformMedia pm;
1159     pm.type = PlatformMedia::AVFoundationMediaPlayerType;
1160     pm.media.avfMediaPlayer = m_avPlayer.get();
1161     return pm;
1162 }
1163
1164 PlatformLayer* MediaPlayerPrivateAVFoundationObjC::platformLayer() const
1165 {
1166     return m_videoFullscreenLayerManager->videoInlineLayer();
1167 }
1168
1169 void MediaPlayerPrivateAVFoundationObjC::updateVideoFullscreenInlineImage()
1170 {
1171     updateLastImage(UpdateType::UpdateSynchronously);
1172     m_videoFullscreenLayerManager->updateVideoFullscreenInlineImage(m_lastImage);
1173 }
1174
1175 void MediaPlayerPrivateAVFoundationObjC::setVideoFullscreenLayer(PlatformLayer* videoFullscreenLayer, Function<void()>&& completionHandler)
1176 {
1177     updateLastImage(UpdateType::UpdateSynchronously);
1178     m_videoFullscreenLayerManager->setVideoFullscreenLayer(videoFullscreenLayer, WTFMove(completionHandler), m_lastImage);
1179     updateDisableExternalPlayback();
1180 }
1181
1182 void MediaPlayerPrivateAVFoundationObjC::setVideoFullscreenFrame(FloatRect frame)
1183 {
1184     m_videoFullscreenLayerManager->setVideoFullscreenFrame(frame);
1185 }
1186
1187 void MediaPlayerPrivateAVFoundationObjC::setVideoFullscreenGravity(MediaPlayer::VideoGravity gravity)
1188 {
1189     m_videoFullscreenGravity = gravity;
1190
1191     if (!m_videoLayer)
1192         return;
1193
1194     NSString *videoGravity = AVLayerVideoGravityResizeAspect;
1195     if (gravity == MediaPlayer::VideoGravityResize)
1196         videoGravity = AVLayerVideoGravityResize;
1197     else if (gravity == MediaPlayer::VideoGravityResizeAspect)
1198         videoGravity = AVLayerVideoGravityResizeAspect;
1199     else if (gravity == MediaPlayer::VideoGravityResizeAspectFill)
1200         videoGravity = AVLayerVideoGravityResizeAspectFill;
1201     else
1202         ASSERT_NOT_REACHED();
1203     
1204     if ([m_videoLayer videoGravity] == videoGravity)
1205         return;
1206
1207     [m_videoLayer setVideoGravity:videoGravity];
1208     syncTextTrackBounds();
1209 }
1210
1211 void MediaPlayerPrivateAVFoundationObjC::setVideoFullscreenMode(MediaPlayer::VideoFullscreenMode mode)
1212 {
1213 #if PLATFORM(IOS) && !ENABLE(EXTRA_ZOOM_MODE)
1214     if ([m_videoLayer respondsToSelector:@selector(setPIPModeEnabled:)])
1215         [m_videoLayer setPIPModeEnabled:(mode & MediaPlayer::VideoFullscreenModePictureInPicture)];
1216     updateDisableExternalPlayback();
1217 #else
1218     UNUSED_PARAM(mode);
1219 #endif
1220 }
1221
1222 #if PLATFORM(IOS)
1223 NSArray *MediaPlayerPrivateAVFoundationObjC::timedMetadata() const
1224 {
1225     if (m_currentMetaData)
1226         return m_currentMetaData.get();
1227     return nil;
1228 }
1229
1230 String MediaPlayerPrivateAVFoundationObjC::accessLog() const
1231 {
1232     if (!m_avPlayerItem)
1233         return emptyString();
1234     
1235     AVPlayerItemAccessLog *log = [m_avPlayerItem.get() accessLog];
1236     RetainPtr<NSString> logString = adoptNS([[NSString alloc] initWithData:[log extendedLogData] encoding:[log extendedLogDataStringEncoding]]);
1237
1238     return logString.get();
1239 }
1240
1241 String MediaPlayerPrivateAVFoundationObjC::errorLog() const
1242 {
1243     if (!m_avPlayerItem)
1244         return emptyString();
1245
1246     AVPlayerItemErrorLog *log = [m_avPlayerItem.get() errorLog];
1247     RetainPtr<NSString> logString = adoptNS([[NSString alloc] initWithData:[log extendedLogData] encoding:[log extendedLogDataStringEncoding]]);
1248
1249     return logString.get();
1250 }
1251 #endif
1252
1253 void MediaPlayerPrivateAVFoundationObjC::platformSetVisible(bool isVisible)
1254 {
1255     [CATransaction begin];
1256     [CATransaction setDisableActions:YES];    
1257     if (m_videoLayer)
1258         [m_videoLayer.get() setHidden:!isVisible];
1259     [CATransaction commit];
1260 }
1261     
1262 void MediaPlayerPrivateAVFoundationObjC::platformPlay()
1263 {
1264     INFO_LOG(LOGIDENTIFIER);
1265     if (!metaDataAvailable())
1266         return;
1267
1268     setDelayCallbacks(true);
1269     m_cachedRate = requestedRate();
1270     [m_avPlayer.get() setRate:requestedRate()];
1271     setDelayCallbacks(false);
1272 }
1273
1274 void MediaPlayerPrivateAVFoundationObjC::platformPause()
1275 {
1276     INFO_LOG(LOGIDENTIFIER);
1277     if (!metaDataAvailable())
1278         return;
1279
1280     setDelayCallbacks(true);
1281     m_cachedRate = 0;
1282     [m_avPlayer.get() setRate:0];
1283     setDelayCallbacks(false);
1284 }
1285
1286 MediaTime MediaPlayerPrivateAVFoundationObjC::platformDuration() const
1287 {
1288     // Do not ask the asset for duration before it has been loaded or it will fetch the
1289     // answer synchronously.
1290     if (!m_avAsset || assetStatus() < MediaPlayerAVAssetStatusLoaded)
1291         return MediaTime::invalidTime();
1292     
1293     CMTime cmDuration;
1294     
1295     // Check the AVItem if we have one and it has loaded duration, some assets never report duration.
1296     if (m_avPlayerItem && playerItemStatus() >= MediaPlayerAVPlayerItemStatusReadyToPlay)
1297         cmDuration = [m_avPlayerItem.get() duration];
1298     else
1299         cmDuration = [m_avAsset.get() duration];
1300
1301     if (CMTIME_IS_NUMERIC(cmDuration))
1302         return PAL::toMediaTime(cmDuration);
1303
1304     if (CMTIME_IS_INDEFINITE(cmDuration))
1305         return MediaTime::positiveInfiniteTime();
1306
1307     INFO_LOG(LOGIDENTIFIER, "returning invalid time");
1308     return MediaTime::invalidTime();
1309 }
1310
1311 MediaTime MediaPlayerPrivateAVFoundationObjC::currentMediaTime() const
1312 {
1313     if (!metaDataAvailable() || !m_avPlayerItem)
1314         return MediaTime::zeroTime();
1315
1316     CMTime itemTime = [m_avPlayerItem.get() currentTime];
1317     if (CMTIME_IS_NUMERIC(itemTime))
1318         return std::max(PAL::toMediaTime(itemTime), MediaTime::zeroTime());
1319
1320     return MediaTime::zeroTime();
1321 }
1322
1323 void MediaPlayerPrivateAVFoundationObjC::seekToTime(const MediaTime& time, const MediaTime& negativeTolerance, const MediaTime& positiveTolerance)
1324 {
1325     // setCurrentTime generates several event callbacks, update afterwards.
1326     setDelayCallbacks(true);
1327
1328     if (m_metadataTrack)
1329         m_metadataTrack->flushPartialCues();
1330
1331     CMTime cmTime = PAL::toCMTime(time);
1332     CMTime cmBefore = PAL::toCMTime(negativeTolerance);
1333     CMTime cmAfter = PAL::toCMTime(positiveTolerance);
1334
1335     // [AVPlayerItem seekToTime] will throw an exception if toleranceBefore is negative.
1336     if (CMTimeCompare(cmBefore, kCMTimeZero) < 0)
1337         cmBefore = kCMTimeZero;
1338     
1339     auto weakThis = createWeakPtr();
1340
1341     [m_avPlayerItem.get() seekToTime:cmTime toleranceBefore:cmBefore toleranceAfter:cmAfter completionHandler:^(BOOL finished) {
1342         callOnMainThread([weakThis, finished] {
1343             auto _this = weakThis.get();
1344             if (!_this)
1345                 return;
1346
1347             _this->seekCompleted(finished);
1348         });
1349     }];
1350
1351     setDelayCallbacks(false);
1352 }
1353
1354 void MediaPlayerPrivateAVFoundationObjC::setVolume(float volume)
1355 {
1356 #if PLATFORM(IOS)
1357     UNUSED_PARAM(volume);
1358     return;
1359 #else
1360     if (!m_avPlayer)
1361         return;
1362
1363     [m_avPlayer.get() setVolume:volume];
1364 #endif
1365 }
1366
1367 void MediaPlayerPrivateAVFoundationObjC::setMuted(bool muted)
1368 {
1369     if (m_muted == muted)
1370         return;
1371
1372     INFO_LOG(LOGIDENTIFIER, "- ", muted);
1373
1374     m_muted = muted;
1375
1376     if (!m_avPlayer)
1377         return;
1378
1379     [m_avPlayer.get() setMuted:m_muted];
1380 }
1381
1382 void MediaPlayerPrivateAVFoundationObjC::setClosedCaptionsVisible(bool closedCaptionsVisible)
1383 {
1384     UNUSED_PARAM(closedCaptionsVisible);
1385
1386     if (!metaDataAvailable())
1387         return;
1388
1389     INFO_LOG(LOGIDENTIFIER, "- ", closedCaptionsVisible);
1390 }
1391
1392 void MediaPlayerPrivateAVFoundationObjC::setRateDouble(double rate)
1393 {
1394     setDelayCallbacks(true);
1395     m_cachedRate = rate;
1396     [m_avPlayer.get() setRate:rate];
1397     setDelayCallbacks(false);
1398 }
1399
1400 double MediaPlayerPrivateAVFoundationObjC::rate() const
1401 {
1402     if (!metaDataAvailable())
1403         return 0;
1404
1405     return m_cachedRate;
1406 }
1407
1408 double MediaPlayerPrivateAVFoundationObjC::seekableTimeRangesLastModifiedTime() const
1409 {
1410 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
1411     return [m_avPlayerItem seekableTimeRangesLastModifiedTime];
1412 #else
1413     return 0;
1414 #endif
1415 }
1416
1417 double MediaPlayerPrivateAVFoundationObjC::liveUpdateInterval() const
1418 {
1419 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
1420     return [m_avPlayerItem liveUpdateInterval];
1421 #else
1422     return 0;
1423 #endif
1424 }
1425
1426 void MediaPlayerPrivateAVFoundationObjC::setPreservesPitch(bool preservesPitch)
1427 {
1428     if (m_avPlayerItem)
1429         [m_avPlayerItem setAudioTimePitchAlgorithm:(preservesPitch ? AVAudioTimePitchAlgorithmSpectral : AVAudioTimePitchAlgorithmVarispeed)];
1430 }
1431
1432 std::unique_ptr<PlatformTimeRanges> MediaPlayerPrivateAVFoundationObjC::platformBufferedTimeRanges() const
1433 {
1434     auto timeRanges = std::make_unique<PlatformTimeRanges>();
1435
1436     if (!m_avPlayerItem)
1437         return timeRanges;
1438
1439     for (NSValue *thisRangeValue in m_cachedLoadedRanges.get()) {
1440         CMTimeRange timeRange = [thisRangeValue CMTimeRangeValue];
1441         if (CMTIMERANGE_IS_VALID(timeRange) && !CMTIMERANGE_IS_EMPTY(timeRange))
1442             timeRanges->add(PAL::toMediaTime(timeRange.start), PAL::toMediaTime(CMTimeRangeGetEnd(timeRange)));
1443     }
1444     return timeRanges;
1445 }
1446
1447 MediaTime MediaPlayerPrivateAVFoundationObjC::platformMinTimeSeekable() const
1448 {
1449     if (!m_cachedSeekableRanges || ![m_cachedSeekableRanges count])
1450         return MediaTime::zeroTime();
1451
1452     MediaTime minTimeSeekable = MediaTime::positiveInfiniteTime();
1453     bool hasValidRange = false;
1454     for (NSValue *thisRangeValue in m_cachedSeekableRanges.get()) {
1455         CMTimeRange timeRange = [thisRangeValue CMTimeRangeValue];
1456         if (!CMTIMERANGE_IS_VALID(timeRange) || CMTIMERANGE_IS_EMPTY(timeRange))
1457             continue;
1458
1459         hasValidRange = true;
1460         MediaTime startOfRange = PAL::toMediaTime(timeRange.start);
1461         if (minTimeSeekable > startOfRange)
1462             minTimeSeekable = startOfRange;
1463     }
1464     return hasValidRange ? minTimeSeekable : MediaTime::zeroTime();
1465 }
1466
1467 MediaTime MediaPlayerPrivateAVFoundationObjC::platformMaxTimeSeekable() const
1468 {
1469     if (!m_cachedSeekableRanges)
1470         m_cachedSeekableRanges = [m_avPlayerItem seekableTimeRanges];
1471
1472     MediaTime maxTimeSeekable;
1473     for (NSValue *thisRangeValue in m_cachedSeekableRanges.get()) {
1474         CMTimeRange timeRange = [thisRangeValue CMTimeRangeValue];
1475         if (!CMTIMERANGE_IS_VALID(timeRange) || CMTIMERANGE_IS_EMPTY(timeRange))
1476             continue;
1477         
1478         MediaTime endOfRange = PAL::toMediaTime(CMTimeRangeGetEnd(timeRange));
1479         if (maxTimeSeekable < endOfRange)
1480             maxTimeSeekable = endOfRange;
1481     }
1482     return maxTimeSeekable;
1483 }
1484
1485 MediaTime MediaPlayerPrivateAVFoundationObjC::platformMaxTimeLoaded() const
1486 {
1487     if (!m_cachedLoadedRanges)
1488         return MediaTime::zeroTime();
1489
1490     MediaTime maxTimeLoaded;
1491     for (NSValue *thisRangeValue in m_cachedLoadedRanges.get()) {
1492         CMTimeRange timeRange = [thisRangeValue CMTimeRangeValue];
1493         if (!CMTIMERANGE_IS_VALID(timeRange) || CMTIMERANGE_IS_EMPTY(timeRange))
1494             continue;
1495         
1496         MediaTime endOfRange = PAL::toMediaTime(CMTimeRangeGetEnd(timeRange));
1497         if (maxTimeLoaded < endOfRange)
1498             maxTimeLoaded = endOfRange;
1499     }
1500
1501     return maxTimeLoaded;   
1502 }
1503
1504 unsigned long long MediaPlayerPrivateAVFoundationObjC::totalBytes() const
1505 {
1506     if (!metaDataAvailable())
1507         return 0;
1508
1509     if (m_cachedTotalBytes)
1510         return m_cachedTotalBytes;
1511
1512     for (AVPlayerItemTrack *thisTrack in m_cachedTracks.get())
1513         m_cachedTotalBytes += [[thisTrack assetTrack] totalSampleDataLength];
1514
1515     return m_cachedTotalBytes;
1516 }
1517
1518 void MediaPlayerPrivateAVFoundationObjC::setAsset(RetainPtr<id> asset)
1519 {
1520     m_avAsset = asset;
1521 }
1522
1523 MediaPlayerPrivateAVFoundation::AssetStatus MediaPlayerPrivateAVFoundationObjC::assetStatus() const
1524 {
1525     if (!m_avAsset)
1526         return MediaPlayerAVAssetStatusDoesNotExist;
1527
1528     for (NSString *keyName in assetMetadataKeyNames()) {
1529         NSError *error = nil;
1530         AVKeyValueStatus keyStatus = [m_avAsset.get() statusOfValueForKey:keyName error:&error];
1531
1532         if (error)
1533             ERROR_LOG(LOGIDENTIFIER, "failed for ", [keyName UTF8String], ", error = ", [[error localizedDescription] UTF8String]);
1534
1535         if (keyStatus < AVKeyValueStatusLoaded)
1536             return MediaPlayerAVAssetStatusLoading;// At least one key is not loaded yet.
1537         
1538         if (keyStatus == AVKeyValueStatusFailed)
1539             return MediaPlayerAVAssetStatusFailed; // At least one key could not be loaded.
1540
1541         if (keyStatus == AVKeyValueStatusCancelled)
1542             return MediaPlayerAVAssetStatusCancelled; // Loading of at least one key was cancelled.
1543     }
1544
1545     if (!player()->shouldCheckHardwareSupport())
1546         m_tracksArePlayable = true;
1547
1548     if (!m_tracksArePlayable) {
1549         m_tracksArePlayable = true;
1550         for (AVAssetTrack *track in [m_avAsset tracks]) {
1551             if (!assetTrackMeetsHardwareDecodeRequirements(track, player()->mediaContentTypesRequiringHardwareSupport())) {
1552                 m_tracksArePlayable = false;
1553                 break;
1554             }
1555         }
1556     }
1557
1558     if ([[m_avAsset.get() valueForKey:@"playable"] boolValue] && m_tracksArePlayable.value())
1559         return MediaPlayerAVAssetStatusPlayable;
1560
1561     return MediaPlayerAVAssetStatusLoaded;
1562 }
1563
1564 long MediaPlayerPrivateAVFoundationObjC::assetErrorCode() const
1565 {
1566     if (!m_avAsset)
1567         return 0;
1568
1569     NSError *error = nil;
1570     [m_avAsset statusOfValueForKey:@"playable" error:&error];
1571     return [error code];
1572 }
1573
1574 void MediaPlayerPrivateAVFoundationObjC::paintCurrentFrameInContext(GraphicsContext& context, const FloatRect& rect)
1575 {
1576     if (!metaDataAvailable() || context.paintingDisabled())
1577         return;
1578
1579     setDelayCallbacks(true);
1580     BEGIN_BLOCK_OBJC_EXCEPTIONS;
1581
1582 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
1583     if (videoOutputHasAvailableFrame())
1584         paintWithVideoOutput(context, rect);
1585     else
1586 #endif
1587         paintWithImageGenerator(context, rect);
1588
1589     END_BLOCK_OBJC_EXCEPTIONS;
1590     setDelayCallbacks(false);
1591
1592     m_videoFrameHasDrawn = true;
1593 }
1594
1595 void MediaPlayerPrivateAVFoundationObjC::paint(GraphicsContext& context, const FloatRect& rect)
1596 {
1597     if (!metaDataAvailable() || context.paintingDisabled())
1598         return;
1599
1600     // We can ignore the request if we are already rendering to a layer.
1601     if (currentRenderingMode() == MediaRenderingToLayer)
1602         return;
1603
1604     // paint() is best effort, so only paint if we already have an image generator or video output available.
1605     if (!hasContextRenderer())
1606         return;
1607
1608     paintCurrentFrameInContext(context, rect);
1609 }
1610
1611 void MediaPlayerPrivateAVFoundationObjC::paintWithImageGenerator(GraphicsContext& context, const FloatRect& rect)
1612 {
1613     INFO_LOG(LOGIDENTIFIER);
1614
1615     RetainPtr<CGImageRef> image = createImageForTimeInRect(currentTime(), rect);
1616     if (image) {
1617         GraphicsContextStateSaver stateSaver(context);
1618         context.translate(rect.x(), rect.y() + rect.height());
1619         context.scale(FloatSize(1.0f, -1.0f));
1620         context.setImageInterpolationQuality(InterpolationLow);
1621         IntRect paintRect(IntPoint(0, 0), IntSize(rect.width(), rect.height()));
1622         CGContextDrawImage(context.platformContext(), CGRectMake(0, 0, paintRect.width(), paintRect.height()), image.get());
1623     }
1624 }
1625
1626 RetainPtr<CGImageRef> MediaPlayerPrivateAVFoundationObjC::createImageForTimeInRect(float time, const FloatRect& rect)
1627 {
1628     if (!m_imageGenerator)
1629         createImageGenerator();
1630     ASSERT(m_imageGenerator);
1631
1632 #if !RELEASE_LOG_DISABLED
1633     MonotonicTime start = MonotonicTime::now();
1634 #endif
1635
1636     [m_imageGenerator.get() setMaximumSize:CGSize(rect.size())];
1637     RetainPtr<CGImageRef> rawImage = adoptCF([m_imageGenerator.get() copyCGImageAtTime:CMTimeMakeWithSeconds(time, 600) actualTime:nil error:nil]);
1638     RetainPtr<CGImageRef> image = adoptCF(CGImageCreateCopyWithColorSpace(rawImage.get(), sRGBColorSpaceRef()));
1639
1640 #if !RELEASE_LOG_DISABLED
1641     DEBUG_LOG(LOGIDENTIFIER, "creating image took ", (MonotonicTime::now() - start).seconds());
1642 #endif
1643
1644     return image;
1645 }
1646
1647 void MediaPlayerPrivateAVFoundationObjC::getSupportedTypes(HashSet<String, ASCIICaseInsensitiveHash>& supportedTypes)
1648 {
1649     supportedTypes = AVFoundationMIMETypeCache::singleton().types();
1650
1651
1652 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
1653 static bool keySystemIsSupported(const String& keySystem)
1654 {
1655     if (equalIgnoringASCIICase(keySystem, "com.apple.fps") || equalIgnoringASCIICase(keySystem, "com.apple.fps.1_0") || equalIgnoringASCIICase(keySystem, "org.w3c.clearkey"))
1656         return true;
1657     return false;
1658 }
1659 #endif
1660
1661 MediaPlayer::SupportsType MediaPlayerPrivateAVFoundationObjC::supportsType(const MediaEngineSupportParameters& parameters)
1662 {
1663 #if ENABLE(MEDIA_SOURCE)
1664     if (parameters.isMediaSource)
1665         return MediaPlayer::IsNotSupported;
1666 #endif
1667 #if ENABLE(MEDIA_STREAM)
1668     if (parameters.isMediaStream)
1669         return MediaPlayer::IsNotSupported;
1670 #endif
1671
1672     auto containerType = parameters.type.containerType();
1673     if (isUnsupportedMIMEType(containerType))
1674         return MediaPlayer::IsNotSupported;
1675
1676     if (!staticMIMETypeList().contains(containerType) && !AVFoundationMIMETypeCache::singleton().types().contains(containerType))
1677         return MediaPlayer::IsNotSupported;
1678
1679     // The spec says:
1680     // "Implementors are encouraged to return "maybe" unless the type can be confidently established as being supported or not."
1681     if (parameters.type.codecs().isEmpty())
1682         return MediaPlayer::MayBeSupported;
1683
1684     if (!contentTypeMeetsHardwareDecodeRequirements(parameters.type, parameters.contentTypesRequiringHardwareSupport))
1685         return MediaPlayer::IsNotSupported;
1686
1687     NSString *typeString = [NSString stringWithFormat:@"%@; codecs=\"%@\"", (NSString *)containerType, (NSString *)parameters.type.parameter(ContentType::codecsParameter())];
1688     return [AVURLAsset isPlayableExtendedMIMEType:typeString] ? MediaPlayer::IsSupported : MediaPlayer::MayBeSupported;
1689 }
1690
1691 bool MediaPlayerPrivateAVFoundationObjC::supportsKeySystem(const String& keySystem, const String& mimeType)
1692 {
1693 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
1694     if (!keySystem.isEmpty()) {
1695         // "Clear Key" is only supported with HLS:
1696         if (equalIgnoringASCIICase(keySystem, "org.w3c.clearkey") && !mimeType.isEmpty() && !equalIgnoringASCIICase(mimeType, "application/x-mpegurl"))
1697             return MediaPlayer::IsNotSupported;
1698
1699         if (!keySystemIsSupported(keySystem))
1700             return false;
1701
1702         if (!mimeType.isEmpty() && isUnsupportedMIMEType(mimeType))
1703             return false;
1704
1705         if (!mimeType.isEmpty() && !staticMIMETypeList().contains(mimeType) && !AVFoundationMIMETypeCache::singleton().types().contains(mimeType))
1706             return false;
1707
1708         return true;
1709     }
1710 #else
1711     UNUSED_PARAM(keySystem);
1712     UNUSED_PARAM(mimeType);
1713 #endif
1714     return false;
1715 }
1716
1717 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
1718 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
1719 static void fulfillRequestWithKeyData(AVAssetResourceLoadingRequest *request, ArrayBuffer* keyData)
1720 {
1721     if (AVAssetResourceLoadingContentInformationRequest *infoRequest = [request contentInformationRequest]) {
1722         [infoRequest setContentLength:keyData->byteLength()];
1723         [infoRequest setByteRangeAccessSupported:YES];
1724     }
1725
1726     if (AVAssetResourceLoadingDataRequest *dataRequest = [request dataRequest]) {
1727         long long start = [dataRequest currentOffset];
1728         long long end = std::min<long long>(keyData->byteLength(), [dataRequest currentOffset] + [dataRequest requestedLength]);
1729
1730         if (start < 0 || end < 0 || start >= static_cast<long long>(keyData->byteLength())) {
1731             [request finishLoadingWithError:nil];
1732             return;
1733         }
1734
1735         ASSERT(start <= std::numeric_limits<int>::max());
1736         ASSERT(end <= std::numeric_limits<int>::max());
1737         RefPtr<ArrayBuffer> requestedKeyData = keyData->slice(static_cast<int>(start), static_cast<int>(end));
1738         RetainPtr<NSData> nsData = adoptNS([[NSData alloc] initWithBytes:requestedKeyData->data() length:requestedKeyData->byteLength()]);
1739         [dataRequest respondWithData:nsData.get()];
1740     }
1741
1742     [request finishLoading];
1743 }
1744 #endif
1745
1746 bool MediaPlayerPrivateAVFoundationObjC::shouldWaitForLoadingOfResource(AVAssetResourceLoadingRequest* avRequest)
1747 {
1748     String scheme = [[[avRequest request] URL] scheme];
1749     String keyURI = [[[avRequest request] URL] absoluteString];
1750
1751 #if ENABLE(LEGACY_ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA)
1752     if (scheme == "skd") {
1753 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
1754         // Create an initData with the following layout:
1755         // [4 bytes: keyURI size], [keyURI size bytes: keyURI]
1756         unsigned keyURISize = keyURI.length() * sizeof(UChar);
1757         RefPtr<ArrayBuffer> initDataBuffer = ArrayBuffer::create(4 + keyURISize, 1);
1758         unsigned byteLength = initDataBuffer->byteLength();
1759         RefPtr<JSC::DataView> initDataView = JSC::DataView::create(initDataBuffer.copyRef(), 0, byteLength);
1760         initDataView->set<uint32_t>(0, keyURISize, true);
1761
1762         RefPtr<Uint16Array> keyURIArray = Uint16Array::create(initDataBuffer.copyRef(), 4, keyURI.length());
1763         keyURIArray->setRange(StringView(keyURI).upconvertedCharacters(), keyURI.length() / sizeof(unsigned char), 0);
1764
1765         RefPtr<Uint8Array> initData = Uint8Array::create(WTFMove(initDataBuffer), 0, byteLength);
1766         if (!player()->keyNeeded(initData.get()))
1767             return false;
1768 #endif
1769         m_keyURIToRequestMap.set(keyURI, avRequest);
1770 #if ENABLE(ENCRYPTED_MEDIA) && HAVE(AVCONTENTKEYSESSION)
1771         if (m_cdmInstance)
1772             return false;
1773
1774         RetainPtr<NSData> keyURIData = [keyURI dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
1775         auto keyURIBuffer = SharedBuffer::create(keyURIData.get());
1776         player()->initializationDataEncountered(ASCIILiteral("skd"), keyURIBuffer->tryCreateArrayBuffer());
1777 #endif
1778         return true;
1779     }
1780
1781 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
1782     if (scheme == "clearkey") {
1783         String keyID = [[[avRequest request] URL] resourceSpecifier];
1784         auto encodedKeyId = UTF8Encoding().encode(keyID, UnencodableHandling::URLEncodedEntities);
1785
1786         auto initData = Uint8Array::create(encodedKeyId.size());
1787         initData->setRange(encodedKeyId.data(), encodedKeyId.size(), 0);
1788
1789         auto keyData = player()->cachedKeyForKeyId(keyID);
1790         if (keyData) {
1791             fulfillRequestWithKeyData(avRequest, keyData.get());
1792             return false;
1793         }
1794
1795         if (!player()->keyNeeded(initData.get()))
1796             return false;
1797
1798         m_keyURIToRequestMap.set(keyID, avRequest);
1799         return true;
1800     }
1801 #endif
1802 #endif
1803
1804     RefPtr<WebCoreAVFResourceLoader> resourceLoader = WebCoreAVFResourceLoader::create(this, avRequest);
1805     m_resourceLoaderMap.add(avRequest, resourceLoader);
1806     resourceLoader->startLoading();
1807     return true;
1808 }
1809
1810 void MediaPlayerPrivateAVFoundationObjC::didCancelLoadingRequest(AVAssetResourceLoadingRequest* avRequest)
1811 {
1812     String scheme = [[[avRequest request] URL] scheme];
1813
1814     WebCoreAVFResourceLoader* resourceLoader = m_resourceLoaderMap.get(avRequest);
1815
1816     if (resourceLoader)
1817         resourceLoader->stopLoading();
1818 }
1819
1820 void MediaPlayerPrivateAVFoundationObjC::didStopLoadingRequest(AVAssetResourceLoadingRequest *avRequest)
1821 {
1822     m_resourceLoaderMap.remove(avRequest);
1823 }
1824 #endif
1825
1826 bool MediaPlayerPrivateAVFoundationObjC::isAvailable()
1827 {
1828     return AVFoundationLibrary() && isCoreMediaFrameworkAvailable();
1829 }
1830
1831 MediaTime MediaPlayerPrivateAVFoundationObjC::mediaTimeForTimeValue(const MediaTime& timeValue) const
1832 {
1833     if (!metaDataAvailable())
1834         return timeValue;
1835
1836     // FIXME - impossible to implement until rdar://8721510 is fixed.
1837     return timeValue;
1838 }
1839
1840 double MediaPlayerPrivateAVFoundationObjC::maximumDurationToCacheMediaTime() const
1841 {
1842 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1010
1843     return 0;
1844 #else
1845     return 5;
1846 #endif
1847 }
1848
1849 void MediaPlayerPrivateAVFoundationObjC::updateVideoLayerGravity()
1850 {
1851     if (!m_videoLayer)
1852         return;
1853
1854     // Do not attempt to change the video gravity while in full screen mode.
1855     // See setVideoFullscreenGravity().
1856     if (m_videoFullscreenLayerManager->videoFullscreenLayer())
1857         return;
1858
1859     [CATransaction begin];
1860     [CATransaction setDisableActions:YES];    
1861     NSString* gravity = shouldMaintainAspectRatio() ? AVLayerVideoGravityResizeAspect : AVLayerVideoGravityResize;
1862     [m_videoLayer.get() setVideoGravity:gravity];
1863     [CATransaction commit];
1864 }
1865
1866 static AVAssetTrack* firstEnabledTrack(NSArray* tracks)
1867 {
1868     NSUInteger index = [tracks indexOfObjectPassingTest:^(id obj, NSUInteger, BOOL *) {
1869         return [static_cast<AVAssetTrack*>(obj) isEnabled];
1870     }];
1871     if (index == NSNotFound)
1872         return nil;
1873     return [tracks objectAtIndex:index];
1874 }
1875
1876 void MediaPlayerPrivateAVFoundationObjC::tracksChanged()
1877 {
1878     String primaryAudioTrackLanguage = m_languageOfPrimaryAudioTrack;
1879     m_languageOfPrimaryAudioTrack = String();
1880
1881     if (!m_avAsset)
1882         return;
1883
1884     setDelayCharacteristicsChangedNotification(true);
1885
1886     bool haveCCTrack = false;
1887     bool hasCaptions = false;
1888
1889     // This is called whenever the tracks collection changes so cache hasVideo and hasAudio since we are
1890     // asked about those fairly fequently.
1891     if (!m_avPlayerItem) {
1892         // We don't have a player item yet, so check with the asset because some assets support inspection
1893         // prior to becoming ready to play.
1894         AVAssetTrack* firstEnabledVideoTrack = firstEnabledTrack([m_avAsset.get() tracksWithMediaCharacteristic:AVMediaCharacteristicVisual]);
1895         setHasVideo(firstEnabledVideoTrack);
1896         setHasAudio(firstEnabledTrack([m_avAsset.get() tracksWithMediaCharacteristic:AVMediaCharacteristicAudible]));
1897 #if !HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1898         hasCaptions = [[m_avAsset.get() tracksWithMediaType:AVMediaTypeClosedCaption] count];
1899 #endif
1900         auto size = firstEnabledVideoTrack ? FloatSize(CGSizeApplyAffineTransform([firstEnabledVideoTrack naturalSize], [firstEnabledVideoTrack preferredTransform])) : FloatSize();
1901         // For videos with rotation tag set, the transformation above might return a CGSize instance with negative width or height.
1902         // See https://bugs.webkit.org/show_bug.cgi?id=172648.
1903         if (size.width() < 0)
1904             size.setWidth(-size.width());
1905         if (size.height() < 0)
1906             size.setHeight(-size.height());
1907         presentationSizeDidChange(size);
1908     } else {
1909         bool hasVideo = false;
1910         bool hasAudio = false;
1911         bool hasMetaData = false;
1912         for (AVPlayerItemTrack *track in m_cachedTracks.get()) {
1913             if ([track isEnabled]) {
1914                 AVAssetTrack *assetTrack = [track assetTrack];
1915                 NSString *mediaType = [assetTrack mediaType];
1916                 if ([mediaType isEqualToString:AVMediaTypeVideo])
1917                     hasVideo = true;
1918                 else if ([mediaType isEqualToString:AVMediaTypeAudio])
1919                     hasAudio = true;
1920                 else if ([mediaType isEqualToString:AVMediaTypeClosedCaption]) {
1921 #if !HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1922                     hasCaptions = true;
1923 #endif
1924                     haveCCTrack = true;
1925                 } else if ([mediaType isEqualToString:AVMediaTypeMetadata]) {
1926                     hasMetaData = true;
1927                 }
1928             }
1929         }
1930
1931 #if ENABLE(VIDEO_TRACK)
1932         updateAudioTracks();
1933         updateVideoTracks();
1934
1935 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1936         hasAudio |= (m_audibleGroup && m_audibleGroup->selectedOption());
1937         hasVideo |= (m_visualGroup && m_visualGroup->selectedOption());
1938 #endif
1939 #endif
1940
1941         // Always says we have video if the AVPlayerLayer is ready for diaplay to work around
1942         // an AVFoundation bug which causes it to sometimes claim a track is disabled even
1943         // when it is not.
1944         setHasVideo(hasVideo || m_cachedIsReadyForDisplay);
1945
1946         setHasAudio(hasAudio);
1947 #if ENABLE(DATACUE_VALUE)
1948         if (hasMetaData)
1949             processMetadataTrack();
1950 #endif
1951     }
1952
1953 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1954     AVMediaSelectionGroupType *legibleGroup = safeMediaSelectionGroupForLegibleMedia();
1955     if (legibleGroup && m_cachedTracks) {
1956         hasCaptions = [[AVMediaSelectionGroup playableMediaSelectionOptionsFromArray:[legibleGroup options]] count];
1957         if (hasCaptions)
1958             processMediaSelectionOptions();
1959     }
1960 #endif
1961
1962 #if !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT) && HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1963     if (!hasCaptions && haveCCTrack)
1964         processLegacyClosedCaptionsTracks();
1965 #elif !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
1966     if (haveCCTrack)
1967         processLegacyClosedCaptionsTracks();
1968 #endif
1969
1970     setHasClosedCaptions(hasCaptions);
1971
1972     INFO_LOG(LOGIDENTIFIER, "has video = ", hasVideo(), ", has audio = ", hasAudio(), ", has captions = ", hasClosedCaptions());
1973
1974     sizeChanged();
1975
1976     if (primaryAudioTrackLanguage != languageOfPrimaryAudioTrack())
1977         characteristicsChanged();
1978
1979 #if ENABLE(WEB_AUDIO) && USE(MEDIATOOLBOX)
1980     if (m_provider)
1981         m_provider->setAudioTrack(firstEnabledTrack(safeAVAssetTracksForAudibleMedia()));
1982 #endif
1983
1984     setDelayCharacteristicsChangedNotification(false);
1985 }
1986
1987 #if ENABLE(VIDEO_TRACK)
1988
1989 template <typename RefT, typename PassRefT>
1990 void determineChangedTracksFromNewTracksAndOldItems(NSArray* tracks, NSString* trackType, Vector<RefT>& oldItems, RefT (*itemFactory)(AVPlayerItemTrack*), MediaPlayer* player, void (MediaPlayer::*removedFunction)(PassRefT), void (MediaPlayer::*addedFunction)(PassRefT))
1991 {
1992     RetainPtr<NSSet> newTracks = adoptNS([[NSSet alloc] initWithArray:[tracks objectsAtIndexes:[tracks indexesOfObjectsPassingTest:^(id track, NSUInteger, BOOL*){
1993         return [[[track assetTrack] mediaType] isEqualToString:trackType];
1994     }]]]);
1995     RetainPtr<NSMutableSet> oldTracks = adoptNS([[NSMutableSet alloc] initWithCapacity:oldItems.size()]);
1996
1997     for (auto& oldItem : oldItems) {
1998         if (oldItem->playerItemTrack())
1999             [oldTracks addObject:oldItem->playerItemTrack()];
2000     }
2001
2002     // Find the added & removed AVPlayerItemTracks:
2003     RetainPtr<NSMutableSet> removedTracks = adoptNS([oldTracks mutableCopy]);
2004     [removedTracks minusSet:newTracks.get()];
2005
2006     RetainPtr<NSMutableSet> addedTracks = adoptNS([newTracks mutableCopy]);
2007     [addedTracks minusSet:oldTracks.get()];
2008
2009     typedef Vector<RefT> ItemVector;
2010     ItemVector replacementItems;
2011     ItemVector addedItems;
2012     ItemVector removedItems;
2013     for (auto& oldItem : oldItems) {
2014         if (oldItem->playerItemTrack() && [removedTracks containsObject:oldItem->playerItemTrack()])
2015             removedItems.append(oldItem);
2016         else
2017             replacementItems.append(oldItem);
2018     }
2019
2020     for (AVPlayerItemTrack* track in addedTracks.get())
2021         addedItems.append(itemFactory(track));
2022
2023     replacementItems.appendVector(addedItems);
2024     oldItems.swap(replacementItems);
2025
2026     for (auto& removedItem : removedItems)
2027         (player->*removedFunction)(*removedItem);
2028
2029     for (auto& addedItem : addedItems)
2030         (player->*addedFunction)(*addedItem);
2031 }
2032
2033 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2034
2035 template <typename RefT, typename PassRefT>
2036 void determineChangedTracksFromNewTracksAndOldItems(MediaSelectionGroupAVFObjC* group, Vector<RefT>& oldItems, const Vector<String>& characteristics, RefT (*itemFactory)(MediaSelectionOptionAVFObjC&), MediaPlayer* player, void (MediaPlayer::*removedFunction)(PassRefT), void (MediaPlayer::*addedFunction)(PassRefT))
2037 {
2038     group->updateOptions(characteristics);
2039
2040     ListHashSet<RefPtr<MediaSelectionOptionAVFObjC>> newSelectionOptions;
2041     for (auto& option : group->options()) {
2042         if (!option)
2043             continue;
2044         AVMediaSelectionOptionType* avOption = option->avMediaSelectionOption();
2045         if (!avOption)
2046             continue;
2047         newSelectionOptions.add(option);
2048     }
2049
2050     ListHashSet<RefPtr<MediaSelectionOptionAVFObjC>> oldSelectionOptions;
2051     for (auto& oldItem : oldItems) {
2052         if (MediaSelectionOptionAVFObjC *option = oldItem->mediaSelectionOption())
2053             oldSelectionOptions.add(option);
2054     }
2055
2056     // Find the added & removed AVMediaSelectionOptions:
2057     ListHashSet<RefPtr<MediaSelectionOptionAVFObjC>> removedSelectionOptions;
2058     for (auto& oldOption : oldSelectionOptions) {
2059         if (!newSelectionOptions.contains(oldOption))
2060             removedSelectionOptions.add(oldOption);
2061     }
2062
2063     ListHashSet<RefPtr<MediaSelectionOptionAVFObjC>> addedSelectionOptions;
2064     for (auto& newOption : newSelectionOptions) {
2065         if (!oldSelectionOptions.contains(newOption))
2066             addedSelectionOptions.add(newOption);
2067     }
2068
2069     typedef Vector<RefT> ItemVector;
2070     ItemVector replacementItems;
2071     ItemVector addedItems;
2072     ItemVector removedItems;
2073     for (auto& oldItem : oldItems) {
2074         if (!oldItem->mediaSelectionOption())
2075             removedItems.append(oldItem);
2076         else if (removedSelectionOptions.contains(oldItem->mediaSelectionOption()))
2077             removedItems.append(oldItem);
2078         else
2079             replacementItems.append(oldItem);
2080     }
2081
2082     for (auto& option : addedSelectionOptions)
2083         addedItems.append(itemFactory(*option.get()));
2084
2085     replacementItems.appendVector(addedItems);
2086     oldItems.swap(replacementItems);
2087     
2088     for (auto& removedItem : removedItems)
2089         (player->*removedFunction)(*removedItem);
2090
2091     for (auto& addedItem : addedItems)
2092         (player->*addedFunction)(*addedItem);
2093 }
2094
2095 #endif
2096
2097 void MediaPlayerPrivateAVFoundationObjC::updateAudioTracks()
2098 {
2099 #if !RELEASE_LOG_DISABLED
2100     size_t count = m_audioTracks.size();
2101 #endif
2102
2103 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2104     Vector<String> characteristics = player()->preferredAudioCharacteristics();
2105     if (!m_audibleGroup) {
2106         if (AVMediaSelectionGroupType *group = safeMediaSelectionGroupForAudibleMedia())
2107             m_audibleGroup = MediaSelectionGroupAVFObjC::create(m_avPlayerItem.get(), group, characteristics);
2108     }
2109
2110     if (m_audibleGroup)
2111         determineChangedTracksFromNewTracksAndOldItems(m_audibleGroup.get(), m_audioTracks, characteristics, &AudioTrackPrivateAVFObjC::create, player(), &MediaPlayer::removeAudioTrack, &MediaPlayer::addAudioTrack);
2112     else
2113 #endif
2114         determineChangedTracksFromNewTracksAndOldItems(m_cachedTracks.get(), AVMediaTypeAudio, m_audioTracks, &AudioTrackPrivateAVFObjC::create, player(), &MediaPlayer::removeAudioTrack, &MediaPlayer::addAudioTrack);
2115
2116     for (auto& track : m_audioTracks)
2117         track->resetPropertiesFromTrack();
2118
2119 #if !RELEASE_LOG_DISABLED
2120     INFO_LOG(LOGIDENTIFIER, "track count was ", count, ", is ", m_audioTracks.size());
2121 #endif
2122 }
2123
2124 void MediaPlayerPrivateAVFoundationObjC::updateVideoTracks()
2125 {
2126 #if !RELEASE_LOG_DISABLED
2127     size_t count = m_videoTracks.size();
2128 #endif
2129
2130     determineChangedTracksFromNewTracksAndOldItems(m_cachedTracks.get(), AVMediaTypeVideo, m_videoTracks, &VideoTrackPrivateAVFObjC::create, player(), &MediaPlayer::removeVideoTrack, &MediaPlayer::addVideoTrack);
2131
2132 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2133     if (!m_visualGroup) {
2134         if (AVMediaSelectionGroupType *group = safeMediaSelectionGroupForVisualMedia())
2135             m_visualGroup = MediaSelectionGroupAVFObjC::create(m_avPlayerItem.get(), group, Vector<String>());
2136     }
2137
2138     if (m_visualGroup)
2139         determineChangedTracksFromNewTracksAndOldItems(m_visualGroup.get(), m_videoTracks, Vector<String>(), &VideoTrackPrivateAVFObjC::create, player(), &MediaPlayer::removeVideoTrack, &MediaPlayer::addVideoTrack);
2140 #endif
2141
2142     for (auto& track : m_audioTracks)
2143         track->resetPropertiesFromTrack();
2144
2145 #if !RELEASE_LOG_DISABLED
2146     INFO_LOG(LOGIDENTIFIER, "track count was ", count, ", is ", m_videoTracks.size());
2147 #endif
2148 }
2149
2150 bool MediaPlayerPrivateAVFoundationObjC::requiresTextTrackRepresentation() const
2151 {
2152     return m_videoFullscreenLayerManager->requiresTextTrackRepresentation();
2153 }
2154
2155 void MediaPlayerPrivateAVFoundationObjC::syncTextTrackBounds()
2156 {
2157     m_videoFullscreenLayerManager->syncTextTrackBounds();
2158 }
2159
2160 void MediaPlayerPrivateAVFoundationObjC::setTextTrackRepresentation(TextTrackRepresentation* representation)
2161 {
2162     m_videoFullscreenLayerManager->setTextTrackRepresentation(representation);
2163 }
2164
2165 #endif // ENABLE(VIDEO_TRACK)
2166
2167 #if ENABLE(WEB_AUDIO) && USE(MEDIATOOLBOX)
2168
2169 AudioSourceProvider* MediaPlayerPrivateAVFoundationObjC::audioSourceProvider()
2170 {
2171     if (!m_provider) {
2172         m_provider = AudioSourceProviderAVFObjC::create(m_avPlayerItem.get());
2173         m_provider->setAudioTrack(firstEnabledTrack(safeAVAssetTracksForAudibleMedia()));
2174     }
2175     return m_provider.get();
2176 }
2177
2178 #endif
2179
2180 void MediaPlayerPrivateAVFoundationObjC::sizeChanged()
2181 {
2182     if (!m_avAsset)
2183         return;
2184
2185     setNaturalSize(m_cachedPresentationSize);
2186 }
2187
2188 void MediaPlayerPrivateAVFoundationObjC::resolvedURLChanged()
2189 {
2190     setResolvedURL(m_avAsset ? URL([m_avAsset resolvedURL]) : URL());
2191 }
2192
2193 bool MediaPlayerPrivateAVFoundationObjC::didPassCORSAccessCheck() const
2194 {
2195 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED > 101100
2196     AVAssetResourceLoader *resourceLoader = m_avAsset.get().resourceLoader;
2197     if (!DeprecatedGlobalSettings::isAVFoundationNSURLSessionEnabled()
2198         || ![resourceLoader respondsToSelector:@selector(URLSession)])
2199         return false;
2200
2201     WebCoreNSURLSession *session = (WebCoreNSURLSession *)resourceLoader.URLSession;
2202     if ([session isKindOfClass:[WebCoreNSURLSession class]])
2203         return session.didPassCORSAccessChecks;
2204 #endif
2205     return false;
2206 }
2207
2208 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
2209
2210 void MediaPlayerPrivateAVFoundationObjC::createVideoOutput()
2211 {
2212     INFO_LOG(LOGIDENTIFIER);
2213
2214     if (!m_avPlayerItem || m_videoOutput)
2215         return;
2216
2217 #if USE(VIDEOTOOLBOX)
2218     NSDictionary* attributes = nil;
2219 #else
2220     NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey, nil];
2221 #endif
2222     m_videoOutput = adoptNS([[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:attributes]);
2223     ASSERT(m_videoOutput);
2224
2225     [m_videoOutput setDelegate:m_videoOutputDelegate.get() queue:globalPullDelegateQueue()];
2226
2227     [m_avPlayerItem.get() addOutput:m_videoOutput.get()];
2228 }
2229
2230 void MediaPlayerPrivateAVFoundationObjC::destroyVideoOutput()
2231 {
2232     if (!m_videoOutput)
2233         return;
2234
2235     if (m_avPlayerItem)
2236         [m_avPlayerItem.get() removeOutput:m_videoOutput.get()];
2237
2238     INFO_LOG(LOGIDENTIFIER);
2239
2240     m_videoOutput = 0;
2241 }
2242
2243 bool MediaPlayerPrivateAVFoundationObjC::updateLastPixelBuffer()
2244 {
2245     if (!m_avPlayerItem)
2246         return false;
2247
2248     if (!m_videoOutput)
2249         createVideoOutput();
2250     ASSERT(m_videoOutput);
2251
2252     CMTime currentTime = [m_avPlayerItem.get() currentTime];
2253
2254     if (![m_videoOutput.get() hasNewPixelBufferForItemTime:currentTime])
2255         return false;
2256
2257     m_lastPixelBuffer = adoptCF([m_videoOutput.get() copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil]);
2258     m_lastImage = nullptr;
2259     return true;
2260 }
2261
2262 bool MediaPlayerPrivateAVFoundationObjC::videoOutputHasAvailableFrame()
2263 {
2264     if (!m_avPlayerItem)
2265         return false;
2266
2267     if (m_lastImage)
2268         return true;
2269
2270     if (!m_videoOutput)
2271         createVideoOutput();
2272
2273     return [m_videoOutput hasNewPixelBufferForItemTime:[m_avPlayerItem currentTime]];
2274 }
2275
2276 void MediaPlayerPrivateAVFoundationObjC::updateLastImage(UpdateType type)
2277 {
2278 #if HAVE(CORE_VIDEO)
2279     if (!m_avPlayerItem)
2280         return;
2281
2282     if (type == UpdateType::UpdateSynchronously && !m_lastImage && !videoOutputHasAvailableFrame())
2283         waitForVideoOutputMediaDataWillChange();
2284
2285     // Calls to copyPixelBufferForItemTime:itemTimeForDisplay: may return nil if the pixel buffer
2286     // for the requested time has already been retrieved. In this case, the last valid image (if any)
2287     // should be displayed.
2288     if (!updateLastPixelBuffer() && (m_lastImage || !m_lastPixelBuffer))
2289         return;
2290
2291     if (!m_pixelBufferConformer) {
2292 #if USE(VIDEOTOOLBOX)
2293         NSDictionary *attributes = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA) };
2294 #else
2295         NSDictionary *attributes = nil;
2296 #endif
2297         m_pixelBufferConformer = std::make_unique<PixelBufferConformerCV>((CFDictionaryRef)attributes);
2298     }
2299
2300 #if !RELEASE_LOG_DISABLED
2301     MonotonicTime start = MonotonicTime::now();
2302 #endif
2303
2304     m_lastImage = m_pixelBufferConformer->createImageFromPixelBuffer(m_lastPixelBuffer.get());
2305
2306 #if !RELEASE_LOG_DISABLED
2307     DEBUG_LOG(LOGIDENTIFIER, "creating buffer took ", (MonotonicTime::now() - start).seconds());
2308 #endif
2309 #endif // HAVE(CORE_VIDEO)
2310 }
2311
2312 void MediaPlayerPrivateAVFoundationObjC::paintWithVideoOutput(GraphicsContext& context, const FloatRect& outputRect)
2313 {
2314     updateLastImage(UpdateType::UpdateSynchronously);
2315     if (!m_lastImage)
2316         return;
2317
2318     AVAssetTrack* firstEnabledVideoTrack = firstEnabledTrack([m_avAsset.get() tracksWithMediaCharacteristic:AVMediaCharacteristicVisual]);
2319     if (!firstEnabledVideoTrack)
2320         return;
2321
2322     INFO_LOG(LOGIDENTIFIER);
2323
2324     GraphicsContextStateSaver stateSaver(context);
2325     FloatRect imageRect(0, 0, CGImageGetWidth(m_lastImage.get()), CGImageGetHeight(m_lastImage.get()));
2326     AffineTransform videoTransform = [firstEnabledVideoTrack preferredTransform];
2327     FloatRect transformedOutputRect = videoTransform.inverse().value_or(AffineTransform()).mapRect(outputRect);
2328
2329     context.concatCTM(videoTransform);
2330     context.drawNativeImage(m_lastImage.get(), imageRect.size(), transformedOutputRect, imageRect);
2331
2332     // If we have created an AVAssetImageGenerator in the past due to m_videoOutput not having an available
2333     // video frame, destroy it now that it is no longer needed.
2334     if (m_imageGenerator)
2335         destroyImageGenerator();
2336
2337 }
2338
2339 bool MediaPlayerPrivateAVFoundationObjC::copyVideoTextureToPlatformTexture(GraphicsContext3D* context, Platform3DObject outputTexture, GC3Denum outputTarget, GC3Dint level, GC3Denum internalFormat, GC3Denum format, GC3Denum type, bool premultiplyAlpha, bool flipY)
2340 {
2341     ASSERT(context);
2342
2343     updateLastPixelBuffer();
2344     if (!m_lastPixelBuffer)
2345         return false;
2346
2347     size_t width = CVPixelBufferGetWidth(m_lastPixelBuffer.get());
2348     size_t height = CVPixelBufferGetHeight(m_lastPixelBuffer.get());
2349
2350     if (!m_videoTextureCopier)
2351         m_videoTextureCopier = std::make_unique<VideoTextureCopierCV>(*context);
2352
2353     return m_videoTextureCopier->copyImageToPlatformTexture(m_lastPixelBuffer.get(), width, height, outputTexture, outputTarget, level, internalFormat, format, type, premultiplyAlpha, flipY);
2354 }
2355
2356 NativeImagePtr MediaPlayerPrivateAVFoundationObjC::nativeImageForCurrentTime()
2357 {
2358     updateLastImage();
2359     return m_lastImage;
2360 }
2361
2362 void MediaPlayerPrivateAVFoundationObjC::waitForVideoOutputMediaDataWillChange()
2363 {
2364     if (!m_videoOutputSemaphore)
2365         m_videoOutputSemaphore = dispatch_semaphore_create(0);
2366
2367     [m_videoOutput requestNotificationOfMediaDataChangeWithAdvanceInterval:0];
2368
2369     // Wait for 1 second.
2370     long result = dispatch_semaphore_wait(m_videoOutputSemaphore, dispatch_time(0, 1 * NSEC_PER_SEC));
2371
2372     if (result)
2373         ERROR_LOG(LOGIDENTIFIER, "timed out");
2374 }
2375
2376 void MediaPlayerPrivateAVFoundationObjC::outputMediaDataWillChange(AVPlayerItemVideoOutputType *)
2377 {
2378     dispatch_semaphore_signal(m_videoOutputSemaphore);
2379 }
2380
2381 #endif
2382
2383 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
2384
2385 RetainPtr<AVAssetResourceLoadingRequest> MediaPlayerPrivateAVFoundationObjC::takeRequestForKeyURI(const String& keyURI)
2386 {
2387     return m_keyURIToRequestMap.take(keyURI);
2388 }
2389
2390 void MediaPlayerPrivateAVFoundationObjC::keyAdded()
2391 {
2392     Vector<String> fulfilledKeyIds;
2393
2394     for (auto& pair : m_keyURIToRequestMap) {
2395         const String& keyId = pair.key;
2396         const RetainPtr<AVAssetResourceLoadingRequest>& request = pair.value;
2397
2398         auto keyData = player()->cachedKeyForKeyId(keyId);
2399         if (!keyData)
2400             continue;
2401
2402         fulfillRequestWithKeyData(request.get(), keyData.get());
2403         fulfilledKeyIds.append(keyId);
2404     }
2405
2406     for (auto& keyId : fulfilledKeyIds)
2407         m_keyURIToRequestMap.remove(keyId);
2408 }
2409
2410 void MediaPlayerPrivateAVFoundationObjC::removeSession(LegacyCDMSession& session)
2411 {
2412     ASSERT_UNUSED(session, &session == m_session);
2413     m_session = nullptr;
2414 }
2415
2416 std::unique_ptr<LegacyCDMSession> MediaPlayerPrivateAVFoundationObjC::createSession(const String& keySystem, LegacyCDMSessionClient* client)
2417 {
2418     if (!keySystemIsSupported(keySystem))
2419         return nullptr;
2420     auto session = std::make_unique<CDMSessionAVFoundationObjC>(this, client);
2421     m_session = session->createWeakPtr();
2422     return WTFMove(session);
2423 }
2424 #endif
2425
2426 #if ENABLE(ENCRYPTED_MEDIA) || ENABLE(LEGACY_ENCRYPTED_MEDIA)
2427 void MediaPlayerPrivateAVFoundationObjC::outputObscuredDueToInsufficientExternalProtectionChanged(bool newValue)
2428 {
2429 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
2430     if (m_session && newValue)
2431         m_session->playerDidReceiveError([NSError errorWithDomain:@"com.apple.WebKit" code:'HDCP' userInfo:nil]);
2432 #endif
2433
2434 #if ENABLE(ENCRYPTED_MEDIA) && HAVE(AVCONTENTKEYSESSION)
2435     if (m_cdmInstance)
2436         m_cdmInstance->outputObscuredDueToInsufficientExternalProtectionChanged(newValue);
2437 #elif !ENABLE(LEGACY_ENCRYPTED_MEDIA)
2438     UNUSED_PARAM(newValue);
2439 #endif
2440 }
2441 #endif
2442
2443 #if ENABLE(ENCRYPTED_MEDIA)
2444 void MediaPlayerPrivateAVFoundationObjC::cdmInstanceAttached(CDMInstance& instance)
2445 {
2446 #if HAVE(AVCONTENTKEYSESSION)
2447     if (!is<CDMInstanceFairPlayStreamingAVFObjC>(instance))
2448         return;
2449
2450     auto& fpsInstance = downcast<CDMInstanceFairPlayStreamingAVFObjC>(instance);
2451     if (&fpsInstance == m_cdmInstance)
2452         return;
2453
2454     if (m_cdmInstance)
2455         cdmInstanceDetached(*m_cdmInstance);
2456
2457     m_cdmInstance = &fpsInstance;
2458     [m_cdmInstance->contentKeySession() addContentKeyRecipient:m_avAsset.get()];
2459 #else
2460     UNUSED_PARAM(instance);
2461 #endif
2462 }
2463
2464 void MediaPlayerPrivateAVFoundationObjC::cdmInstanceDetached(CDMInstance& instance)
2465 {
2466 #if HAVE(AVCONTENTKEYSESSION)
2467     ASSERT_UNUSED(instance, m_cdmInstance && m_cdmInstance == &instance);
2468     [m_cdmInstance->contentKeySession() removeContentKeyRecipient:m_avAsset.get()];
2469     m_cdmInstance = nullptr;
2470 #else
2471     UNUSED_PARAM(instance);
2472 #endif
2473 }
2474
2475 void MediaPlayerPrivateAVFoundationObjC::attemptToDecryptWithInstance(CDMInstance&)
2476 {
2477     auto keyURIToRequestMap = WTFMove(m_keyURIToRequestMap);
2478     for (auto& request : keyURIToRequestMap.values())
2479         [request finishLoading];
2480 }
2481 #endif
2482
2483 #if !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
2484
2485 void MediaPlayerPrivateAVFoundationObjC::processLegacyClosedCaptionsTracks()
2486 {
2487 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2488     [m_avPlayerItem.get() selectMediaOption:nil inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2489 #endif
2490
2491     Vector<RefPtr<InbandTextTrackPrivateAVF>> removedTextTracks = m_textTracks;
2492     for (AVPlayerItemTrack *playerItemTrack in m_cachedTracks.get()) {
2493
2494         AVAssetTrack *assetTrack = [playerItemTrack assetTrack];
2495         if (![[assetTrack mediaType] isEqualToString:AVMediaTypeClosedCaption])
2496             continue;
2497
2498         bool newCCTrack = true;
2499         for (unsigned i = removedTextTracks.size(); i > 0; --i) {
2500             if (removedTextTracks[i - 1]->textTrackCategory() != InbandTextTrackPrivateAVF::LegacyClosedCaption)
2501                 continue;
2502
2503             RefPtr<InbandTextTrackPrivateLegacyAVFObjC> track = static_cast<InbandTextTrackPrivateLegacyAVFObjC*>(m_textTracks[i - 1].get());
2504             if (track->avPlayerItemTrack() == playerItemTrack) {
2505                 removedTextTracks.remove(i - 1);
2506                 newCCTrack = false;
2507                 break;
2508             }
2509         }
2510
2511         if (!newCCTrack)
2512             continue;
2513         
2514         m_textTracks.append(InbandTextTrackPrivateLegacyAVFObjC::create(this, playerItemTrack));
2515     }
2516
2517     processNewAndRemovedTextTracks(removedTextTracks);
2518 }
2519
2520 #endif
2521
2522 NSArray* MediaPlayerPrivateAVFoundationObjC::safeAVAssetTracksForAudibleMedia()
2523 {
2524     if (!m_avAsset)
2525         return nil;
2526
2527     if ([m_avAsset.get() statusOfValueForKey:@"tracks" error:NULL] != AVKeyValueStatusLoaded)
2528         return nil;
2529
2530     return [m_avAsset tracksWithMediaCharacteristic:AVMediaCharacteristicAudible];
2531 }
2532
2533 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2534
2535 bool MediaPlayerPrivateAVFoundationObjC::hasLoadedMediaSelectionGroups()
2536 {
2537     if (!m_avAsset)
2538         return false;
2539
2540     if ([m_avAsset.get() statusOfValueForKey:@"availableMediaCharacteristicsWithMediaSelectionOptions" error:NULL] != AVKeyValueStatusLoaded)
2541         return false;
2542
2543     return true;
2544 }
2545
2546 AVMediaSelectionGroupType* MediaPlayerPrivateAVFoundationObjC::safeMediaSelectionGroupForLegibleMedia()
2547 {
2548     if (!hasLoadedMediaSelectionGroups())
2549         return nil;
2550
2551     return [m_avAsset.get() mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicLegible];
2552 }
2553
2554 AVMediaSelectionGroupType* MediaPlayerPrivateAVFoundationObjC::safeMediaSelectionGroupForAudibleMedia()
2555 {
2556     if (!hasLoadedMediaSelectionGroups())
2557         return nil;
2558
2559     return [m_avAsset.get() mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
2560 }
2561
2562 AVMediaSelectionGroupType* MediaPlayerPrivateAVFoundationObjC::safeMediaSelectionGroupForVisualMedia()
2563 {
2564     if (!hasLoadedMediaSelectionGroups())
2565         return nil;
2566
2567     return [m_avAsset.get() mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicVisual];
2568 }
2569
2570 void MediaPlayerPrivateAVFoundationObjC::processMediaSelectionOptions()
2571 {
2572     AVMediaSelectionGroupType *legibleGroup = safeMediaSelectionGroupForLegibleMedia();
2573     if (!legibleGroup) {
2574         INFO_LOG(LOGIDENTIFIER, "no mediaSelectionGroup");
2575         return;
2576     }
2577
2578     // We enabled automatic media selection because we want alternate audio tracks to be enabled/disabled automatically,
2579     // but set the selected legible track to nil so text tracks will not be automatically configured.
2580     if (!m_textTracks.size())
2581         [m_avPlayerItem.get() selectMediaOption:nil inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2582
2583     Vector<RefPtr<InbandTextTrackPrivateAVF>> removedTextTracks = m_textTracks;
2584     NSArray *legibleOptions = [AVMediaSelectionGroup playableMediaSelectionOptionsFromArray:[legibleGroup options]];
2585     for (AVMediaSelectionOptionType *option in legibleOptions) {
2586         bool newTrack = true;
2587         for (unsigned i = removedTextTracks.size(); i > 0; --i) {
2588             if (removedTextTracks[i - 1]->textTrackCategory() == InbandTextTrackPrivateAVF::LegacyClosedCaption)
2589                 continue;
2590             
2591             RetainPtr<AVMediaSelectionOptionType> currentOption;
2592 #if ENABLE(AVF_CAPTIONS)
2593             if (removedTextTracks[i - 1]->textTrackCategory() == InbandTextTrackPrivateAVF::OutOfBand) {
2594                 RefPtr<OutOfBandTextTrackPrivateAVF> track = static_cast<OutOfBandTextTrackPrivateAVF*>(removedTextTracks[i - 1].get());
2595                 currentOption = track->mediaSelectionOption();
2596             } else
2597 #endif
2598             {
2599                 RefPtr<InbandTextTrackPrivateAVFObjC> track = static_cast<InbandTextTrackPrivateAVFObjC*>(removedTextTracks[i - 1].get());
2600                 currentOption = track->mediaSelectionOption();
2601             }
2602             
2603             if ([currentOption.get() isEqual:option]) {
2604                 removedTextTracks.remove(i - 1);
2605                 newTrack = false;
2606                 break;
2607             }
2608         }
2609         if (!newTrack)
2610             continue;
2611
2612 #if ENABLE(AVF_CAPTIONS)
2613         if ([option outOfBandSource]) {
2614             m_textTracks.append(OutOfBandTextTrackPrivateAVF::create(this, option));
2615             m_textTracks.last()->setHasBeenReported(true); // Ignore out-of-band tracks that we passed to AVFoundation so we do not double-count them
2616             continue;
2617         }
2618 #endif
2619
2620         m_textTracks.append(InbandTextTrackPrivateAVFObjC::create(this, option, InbandTextTrackPrivate::Generic));
2621     }
2622
2623     processNewAndRemovedTextTracks(removedTextTracks);
2624 }
2625
2626 void MediaPlayerPrivateAVFoundationObjC::processMetadataTrack()
2627 {
2628     if (m_metadataTrack)
2629         return;
2630
2631     m_metadataTrack = InbandMetadataTextTrackPrivateAVF::create(InbandTextTrackPrivate::Metadata, InbandTextTrackPrivate::Data);
2632     m_metadataTrack->setInBandMetadataTrackDispatchType("com.apple.streaming");
2633     player()->addTextTrack(*m_metadataTrack);
2634 }
2635
2636 void MediaPlayerPrivateAVFoundationObjC::processCue(NSArray *attributedStrings, NSArray *nativeSamples, const MediaTime& time)
2637 {
2638     ASSERT(time >= MediaTime::zeroTime());
2639
2640     if (!m_currentTextTrack)
2641         return;
2642
2643     m_currentTextTrack->processCue(reinterpret_cast<CFArrayRef>(attributedStrings), reinterpret_cast<CFArrayRef>(nativeSamples), time);
2644 }
2645
2646 void MediaPlayerPrivateAVFoundationObjC::flushCues()
2647 {
2648     INFO_LOG(LOGIDENTIFIER);
2649
2650     if (!m_currentTextTrack)
2651         return;
2652     
2653     m_currentTextTrack->resetCueValues();
2654 }
2655
2656 #endif // HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2657
2658 void MediaPlayerPrivateAVFoundationObjC::setCurrentTextTrack(InbandTextTrackPrivateAVF *track)
2659 {
2660     if (m_currentTextTrack == track)
2661         return;
2662
2663     INFO_LOG(LOGIDENTIFIER, "selecting track with language ", track ? track->language() : "");
2664
2665     m_currentTextTrack = track;
2666
2667     if (track) {
2668         if (track->textTrackCategory() == InbandTextTrackPrivateAVF::LegacyClosedCaption)
2669 #pragma clang diagnostic push
2670 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2671             [m_avPlayer.get() setClosedCaptionDisplayEnabled:YES];
2672 #pragma clang diagnostic pop
2673 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2674 #if ENABLE(AVF_CAPTIONS)
2675         else if (track->textTrackCategory() == InbandTextTrackPrivateAVF::OutOfBand)
2676             [m_avPlayerItem.get() selectMediaOption:static_cast<OutOfBandTextTrackPrivateAVF*>(track)->mediaSelectionOption() inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2677 #endif
2678         else
2679             [m_avPlayerItem.get() selectMediaOption:static_cast<InbandTextTrackPrivateAVFObjC*>(track)->mediaSelectionOption() inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2680 #endif
2681     } else {
2682 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2683         [m_avPlayerItem.get() selectMediaOption:0 inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2684 #endif
2685 #pragma clang diagnostic push
2686 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2687         [m_avPlayer.get() setClosedCaptionDisplayEnabled:NO];
2688 #pragma clang diagnostic pop
2689     }
2690
2691 }
2692
2693 String MediaPlayerPrivateAVFoundationObjC::languageOfPrimaryAudioTrack() const
2694 {
2695     if (!m_languageOfPrimaryAudioTrack.isNull())
2696         return m_languageOfPrimaryAudioTrack;
2697
2698     if (!m_avPlayerItem.get())
2699         return emptyString();
2700
2701 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2702     // If AVFoundation has an audible group, return the language of the currently selected audible option.
2703     AVMediaSelectionGroupType *audibleGroup = [m_avAsset.get() mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
2704 #pragma clang diagnostic push
2705 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2706     AVMediaSelectionOptionType *currentlySelectedAudibleOption = [m_avPlayerItem.get() selectedMediaOptionInMediaSelectionGroup:audibleGroup];
2707 #pragma clang diagnostic pop
2708     if (currentlySelectedAudibleOption) {
2709         m_languageOfPrimaryAudioTrack = [[currentlySelectedAudibleOption locale] localeIdentifier];
2710         INFO_LOG(LOGIDENTIFIER, "language of selected audible option ", m_languageOfPrimaryAudioTrack);
2711
2712         return m_languageOfPrimaryAudioTrack;
2713     }
2714 #endif // HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2715
2716     // AVFoundation synthesizes an audible group when there is only one ungrouped audio track if there is also a legible group (one or
2717     // more in-band text tracks). It doesn't know about out-of-band tracks, so if there is a single audio track return its language.
2718     NSArray *tracks = [m_avAsset.get() tracksWithMediaType:AVMediaTypeAudio];
2719     if (!tracks || [tracks count] != 1) {
2720         m_languageOfPrimaryAudioTrack = emptyString();
2721         INFO_LOG(LOGIDENTIFIER, tracks ? [tracks count] : 0, " audio tracks, returning empty");
2722         return m_languageOfPrimaryAudioTrack;
2723     }
2724
2725     AVAssetTrack *track = [tracks objectAtIndex:0];
2726     m_languageOfPrimaryAudioTrack = AVTrackPrivateAVFObjCImpl::languageForAVAssetTrack(track);
2727
2728     INFO_LOG(LOGIDENTIFIER, "single audio track has language \"", m_languageOfPrimaryAudioTrack, "\"");
2729
2730     return m_languageOfPrimaryAudioTrack;
2731 }
2732
2733 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
2734 bool MediaPlayerPrivateAVFoundationObjC::isCurrentPlaybackTargetWireless() const
2735 {
2736     bool wirelessTarget = false;
2737
2738 #if !PLATFORM(IOS)
2739     if (m_playbackTarget) {
2740         if (m_playbackTarget->targetType() == MediaPlaybackTarget::AVFoundation)
2741             wirelessTarget = m_avPlayer && m_avPlayer.get().externalPlaybackActive;
2742         else
2743             wirelessTarget = m_shouldPlayToPlaybackTarget && m_playbackTarget->hasActiveRoute();
2744     }
2745 #else
2746     wirelessTarget = m_avPlayer && m_avPlayer.get().externalPlaybackActive;
2747 #endif
2748
2749     INFO_LOG(LOGIDENTIFIER, "- ", wirelessTarget);
2750
2751     return wirelessTarget;
2752 }
2753
2754 MediaPlayer::WirelessPlaybackTargetType MediaPlayerPrivateAVFoundationObjC::wirelessPlaybackTargetType() const
2755 {
2756     if (!m_avPlayer)
2757         return MediaPlayer::TargetTypeNone;
2758
2759 #if PLATFORM(IOS)
2760     if (!AVFoundationLibrary())
2761         return MediaPlayer::TargetTypeNone;
2762
2763     switch ([m_avPlayer externalPlaybackType]) {
2764     case AVPlayerExternalPlaybackTypeNone:
2765         return MediaPlayer::TargetTypeNone;
2766     case AVPlayerExternalPlaybackTypeAirPlay:
2767         return MediaPlayer::TargetTypeAirPlay;
2768     case AVPlayerExternalPlaybackTypeTVOut:
2769         return MediaPlayer::TargetTypeTVOut;
2770     }
2771
2772     ASSERT_NOT_REACHED();
2773     return MediaPlayer::TargetTypeNone;
2774
2775 #else
2776     return MediaPlayer::TargetTypeAirPlay;
2777 #endif
2778 }
2779     
2780 #if PLATFORM(IOS)
2781 static NSString *exernalDeviceDisplayNameForPlayer(AVPlayerType *player)
2782 {
2783 #if HAVE(CELESTIAL)
2784     NSString *displayName = nil;
2785
2786     if (!AVFoundationLibrary())
2787         return nil;
2788
2789     if (player.externalPlaybackType != AVPlayerExternalPlaybackTypeAirPlay)
2790         return nil;
2791
2792     NSArray *pickableRoutes = CFBridgingRelease(MRMediaRemoteCopyPickableRoutes());
2793     if (!pickableRoutes.count)
2794         return nil;
2795
2796     for (NSDictionary *pickableRoute in pickableRoutes) {
2797         if (![pickableRoute[AVController_RouteDescriptionKey_RouteCurrentlyPicked] boolValue])
2798             continue;
2799
2800         displayName = pickableRoute[AVController_RouteDescriptionKey_RouteName];
2801
2802         NSString *routeName = pickableRoute[AVController_RouteDescriptionKey_AVAudioRouteName];
2803         if (![routeName isEqualToString:@"Speaker"] && ![routeName isEqualToString:@"HDMIOutput"])
2804             break;
2805
2806         // The route is a speaker or HDMI out, override the name to be the localized device model.
2807         NSString *localizedDeviceModel = [[UIDevice currentDevice] localizedModel];
2808
2809         // In cases where a route with that name already exists, prefix the name with the model.
2810         BOOL includeLocalizedDeviceModelName = NO;
2811         for (NSDictionary *otherRoute in pickableRoutes) {
2812             if (otherRoute == pickableRoute)
2813                 continue;
2814
2815             if ([otherRoute[AVController_RouteDescriptionKey_RouteName] rangeOfString:displayName].location != NSNotFound) {
2816                 includeLocalizedDeviceModelName = YES;
2817                 break;
2818             }
2819         }
2820
2821         if (includeLocalizedDeviceModelName)
2822             displayName =  [NSString stringWithFormat:@"%@ %@", localizedDeviceModel, displayName];
2823         else
2824             displayName = localizedDeviceModel;
2825
2826         break;
2827     }
2828
2829     return displayName;
2830 #else
2831     UNUSED_PARAM(player);
2832     return nil;
2833 #endif
2834 }
2835 #endif
2836
2837 String MediaPlayerPrivateAVFoundationObjC::wirelessPlaybackTargetName() const
2838 {
2839     if (!m_avPlayer)
2840         return emptyString();
2841
2842     String wirelessTargetName;
2843 #if !PLATFORM(IOS)
2844     if (m_playbackTarget)
2845         wirelessTargetName = m_playbackTarget->deviceName();
2846 #else
2847     wirelessTargetName = exernalDeviceDisplayNameForPlayer(m_avPlayer.get());
2848 #endif
2849
2850     return wirelessTargetName;
2851 }
2852
2853 bool MediaPlayerPrivateAVFoundationObjC::wirelessVideoPlaybackDisabled() const
2854 {
2855     if (!m_avPlayer)
2856         return !m_allowsWirelessVideoPlayback;
2857
2858     m_allowsWirelessVideoPlayback = [m_avPlayer.get() allowsExternalPlayback];
2859     INFO_LOG(LOGIDENTIFIER, "- ", !m_allowsWirelessVideoPlayback);
2860
2861     return !m_allowsWirelessVideoPlayback;
2862 }
2863
2864 void MediaPlayerPrivateAVFoundationObjC::setWirelessVideoPlaybackDisabled(bool disabled)
2865 {
2866     INFO_LOG(LOGIDENTIFIER, "- ", disabled);
2867     m_allowsWirelessVideoPlayback = !disabled;
2868     if (!m_avPlayer)
2869         return;
2870
2871     setDelayCallbacks(true);
2872     [m_avPlayer.get() setAllowsExternalPlayback:!disabled];
2873     setDelayCallbacks(false);
2874 }
2875
2876 #if !PLATFORM(IOS)
2877
2878 void MediaPlayerPrivateAVFoundationObjC::setWirelessPlaybackTarget(Ref<MediaPlaybackTarget>&& target)
2879 {
2880     m_playbackTarget = WTFMove(target);
2881
2882     m_outputContext = m_playbackTarget->targetType() == MediaPlaybackTarget::AVFoundation ? toMediaPlaybackTargetMac(m_playbackTarget.get())->outputContext() : nullptr;
2883
2884     INFO_LOG(LOGIDENTIFIER);
2885
2886     if (!m_playbackTarget->hasActiveRoute())
2887         setShouldPlayToPlaybackTarget(false);
2888 }
2889
2890 void MediaPlayerPrivateAVFoundationObjC::setShouldPlayToPlaybackTarget(bool shouldPlay)
2891 {
2892     if (m_shouldPlayToPlaybackTarget == shouldPlay)
2893         return;
2894
2895     m_shouldPlayToPlaybackTarget = shouldPlay;
2896
2897     if (!m_playbackTarget)
2898         return;
2899
2900     INFO_LOG(LOGIDENTIFIER, "- ", shouldPlay);
2901
2902     if (m_playbackTarget->targetType() == MediaPlaybackTarget::AVFoundation) {
2903         AVOutputContext *newContext = shouldPlay ? m_outputContext.get() : nil;
2904
2905         if (!m_avPlayer)
2906             return;
2907
2908         RetainPtr<AVOutputContext> currentContext = m_avPlayer.get().outputContext;
2909         if ((!newContext && !currentContext.get()) || [currentContext.get() isEqual:newContext])
2910             return;
2911
2912         setDelayCallbacks(true);
2913         m_avPlayer.get().outputContext = newContext;
2914         setDelayCallbacks(false);
2915
2916         return;
2917     }
2918
2919     ASSERT(m_playbackTarget->targetType() == MediaPlaybackTarget::Mock);
2920
2921     setDelayCallbacks(true);
2922     auto weakThis = createWeakPtr();
2923     scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification([weakThis] {
2924         if (!weakThis)
2925             return;
2926         weakThis->playbackTargetIsWirelessDidChange();
2927     }));
2928     setDelayCallbacks(false);
2929 }
2930
2931 #endif // !PLATFORM(IOS)
2932
2933 void MediaPlayerPrivateAVFoundationObjC::updateDisableExternalPlayback()
2934 {
2935 #if PLATFORM(IOS)
2936     if (!m_avPlayer)
2937         return;
2938
2939     [m_avPlayer setUsesExternalPlaybackWhileExternalScreenIsActive:player()->fullscreenMode() & MediaPlayer::VideoFullscreenModeStandard];
2940 #endif
2941 }
2942
2943 #endif
2944
2945 void MediaPlayerPrivateAVFoundationObjC::playerItemStatusDidChange(int status)
2946 {
2947     m_cachedItemStatus = status;
2948
2949     updateStates();
2950 }
2951
2952 void MediaPlayerPrivateAVFoundationObjC::playbackLikelyToKeepUpWillChange()
2953 {
2954     m_pendingStatusChanges++;
2955 }
2956
2957 void MediaPlayerPrivateAVFoundationObjC::playbackLikelyToKeepUpDidChange(bool likelyToKeepUp)
2958 {
2959     m_cachedLikelyToKeepUp = likelyToKeepUp;
2960
2961     ASSERT(m_pendingStatusChanges);
2962     if (!--m_pendingStatusChanges)
2963         updateStates();
2964 }
2965
2966 void MediaPlayerPrivateAVFoundationObjC::playbackBufferEmptyWillChange()
2967 {
2968     m_pendingStatusChanges++;
2969 }
2970
2971 void MediaPlayerPrivateAVFoundationObjC::playbackBufferEmptyDidChange(bool bufferEmpty)
2972 {
2973     m_cachedBufferEmpty = bufferEmpty;
2974
2975     ASSERT(m_pendingStatusChanges);
2976     if (!--m_pendingStatusChanges)
2977         updateStates();
2978 }
2979
2980 void MediaPlayerPrivateAVFoundationObjC::playbackBufferFullWillChange()
2981 {
2982     m_pendingStatusChanges++;
2983 }
2984
2985 void MediaPlayerPrivateAVFoundationObjC::playbackBufferFullDidChange(bool bufferFull)
2986 {
2987     m_cachedBufferFull = bufferFull;
2988
2989     ASSERT(m_pendingStatusChanges);
2990     if (!--m_pendingStatusChanges)
2991         updateStates();
2992 }
2993
2994 void MediaPlayerPrivateAVFoundationObjC::seekableTimeRangesDidChange(RetainPtr<NSArray> seekableRanges)
2995 {
2996     m_cachedSeekableRanges = seekableRanges;
2997
2998     seekableTimeRangesChanged();
2999     updateStates();
3000 }
3001
3002 void MediaPlayerPrivateAVFoundationObjC::loadedTimeRangesDidChange(RetainPtr<NSArray> loadedRanges)
3003 {
3004     m_cachedLoadedRanges = loadedRanges;
3005
3006     loadedTimeRangesChanged();
3007     updateStates();
3008 }
3009
3010 void MediaPlayerPrivateAVFoundationObjC::firstFrameAvailableDidChange(bool isReady)
3011 {
3012     m_cachedIsReadyForDisplay = isReady;
3013     if (!hasVideo() && isReady)
3014         tracksChanged();
3015     updateStates();
3016 }
3017
3018 void MediaPlayerPrivateAVFoundationObjC::trackEnabledDidChange(bool)
3019 {
3020     tracksChanged();
3021     updateStates();
3022 }
3023
3024 void MediaPlayerPrivateAVFoundationObjC::setShouldBufferData(bool shouldBuffer)
3025 {
3026     INFO_LOG(LOGIDENTIFIER, "- ", shouldBuffer);
3027
3028     if (m_shouldBufferData == shouldBuffer)
3029         return;
3030
3031     m_shouldBufferData = shouldBuffer;
3032     
3033     if (!m_avPlayer)
3034         return;
3035
3036     setAVPlayerItem(shouldBuffer ? m_avPlayerItem.get() : nil);
3037 }
3038
3039 #if ENABLE(DATACUE_VALUE)
3040
3041 static const AtomicString& metadataType(NSString *avMetadataKeySpace)
3042 {
3043     static NeverDestroyed<const AtomicString> quickTimeUserData("com.apple.quicktime.udta", AtomicString::ConstructFromLiteral);
3044     static NeverDestroyed<const AtomicString> isoUserData("org.mp4ra", AtomicString::ConstructFromLiteral);
3045     static NeverDestroyed<const AtomicString> quickTimeMetadata("com.apple.quicktime.mdta", AtomicString::ConstructFromLiteral);
3046     static NeverDestroyed<const AtomicString> iTunesMetadata("com.apple.itunes", AtomicString::ConstructFromLiteral);
3047     static NeverDestroyed<const AtomicString> id3Metadata("org.id3", AtomicString::ConstructFromLiteral);
3048
3049     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceQuickTimeUserData])
3050         return quickTimeUserData;
3051     if (AVMetadataKeySpaceISOUserData && [avMetadataKeySpace isEqualToString:AVMetadataKeySpaceISOUserData])
3052         return isoUserData;
3053     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceQuickTimeMetadata])
3054         return quickTimeMetadata;
3055     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceiTunes])
3056         return iTunesMetadata;
3057     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceID3])
3058         return id3Metadata;
3059
3060     return emptyAtom();
3061 }
3062
3063 #endif
3064
3065 void MediaPlayerPrivateAVFoundationObjC::metadataDidArrive(RetainPtr<NSArray> metadata, const MediaTime& mediaTime)
3066 {
3067     m_currentMetaData = metadata && ![metadata isKindOfClass:[NSNull class]] ? metadata : nil;
3068
3069     DEBUG_LOG(LOGIDENTIFIER, "adding ", m_currentMetaData ? [m_currentMetaData.get() count] : 0, " at time ", mediaTime);
3070
3071 #if ENABLE(DATACUE_VALUE)
3072     if (seeking())
3073         return;
3074
3075     if (!m_metadataTrack)
3076         processMetadataTrack();
3077
3078     if (!metadata || [metadata isKindOfClass:[NSNull class]]) {
3079         m_metadataTrack->updatePendingCueEndTimes(mediaTime);
3080         return;
3081     }
3082
3083     // Set the duration of all incomplete cues before adding new ones.
3084     MediaTime earliestStartTime = MediaTime::positiveInfiniteTime();
3085     for (AVMetadataItemType *item in m_currentMetaData.get()) {
3086         MediaTime start = std::max(PAL::toMediaTime(item.time), MediaTime::zeroTime());
3087         if (start < earliestStartTime)
3088             earliestStartTime = start;
3089     }
3090     m_metadataTrack->updatePendingCueEndTimes(earliestStartTime);
3091
3092     for (AVMetadataItemType *item in m_currentMetaData.get()) {
3093         MediaTime start = std::max(PAL::toMediaTime(item.time), MediaTime::zeroTime());
3094         MediaTime end = MediaTime::positiveInfiniteTime();
3095         if (CMTIME_IS_VALID(item.duration))
3096             end = start + PAL::toMediaTime(item.duration);
3097
3098         AtomicString type = nullAtom();
3099         if (item.keySpace)
3100             type = metadataType(item.keySpace);
3101
3102         m_metadataTrack->addDataCue(start, end, SerializedPlatformRepresentationMac::create(item), type);
3103     }
3104 #endif
3105 }
3106
3107 void MediaPlayerPrivateAVFoundationObjC::tracksDidChange(RetainPtr<NSArray> tracks)
3108 {
3109     for (AVPlayerItemTrack *track in m_cachedTracks.get())
3110         [track removeObserver:m_objcObserver.get() forKeyPath:@"enabled"];
3111
3112     NSArray *assetTracks = [m_avAsset tracks];
3113
3114     m_cachedTracks = [tracks objectsAtIndexes:[tracks indexesOfObjectsPassingTest:^(id obj, NSUInteger, BOOL*) {
3115         AVAssetTrack* assetTrack = [obj assetTrack];
3116
3117         if ([assetTracks containsObject:assetTrack])
3118             return YES;
3119
3120         // Track is a streaming track. Omit if it belongs to a valid AVMediaSelectionGroup.
3121         if (!hasLoadedMediaSelectionGroups())
3122             return NO;
3123
3124         if ([assetTrack hasMediaCharacteristic:AVMediaCharacteristicAudible] && safeMediaSelectionGroupForAudibleMedia())
3125             return NO;
3126
3127         if ([assetTrack hasMediaCharacteristic:AVMediaCharacteristicVisual] && safeMediaSelectionGroupForVisualMedia())
3128             return NO;
3129
3130         if ([assetTrack hasMediaCharacteristic:AVMediaCharacteristicLegible] && safeMediaSelectionGroupForLegibleMedia())
3131             return NO;
3132
3133         return YES;
3134     }]];
3135
3136     for (AVPlayerItemTrack *track in m_cachedTracks.get())
3137         [track addObserver:m_objcObserver.get() forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:(void *)MediaPlayerAVFoundationObservationContextPlayerItemTrack];
3138
3139     m_cachedTotalBytes = 0;
3140
3141     tracksChanged();
3142     updateStates();
3143 }
3144
3145 void MediaPlayerPrivateAVFoundationObjC::hasEnabledAudioDidChange(bool hasEnabledAudio)
3146 {
3147     m_cachedHasEnabledAudio = hasEnabledAudio;
3148
3149     tracksChanged();
3150     updateStates();
3151 }
3152
3153 void MediaPlayerPrivateAVFoundationObjC::presentationSizeDidChange(FloatSize size)
3154 {
3155     m_cachedPresentationSize = size;
3156
3157     sizeChanged();
3158     updateStates();
3159 }
3160
3161 void MediaPlayerPrivateAVFoundationObjC::durationDidChange(const MediaTime& duration)
3162 {
3163     m_cachedDuration = duration;
3164
3165     invalidateCachedDuration();
3166 }
3167
3168 void MediaPlayerPrivateAVFoundationObjC::rateDidChange(double rate)
3169 {
3170     m_cachedRate = rate;
3171
3172     updateStates();
3173     rateChanged();
3174 }
3175
3176 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
3177
3178 void MediaPlayerPrivateAVFoundationObjC::playbackTargetIsWirelessDidChange()
3179 {
3180     playbackTargetIsWirelessChanged();
3181 }
3182
3183 #endif
3184
3185 void MediaPlayerPrivateAVFoundationObjC::canPlayFastForwardDidChange(bool newValue)
3186 {
3187     m_cachedCanPlayFastForward = newValue;
3188 }
3189
3190 void MediaPlayerPrivateAVFoundationObjC::canPlayFastReverseDidChange(bool newValue)
3191 {
3192     m_cachedCanPlayFastReverse = newValue;
3193 }
3194
3195 void MediaPlayerPrivateAVFoundationObjC::setShouldDisableSleep(bool flag)
3196 {
3197 #if PLATFORM(IOS) && !PLATFORM(IOS_SIMULATOR)
3198     [m_avPlayer _setPreventsSleepDuringVideoPlayback:flag];
3199 #else
3200     UNUSED_PARAM(flag);
3201 #endif
3202 }
3203
3204 NSArray* assetMetadataKeyNames()
3205 {
3206     static NSArray* keys = [[NSArray alloc] initWithObjects:
3207         @"duration",
3208         @"naturalSize",
3209         @"preferredTransform",
3210         @"preferredVolume",
3211         @"preferredRate",
3212         @"playable",
3213         @"resolvedURL",
3214         @"tracks",
3215         @"availableMediaCharacteristicsWithMediaSelectionOptions",
3216     nil];
3217     return keys;
3218 }
3219
3220 NSArray* itemKVOProperties()
3221 {
3222     static NSArray* keys = [[NSArray alloc] initWithObjects:
3223         @"presentationSize",
3224         @"status",
3225         @"asset",
3226         @"tracks",
3227         @"seekableTimeRanges",
3228         @"loadedTimeRanges",
3229         @"playbackLikelyToKeepUp",
3230         @"playbackBufferFull",
3231         @"playbackBufferEmpty",
3232         @"duration",
3233         @"hasEnabledAudio",
3234         @"timedMetadata",
3235         @"canPlayFastForward",
3236         @"canPlayFastReverse",
3237     nil];
3238     return keys;
3239 }
3240
3241 NSArray* assetTrackMetadataKeyNames()
3242 {
3243     static NSArray* keys = [[NSArray alloc] initWithObjects:@"totalSampleDataLength", @"mediaType", @"enabled", @"preferredTransform", @"naturalSize", nil];
3244     return keys;
3245 }
3246
3247 NSArray* playerKVOProperties()
3248 {
3249     static NSArray* keys = [[NSArray alloc] initWithObjects:
3250         @"rate",
3251 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
3252         @"externalPlaybackActive",
3253         @"allowsExternalPlayback",
3254 #endif
3255 #if ENABLE(LEGACY_ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA)
3256         @"outputObscuredDueToInsufficientExternalProtection",
3257 #endif
3258     nil];
3259     return keys;
3260 }
3261 } // namespace WebCore
3262
3263 @implementation WebCoreAVFMovieObserver
3264
3265 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC*)callback
3266 {
3267     self = [super init];
3268     if (!self)
3269         return nil;
3270     m_callback = callback;
3271     return self;
3272 }
3273
3274 - (void)disconnect
3275 {
3276     [NSObject cancelPreviousPerformRequestsWithTarget:self];
3277     m_callback = nil;
3278 }
3279
3280 - (void)metadataLoaded
3281 {
3282     if (!m_callback)
3283         return;
3284     m_callback->scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification::AssetMetadataLoaded);
3285 }
3286
3287 - (void)didEnd:(NSNotification *)unusedNotification
3288 {
3289     UNUSED_PARAM(unusedNotification);
3290     if (!m_callback)
3291         return;
3292     m_callback->scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification::ItemDidPlayToEndTime);
3293 }
3294
3295 - (void)observeValueForKeyPath:keyPath ofObject:(id)object change:(NSDictionary *)change context:(MediaPlayerAVFoundationObservationContext)context
3296 {
3297     UNUSED_PARAM(object);
3298     id newValue = [change valueForKey:NSKeyValueChangeNewKey];
3299
3300     if (!m_callback)
3301         return;
3302
3303     bool willChange = [[change valueForKey:NSKeyValueChangeNotificationIsPriorKey] boolValue];
3304     bool shouldLogValue = !willChange;
3305     WTF::Function<void ()> function;
3306
3307     if (context == MediaPlayerAVFoundationObservationContextAVPlayerLayer) {
3308         if ([keyPath isEqualToString:@"readyForDisplay"])
3309             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::firstFrameAvailableDidChange, m_callback, [newValue boolValue]);
3310     }
3311
3312     if (context == MediaPlayerAVFoundationObservationContextPlayerItemTrack) {
3313         if ([keyPath isEqualToString:@"enabled"])
3314             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::trackEnabledDidChange, m_callback, [newValue boolValue]);
3315     }
3316
3317     if (context == MediaPlayerAVFoundationObservationContextPlayerItem && willChange) {
3318         if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"])
3319             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::playbackLikelyToKeepUpWillChange, m_callback);
3320         else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
3321             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::playbackBufferEmptyWillChange, m_callback);
3322         else if ([keyPath isEqualToString:@"playbackBufferFull"])
3323             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::playbackBufferFullWillChange, m_callback);
3324     }
3325
3326     if (context == MediaPlayerAVFoundationObservationContextPlayerItem && !willChange) {
3327         // A value changed for an AVPlayerItem
3328         if ([keyPath isEqualToString:@"status"])
3329             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::playerItemStatusDidChange, m_callback, [newValue intValue]);
3330         else if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"])
3331             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::playbackLikelyToKeepUpDidChange, m_callback, [newValue boolValue]);
3332         else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
3333             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::playbackBufferEmptyDidChange, m_callback, [newValue boolValue]);
3334         else if ([keyPath isEqualToString:@"playbackBufferFull"])
3335             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::playbackBufferFullDidChange, m_callback, [newValue boolValue]);
3336         else if ([keyPath isEqualToString:@"asset"]) {
3337             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::setAsset, m_callback, RetainPtr<id>(newValue));
3338             shouldLogValue = false;
3339         } else if ([keyPath isEqualToString:@"loadedTimeRanges"])
3340             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::loadedTimeRangesDidChange, m_callback, RetainPtr<NSArray>(newValue));
3341         else if ([keyPath isEqualToString:@"seekableTimeRanges"])
3342             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::seekableTimeRangesDidChange, m_callback, RetainPtr<NSArray>(newValue));
3343         else if ([keyPath isEqualToString:@"tracks"]) {
3344             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::tracksDidChange, m_callback, RetainPtr<NSArray>(newValue));
3345             shouldLogValue = false;
3346         } else if ([keyPath isEqualToString:@"hasEnabledAudio"])
3347             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::hasEnabledAudioDidChange, m_callback, [newValue boolValue]);
3348         else if ([keyPath isEqualToString:@"presentationSize"])
3349             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::presentationSizeDidChange, m_callback, FloatSize([newValue sizeValue]));
3350         else if ([keyPath isEqualToString:@"duration"])
3351             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::durationDidChange, m_callback, PAL::toMediaTime([newValue CMTimeValue]));
3352         else if ([keyPath isEqualToString:@"timedMetadata"] && newValue) {
3353             MediaTime now;
3354             CMTime itemTime = [(AVPlayerItemType *)object currentTime];
3355             if (CMTIME_IS_NUMERIC(itemTime))
3356                 now = std::max(PAL::toMediaTime(itemTime), MediaTime::zeroTime());
3357             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::metadataDidArrive, m_callback, RetainPtr<NSArray>(newValue), now);
3358             shouldLogValue = false;
3359         } else if ([keyPath isEqualToString:@"canPlayFastReverse"])
3360             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::canPlayFastReverseDidChange, m_callback, [newValue boolValue]);
3361         else if ([keyPath isEqualToString:@"canPlayFastForward"])
3362             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::canPlayFastForwardDidChange, m_callback, [newValue boolValue]);
3363     }
3364
3365     if (context == MediaPlayerAVFoundationObservationContextPlayer && !willChange) {
3366         // A value changed for an AVPlayer.
3367         if ([keyPath isEqualToString:@"rate"])
3368             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::rateDidChange, m_callback, [newValue doubleValue]);
3369 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
3370         else if ([keyPath isEqualToString:@"externalPlaybackActive"] || [keyPath isEqualToString:@"allowsExternalPlayback"])
3371             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::playbackTargetIsWirelessDidChange, m_callback);
3372 #endif
3373 #if ENABLE(LEGACY_ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA)
3374         else if ([keyPath isEqualToString:@"outputObscuredDueToInsufficientExternalProtection"])
3375             function = std::bind(&MediaPlayerPrivateAVFoundationObjC::outputObscuredDueToInsufficientExternalProtectionChanged, m_callback, [newValue boolValue]);
3376 #endif
3377     }
3378
3379 #if !RELEASE_LOG_DISABLED
3380     if (m_callback->logger().willLog(m_callback->logChannel(), WTFLogLevelDebug) && !([keyPath isEqualToString:@"loadedTimeRanges"] || [keyPath isEqualToString:@"seekableTimeRanges"])) {
3381         auto identifier = Logger::LogSiteIdentifier("MediaPlayerPrivateAVFoundation", "observeValueForKeyPath", m_callback->logIdentifier());
3382
3383         if (shouldLogValue) {
3384             if ([keyPath isEqualToString:@"duration"])
3385                 m_callback->logger().debug(m_callback->logChannel(), identifier, "did change '", [keyPath UTF8String], "' to ", PAL::toMediaTime([newValue CMTimeValue]));
3386             else {
3387                 RetainPtr<NSString> valueString = adoptNS([[NSString alloc] initWithFormat:@"%@", newValue]);
3388                 m_callback->logger().debug(m_callback->logChannel(), identifier, "did change '", [keyPath UTF8String], "' to ", [valueString.get() UTF8String]);
3389             }
3390         } else
3391             m_callback->logger().debug(m_callback->logChannel(), identifier, willChange ? "will" : "did", " change '", [keyPath UTF8String], "'");
3392     }
3393 #endif
3394
3395     if (!function)
3396         return;
3397
3398     auto weakThis = m_callback->createWeakPtr();
3399     m_callback->scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification([weakThis, function = WTFMove(function)]{
3400         // weakThis and function both refer to the same MediaPlayerPrivateAVFoundationObjC instance. If the WeakPtr has
3401         // been cleared, the underlying object has been destroyed, and it is unsafe to call function().
3402         if (!weakThis)
3403             return;
3404         function();
3405     }));
3406 }
3407
3408 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
3409
3410 - (void)legibleOutput:(id)output didOutputAttributedStrings:(NSArray *)strings nativeSampleBuffers:(NSArray *)nativeSamples forItemTime:(CMTime)itemTime
3411 {
3412     UNUSED_PARAM(output);
3413     UNUSED_PARAM(nativeSamples);
3414
3415     if (!m_callback)
3416         return;
3417
3418     RetainPtr<WebCoreAVFMovieObserver> protectedSelf = self;
3419     RetainPtr<NSArray> protectedStrings = strings;
3420     RetainPtr<NSArray> protectedNativeSamples = nativeSamples;
3421     callOnMainThread([protectedSelf = WTFMove(protectedSelf), protectedStrings = WTFMove(protectedStrings), protectedNativeSamples = WTFMove(protectedNativeSamples), itemTime] {
3422         MediaPlayerPrivateAVFoundationObjC* callback = protectedSelf->m_callback;
3423         if (!callback)
3424             return;
3425         MediaTime time = std::max(PAL::toMediaTime(itemTime), MediaTime::zeroTime());
3426         callback->processCue(protectedStrings.get(), protectedNativeSamples.get(), time);
3427     });
3428 }
3429
3430 - (void)outputSequenceWasFlushed:(id)output
3431 {
3432     UNUSED_PARAM(output);
3433
3434     if (!m_callback)
3435         return;
3436     
3437     callOnMainThread([protectedSelf = RetainPtr<WebCoreAVFMovieObserver>(self)] {
3438         if (MediaPlayerPrivateAVFoundationObjC* callback = protectedSelf->m_callback)
3439             callback->flushCues();
3440     });
3441 }
3442
3443 #endif
3444
3445 @end
3446
3447 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
3448
3449 @implementation WebCoreAVFLoaderDelegate
3450
3451 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC*)callback
3452 {
3453     self = [super init];
3454     if (!self)
3455         return nil;
3456     m_callback = callback;
3457     return self;
3458 }
3459
3460 - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
3461 {
3462     UNUSED_PARAM(resourceLoader);
3463     if (!m_callback)
3464         return NO;
3465
3466     RetainPtr<WebCoreAVFLoaderDelegate> protectedSelf = self;
3467     RetainPtr<AVAssetResourceLoadingRequest> protectedLoadingRequest = loadingRequest;
3468     callOnMainThread([protectedSelf = WTFMove(protectedSelf), protectedLoadingRequest = WTFMove(protectedLoadingRequest)] {
3469         MediaPlayerPrivateAVFoundationObjC* callback = protectedSelf->m_callback;
3470         if (!callback) {
3471             [protectedLoadingRequest finishLoadingWithError:nil];
3472             return;
3473         }
3474
3475         if (!callback->shouldWaitForLoadingOfResource(protectedLoadingRequest.get()))
3476             [protectedLoadingRequest finishLoadingWithError:nil];
3477     });
3478
3479     return YES;
3480 }
3481
3482 - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForResponseToAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
3483 {
3484     UNUSED_PARAM(resourceLoader);
3485     UNUSED_PARAM(challenge);
3486     ASSERT_NOT_REACHED();
3487     return NO;
3488 }
3489
3490 - (void)resourceLoader:(AVAssetResourceLoader *)resourceLoader didCancelLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest
3491 {
3492     UNUSED_PARAM(resourceLoader);
3493     if (!m_callback)
3494         return;
3495
3496     RetainPtr<WebCoreAVFLoaderDelegate> protectedSelf = self;
3497     RetainPtr<AVAssetResourceLoadingRequest> protectedLoadingRequest = loadingRequest;
3498     callOnMainThread([protectedSelf = WTFMove(protectedSelf), protectedLoadingRequest = WTFMove(protectedLoadingRequest)] {
3499         MediaPlayerPrivateAVFoundationObjC* callback = protectedSelf->m_callback;
3500         if (callback)
3501             callback->didCancelLoadingRequest(protectedLoadingRequest.get());
3502     });
3503 }
3504
3505 - (void)setCallback:(MediaPlayerPrivateAVFoundationObjC*)callback
3506 {
3507     m_callback = callback;