96acc3c981d5caceddecf9ef28a9e8b355811d14
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / MediaPlayerPrivateAVFoundationObjC.mm
1 /*
2  * Copyright (C) 2011-2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "config.h"
27
28 #if ENABLE(VIDEO) && USE(AVFOUNDATION)
29 #import "MediaPlayerPrivateAVFoundationObjC.h"
30
31 #import "AVTrackPrivateAVFObjCImpl.h"
32 #import "AudioTrackPrivateAVFObjC.h"
33 #import "AuthenticationChallenge.h"
34 #import "BlockExceptions.h"
35 #import "CDMSessionAVFoundationObjC.h"
36 #import "ExceptionCodePlaceholder.h"
37 #import "FloatConversion.h"
38 #import "FloatConversion.h"
39 #import "FrameView.h"
40 #import "GraphicsContext.h"
41 #import "GraphicsContextCG.h"
42 #import "InbandMetadataTextTrackPrivateAVF.h"
43 #import "InbandTextTrackPrivateAVFObjC.h"
44 #import "InbandTextTrackPrivateLegacyAVFObjC.h"
45 #import "OutOfBandTextTrackPrivateAVF.h"
46 #import "URL.h"
47 #import "Logging.h"
48 #import "PlatformTimeRanges.h"
49 #import "SecurityOrigin.h"
50 #import "SerializedPlatformRepresentationMac.h"
51 #import "SoftLinking.h"
52 #import "TextTrackRepresentation.h"
53 #import "UUID.h"
54 #import "VideoTrackPrivateAVFObjC.h"
55 #import "WebCoreAVFResourceLoader.h"
56 #import "WebCoreSystemInterface.h"
57 #import <objc/runtime.h>
58 #import <runtime/DataView.h>
59 #import <runtime/JSCInlines.h>
60 #import <runtime/TypedArrayInlines.h>
61 #import <runtime/Uint16Array.h>
62 #import <runtime/Uint32Array.h>
63 #import <runtime/Uint8Array.h>
64 #import <wtf/CurrentTime.h>
65 #import <wtf/Functional.h>
66 #import <wtf/NeverDestroyed.h>
67 #import <wtf/text/CString.h>
68 #import <wtf/text/StringBuilder.h>
69
70 #if ENABLE(AVF_CAPTIONS)
71 #include "TextTrack.h"
72 #endif
73
74 #import <Foundation/NSGeometry.h>
75 #import <AVFoundation/AVFoundation.h>
76 #if PLATFORM(IOS)
77 #import <CoreImage/CoreImage.h>
78 #else
79 #import <QuartzCore/CoreImage.h>
80 #endif
81 #import <CoreMedia/CoreMedia.h>
82
83 #if USE(VIDEOTOOLBOX)
84 #import <CoreVideo/CoreVideo.h>
85 #import <VideoToolbox/VideoToolbox.h>
86 #endif
87
88 #if ENABLE(AVF_CAPTIONS)
89 // Note: This must be defined before our SOFT_LINK macros:
90 @class AVMediaSelectionOption;
91 @interface AVMediaSelectionOption (OutOfBandExtensions)
92 @property (nonatomic, readonly) NSString* outOfBandSource;
93 @property (nonatomic, readonly) NSString* outOfBandIdentifier;
94 @end
95 #endif
96
97 #if PLATFORM(IOS)
98 @class AVPlayerItem;
99 @interface AVPlayerItem (WebKitExtensions)
100 @property (nonatomic, copy) NSString* dataYouTubeID;
101 @end
102 #endif
103
104 typedef AVPlayerItem AVPlayerItemType;
105 typedef AVMetadataItem AVMetadataItemType;
106
107 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
108 SOFT_LINK_FRAMEWORK_OPTIONAL(CoreMedia)
109 SOFT_LINK_FRAMEWORK_OPTIONAL(CoreImage)
110 SOFT_LINK_FRAMEWORK_OPTIONAL(CoreVideo)
111
112 #if USE(VIDEOTOOLBOX)
113 SOFT_LINK_FRAMEWORK_OPTIONAL(VideoToolbox)
114 #endif
115
116 SOFT_LINK(CoreMedia, CMTimeCompare, int32_t, (CMTime time1, CMTime time2), (time1, time2))
117 SOFT_LINK(CoreMedia, CMTimeMakeWithSeconds, CMTime, (Float64 seconds, int32_t preferredTimeScale), (seconds, preferredTimeScale))
118 SOFT_LINK(CoreMedia, CMTimeGetSeconds, Float64, (CMTime time), (time))
119 SOFT_LINK(CoreMedia, CMTimeRangeGetEnd, CMTime, (CMTimeRange range), (range))
120
121 SOFT_LINK(CoreVideo, CVPixelBufferGetWidth, size_t, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
122 SOFT_LINK(CoreVideo, CVPixelBufferGetHeight, size_t, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
123 SOFT_LINK(CoreVideo, CVPixelBufferGetBaseAddress, void*, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
124 SOFT_LINK(CoreVideo, CVPixelBufferGetBytesPerRow, size_t, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
125 SOFT_LINK(CoreVideo, CVPixelBufferGetDataSize, size_t, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
126 SOFT_LINK(CoreVideo, CVPixelBufferGetPixelFormatType, OSType, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
127 SOFT_LINK(CoreVideo, CVPixelBufferLockBaseAddress, CVReturn, (CVPixelBufferRef pixelBuffer, CVOptionFlags lockFlags), (pixelBuffer, lockFlags))
128 SOFT_LINK(CoreVideo, CVPixelBufferUnlockBaseAddress, CVReturn, (CVPixelBufferRef pixelBuffer, CVOptionFlags lockFlags), (pixelBuffer, lockFlags))
129
130 #if USE(VIDEOTOOLBOX)
131 SOFT_LINK(VideoToolbox, VTPixelTransferSessionCreate, OSStatus, (CFAllocatorRef allocator, VTPixelTransferSessionRef *pixelTransferSessionOut), (allocator, pixelTransferSessionOut))
132 SOFT_LINK(VideoToolbox, VTPixelTransferSessionTransferImage, OSStatus, (VTPixelTransferSessionRef session, CVPixelBufferRef sourceBuffer, CVPixelBufferRef destinationBuffer), (session, sourceBuffer, destinationBuffer))
133 #endif
134
135 SOFT_LINK_CLASS(AVFoundation, AVPlayer)
136 SOFT_LINK_CLASS(AVFoundation, AVPlayerItem)
137 SOFT_LINK_CLASS(AVFoundation, AVPlayerItemVideoOutput)
138 SOFT_LINK_CLASS(AVFoundation, AVPlayerLayer)
139 SOFT_LINK_CLASS(AVFoundation, AVURLAsset)
140 SOFT_LINK_CLASS(AVFoundation, AVAssetImageGenerator)
141 SOFT_LINK_CLASS(AVFoundation, AVMetadataItem)
142
143 SOFT_LINK_CLASS(CoreImage, CIContext)
144 SOFT_LINK_CLASS(CoreImage, CIImage)
145
146 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicVisual, NSString *)
147 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicAudible, NSString *)
148 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeClosedCaption, NSString *)
149 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeVideo, NSString *)
150 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeAudio, NSString *)
151 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeMetadata, NSString *)
152 SOFT_LINK_POINTER(AVFoundation, AVPlayerItemDidPlayToEndTimeNotification, NSString *)
153 SOFT_LINK_POINTER(AVFoundation, AVURLAssetInheritURIQueryComponentFromReferencingURIKey, NSString *)
154 SOFT_LINK_POINTER(AVFoundation, AVAssetImageGeneratorApertureModeCleanAperture, NSString *)
155 SOFT_LINK_POINTER(AVFoundation, AVURLAssetReferenceRestrictionsKey, NSString *)
156 SOFT_LINK_POINTER(AVFoundation, AVLayerVideoGravityResizeAspect, NSString *)
157 SOFT_LINK_POINTER(AVFoundation, AVLayerVideoGravityResizeAspectFill, NSString *)
158 SOFT_LINK_POINTER(AVFoundation, AVLayerVideoGravityResize, NSString *)
159 SOFT_LINK_POINTER(CoreVideo, kCVPixelBufferPixelFormatTypeKey, NSString *)
160
161 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVURLAssetClientBundleIdentifierKey, NSString *)
162
163 SOFT_LINK_CONSTANT(CoreMedia, kCMTimeZero, CMTime)
164
165 #define AVPlayer getAVPlayerClass()
166 #define AVPlayerItem getAVPlayerItemClass()
167 #define AVPlayerLayer getAVPlayerLayerClass()
168 #define AVURLAsset getAVURLAssetClass()
169 #define AVAssetImageGenerator getAVAssetImageGeneratorClass()
170 #define AVMetadataItem getAVMetadataItemClass()
171
172 #define AVMediaCharacteristicVisual getAVMediaCharacteristicVisual()
173 #define AVMediaCharacteristicAudible getAVMediaCharacteristicAudible()
174 #define AVMediaTypeClosedCaption getAVMediaTypeClosedCaption()
175 #define AVMediaTypeVideo getAVMediaTypeVideo()
176 #define AVMediaTypeAudio getAVMediaTypeAudio()
177 #define AVMediaTypeMetadata getAVMediaTypeMetadata()
178 #define AVPlayerItemDidPlayToEndTimeNotification getAVPlayerItemDidPlayToEndTimeNotification()
179 #define AVURLAssetInheritURIQueryComponentFromReferencingURIKey getAVURLAssetInheritURIQueryComponentFromReferencingURIKey()
180 #define AVURLAssetClientBundleIdentifierKey getAVURLAssetClientBundleIdentifierKey()
181 #define AVAssetImageGeneratorApertureModeCleanAperture getAVAssetImageGeneratorApertureModeCleanAperture()
182 #define AVURLAssetReferenceRestrictionsKey getAVURLAssetReferenceRestrictionsKey()
183 #define AVLayerVideoGravityResizeAspect getAVLayerVideoGravityResizeAspect()
184 #define AVLayerVideoGravityResizeAspectFill getAVLayerVideoGravityResizeAspectFill()
185 #define AVLayerVideoGravityResize getAVLayerVideoGravityResize()
186 #define kCVPixelBufferPixelFormatTypeKey getkCVPixelBufferPixelFormatTypeKey()
187
188 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
189 typedef AVMediaSelectionGroup AVMediaSelectionGroupType;
190 typedef AVMediaSelectionOption AVMediaSelectionOptionType;
191
192 SOFT_LINK_CLASS(AVFoundation, AVPlayerItemLegibleOutput)
193 SOFT_LINK_CLASS(AVFoundation, AVMediaSelectionGroup)
194 SOFT_LINK_CLASS(AVFoundation, AVMediaSelectionOption)
195
196 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicLegible, NSString *)
197 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeSubtitle, NSString *)
198 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicContainsOnlyForcedSubtitles, NSString *)
199 SOFT_LINK_POINTER(AVFoundation, AVPlayerItemLegibleOutputTextStylingResolutionSourceAndRulesOnly, NSString *)
200
201 #define AVPlayerItemLegibleOutput getAVPlayerItemLegibleOutputClass()
202 #define AVMediaSelectionGroup getAVMediaSelectionGroupClass()
203 #define AVMediaSelectionOption getAVMediaSelectionOptionClass()
204 #define AVMediaCharacteristicLegible getAVMediaCharacteristicLegible()
205 #define AVMediaTypeSubtitle getAVMediaTypeSubtitle()
206 #define AVMediaCharacteristicContainsOnlyForcedSubtitles getAVMediaCharacteristicContainsOnlyForcedSubtitles()
207 #define AVPlayerItemLegibleOutputTextStylingResolutionSourceAndRulesOnly getAVPlayerItemLegibleOutputTextStylingResolutionSourceAndRulesOnly()
208 #endif
209
210 #if ENABLE(AVF_CAPTIONS)
211 SOFT_LINK_POINTER(AVFoundation, AVURLAssetOutOfBandAlternateTracksKey, NSString*)
212 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackDisplayNameKey, NSString*)
213 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackExtendedLanguageTagKey, NSString*)
214 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackIsDefaultKey, NSString*)
215 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackMediaCharactersticsKey, NSString*)
216 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackIdentifierKey, NSString*)
217 SOFT_LINK_POINTER(AVFoundation, AVOutOfBandAlternateTrackSourceKey, NSString*)
218 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicDescribesMusicAndSoundForAccessibility, NSString*)
219 SOFT_LINK_POINTER(AVFoundation, AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, NSString*)
220
221 #define AVURLAssetOutOfBandAlternateTracksKey getAVURLAssetOutOfBandAlternateTracksKey()
222 #define AVOutOfBandAlternateTrackDisplayNameKey getAVOutOfBandAlternateTrackDisplayNameKey()
223 #define AVOutOfBandAlternateTrackExtendedLanguageTagKey getAVOutOfBandAlternateTrackExtendedLanguageTagKey()
224 #define AVOutOfBandAlternateTrackIsDefaultKey getAVOutOfBandAlternateTrackIsDefaultKey()
225 #define AVOutOfBandAlternateTrackMediaCharactersticsKey getAVOutOfBandAlternateTrackMediaCharactersticsKey()
226 #define AVOutOfBandAlternateTrackIdentifierKey getAVOutOfBandAlternateTrackIdentifierKey()
227 #define AVOutOfBandAlternateTrackSourceKey getAVOutOfBandAlternateTrackSourceKey()
228 #define AVMediaCharacteristicDescribesMusicAndSoundForAccessibility getAVMediaCharacteristicDescribesMusicAndSoundForAccessibility()
229 #define AVMediaCharacteristicTranscribesSpokenDialogForAccessibility getAVMediaCharacteristicTranscribesSpokenDialogForAccessibility()
230 #endif
231
232 #if ENABLE(DATACUE_VALUE)
233 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceQuickTimeUserData, NSString*)
234 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceISOUserData, NSString*)
235 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceQuickTimeMetadata, NSString*)
236 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceiTunes, NSString*)
237 SOFT_LINK_POINTER(AVFoundation, AVMetadataKeySpaceID3, NSString*)
238
239 #define AVMetadataKeySpaceQuickTimeUserData getAVMetadataKeySpaceQuickTimeUserData()
240 #define AVMetadataKeySpaceISOUserData getAVMetadataKeySpaceISOUserData()
241 #define AVMetadataKeySpaceQuickTimeMetadata getAVMetadataKeySpaceQuickTimeMetadata()
242 #define AVMetadataKeySpaceiTunes getAVMetadataKeySpaceiTunes()
243 #define AVMetadataKeySpaceID3 getAVMetadataKeySpaceID3()
244 #endif
245
246 #define kCMTimeZero getkCMTimeZero()
247
248 using namespace WebCore;
249
250 enum MediaPlayerAVFoundationObservationContext {
251     MediaPlayerAVFoundationObservationContextPlayerItem,
252     MediaPlayerAVFoundationObservationContextPlayerItemTrack,
253     MediaPlayerAVFoundationObservationContextPlayer,
254     MediaPlayerAVFoundationObservationContextAVPlayerLayer,
255 };
256
257 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
258 @interface WebCoreAVFMovieObserver : NSObject <AVPlayerItemLegibleOutputPushDelegate>
259 #else
260 @interface WebCoreAVFMovieObserver : NSObject
261 #endif
262 {
263     MediaPlayerPrivateAVFoundationObjC* m_callback;
264     int m_delayCallbacks;
265 }
266 -(id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC*)callback;
267 -(void)disconnect;
268 -(void)metadataLoaded;
269 -(void)didEnd:(NSNotification *)notification;
270 -(void)observeValueForKeyPath:keyPath ofObject:(id)object change:(NSDictionary *)change context:(MediaPlayerAVFoundationObservationContext)context;
271 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
272 - (void)legibleOutput:(id)output didOutputAttributedStrings:(NSArray *)strings nativeSampleBuffers:(NSArray *)nativeSamples forItemTime:(CMTime)itemTime;
273 - (void)outputSequenceWasFlushed:(id)output;
274 #endif
275 @end
276
277 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
278 @interface WebCoreAVFLoaderDelegate : NSObject<AVAssetResourceLoaderDelegate> {
279     MediaPlayerPrivateAVFoundationObjC* m_callback;
280 }
281 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC*)callback;
282 - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest;
283 - (void)setCallback:(MediaPlayerPrivateAVFoundationObjC*)callback;
284 @end
285 #endif
286
287 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
288 @interface WebCoreAVFPullDelegate : NSObject<AVPlayerItemOutputPullDelegate> {
289     MediaPlayerPrivateAVFoundationObjC *m_callback;
290     dispatch_semaphore_t m_semaphore;
291 }
292 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC *)callback;
293 - (void)setCallback:(MediaPlayerPrivateAVFoundationObjC*)callback;
294 - (void)outputMediaDataWillChange:(AVPlayerItemOutput *)sender;
295 - (void)outputSequenceWasFlushed:(AVPlayerItemOutput *)output;
296 @end
297 #endif
298
299 namespace WebCore {
300
301 static NSArray *assetMetadataKeyNames();
302 static NSArray *itemKVOProperties();
303 static NSArray* assetTrackMetadataKeyNames();
304
305 #if !LOG_DISABLED
306 static const char *boolString(bool val)
307 {
308     return val ? "true" : "false";
309 }
310 #endif
311
312 #if ENABLE(ENCRYPTED_MEDIA_V2)
313 typedef HashMap<MediaPlayer*, MediaPlayerPrivateAVFoundationObjC*> PlayerToPrivateMapType;
314 static PlayerToPrivateMapType& playerToPrivateMap()
315 {
316     DEPRECATED_DEFINE_STATIC_LOCAL(PlayerToPrivateMapType, map, ());
317     return map;
318 };
319 #endif
320
321 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
322 static dispatch_queue_t globalLoaderDelegateQueue()
323 {
324     static dispatch_queue_t globalQueue;
325     static dispatch_once_t onceToken;
326     dispatch_once(&onceToken, ^{
327         globalQueue = dispatch_queue_create("WebCoreAVFLoaderDelegate queue", DISPATCH_QUEUE_SERIAL);
328     });
329     return globalQueue;
330 }
331 #endif
332
333 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
334 static dispatch_queue_t globalPullDelegateQueue()
335 {
336     static dispatch_queue_t globalQueue;
337     static dispatch_once_t onceToken;
338     dispatch_once(&onceToken, ^{
339         globalQueue = dispatch_queue_create("WebCoreAVFPullDelegate queue", DISPATCH_QUEUE_SERIAL);
340     });
341     return globalQueue;
342 }
343 #endif
344
345 PassOwnPtr<MediaPlayerPrivateInterface> MediaPlayerPrivateAVFoundationObjC::create(MediaPlayer* player)
346
347     return adoptPtr(new MediaPlayerPrivateAVFoundationObjC(player));
348 }
349
350 void MediaPlayerPrivateAVFoundationObjC::registerMediaEngine(MediaEngineRegistrar registrar)
351 {
352     if (isAvailable())
353         registrar(create, getSupportedTypes, supportsType, 0, 0, 0, supportsKeySystem);
354 }
355
356 MediaPlayerPrivateAVFoundationObjC::MediaPlayerPrivateAVFoundationObjC(MediaPlayer* player)
357     : MediaPlayerPrivateAVFoundation(player)
358     , m_weakPtrFactory(this)
359 #if PLATFORM(IOS)
360     , m_videoFullscreenGravity(MediaPlayer::VideoGravityResizeAspect)
361 #endif
362     , m_objcObserver(adoptNS([[WebCoreAVFMovieObserver alloc] initWithCallback:this]))
363     , m_videoFrameHasDrawn(false)
364     , m_haveCheckedPlayability(false)
365 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
366     , m_videoOutputDelegate(adoptNS([[WebCoreAVFPullDelegate alloc] initWithCallback:this]))
367     , m_videoOutputSemaphore(nullptr)
368 #endif
369 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
370     , m_loaderDelegate(adoptNS([[WebCoreAVFLoaderDelegate alloc] initWithCallback:this]))
371 #endif
372     , m_currentTextTrack(0)
373     , m_cachedDuration(MediaPlayer::invalidTime())
374     , m_cachedRate(0)
375     , m_cachedTotalBytes(0)
376     , m_pendingStatusChanges(0)
377     , m_cachedItemStatus(MediaPlayerAVPlayerItemStatusDoesNotExist)
378     , m_cachedLikelyToKeepUp(false)
379     , m_cachedBufferEmpty(false)
380     , m_cachedBufferFull(false)
381     , m_cachedHasEnabledAudio(false)
382     , m_shouldBufferData(true)
383     , m_cachedIsReadyForDisplay(false)
384 #if ENABLE(IOS_AIRPLAY)
385     , m_allowsWirelessVideoPlayback(true)
386 #endif
387 {
388 #if ENABLE(ENCRYPTED_MEDIA_V2)
389     playerToPrivateMap().set(player, this);
390 #endif
391 }
392
393 MediaPlayerPrivateAVFoundationObjC::~MediaPlayerPrivateAVFoundationObjC()
394 {
395 #if ENABLE(ENCRYPTED_MEDIA_V2)
396     playerToPrivateMap().remove(player());
397 #endif
398 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
399     [m_loaderDelegate.get() setCallback:0];
400     [[m_avAsset.get() resourceLoader] setDelegate:nil queue:0];
401
402     for (auto& pair : m_resourceLoaderMap)
403         pair.value->invalidate();
404 #endif
405 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
406     [m_videoOutputDelegate setCallback:0];
407     [m_videoOutput setDelegate:nil queue:0];
408     if (m_videoOutputSemaphore)
409         dispatch_release(m_videoOutputSemaphore);
410 #endif
411     cancelLoad();
412 }
413
414 void MediaPlayerPrivateAVFoundationObjC::cancelLoad()
415 {
416     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::cancelLoad(%p)", this);
417     tearDownVideoRendering();
418
419     [[NSNotificationCenter defaultCenter] removeObserver:m_objcObserver.get()];
420     [m_objcObserver.get() disconnect];
421
422     // Tell our observer to do nothing when our cancellation of pending loading calls its completion handler.
423     setIgnoreLoadStateChanges(true);
424     if (m_avAsset) {
425         [m_avAsset.get() cancelLoading];
426         m_avAsset = nil;
427     }
428
429     clearTextTracks();
430
431 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
432     if (m_legibleOutput) {
433         if (m_avPlayerItem)
434             [m_avPlayerItem.get() removeOutput:m_legibleOutput.get()];
435         m_legibleOutput = nil;
436     }
437 #endif
438
439     if (m_avPlayerItem) {
440         for (NSString *keyName in itemKVOProperties())
441             [m_avPlayerItem.get() removeObserver:m_objcObserver.get() forKeyPath:keyName];
442         
443         m_avPlayerItem = nil;
444     }
445     if (m_avPlayer) {
446         if (m_timeObserver)
447             [m_avPlayer.get() removeTimeObserver:m_timeObserver.get()];
448         m_timeObserver = nil;
449         [m_avPlayer.get() removeObserver:m_objcObserver.get() forKeyPath:@"rate"];
450 #if ENABLE(IOS_AIRPLAY)
451         [m_avPlayer.get() removeObserver:m_objcObserver.get() forKeyPath:@"externalPlaybackActive"];
452 #endif
453         m_avPlayer = nil;
454     }
455
456     // Reset cached properties
457     m_pendingStatusChanges = 0;
458     m_cachedItemStatus = MediaPlayerAVPlayerItemStatusDoesNotExist;
459     m_cachedSeekableRanges = nullptr;
460     m_cachedLoadedRanges = nullptr;
461     m_cachedHasEnabledAudio = false;
462     m_cachedPresentationSize = FloatSize();
463     m_cachedDuration = 0;
464
465     for (AVPlayerItemTrack *track in m_cachedTracks.get())
466         [track removeObserver:m_objcObserver.get() forKeyPath:@"enabled"];
467     m_cachedTracks = nullptr;
468
469     setIgnoreLoadStateChanges(false);
470 }
471
472 bool MediaPlayerPrivateAVFoundationObjC::hasLayerRenderer() const
473 {
474     return m_videoLayer;
475 }
476
477 bool MediaPlayerPrivateAVFoundationObjC::hasContextRenderer() const
478 {
479 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
480     if (m_videoOutput)
481         return true;
482 #endif
483     return m_imageGenerator;
484 }
485
486 void MediaPlayerPrivateAVFoundationObjC::createContextVideoRenderer()
487 {
488 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
489     createVideoOutput();
490 #else
491     createImageGenerator();
492 #endif
493 }
494
495 void MediaPlayerPrivateAVFoundationObjC::createImageGenerator()
496 {
497     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createImageGenerator(%p)", this);
498
499     if (!m_avAsset || m_imageGenerator)
500         return;
501
502     m_imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:m_avAsset.get()];
503
504     [m_imageGenerator.get() setApertureMode:AVAssetImageGeneratorApertureModeCleanAperture];
505     [m_imageGenerator.get() setAppliesPreferredTrackTransform:YES];
506     [m_imageGenerator.get() setRequestedTimeToleranceBefore:kCMTimeZero];
507     [m_imageGenerator.get() setRequestedTimeToleranceAfter:kCMTimeZero];
508
509     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createImageGenerator(%p) - returning %p", this, m_imageGenerator.get());
510 }
511
512 void MediaPlayerPrivateAVFoundationObjC::destroyContextVideoRenderer()
513 {
514 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
515     destroyVideoOutput();
516 #endif
517     destroyImageGenerator();
518 }
519
520 void MediaPlayerPrivateAVFoundationObjC::destroyImageGenerator()
521 {
522     if (!m_imageGenerator)
523         return;
524
525     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::destroyImageGenerator(%p) - destroying  %p", this, m_imageGenerator.get());
526
527     m_imageGenerator = 0;
528 }
529
530 void MediaPlayerPrivateAVFoundationObjC::createVideoLayer()
531 {
532     if (!m_avPlayer || m_videoLayer)
533         return;
534
535     auto weakThis = createWeakPtr();
536     callOnMainThread([this, weakThis] {
537         if (!weakThis)
538             return;
539
540         if (!m_avPlayer || m_videoLayer)
541             return;
542
543         m_videoLayer = adoptNS([[AVPlayerLayer alloc] init]);
544         [m_videoLayer.get() setPlayer:m_avPlayer.get()];
545         [m_videoLayer.get() setBackgroundColor:cachedCGColor(Color::black, ColorSpaceDeviceRGB)];
546 #ifndef NDEBUG
547         [m_videoLayer.get() setName:@"MediaPlayerPrivate AVPlayerLayer"];
548 #endif
549         [m_videoLayer.get() addObserver:m_objcObserver.get() forKeyPath:@"readyForDisplay" options:NSKeyValueObservingOptionNew context:(void *)MediaPlayerAVFoundationObservationContextAVPlayerLayer];
550         updateVideoLayerGravity();
551         LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createVideoLayer(%p) - returning %p", this, m_videoLayer.get());
552
553 #if PLATFORM(IOS)
554         if (m_videoFullscreenLayer) {
555             [m_videoLayer setFrame:CGRectMake(0, 0, m_videoFullscreenFrame.width(), m_videoFullscreenFrame.height())];
556             [m_videoFullscreenLayer insertSublayer:m_videoLayer.get() atIndex:0];
557         }
558 #endif
559         player()->mediaPlayerClient()->mediaPlayerRenderingModeChanged(player());
560     });
561 }
562
563 void MediaPlayerPrivateAVFoundationObjC::destroyVideoLayer()
564 {
565     if (!m_videoLayer)
566         return;
567
568     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::destroyVideoLayer(%p) - destroying %p", this, m_videoLayer.get());
569
570     [m_videoLayer.get() removeObserver:m_objcObserver.get() forKeyPath:@"readyForDisplay"];
571     [m_videoLayer.get() setPlayer:nil];
572
573 #if PLATFORM(IOS)
574     if (m_videoFullscreenLayer)
575         [m_videoLayer removeFromSuperlayer];
576 #endif
577
578     m_videoLayer = 0;
579 }
580
581 bool MediaPlayerPrivateAVFoundationObjC::hasAvailableVideoFrame() const
582 {
583     if (currentRenderingMode() == MediaRenderingToLayer)
584         return m_cachedIsReadyForDisplay;
585
586     return m_videoFrameHasDrawn;
587 }
588
589 #if ENABLE(AVF_CAPTIONS)
590 static const NSArray* mediaDescriptionForKind(PlatformTextTrack::TrackKind kind)
591 {
592     // FIXME: Match these to correct types:
593     if (kind == PlatformTextTrack::Caption)
594         return [NSArray arrayWithObjects: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, nil];
595
596     if (kind == PlatformTextTrack::Subtitle)
597         return [NSArray arrayWithObjects: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, nil];
598
599     if (kind == PlatformTextTrack::Description)
600         return [NSArray arrayWithObjects: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, AVMediaCharacteristicDescribesMusicAndSoundForAccessibility, nil];
601
602     if (kind == PlatformTextTrack::Forced)
603         return [NSArray arrayWithObjects: AVMediaCharacteristicContainsOnlyForcedSubtitles, nil];
604
605     return [NSArray arrayWithObjects: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility, nil];
606 }
607     
608 void MediaPlayerPrivateAVFoundationObjC::notifyTrackModeChanged()
609 {
610     trackModeChanged();
611 }
612     
613 void MediaPlayerPrivateAVFoundationObjC::synchronizeTextTrackState()
614 {
615     const Vector<RefPtr<PlatformTextTrack>>& outOfBandTrackSources = player()->outOfBandTrackSources();
616     
617     for (auto& textTrack : m_textTracks) {
618         if (textTrack->textTrackCategory() != InbandTextTrackPrivateAVF::OutOfBand)
619             continue;
620         
621         RefPtr<OutOfBandTextTrackPrivateAVF> trackPrivate = static_cast<OutOfBandTextTrackPrivateAVF*>(textTrack.get());
622         RetainPtr<AVMediaSelectionOptionType> currentOption = trackPrivate->mediaSelectionOption();
623         
624         for (auto& track : outOfBandTrackSources) {
625             RetainPtr<CFStringRef> uniqueID = String::number(track->uniqueId()).createCFString();
626             
627             if (![[currentOption.get() outOfBandIdentifier] isEqual: reinterpret_cast<const NSString*>(uniqueID.get())])
628                 continue;
629             
630             InbandTextTrackPrivate::Mode mode = InbandTextTrackPrivate::Hidden;
631             if (track->mode() == PlatformTextTrack::Hidden)
632                 mode = InbandTextTrackPrivate::Hidden;
633             else if (track->mode() == PlatformTextTrack::Disabled)
634                 mode = InbandTextTrackPrivate::Disabled;
635             else if (track->mode() == PlatformTextTrack::Showing)
636                 mode = InbandTextTrackPrivate::Showing;
637             
638             textTrack->setMode(mode);
639             break;
640         }
641     }
642 }
643 #endif
644
645 void MediaPlayerPrivateAVFoundationObjC::createAVAssetForURL(const String& url)
646 {
647     if (m_avAsset)
648         return;
649
650     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createAVAssetForURL(%p)", this);
651
652     setDelayCallbacks(true);
653
654     RetainPtr<NSMutableDictionary> options = adoptNS([[NSMutableDictionary alloc] init]);    
655
656     [options.get() setObject:[NSNumber numberWithInt:AVAssetReferenceRestrictionForbidRemoteReferenceToLocal | AVAssetReferenceRestrictionForbidLocalReferenceToRemote] forKey:AVURLAssetReferenceRestrictionsKey];
657
658     RetainPtr<NSMutableDictionary> headerFields = adoptNS([[NSMutableDictionary alloc] init]);
659
660     String referrer = player()->referrer();
661     if (!referrer.isEmpty())
662         [headerFields.get() setObject:referrer forKey:@"Referer"];
663
664     String userAgent = player()->userAgent();
665     if (!userAgent.isEmpty())
666         [headerFields.get() setObject:userAgent forKey:@"User-Agent"];
667
668     if ([headerFields.get() count])
669         [options.get() setObject:headerFields.get() forKey:@"AVURLAssetHTTPHeaderFieldsKey"];
670
671     if (player()->doesHaveAttribute("x-itunes-inherit-uri-query-component"))
672         [options.get() setObject: [NSNumber numberWithBool: TRUE] forKey: AVURLAssetInheritURIQueryComponentFromReferencingURIKey];
673
674     String identifier = player()->sourceApplicationIdentifier();
675     if (!identifier.isEmpty() && AVURLAssetClientBundleIdentifierKey)
676         [options setObject:identifier forKey:AVURLAssetClientBundleIdentifierKey];
677
678 #if ENABLE(AVF_CAPTIONS)
679     const Vector<RefPtr<PlatformTextTrack>>& outOfBandTrackSources = player()->outOfBandTrackSources();
680     if (!outOfBandTrackSources.isEmpty()) {
681         NSMutableArray* outOfBandTracks = [[NSMutableArray alloc] init];
682         for (auto& trackSource : outOfBandTrackSources) {
683             RetainPtr<CFStringRef> label = trackSource->label().createCFString();
684             RetainPtr<CFStringRef> language = trackSource->language().createCFString();
685             RetainPtr<CFStringRef> uniqueID = String::number(trackSource->uniqueId()).createCFString();
686             RetainPtr<CFStringRef> url = trackSource->url().createCFString();
687             [outOfBandTracks addObject:
688                 [NSDictionary dictionaryWithObjectsAndKeys:
689                     reinterpret_cast<const NSString*>(label.get()), AVOutOfBandAlternateTrackDisplayNameKey,
690                     reinterpret_cast<const NSString*>(language.get()), AVOutOfBandAlternateTrackExtendedLanguageTagKey,
691                     [NSNumber numberWithBool: (trackSource->isDefault() ? YES : NO)], AVOutOfBandAlternateTrackIsDefaultKey,
692                     reinterpret_cast<const NSString*>(uniqueID.get()), AVOutOfBandAlternateTrackIdentifierKey,
693                     reinterpret_cast<const NSString*>(url.get()), AVOutOfBandAlternateTrackSourceKey,
694                     mediaDescriptionForKind(trackSource->kind()), AVOutOfBandAlternateTrackMediaCharactersticsKey,
695                     nil]];
696         }
697
698         [options.get() setObject: outOfBandTracks forKey: AVURLAssetOutOfBandAlternateTracksKey];
699     }
700 #endif
701     
702     NSURL *cocoaURL = URL(ParsedURLString, url);
703     m_avAsset = adoptNS([[AVURLAsset alloc] initWithURL:cocoaURL options:options.get()]);
704
705 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
706     [[m_avAsset.get() resourceLoader] setDelegate:m_loaderDelegate.get() queue:globalLoaderDelegateQueue()];
707 #endif
708
709     m_haveCheckedPlayability = false;
710
711     setDelayCallbacks(false);
712 }
713
714 void MediaPlayerPrivateAVFoundationObjC::createAVPlayer()
715 {
716     if (m_avPlayer)
717         return;
718
719     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createAVPlayer(%p)", this);
720
721     setDelayCallbacks(true);
722
723     m_avPlayer = adoptNS([[AVPlayer alloc] init]);
724     [m_avPlayer.get() addObserver:m_objcObserver.get() forKeyPath:@"rate" options:NSKeyValueObservingOptionNew context:(void *)MediaPlayerAVFoundationObservationContextPlayer];
725 #if ENABLE(IOS_AIRPLAY)
726     [m_avPlayer.get() addObserver:m_objcObserver.get() forKeyPath:@"externalPlaybackActive" options:NSKeyValueObservingOptionNew context:(void *)MediaPlayerAVFoundationObservationContextPlayer];
727     [m_avPlayer.get() setUsesExternalPlaybackWhileExternalScreenIsActive:YES];
728 #endif
729
730 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
731     [m_avPlayer.get() setAppliesMediaSelectionCriteriaAutomatically:YES];
732 #endif
733
734 #if ENABLE(IOS_AIRPLAY)
735     [m_avPlayer.get() setAllowsExternalPlayback:m_allowsWirelessVideoPlayback];
736 #endif
737
738     if (m_avPlayerItem)
739         [m_avPlayer.get() replaceCurrentItemWithPlayerItem:m_avPlayerItem.get()];
740
741     setDelayCallbacks(false);
742 }
743
744 void MediaPlayerPrivateAVFoundationObjC::createAVPlayerItem()
745 {
746     if (m_avPlayerItem)
747         return;
748
749     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createAVPlayerItem(%p)", this);
750
751     setDelayCallbacks(true);
752
753     // Create the player item so we can load media data. 
754     m_avPlayerItem = adoptNS([[AVPlayerItem alloc] initWithAsset:m_avAsset.get()]);
755
756     [[NSNotificationCenter defaultCenter] addObserver:m_objcObserver.get() selector:@selector(didEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:m_avPlayerItem.get()];
757
758     NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionPrior;
759     for (NSString *keyName in itemKVOProperties())
760         [m_avPlayerItem.get() addObserver:m_objcObserver.get() forKeyPath:keyName options:options context:(void *)MediaPlayerAVFoundationObservationContextPlayerItem];
761
762     if (m_avPlayer)
763         [m_avPlayer.get() replaceCurrentItemWithPlayerItem:m_avPlayerItem.get()];
764
765 #if PLATFORM(IOS)
766     AtomicString value;
767     if (player()->doesHaveAttribute("data-youtube-id", &value))
768         [m_avPlayerItem.get() setDataYouTubeID: value];
769  #endif
770
771 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
772     const NSTimeInterval legibleOutputAdvanceInterval = 2;
773
774     m_legibleOutput = adoptNS([[AVPlayerItemLegibleOutput alloc] initWithMediaSubtypesForNativeRepresentation:[NSArray array]]);
775     [m_legibleOutput.get() setSuppressesPlayerRendering:YES];
776
777     [m_legibleOutput.get() setDelegate:m_objcObserver.get() queue:dispatch_get_main_queue()];
778     [m_legibleOutput.get() setAdvanceIntervalForDelegateInvocation:legibleOutputAdvanceInterval];
779     [m_legibleOutput.get() setTextStylingResolution:AVPlayerItemLegibleOutputTextStylingResolutionSourceAndRulesOnly];
780     [m_avPlayerItem.get() addOutput:m_legibleOutput.get()];
781 #endif
782
783     setDelayCallbacks(false);
784 }
785
786 void MediaPlayerPrivateAVFoundationObjC::checkPlayability()
787 {
788     if (m_haveCheckedPlayability)
789         return;
790     m_haveCheckedPlayability = true;
791
792     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::checkPlayability(%p)", this);
793     auto weakThis = createWeakPtr();
794
795     [m_avAsset.get() loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"playable"] completionHandler:^{
796         callOnMainThread([weakThis] {
797             if (weakThis)
798                 weakThis->scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification::AssetPlayabilityKnown);
799         });
800     }];
801 }
802
803 void MediaPlayerPrivateAVFoundationObjC::beginLoadingMetadata()
804 {
805     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::beginLoadingMetadata(%p) - requesting metadata loading", this);
806
807     dispatch_group_t metadataLoadingGroup = dispatch_group_create();
808     dispatch_group_enter(metadataLoadingGroup);
809     auto weakThis = createWeakPtr();
810     [m_avAsset.get() loadValuesAsynchronouslyForKeys:assetMetadataKeyNames() completionHandler:^{
811
812         callOnMainThread([weakThis, metadataLoadingGroup] {
813             if (weakThis && [weakThis->m_avAsset.get() statusOfValueForKey:@"tracks" error:nil] == AVKeyValueStatusLoaded) {
814                 for (AVAssetTrack *track in [weakThis->m_avAsset.get() tracks]) {
815                     dispatch_group_enter(metadataLoadingGroup);
816                     [track loadValuesAsynchronouslyForKeys:assetTrackMetadataKeyNames() completionHandler:^{
817                         dispatch_group_leave(metadataLoadingGroup);
818                     }];
819                 }
820             }
821             dispatch_group_leave(metadataLoadingGroup);
822         });
823     }];
824
825     dispatch_group_notify(metadataLoadingGroup, dispatch_get_main_queue(), ^{
826         callOnMainThread([weakThis] {
827             if (weakThis)
828                 [weakThis->m_objcObserver.get() metadataLoaded];
829         });
830
831         dispatch_release(metadataLoadingGroup);
832     });
833 }
834
835 MediaPlayerPrivateAVFoundation::ItemStatus MediaPlayerPrivateAVFoundationObjC::playerItemStatus() const
836 {
837     if (!m_avPlayerItem)
838         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusDoesNotExist;
839
840     if (m_cachedItemStatus == AVPlayerItemStatusUnknown)
841         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusUnknown;
842     if (m_cachedItemStatus == AVPlayerItemStatusFailed)
843         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusFailed;
844     if (m_cachedLikelyToKeepUp)
845         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusPlaybackLikelyToKeepUp;
846     if (m_cachedBufferFull)
847         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusPlaybackBufferFull;
848     if (m_cachedBufferEmpty)
849         return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusPlaybackBufferEmpty;
850
851     return MediaPlayerPrivateAVFoundation::MediaPlayerAVPlayerItemStatusReadyToPlay;
852 }
853
854 PlatformMedia MediaPlayerPrivateAVFoundationObjC::platformMedia() const
855 {
856     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::platformMedia(%p)", this);
857     PlatformMedia pm;
858     pm.type = PlatformMedia::AVFoundationMediaPlayerType;
859     pm.media.avfMediaPlayer = m_avPlayer.get();
860     return pm;
861 }
862
863 PlatformLayer* MediaPlayerPrivateAVFoundationObjC::platformLayer() const
864 {
865     return m_videoLayer.get();
866 }
867
868 #if PLATFORM(IOS)
869 void MediaPlayerPrivateAVFoundationObjC::setVideoFullscreenLayer(PlatformLayer* videoFullscreenLayer)
870 {
871     if (m_videoFullscreenLayer == videoFullscreenLayer)
872         return;
873
874     if (m_videoFullscreenLayer)
875        [m_videoLayer removeFromSuperlayer];
876
877     m_videoFullscreenLayer = videoFullscreenLayer;
878
879     if (m_videoFullscreenLayer && m_videoLayer) {
880         CGRect frame = CGRectMake(0, 0, m_videoFullscreenFrame.width(), m_videoFullscreenFrame.height());
881         [m_videoLayer setFrame:frame];
882         [m_videoFullscreenLayer insertSublayer:m_videoLayer.get() atIndex:0];
883     }
884
885     if (m_videoFullscreenLayer && m_textTrackRepresentationLayer) {
886         syncTextTrackBounds();
887         [m_videoFullscreenLayer addSublayer:m_textTrackRepresentationLayer.get()];
888     }
889 }
890
891 void MediaPlayerPrivateAVFoundationObjC::setVideoFullscreenFrame(FloatRect frame)
892 {
893     m_videoFullscreenFrame = frame;
894     if (!m_videoFullscreenLayer)
895         return;
896
897     if (m_videoLayer)
898         [m_videoLayer setFrame:CGRectMake(0, 0, frame.width(), frame.height())];
899
900     syncTextTrackBounds();
901 }
902
903 void MediaPlayerPrivateAVFoundationObjC::setVideoFullscreenGravity(MediaPlayer::VideoGravity gravity)
904 {
905     m_videoFullscreenGravity = gravity;
906     if (!m_videoLayer)
907         return;
908
909     NSString *videoGravity = AVLayerVideoGravityResizeAspect;
910     if (gravity == MediaPlayer::VideoGravityResize)
911         videoGravity = AVLayerVideoGravityResize;
912     else if (gravity == MediaPlayer::VideoGravityResizeAspect)
913         videoGravity = AVLayerVideoGravityResizeAspect;
914     else if (gravity == MediaPlayer::VideoGravityResizeAspectFill)
915         videoGravity = AVLayerVideoGravityResizeAspectFill;
916     else
917         ASSERT_NOT_REACHED();
918
919     [m_videoLayer setVideoGravity:videoGravity];
920 }
921
922 NSArray *MediaPlayerPrivateAVFoundationObjC::timedMetadata() const
923 {
924     if (m_currentMetaData)
925         return m_currentMetaData.get();
926     return nil;
927 }
928
929 String MediaPlayerPrivateAVFoundationObjC::accessLog() const
930 {
931     if (!m_avPlayerItem)
932         return emptyString();
933     
934     AVPlayerItemAccessLog *log = [m_avPlayerItem.get() accessLog];
935     RetainPtr<NSString> logString = adoptNS([[NSString alloc] initWithData:[log extendedLogData] encoding:[log extendedLogDataStringEncoding]]);
936
937     return logString.get();
938 }
939
940 String MediaPlayerPrivateAVFoundationObjC::errorLog() const
941 {
942     if (!m_avPlayerItem)
943         return emptyString();
944
945     AVPlayerItemErrorLog *log = [m_avPlayerItem.get() errorLog];
946     RetainPtr<NSString> logString = adoptNS([[NSString alloc] initWithData:[log extendedLogData] encoding:[log extendedLogDataStringEncoding]]);
947
948     return logString.get();
949 }
950 #endif
951
952 void MediaPlayerPrivateAVFoundationObjC::platformSetVisible(bool isVisible)
953 {
954     [CATransaction begin];
955     [CATransaction setDisableActions:YES];    
956     if (m_videoLayer)
957         [m_videoLayer.get() setHidden:!isVisible];
958     [CATransaction commit];
959 }
960     
961 void MediaPlayerPrivateAVFoundationObjC::platformPlay()
962 {
963     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::platformPlay(%p)", this);
964     if (!metaDataAvailable())
965         return;
966
967     setDelayCallbacks(true);
968     m_cachedRate = requestedRate();
969     [m_avPlayer.get() setRate:requestedRate()];
970     setDelayCallbacks(false);
971 }
972
973 void MediaPlayerPrivateAVFoundationObjC::platformPause()
974 {
975     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::platformPause(%p)", this);
976     if (!metaDataAvailable())
977         return;
978
979     setDelayCallbacks(true);
980     m_cachedRate = 0;
981     [m_avPlayer.get() setRate:0];
982     setDelayCallbacks(false);
983 }
984
985 float MediaPlayerPrivateAVFoundationObjC::platformDuration() const
986 {
987     // Do not ask the asset for duration before it has been loaded or it will fetch the
988     // answer synchronously.
989     if (!m_avAsset || assetStatus() < MediaPlayerAVAssetStatusLoaded)
990          return MediaPlayer::invalidTime();
991     
992     CMTime cmDuration;
993     
994     // Check the AVItem if we have one and it has loaded duration, some assets never report duration.
995     if (m_avPlayerItem && playerItemStatus() >= MediaPlayerAVPlayerItemStatusReadyToPlay)
996         cmDuration = [m_avPlayerItem.get() duration];
997     else
998         cmDuration= [m_avAsset.get() duration];
999
1000     if (CMTIME_IS_NUMERIC(cmDuration))
1001         return narrowPrecisionToFloat(CMTimeGetSeconds(cmDuration));
1002
1003     if (CMTIME_IS_INDEFINITE(cmDuration)) {
1004         return std::numeric_limits<float>::infinity();
1005     }
1006
1007     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::platformDuration(%p) - invalid duration, returning %.0f", this, MediaPlayer::invalidTime());
1008     return MediaPlayer::invalidTime();
1009 }
1010
1011 float MediaPlayerPrivateAVFoundationObjC::currentTime() const
1012 {
1013     if (!metaDataAvailable() || !m_avPlayerItem)
1014         return 0;
1015
1016     CMTime itemTime = [m_avPlayerItem.get() currentTime];
1017     if (CMTIME_IS_NUMERIC(itemTime))
1018         return std::max(narrowPrecisionToFloat(CMTimeGetSeconds(itemTime)), 0.0f);
1019
1020     return 0;
1021 }
1022
1023 void MediaPlayerPrivateAVFoundationObjC::seekToTime(double time, double negativeTolerance, double positiveTolerance)
1024 {
1025     // setCurrentTime generates several event callbacks, update afterwards.
1026     setDelayCallbacks(true);
1027
1028     if (m_metadataTrack)
1029         m_metadataTrack->flushPartialCues();
1030
1031     CMTime cmTime = CMTimeMakeWithSeconds(time, 600);
1032     CMTime cmBefore = CMTimeMakeWithSeconds(negativeTolerance, 600);
1033     CMTime cmAfter = CMTimeMakeWithSeconds(positiveTolerance, 600);
1034
1035     auto weakThis = createWeakPtr();
1036
1037     [m_avPlayerItem.get() seekToTime:cmTime toleranceBefore:cmBefore toleranceAfter:cmAfter completionHandler:^(BOOL finished) {
1038         callOnMainThread([weakThis, finished] {
1039             auto _this = weakThis.get();
1040             if (!_this)
1041                 return;
1042
1043             _this->seekCompleted(finished);
1044         });
1045     }];
1046
1047     setDelayCallbacks(false);
1048 }
1049
1050 void MediaPlayerPrivateAVFoundationObjC::setVolume(float volume)
1051 {
1052     if (!metaDataAvailable())
1053         return;
1054
1055     [m_avPlayer.get() setVolume:volume];
1056 }
1057
1058 void MediaPlayerPrivateAVFoundationObjC::setClosedCaptionsVisible(bool closedCaptionsVisible)
1059 {
1060     UNUSED_PARAM(closedCaptionsVisible);
1061
1062     if (!metaDataAvailable())
1063         return;
1064
1065     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::setClosedCaptionsVisible(%p) - set to %s", this, boolString(closedCaptionsVisible));
1066 }
1067
1068 void MediaPlayerPrivateAVFoundationObjC::updateRate()
1069 {
1070     setDelayCallbacks(true);
1071     m_cachedRate = requestedRate();
1072     [m_avPlayer.get() setRate:requestedRate()];
1073     setDelayCallbacks(false);
1074 }
1075
1076 float MediaPlayerPrivateAVFoundationObjC::rate() const
1077 {
1078     if (!metaDataAvailable())
1079         return 0;
1080
1081     return m_cachedRate;
1082 }
1083
1084 std::unique_ptr<PlatformTimeRanges> MediaPlayerPrivateAVFoundationObjC::platformBufferedTimeRanges() const
1085 {
1086     auto timeRanges = PlatformTimeRanges::create();
1087
1088     if (!m_avPlayerItem)
1089         return timeRanges;
1090
1091     for (NSValue *thisRangeValue in m_cachedLoadedRanges.get()) {
1092         CMTimeRange timeRange = [thisRangeValue CMTimeRangeValue];
1093         if (CMTIMERANGE_IS_VALID(timeRange) && !CMTIMERANGE_IS_EMPTY(timeRange)) {
1094             float rangeStart = narrowPrecisionToFloat(CMTimeGetSeconds(timeRange.start));
1095             float rangeEnd = narrowPrecisionToFloat(CMTimeGetSeconds(CMTimeRangeGetEnd(timeRange)));
1096             timeRanges->add(rangeStart, rangeEnd);
1097         }
1098     }
1099     return timeRanges;
1100 }
1101
1102 double MediaPlayerPrivateAVFoundationObjC::platformMinTimeSeekable() const
1103 {
1104     if (!m_cachedSeekableRanges || ![m_cachedSeekableRanges count])
1105         return 0;
1106
1107     double minTimeSeekable = std::numeric_limits<double>::infinity();
1108     bool hasValidRange = false;
1109     for (NSValue *thisRangeValue in m_cachedSeekableRanges.get()) {
1110         CMTimeRange timeRange = [thisRangeValue CMTimeRangeValue];
1111         if (!CMTIMERANGE_IS_VALID(timeRange) || CMTIMERANGE_IS_EMPTY(timeRange))
1112             continue;
1113
1114         hasValidRange = true;
1115         double startOfRange = CMTimeGetSeconds(timeRange.start);
1116         if (minTimeSeekable > startOfRange)
1117             minTimeSeekable = startOfRange;
1118     }
1119     return hasValidRange ? minTimeSeekable : 0;
1120 }
1121
1122 double MediaPlayerPrivateAVFoundationObjC::platformMaxTimeSeekable() const
1123 {
1124     if (!m_cachedSeekableRanges)
1125         m_cachedSeekableRanges = [m_avPlayerItem seekableTimeRanges];
1126
1127     double maxTimeSeekable = 0;
1128     for (NSValue *thisRangeValue in m_cachedSeekableRanges.get()) {
1129         CMTimeRange timeRange = [thisRangeValue CMTimeRangeValue];
1130         if (!CMTIMERANGE_IS_VALID(timeRange) || CMTIMERANGE_IS_EMPTY(timeRange))
1131             continue;
1132         
1133         double endOfRange = CMTimeGetSeconds(CMTimeRangeGetEnd(timeRange));
1134         if (maxTimeSeekable < endOfRange)
1135             maxTimeSeekable = endOfRange;
1136     }
1137     return maxTimeSeekable;
1138 }
1139
1140 float MediaPlayerPrivateAVFoundationObjC::platformMaxTimeLoaded() const
1141 {
1142 #if !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 1080
1143     // AVFoundation on Mountain Lion will occasionally not send a KVO notification
1144     // when loadedTimeRanges changes when there is no video output. In that case
1145     // update the cached value explicitly.
1146     if (!hasLayerRenderer() && !hasContextRenderer())
1147         m_cachedLoadedRanges = [m_avPlayerItem loadedTimeRanges];
1148 #endif
1149
1150     if (!m_cachedLoadedRanges)
1151         return 0;
1152
1153     float maxTimeLoaded = 0;
1154     for (NSValue *thisRangeValue in m_cachedLoadedRanges.get()) {
1155         CMTimeRange timeRange = [thisRangeValue CMTimeRangeValue];
1156         if (!CMTIMERANGE_IS_VALID(timeRange) || CMTIMERANGE_IS_EMPTY(timeRange))
1157             continue;
1158         
1159         float endOfRange = narrowPrecisionToFloat(CMTimeGetSeconds(CMTimeRangeGetEnd(timeRange)));
1160         if (maxTimeLoaded < endOfRange)
1161             maxTimeLoaded = endOfRange;
1162     }
1163
1164     return maxTimeLoaded;   
1165 }
1166
1167 unsigned long long MediaPlayerPrivateAVFoundationObjC::totalBytes() const
1168 {
1169     if (!metaDataAvailable())
1170         return 0;
1171
1172     if (m_cachedTotalBytes)
1173         return m_cachedTotalBytes;
1174
1175     for (AVPlayerItemTrack *thisTrack in m_cachedTracks.get())
1176         m_cachedTotalBytes += [[thisTrack assetTrack] totalSampleDataLength];
1177
1178     return m_cachedTotalBytes;
1179 }
1180
1181 void MediaPlayerPrivateAVFoundationObjC::setAsset(id asset)
1182 {
1183     m_avAsset = asset;
1184 }
1185
1186 MediaPlayerPrivateAVFoundation::AssetStatus MediaPlayerPrivateAVFoundationObjC::assetStatus() const
1187 {
1188     if (!m_avAsset)
1189         return MediaPlayerAVAssetStatusDoesNotExist;
1190
1191     for (NSString *keyName in assetMetadataKeyNames()) {
1192         AVKeyValueStatus keyStatus = [m_avAsset.get() statusOfValueForKey:keyName error:nil];
1193
1194         if (keyStatus < AVKeyValueStatusLoaded)
1195             return MediaPlayerAVAssetStatusLoading;// At least one key is not loaded yet.
1196         
1197         if (keyStatus == AVKeyValueStatusFailed)
1198             return MediaPlayerAVAssetStatusFailed; // At least one key could not be loaded.
1199
1200         if (keyStatus == AVKeyValueStatusCancelled)
1201             return MediaPlayerAVAssetStatusCancelled; // Loading of at least one key was cancelled.
1202     }
1203
1204     if ([[m_avAsset.get() valueForKey:@"playable"] boolValue])
1205         return MediaPlayerAVAssetStatusPlayable;
1206
1207     return MediaPlayerAVAssetStatusLoaded;
1208 }
1209
1210 void MediaPlayerPrivateAVFoundationObjC::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& rect)
1211 {
1212     if (!metaDataAvailable() || context->paintingDisabled())
1213         return;
1214
1215     setDelayCallbacks(true);
1216     BEGIN_BLOCK_OBJC_EXCEPTIONS;
1217
1218 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
1219     if (videoOutputHasAvailableFrame())
1220         paintWithVideoOutput(context, rect);
1221     else
1222 #endif
1223         paintWithImageGenerator(context, rect);
1224
1225     END_BLOCK_OBJC_EXCEPTIONS;
1226     setDelayCallbacks(false);
1227
1228     m_videoFrameHasDrawn = true;
1229 }
1230
1231 void MediaPlayerPrivateAVFoundationObjC::paint(GraphicsContext* context, const IntRect& rect)
1232 {
1233     if (!metaDataAvailable() || context->paintingDisabled())
1234         return;
1235
1236     // We can ignore the request if we are already rendering to a layer.
1237     if (currentRenderingMode() == MediaRenderingToLayer)
1238         return;
1239
1240     paintCurrentFrameInContext(context, rect);
1241 }
1242
1243 void MediaPlayerPrivateAVFoundationObjC::paintWithImageGenerator(GraphicsContext* context, const IntRect& rect)
1244 {
1245     RetainPtr<CGImageRef> image = createImageForTimeInRect(currentTime(), rect);
1246     if (image) {
1247         GraphicsContextStateSaver stateSaver(*context);
1248         context->translate(rect.x(), rect.y() + rect.height());
1249         context->scale(FloatSize(1.0f, -1.0f));
1250         context->setImageInterpolationQuality(InterpolationLow);
1251         IntRect paintRect(IntPoint(0, 0), IntSize(rect.width(), rect.height()));
1252         CGContextDrawImage(context->platformContext(), CGRectMake(0, 0, paintRect.width(), paintRect.height()), image.get());
1253         image = 0;
1254     }
1255 }
1256
1257 static HashSet<String> mimeTypeCache()
1258 {
1259     DEPRECATED_DEFINE_STATIC_LOCAL(HashSet<String>, cache, ());
1260     static bool typeListInitialized = false;
1261
1262     if (typeListInitialized)
1263         return cache;
1264     typeListInitialized = true;
1265
1266     NSArray *types = [AVURLAsset audiovisualMIMETypes];
1267     for (NSString *mimeType in types)
1268         cache.add(mimeType);
1269
1270     return cache;
1271
1272
1273 RetainPtr<CGImageRef> MediaPlayerPrivateAVFoundationObjC::createImageForTimeInRect(float time, const IntRect& rect)
1274 {
1275     if (!m_imageGenerator)
1276         createImageGenerator();
1277     ASSERT(m_imageGenerator);
1278
1279 #if !LOG_DISABLED
1280     double start = monotonicallyIncreasingTime();
1281 #endif
1282
1283     [m_imageGenerator.get() setMaximumSize:CGSize(rect.size())];
1284     RetainPtr<CGImageRef> rawImage = adoptCF([m_imageGenerator.get() copyCGImageAtTime:CMTimeMakeWithSeconds(time, 600) actualTime:nil error:nil]);
1285     RetainPtr<CGImageRef> image = adoptCF(CGImageCreateCopyWithColorSpace(rawImage.get(), deviceRGBColorSpaceRef()));
1286
1287 #if !LOG_DISABLED
1288     double duration = monotonicallyIncreasingTime() - start;
1289     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createImageForTimeInRect(%p) - creating image took %.4f", this, narrowPrecisionToFloat(duration));
1290 #endif
1291
1292     return image;
1293 }
1294
1295 void MediaPlayerPrivateAVFoundationObjC::getSupportedTypes(HashSet<String>& supportedTypes)
1296 {
1297     supportedTypes = mimeTypeCache();
1298
1299
1300 #if ENABLE(ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA_V2)
1301 static bool keySystemIsSupported(const String& keySystem)
1302 {
1303     if (equalIgnoringCase(keySystem, "com.apple.fps") || equalIgnoringCase(keySystem, "com.apple.fps.1_0"))
1304         return true;
1305     return false;
1306 }
1307 #endif
1308
1309 MediaPlayer::SupportsType MediaPlayerPrivateAVFoundationObjC::supportsType(const MediaEngineSupportParameters& parameters)
1310 {
1311 #if ENABLE(ENCRYPTED_MEDIA)
1312     // From: <http://dvcs.w3.org/hg/html-media/raw-file/eme-v0.1b/encrypted-media/encrypted-media.html#dom-canplaytype>
1313     // In addition to the steps in the current specification, this method must run the following steps:
1314
1315     // 1. Check whether the Key System is supported with the specified container and codec type(s) by following the steps for the first matching condition from the following list:
1316     //    If keySystem is null, continue to the next step.
1317     if (!parameters.keySystem.isNull() && !parameters.keySystem.isEmpty()) {
1318         // If keySystem contains an unrecognized or unsupported Key System, return the empty string
1319         if (!keySystemIsSupported(parameters.keySystem))
1320             return MediaPlayer::IsNotSupported;
1321
1322         // If the Key System specified by keySystem does not support decrypting the container and/or codec specified in the rest of the type string.
1323         // (AVFoundation does not provide an API which would allow us to determine this, so this is a no-op)
1324     }
1325
1326     // 2. Return "maybe" or "probably" as appropriate per the existing specification of canPlayType().
1327 #endif
1328
1329 #if ENABLE(MEDIA_SOURCE)
1330     if (parameters.isMediaSource)
1331         return MediaPlayer::IsNotSupported;
1332 #endif
1333
1334     if (!mimeTypeCache().contains(parameters.type))
1335         return MediaPlayer::IsNotSupported;
1336
1337     // The spec says:
1338     // "Implementors are encouraged to return "maybe" unless the type can be confidently established as being supported or not."
1339     if (parameters.codecs.isEmpty())
1340         return MediaPlayer::MayBeSupported;
1341
1342     NSString *typeString = [NSString stringWithFormat:@"%@; codecs=\"%@\"", (NSString *)parameters.type, (NSString *)parameters.codecs];
1343     return [AVURLAsset isPlayableExtendedMIMEType:typeString] ? MediaPlayer::IsSupported : MediaPlayer::MayBeSupported;;
1344 }
1345
1346 bool MediaPlayerPrivateAVFoundationObjC::supportsKeySystem(const String& keySystem, const String& mimeType)
1347 {
1348 #if ENABLE(ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA_V2)
1349     if (!keySystem.isEmpty()) {
1350         if (!keySystemIsSupported(keySystem))
1351             return false;
1352
1353         if (!mimeType.isEmpty() && !mimeTypeCache().contains(mimeType))
1354             return false;
1355
1356         return true;
1357     }
1358 #else
1359     UNUSED_PARAM(keySystem);
1360     UNUSED_PARAM(mimeType);
1361 #endif
1362     return false;
1363 }
1364
1365 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
1366 bool MediaPlayerPrivateAVFoundationObjC::shouldWaitForLoadingOfResource(AVAssetResourceLoadingRequest* avRequest)
1367 {
1368     String scheme = [[[avRequest request] URL] scheme];
1369     String keyURI = [[[avRequest request] URL] absoluteString];
1370
1371 #if ENABLE(ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA_V2)
1372     if (scheme == "skd") {
1373         // Create an initData with the following layout:
1374         // [4 bytes: keyURI size], [keyURI size bytes: keyURI]
1375         unsigned keyURISize = keyURI.length() * sizeof(UChar);
1376         RefPtr<ArrayBuffer> initDataBuffer = ArrayBuffer::create(4 + keyURISize, 1);
1377         RefPtr<JSC::DataView> initDataView = JSC::DataView::create(initDataBuffer, 0, initDataBuffer->byteLength());
1378         initDataView->set<uint32_t>(0, keyURISize, true);
1379
1380         RefPtr<Uint16Array> keyURIArray = Uint16Array::create(initDataBuffer, 4, keyURI.length());
1381         keyURIArray->setRange(StringView(keyURI).upconvertedCharacters(), keyURI.length() / sizeof(unsigned char), 0);
1382
1383 #if ENABLE(ENCRYPTED_MEDIA)
1384         if (!player()->keyNeeded("com.apple.lskd", emptyString(), static_cast<const unsigned char*>(initDataBuffer->data()), initDataBuffer->byteLength()))
1385 #elif ENABLE(ENCRYPTED_MEDIA_V2)
1386         RefPtr<Uint8Array> initData = Uint8Array::create(initDataBuffer, 0, initDataBuffer->byteLength());
1387         if (!player()->keyNeeded(initData.get()))
1388 #endif
1389             return false;
1390
1391         m_keyURIToRequestMap.set(keyURI, avRequest);
1392         return true;
1393     }
1394 #endif
1395
1396     RefPtr<WebCoreAVFResourceLoader> resourceLoader = WebCoreAVFResourceLoader::create(this, avRequest);
1397     m_resourceLoaderMap.add(avRequest, resourceLoader);
1398     resourceLoader->startLoading();
1399     return true;
1400 }
1401
1402 bool MediaPlayerPrivateAVFoundationObjC::shouldWaitForResponseToAuthenticationChallenge(NSURLAuthenticationChallenge* nsChallenge)
1403 {
1404 #if USE(CFNETWORK)
1405     UNUSED_PARAM(nsChallenge);
1406     // FIXME: <rdar://problem/15799844>
1407     return false;
1408 #else
1409     AuthenticationChallenge challenge(nsChallenge);
1410
1411     return player()->shouldWaitForResponseToAuthenticationChallenge(challenge);
1412 #endif
1413 }
1414
1415 void MediaPlayerPrivateAVFoundationObjC::didCancelLoadingRequest(AVAssetResourceLoadingRequest* avRequest)
1416 {
1417     String scheme = [[[avRequest request] URL] scheme];
1418
1419     WebCoreAVFResourceLoader* resourceLoader = m_resourceLoaderMap.get(avRequest);
1420
1421     if (resourceLoader)
1422         resourceLoader->stopLoading();
1423 }
1424
1425 void MediaPlayerPrivateAVFoundationObjC::didStopLoadingRequest(AVAssetResourceLoadingRequest *avRequest)
1426 {
1427     m_resourceLoaderMap.remove(avRequest);
1428 }
1429 #endif
1430
1431 bool MediaPlayerPrivateAVFoundationObjC::isAvailable()
1432 {
1433     return AVFoundationLibrary() && CoreMediaLibrary();
1434 }
1435
1436 float MediaPlayerPrivateAVFoundationObjC::mediaTimeForTimeValue(float timeValue) const
1437 {
1438     if (!metaDataAvailable())
1439         return timeValue;
1440
1441     // FIXME - impossible to implement until rdar://8721510 is fixed.
1442     return timeValue;
1443 }
1444
1445 void MediaPlayerPrivateAVFoundationObjC::updateVideoLayerGravity()
1446 {
1447     if (!m_videoLayer)
1448         return;
1449
1450     [CATransaction begin];
1451     [CATransaction setDisableActions:YES];    
1452     NSString* gravity = shouldMaintainAspectRatio() ? AVLayerVideoGravityResizeAspect : AVLayerVideoGravityResize;
1453     [m_videoLayer.get() setVideoGravity:gravity];
1454     [CATransaction commit];
1455 }
1456
1457 static AVAssetTrack* firstEnabledTrack(NSArray* tracks)
1458 {
1459     NSUInteger index = [tracks indexOfObjectPassingTest:^(id obj, NSUInteger, BOOL *) {
1460         return [static_cast<AVAssetTrack*>(obj) isEnabled];
1461     }];
1462     if (index == NSNotFound)
1463         return nil;
1464     return [tracks objectAtIndex:index];
1465 }
1466
1467 void MediaPlayerPrivateAVFoundationObjC::tracksChanged()
1468 {
1469     String primaryAudioTrackLanguage = m_languageOfPrimaryAudioTrack;
1470     m_languageOfPrimaryAudioTrack = String();
1471
1472     if (!m_avAsset)
1473         return;
1474
1475     setDelayCharacteristicsChangedNotification(true);
1476
1477     bool haveCCTrack = false;
1478     bool hasCaptions = false;
1479
1480     // This is called whenever the tracks collection changes so cache hasVideo and hasAudio since we are
1481     // asked about those fairly fequently.
1482     if (!m_avPlayerItem) {
1483         // We don't have a player item yet, so check with the asset because some assets support inspection
1484         // prior to becoming ready to play.
1485         AVAssetTrack* firstEnabledVideoTrack = firstEnabledTrack([m_avAsset.get() tracksWithMediaCharacteristic:AVMediaCharacteristicVisual]);
1486         setHasVideo(firstEnabledVideoTrack);
1487         setHasAudio(firstEnabledTrack([m_avAsset.get() tracksWithMediaCharacteristic:AVMediaCharacteristicAudible]));
1488 #if !HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1489         hasCaptions = [[m_avAsset.get() tracksWithMediaType:AVMediaTypeClosedCaption] count];
1490 #endif
1491
1492         presentationSizeDidChange(firstEnabledVideoTrack ? IntSize(CGSizeApplyAffineTransform([firstEnabledVideoTrack naturalSize], [firstEnabledVideoTrack preferredTransform])) : IntSize());
1493     } else {
1494         bool hasVideo = false;
1495         bool hasAudio = false;
1496         bool hasMetaData = false;
1497         for (AVPlayerItemTrack *track in m_cachedTracks.get()) {
1498             if ([track isEnabled]) {
1499                 AVAssetTrack *assetTrack = [track assetTrack];
1500                 NSString *mediaType = [assetTrack mediaType];
1501                 if ([mediaType isEqualToString:AVMediaTypeVideo])
1502                     hasVideo = true;
1503                 else if ([mediaType isEqualToString:AVMediaTypeAudio])
1504                     hasAudio = true;
1505                 else if ([mediaType isEqualToString:AVMediaTypeClosedCaption]) {
1506 #if !HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1507                     hasCaptions = true;
1508 #endif
1509                     haveCCTrack = true;
1510                 } else if ([mediaType isEqualToString:AVMediaTypeMetadata]) {
1511                     hasMetaData = true;
1512                 }
1513             }
1514         }
1515
1516         // Always says we have video if the AVPlayerLayer is ready for diaplay to work around
1517         // an AVFoundation bug which causes it to sometimes claim a track is disabled even
1518         // when it is not.
1519         setHasVideo(hasVideo || m_cachedIsReadyForDisplay);
1520
1521         setHasAudio(hasAudio);
1522 #if ENABLE(DATACUE_VALUE)
1523         if (hasMetaData)
1524             processMetadataTrack();
1525 #endif
1526
1527 #if ENABLE(VIDEO_TRACK)
1528         updateAudioTracks();
1529         updateVideoTracks();
1530 #endif
1531     }
1532
1533 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1534     if (AVMediaSelectionGroupType *legibleGroup = safeMediaSelectionGroupForLegibleMedia()) {
1535         hasCaptions = [[AVMediaSelectionGroup playableMediaSelectionOptionsFromArray:[legibleGroup options]] count];
1536         if (hasCaptions)
1537             processMediaSelectionOptions();
1538     }
1539 #endif
1540
1541 #if !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT) && HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
1542     if (!hasCaptions && haveCCTrack)
1543         processLegacyClosedCaptionsTracks();
1544 #elif !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
1545     if (haveCCTrack)
1546         processLegacyClosedCaptionsTracks();
1547 #endif
1548
1549     setHasClosedCaptions(hasCaptions);
1550
1551     LOG(Media, "MediaPlayerPrivateAVFoundation:tracksChanged(%p) - hasVideo = %s, hasAudio = %s, hasCaptions = %s",
1552         this, boolString(hasVideo()), boolString(hasAudio()), boolString(hasClosedCaptions()));
1553
1554     sizeChanged();
1555
1556     if (primaryAudioTrackLanguage != languageOfPrimaryAudioTrack())
1557         characteristicsChanged();
1558
1559     setDelayCharacteristicsChangedNotification(false);
1560 }
1561
1562 #if ENABLE(VIDEO_TRACK)
1563 template <typename RefT, typename PassRefT>
1564 void determineChangedTracksFromNewTracksAndOldItems(NSArray* tracks, NSString* trackType, Vector<RefT>& oldItems, RefT (*itemFactory)(AVPlayerItemTrack*), MediaPlayer* player, void (MediaPlayer::*removedFunction)(PassRefT), void (MediaPlayer::*addedFunction)(PassRefT))
1565 {
1566     RetainPtr<NSSet> newTracks = adoptNS([[NSSet alloc] initWithArray:[tracks objectsAtIndexes:[tracks indexesOfObjectsPassingTest:^(id track, NSUInteger, BOOL*){
1567         return [[[track assetTrack] mediaType] isEqualToString:trackType];
1568     }]]]);
1569     RetainPtr<NSMutableSet> oldTracks = adoptNS([[NSMutableSet alloc] initWithCapacity:oldItems.size()]);
1570
1571     typedef Vector<RefT> ItemVector;
1572     for (auto i = oldItems.begin(); i != oldItems.end(); ++i)
1573         [oldTracks addObject:(*i)->playerItemTrack()];
1574
1575     RetainPtr<NSMutableSet> removedTracks = adoptNS([oldTracks mutableCopy]);
1576     [removedTracks minusSet:newTracks.get()];
1577
1578     RetainPtr<NSMutableSet> addedTracks = adoptNS([newTracks mutableCopy]);
1579     [addedTracks minusSet:oldTracks.get()];
1580
1581     ItemVector replacementItems;
1582     ItemVector addedItems;
1583     ItemVector removedItems;
1584     for (auto i = oldItems.begin(); i != oldItems.end(); ++i) {
1585         if ([removedTracks containsObject:(*i)->playerItemTrack()])
1586             removedItems.append(*i);
1587         else
1588             replacementItems.append(*i);
1589     }
1590
1591     for (AVPlayerItemTrack* track in addedTracks.get())
1592         addedItems.append(itemFactory(track));
1593
1594     replacementItems.appendVector(addedItems);
1595     oldItems.swap(replacementItems);
1596
1597     for (auto i = removedItems.begin(); i != removedItems.end(); ++i)
1598         (player->*removedFunction)(*i);
1599
1600     for (auto i = addedItems.begin(); i != addedItems.end(); ++i)
1601         (player->*addedFunction)(*i);
1602 }
1603
1604 void MediaPlayerPrivateAVFoundationObjC::updateAudioTracks()
1605 {
1606 #if !LOG_DISABLED
1607     size_t count = m_audioTracks.size();
1608 #endif
1609
1610     determineChangedTracksFromNewTracksAndOldItems(m_cachedTracks.get(), AVMediaTypeAudio, m_audioTracks, &AudioTrackPrivateAVFObjC::create, player(), &MediaPlayer::removeAudioTrack, &MediaPlayer::addAudioTrack);
1611
1612 #if !LOG_DISABLED
1613     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::updateAudioTracks(%p) - audio track count was %lu, is %lu", this, count, m_audioTracks.size());
1614 #endif
1615 }
1616
1617 void MediaPlayerPrivateAVFoundationObjC::updateVideoTracks()
1618 {
1619 #if !LOG_DISABLED
1620     size_t count = m_videoTracks.size();
1621 #endif
1622
1623     determineChangedTracksFromNewTracksAndOldItems(m_cachedTracks.get(), AVMediaTypeVideo, m_videoTracks, &VideoTrackPrivateAVFObjC::create, player(), &MediaPlayer::removeVideoTrack, &MediaPlayer::addVideoTrack);
1624
1625 #if !LOG_DISABLED
1626     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::updateVideoTracks(%p) - video track count was %lu, is %lu", this, count, m_videoTracks.size());
1627 #endif
1628 }
1629
1630 bool MediaPlayerPrivateAVFoundationObjC::requiresTextTrackRepresentation() const
1631 {
1632 #if PLATFORM(IOS)
1633     if (m_videoFullscreenLayer)
1634         return true;
1635 #endif
1636     return false;
1637 }
1638
1639 void MediaPlayerPrivateAVFoundationObjC::syncTextTrackBounds()
1640 {
1641 #if PLATFORM(IOS)
1642     if (!m_videoFullscreenLayer || !m_textTrackRepresentationLayer)
1643         return;
1644     
1645     CGRect textFrame = m_videoLayer ? [m_videoLayer videoRect] : CGRectMake(0, 0, m_videoFullscreenFrame.width(), m_videoFullscreenFrame.height());
1646     [m_textTrackRepresentationLayer setFrame:textFrame];
1647 #endif
1648 }
1649
1650 void MediaPlayerPrivateAVFoundationObjC::setTextTrackRepresentation(TextTrackRepresentation* representation)
1651 {
1652 #if PLATFORM(IOS)
1653     PlatformLayer* representationLayer = representation ? representation->platformLayer() : nil;
1654     if (representationLayer == m_textTrackRepresentationLayer) {
1655         syncTextTrackBounds();
1656         return;
1657     }
1658
1659     if (m_textTrackRepresentationLayer)
1660         [m_textTrackRepresentationLayer removeFromSuperlayer];
1661
1662     m_textTrackRepresentationLayer = representationLayer;
1663
1664     if (m_videoFullscreenLayer && m_textTrackRepresentationLayer) {
1665         syncTextTrackBounds();
1666         [m_videoFullscreenLayer addSublayer:m_textTrackRepresentationLayer.get()];
1667     }
1668
1669 #else
1670     UNUSED_PARAM(representation);
1671 #endif
1672 }
1673 #endif // ENABLE(VIDEO_TRACK)
1674
1675 void MediaPlayerPrivateAVFoundationObjC::sizeChanged()
1676 {
1677     if (!m_avAsset)
1678         return;
1679
1680     setNaturalSize(roundedIntSize(m_cachedPresentationSize));
1681 }
1682
1683 #if PLATFORM(IOS)
1684 // FIXME: Implement for iOS in WebKit System Interface.
1685 static inline NSURL *wkAVAssetResolvedURL(AVAsset*)
1686 {
1687     return nil;
1688 }
1689 #endif
1690
1691 bool MediaPlayerPrivateAVFoundationObjC::hasSingleSecurityOrigin() const 
1692 {
1693     if (!m_avAsset)
1694         return false;
1695     
1696     RefPtr<SecurityOrigin> resolvedOrigin = SecurityOrigin::create(URL(wkAVAssetResolvedURL(m_avAsset.get())));
1697     RefPtr<SecurityOrigin> requestedOrigin = SecurityOrigin::createFromString(assetURL());
1698     return resolvedOrigin->isSameSchemeHostPort(requestedOrigin.get());
1699 }
1700
1701 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
1702 void MediaPlayerPrivateAVFoundationObjC::createVideoOutput()
1703 {
1704     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createVideoOutput(%p)", this);
1705
1706     if (!m_avPlayerItem || m_videoOutput)
1707         return;
1708
1709 #if USE(VIDEOTOOLBOX)
1710     NSDictionary* attributes = @{ (NSString*)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_422YpCbCr8) };
1711 #else
1712     NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
1713                                 nil];
1714 #endif
1715     m_videoOutput = adoptNS([[getAVPlayerItemVideoOutputClass() alloc] initWithPixelBufferAttributes:attributes]);
1716     ASSERT(m_videoOutput);
1717
1718     [m_videoOutput setDelegate:m_videoOutputDelegate.get() queue:globalPullDelegateQueue()];
1719
1720     [m_avPlayerItem.get() addOutput:m_videoOutput.get()];
1721
1722     waitForVideoOutputMediaDataWillChange();
1723
1724     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createVideoOutput(%p) - returning %p", this, m_videoOutput.get());
1725 }
1726
1727 void MediaPlayerPrivateAVFoundationObjC::destroyVideoOutput()
1728 {
1729     if (!m_videoOutput)
1730         return;
1731
1732     if (m_avPlayerItem)
1733         [m_avPlayerItem.get() removeOutput:m_videoOutput.get()];
1734     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::destroyVideoOutput(%p) - destroying  %p", this, m_videoOutput.get());
1735
1736     m_videoOutput = 0;
1737 }
1738
1739 RetainPtr<CVPixelBufferRef> MediaPlayerPrivateAVFoundationObjC::createPixelBuffer()
1740 {
1741     if (!m_videoOutput)
1742         createVideoOutput();
1743     ASSERT(m_videoOutput);
1744
1745 #if !LOG_DISABLED
1746     double start = monotonicallyIncreasingTime();
1747 #endif
1748
1749     CMTime currentTime = [m_avPlayerItem.get() currentTime];
1750
1751     if (![m_videoOutput.get() hasNewPixelBufferForItemTime:currentTime])
1752         return 0;
1753
1754     RetainPtr<CVPixelBufferRef> buffer = adoptCF([m_videoOutput.get() copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil]);
1755     if (!buffer)
1756         return 0;
1757
1758 #if USE(VIDEOTOOLBOX)
1759     // Create a VTPixelTransferSession, if necessary, as we cannot guarantee timely delivery of ARGB pixels.
1760     if (!m_pixelTransferSession) {
1761         VTPixelTransferSessionRef session = 0;
1762         VTPixelTransferSessionCreate(kCFAllocatorDefault, &session);
1763         m_pixelTransferSession = adoptCF(session);
1764     }
1765
1766     CVPixelBufferRef outputBuffer;
1767     CVPixelBufferCreate(kCFAllocatorDefault, CVPixelBufferGetWidth(buffer.get()), CVPixelBufferGetHeight(buffer.get()), kCVPixelFormatType_32BGRA, 0, &outputBuffer);
1768     VTPixelTransferSessionTransferImage(m_pixelTransferSession.get(), buffer.get(), outputBuffer);
1769     buffer = adoptCF(outputBuffer);
1770 #endif
1771
1772 #if !LOG_DISABLED
1773     double duration = monotonicallyIncreasingTime() - start;
1774     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::createPixelBuffer(%p) - creating buffer took %.4f", this, narrowPrecisionToFloat(duration));
1775 #endif
1776
1777     return buffer;
1778 }
1779
1780 bool MediaPlayerPrivateAVFoundationObjC::videoOutputHasAvailableFrame()
1781 {
1782     if (!m_avPlayerItem)
1783         return false;
1784
1785     if (m_lastImage)
1786         return true;
1787
1788     if (!m_videoOutput)
1789         createVideoOutput();
1790
1791     return [m_videoOutput hasNewPixelBufferForItemTime:[m_avPlayerItem currentTime]];
1792 }
1793
1794 static const void* CVPixelBufferGetBytePointerCallback(void* info)
1795 {
1796     CVPixelBufferRef pixelBuffer = static_cast<CVPixelBufferRef>(info);
1797     CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
1798     return CVPixelBufferGetBaseAddress(pixelBuffer);
1799 }
1800
1801 static void CVPixelBufferReleaseBytePointerCallback(void* info, const void*)
1802 {
1803     CVPixelBufferRef pixelBuffer = static_cast<CVPixelBufferRef>(info);
1804     CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
1805 }
1806
1807 static void CVPixelBufferReleaseInfoCallback(void* info)
1808 {
1809     CVPixelBufferRef pixelBuffer = static_cast<CVPixelBufferRef>(info);
1810     CFRelease(pixelBuffer);
1811 }
1812
1813 static RetainPtr<CGImageRef> createImageFromPixelBuffer(CVPixelBufferRef pixelBuffer)
1814 {
1815     // pixelBuffer will be of type kCVPixelFormatType_32BGRA.
1816     ASSERT(CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_32BGRA);
1817
1818     size_t width = CVPixelBufferGetWidth(pixelBuffer);
1819     size_t height = CVPixelBufferGetHeight(pixelBuffer);
1820     size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
1821     size_t byteLength = CVPixelBufferGetDataSize(pixelBuffer);
1822     CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaFirst;
1823
1824     CFRetain(pixelBuffer); // Balanced by CVPixelBufferReleaseInfoCallback in providerCallbacks.
1825     CGDataProviderDirectCallbacks providerCallbacks = { 0, CVPixelBufferGetBytePointerCallback, CVPixelBufferReleaseBytePointerCallback, 0, CVPixelBufferReleaseInfoCallback };
1826     RetainPtr<CGDataProviderRef> provider = adoptCF(CGDataProviderCreateDirect(pixelBuffer, byteLength, &providerCallbacks));
1827
1828     return adoptCF(CGImageCreate(width, height, 8, 32, bytesPerRow, deviceRGBColorSpaceRef(), bitmapInfo, provider.get(), NULL, false, kCGRenderingIntentDefault));
1829 }
1830
1831 void MediaPlayerPrivateAVFoundationObjC::updateLastImage()
1832 {
1833     RetainPtr<CVPixelBufferRef> pixelBuffer = createPixelBuffer();
1834
1835     // Calls to copyPixelBufferForItemTime:itemTimeForDisplay: may return nil if the pixel buffer
1836     // for the requested time has already been retrieved. In this case, the last valid image (if any)
1837     // should be displayed.
1838     if (pixelBuffer)
1839         m_lastImage = createImageFromPixelBuffer(pixelBuffer.get());
1840 }
1841
1842 void MediaPlayerPrivateAVFoundationObjC::paintWithVideoOutput(GraphicsContext* context, const IntRect& outputRect)
1843 {
1844     updateLastImage();
1845
1846     if (m_lastImage) {
1847         GraphicsContextStateSaver stateSaver(*context);
1848
1849         IntRect imageRect(0, 0, CGImageGetWidth(m_lastImage.get()), CGImageGetHeight(m_lastImage.get()));
1850
1851         context->drawNativeImage(m_lastImage.get(), imageRect.size(), ColorSpaceDeviceRGB, outputRect, imageRect);
1852
1853         // If we have created an AVAssetImageGenerator in the past due to m_videoOutput not having an available
1854         // video frame, destroy it now that it is no longer needed.
1855         if (m_imageGenerator)
1856             destroyImageGenerator();
1857     }
1858 }
1859
1860 PassNativeImagePtr MediaPlayerPrivateAVFoundationObjC::nativeImageForCurrentTime()
1861 {
1862     updateLastImage();
1863     return m_lastImage.get();
1864 }
1865
1866 void MediaPlayerPrivateAVFoundationObjC::waitForVideoOutputMediaDataWillChange()
1867 {
1868     if (!m_videoOutputSemaphore)
1869         m_videoOutputSemaphore = dispatch_semaphore_create(0);
1870
1871     [m_videoOutput requestNotificationOfMediaDataChangeWithAdvanceInterval:0];
1872
1873     // Wait for 1 second.
1874     long result = dispatch_semaphore_wait(m_videoOutputSemaphore, dispatch_time(0, 1 * NSEC_PER_SEC));
1875
1876     if (result)
1877         LOG(Media, "MediaPlayerPrivateAVFoundationObjC::waitForVideoOutputMediaDataWillChange(%p) timed out", this);
1878 }
1879
1880 void MediaPlayerPrivateAVFoundationObjC::outputMediaDataWillChange(AVPlayerItemVideoOutput*)
1881 {
1882     dispatch_semaphore_signal(m_videoOutputSemaphore);
1883 }
1884 #endif
1885
1886 #if ENABLE(ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA_V2)
1887 bool MediaPlayerPrivateAVFoundationObjC::extractKeyURIKeyIDAndCertificateFromInitData(Uint8Array* initData, String& keyURI, String& keyID, RefPtr<Uint8Array>& certificate)
1888 {
1889     // initData should have the following layout:
1890     // [4 bytes: keyURI length][N bytes: keyURI][4 bytes: contentID length], [N bytes: contentID], [4 bytes: certificate length][N bytes: certificate]
1891     if (initData->byteLength() < 4)
1892         return false;
1893
1894     RefPtr<ArrayBuffer> initDataBuffer = initData->buffer();
1895
1896     // Use a DataView to read uint32 values from the buffer, as Uint32Array requires the reads be aligned on 4-byte boundaries. 
1897     RefPtr<JSC::DataView> initDataView = JSC::DataView::create(initDataBuffer, 0, initDataBuffer->byteLength());
1898     uint32_t offset = 0;
1899     bool status = true;
1900
1901     uint32_t keyURILength = initDataView->get<uint32_t>(offset, true, &status);
1902     offset += 4;
1903     if (!status || offset + keyURILength > initData->length())
1904         return false;
1905
1906     RefPtr<Uint16Array> keyURIArray = Uint16Array::create(initDataBuffer, offset, keyURILength);
1907     if (!keyURIArray)
1908         return false;
1909
1910     keyURI = String(keyURIArray->data(), keyURILength / sizeof(unsigned short));
1911     offset += keyURILength;
1912
1913     uint32_t keyIDLength = initDataView->get<uint32_t>(offset, true, &status);
1914     offset += 4;
1915     if (!status || offset + keyIDLength > initData->length())
1916         return false;
1917
1918     RefPtr<Uint16Array> keyIDArray = Uint16Array::create(initDataBuffer, offset, keyIDLength);
1919     if (!keyIDArray)
1920         return false;
1921
1922     keyID = String(keyIDArray->data(), keyIDLength / sizeof(unsigned short));
1923     offset += keyIDLength;
1924
1925     uint32_t certificateLength = initDataView->get<uint32_t>(offset, true, &status);
1926     offset += 4;
1927     if (!status || offset + certificateLength > initData->length())
1928         return false;
1929
1930     certificate = Uint8Array::create(initDataBuffer, offset, certificateLength);
1931     if (!certificate)
1932         return false;
1933
1934     return true;
1935 }
1936 #endif
1937
1938 #if ENABLE(ENCRYPTED_MEDIA)
1939 MediaPlayer::MediaKeyException MediaPlayerPrivateAVFoundationObjC::generateKeyRequest(const String& keySystem, const unsigned char* initDataPtr, unsigned initDataLength)
1940 {
1941     if (!keySystemIsSupported(keySystem))
1942         return MediaPlayer::KeySystemNotSupported;
1943
1944     RefPtr<Uint8Array> initData = Uint8Array::create(initDataPtr, initDataLength);
1945     String keyURI;
1946     String keyID;
1947     RefPtr<Uint8Array> certificate;
1948     if (!extractKeyURIKeyIDAndCertificateFromInitData(initData.get(), keyURI, keyID, certificate))
1949         return MediaPlayer::InvalidPlayerState;
1950
1951     if (!m_keyURIToRequestMap.contains(keyURI))
1952         return MediaPlayer::InvalidPlayerState;
1953
1954     String sessionID = createCanonicalUUIDString();
1955
1956     RetainPtr<AVAssetResourceLoadingRequest> avRequest = m_keyURIToRequestMap.get(keyURI);
1957
1958     RetainPtr<NSData> certificateData = adoptNS([[NSData alloc] initWithBytes:certificate->baseAddress() length:certificate->byteLength()]);
1959     NSString* assetStr = keyID;
1960     RetainPtr<NSData> assetID = [NSData dataWithBytes: [assetStr cStringUsingEncoding:NSUTF8StringEncoding] length:[assetStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
1961     NSError* error = 0;
1962     RetainPtr<NSData> keyRequest = [avRequest.get() streamingContentKeyRequestDataForApp:certificateData.get() contentIdentifier:assetID.get() options:nil error:&error];
1963
1964     if (!keyRequest) {
1965         NSError* underlyingError = [[error userInfo] objectForKey:NSUnderlyingErrorKey];
1966         player()->keyError(keySystem, sessionID, MediaPlayerClient::DomainError, [underlyingError code]);
1967         return MediaPlayer::NoError;
1968     }
1969
1970     RefPtr<ArrayBuffer> keyRequestBuffer = ArrayBuffer::create([keyRequest.get() bytes], [keyRequest.get() length]);
1971     RefPtr<Uint8Array> keyRequestArray = Uint8Array::create(keyRequestBuffer, 0, keyRequestBuffer->byteLength());
1972     player()->keyMessage(keySystem, sessionID, keyRequestArray->data(), keyRequestArray->byteLength(), URL());
1973
1974     // Move ownership of the AVAssetResourceLoadingRequestfrom the keyIDToRequestMap to the sessionIDToRequestMap:
1975     m_sessionIDToRequestMap.set(sessionID, avRequest);
1976     m_keyURIToRequestMap.remove(keyURI);
1977
1978     return MediaPlayer::NoError;
1979 }
1980
1981 MediaPlayer::MediaKeyException MediaPlayerPrivateAVFoundationObjC::addKey(const String& keySystem, const unsigned char* keyPtr, unsigned keyLength, const unsigned char* initDataPtr, unsigned initDataLength, const String& sessionID)
1982 {
1983     if (!keySystemIsSupported(keySystem))
1984         return MediaPlayer::KeySystemNotSupported;
1985
1986     if (!m_sessionIDToRequestMap.contains(sessionID))
1987         return MediaPlayer::InvalidPlayerState;
1988
1989     RetainPtr<AVAssetResourceLoadingRequest> avRequest = m_sessionIDToRequestMap.get(sessionID);
1990     RetainPtr<NSData> keyData = adoptNS([[NSData alloc] initWithBytes:keyPtr length:keyLength]);
1991     [[avRequest.get() dataRequest] respondWithData:keyData.get()];
1992     [avRequest.get() finishLoading];
1993     m_sessionIDToRequestMap.remove(sessionID);
1994
1995     player()->keyAdded(keySystem, sessionID);
1996
1997     UNUSED_PARAM(initDataPtr);
1998     UNUSED_PARAM(initDataLength);
1999     return MediaPlayer::NoError;
2000 }
2001
2002 MediaPlayer::MediaKeyException MediaPlayerPrivateAVFoundationObjC::cancelKeyRequest(const String& keySystem, const String& sessionID)
2003 {
2004     if (!keySystemIsSupported(keySystem))
2005         return MediaPlayer::KeySystemNotSupported;
2006
2007     if (!m_sessionIDToRequestMap.contains(sessionID))
2008         return MediaPlayer::InvalidPlayerState;
2009
2010     m_sessionIDToRequestMap.remove(sessionID);
2011     return MediaPlayer::NoError;
2012 }
2013 #endif
2014
2015 #if ENABLE(ENCRYPTED_MEDIA_V2)
2016 RetainPtr<AVAssetResourceLoadingRequest> MediaPlayerPrivateAVFoundationObjC::takeRequestForKeyURI(const String& keyURI)
2017 {
2018     return m_keyURIToRequestMap.take(keyURI);
2019 }
2020
2021 std::unique_ptr<CDMSession> MediaPlayerPrivateAVFoundationObjC::createSession(const String& keySystem)
2022 {
2023     if (!keySystemIsSupported(keySystem))
2024         return nullptr;
2025
2026     return std::make_unique<CDMSessionAVFoundationObjC>(this);
2027 }
2028 #endif
2029
2030 #if !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
2031 void MediaPlayerPrivateAVFoundationObjC::processLegacyClosedCaptionsTracks()
2032 {
2033 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2034     [m_avPlayerItem.get() selectMediaOption:nil inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2035 #endif
2036
2037     Vector<RefPtr<InbandTextTrackPrivateAVF>> removedTextTracks = m_textTracks;
2038     for (AVPlayerItemTrack *playerItemTrack in m_cachedTracks.get()) {
2039
2040         AVAssetTrack *assetTrack = [playerItemTrack assetTrack];
2041         if (![[assetTrack mediaType] isEqualToString:AVMediaTypeClosedCaption])
2042             continue;
2043
2044         bool newCCTrack = true;
2045         for (unsigned i = removedTextTracks.size(); i > 0; --i) {
2046             if (removedTextTracks[i - 1]->textTrackCategory() != InbandTextTrackPrivateAVF::LegacyClosedCaption)
2047                 continue;
2048
2049             RefPtr<InbandTextTrackPrivateLegacyAVFObjC> track = static_cast<InbandTextTrackPrivateLegacyAVFObjC*>(m_textTracks[i - 1].get());
2050             if (track->avPlayerItemTrack() == playerItemTrack) {
2051                 removedTextTracks.remove(i - 1);
2052                 newCCTrack = false;
2053                 break;
2054             }
2055         }
2056
2057         if (!newCCTrack)
2058             continue;
2059         
2060         m_textTracks.append(InbandTextTrackPrivateLegacyAVFObjC::create(this, playerItemTrack));
2061     }
2062
2063     processNewAndRemovedTextTracks(removedTextTracks);
2064 }
2065 #endif
2066
2067 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2068 AVMediaSelectionGroupType* MediaPlayerPrivateAVFoundationObjC::safeMediaSelectionGroupForLegibleMedia()
2069 {
2070     if (!m_avAsset)
2071         return nil;
2072     
2073     if ([m_avAsset.get() statusOfValueForKey:@"availableMediaCharacteristicsWithMediaSelectionOptions" error:NULL] != AVKeyValueStatusLoaded)
2074         return nil;
2075     
2076     return [m_avAsset.get() mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicLegible];
2077 }
2078
2079 void MediaPlayerPrivateAVFoundationObjC::processMediaSelectionOptions()
2080 {
2081     AVMediaSelectionGroupType *legibleGroup = safeMediaSelectionGroupForLegibleMedia();
2082     if (!legibleGroup) {
2083         LOG(Media, "MediaPlayerPrivateAVFoundationObjC::processMediaSelectionOptions(%p) - nil mediaSelectionGroup", this);
2084         return;
2085     }
2086
2087     // We enabled automatic media selection because we want alternate audio tracks to be enabled/disabled automatically,
2088     // but set the selected legible track to nil so text tracks will not be automatically configured.
2089     if (!m_textTracks.size())
2090         [m_avPlayerItem.get() selectMediaOption:nil inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2091
2092     Vector<RefPtr<InbandTextTrackPrivateAVF>> removedTextTracks = m_textTracks;
2093     NSArray *legibleOptions = [AVMediaSelectionGroup playableMediaSelectionOptionsFromArray:[legibleGroup options]];
2094     for (AVMediaSelectionOptionType *option in legibleOptions) {
2095         bool newTrack = true;
2096         for (unsigned i = removedTextTracks.size(); i > 0; --i) {
2097             if (removedTextTracks[i - 1]->textTrackCategory() == InbandTextTrackPrivateAVF::LegacyClosedCaption)
2098                 continue;
2099             
2100             RetainPtr<AVMediaSelectionOptionType> currentOption;
2101 #if ENABLE(AVF_CAPTIONS)
2102             if (removedTextTracks[i - 1]->textTrackCategory() == InbandTextTrackPrivateAVF::OutOfBand) {
2103                 RefPtr<OutOfBandTextTrackPrivateAVF> track = static_cast<OutOfBandTextTrackPrivateAVF*>(removedTextTracks[i - 1].get());
2104                 currentOption = track->mediaSelectionOption();
2105             } else
2106 #endif
2107             {
2108                 RefPtr<InbandTextTrackPrivateAVFObjC> track = static_cast<InbandTextTrackPrivateAVFObjC*>(removedTextTracks[i - 1].get());
2109                 currentOption = track->mediaSelectionOption();
2110             }
2111             
2112             if ([currentOption.get() isEqual:option]) {
2113                 removedTextTracks.remove(i - 1);
2114                 newTrack = false;
2115                 break;
2116             }
2117         }
2118         if (!newTrack)
2119             continue;
2120
2121 #if ENABLE(AVF_CAPTIONS)
2122         if ([option outOfBandSource]) {
2123             m_textTracks.append(OutOfBandTextTrackPrivateAVF::create(this, option));
2124             m_textTracks.last()->setHasBeenReported(true); // Ignore out-of-band tracks that we passed to AVFoundation so we do not double-count them
2125             continue;
2126         }
2127 #endif
2128
2129         m_textTracks.append(InbandTextTrackPrivateAVFObjC::create(this, option));
2130     }
2131
2132     processNewAndRemovedTextTracks(removedTextTracks);
2133 }
2134
2135 void MediaPlayerPrivateAVFoundationObjC::processMetadataTrack()
2136 {
2137     if (m_metadataTrack)
2138         return;
2139
2140     m_metadataTrack = InbandMetadataTextTrackPrivateAVF::create(InbandTextTrackPrivate::Metadata, InbandTextTrackPrivate::Data);
2141     m_metadataTrack->setInBandMetadataTrackDispatchType("com.apple.streaming");
2142     player()->addTextTrack(m_metadataTrack);
2143 }
2144
2145 void MediaPlayerPrivateAVFoundationObjC::processCue(NSArray *attributedStrings, double time)
2146 {
2147     if (!m_currentTextTrack)
2148         return;
2149
2150     m_currentTextTrack->processCue(reinterpret_cast<CFArrayRef>(attributedStrings), time);
2151 }
2152
2153 void MediaPlayerPrivateAVFoundationObjC::flushCues()
2154 {
2155     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::flushCues(%p)", this);
2156
2157     if (!m_currentTextTrack)
2158         return;
2159     
2160     m_currentTextTrack->resetCueValues();
2161 }
2162 #endif // HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2163
2164 void MediaPlayerPrivateAVFoundationObjC::setCurrentTextTrack(InbandTextTrackPrivateAVF *track)
2165 {
2166     if (m_currentTextTrack == track)
2167         return;
2168
2169     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::setCurrentTextTrack(%p) - selecting track %p, language = %s", this, track, track ? track->language().string().utf8().data() : "");
2170         
2171     m_currentTextTrack = track;
2172
2173     if (track) {
2174         if (track->textTrackCategory() == InbandTextTrackPrivateAVF::LegacyClosedCaption)
2175             [m_avPlayer.get() setClosedCaptionDisplayEnabled:YES];
2176 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2177 #if ENABLE(AVF_CAPTIONS)
2178         else if (track->textTrackCategory() == InbandTextTrackPrivateAVF::OutOfBand)
2179             [m_avPlayerItem.get() selectMediaOption:static_cast<OutOfBandTextTrackPrivateAVF*>(track)->mediaSelectionOption() inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2180 #endif
2181         else
2182             [m_avPlayerItem.get() selectMediaOption:static_cast<InbandTextTrackPrivateAVFObjC*>(track)->mediaSelectionOption() inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2183 #endif
2184     } else {
2185 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2186         [m_avPlayerItem.get() selectMediaOption:0 inMediaSelectionGroup:safeMediaSelectionGroupForLegibleMedia()];
2187 #endif
2188         [m_avPlayer.get() setClosedCaptionDisplayEnabled:NO];
2189     }
2190
2191 }
2192
2193 String MediaPlayerPrivateAVFoundationObjC::languageOfPrimaryAudioTrack() const
2194 {
2195     if (!m_languageOfPrimaryAudioTrack.isNull())
2196         return m_languageOfPrimaryAudioTrack;
2197
2198     if (!m_avPlayerItem.get())
2199         return emptyString();
2200
2201 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2202     // If AVFoundation has an audible group, return the language of the currently selected audible option.
2203     AVMediaSelectionGroupType *audibleGroup = [m_avAsset.get() mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
2204     AVMediaSelectionOptionType *currentlySelectedAudibleOption = [m_avPlayerItem.get() selectedMediaOptionInMediaSelectionGroup:audibleGroup];
2205     if (currentlySelectedAudibleOption) {
2206         m_languageOfPrimaryAudioTrack = [[currentlySelectedAudibleOption locale] localeIdentifier];
2207         LOG(Media, "MediaPlayerPrivateAVFoundationObjC::languageOfPrimaryAudioTrack(%p) - returning language of selected audible option: %s", this, m_languageOfPrimaryAudioTrack.utf8().data());
2208
2209         return m_languageOfPrimaryAudioTrack;
2210     }
2211 #endif // HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2212
2213     // AVFoundation synthesizes an audible group when there is only one ungrouped audio track if there is also a legible group (one or
2214     // 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.
2215     NSArray *tracks = [m_avAsset.get() tracksWithMediaType:AVMediaTypeAudio];
2216     if (!tracks || [tracks count] != 1) {
2217         m_languageOfPrimaryAudioTrack = emptyString();
2218         LOG(Media, "MediaPlayerPrivateAVFoundationObjC::languageOfPrimaryAudioTrack(%p) - %lu audio tracks, returning emptyString()", this, static_cast<unsigned long>(tracks ? [tracks count] : 0));
2219         return m_languageOfPrimaryAudioTrack;
2220     }
2221
2222     AVAssetTrack *track = [tracks objectAtIndex:0];
2223     m_languageOfPrimaryAudioTrack = AVTrackPrivateAVFObjCImpl::languageForAVAssetTrack(track);
2224
2225 #if !LOG_DISABLED
2226     if (m_languageOfPrimaryAudioTrack == emptyString())
2227         LOG(Media, "MediaPlayerPrivateAVFoundationObjC::languageOfPrimaryAudioTrack(%p) - single audio track has no language, returning emptyString()", this);
2228     else
2229         LOG(Media, "MediaPlayerPrivateAVFoundationObjC::languageOfPrimaryAudioTrack(%p) - returning language of single audio track: %s", this, m_languageOfPrimaryAudioTrack.utf8().data());
2230 #endif
2231
2232     return m_languageOfPrimaryAudioTrack;
2233 }
2234
2235 #if ENABLE(IOS_AIRPLAY)
2236 bool MediaPlayerPrivateAVFoundationObjC::isCurrentPlaybackTargetWireless() const
2237 {
2238     if (!m_avPlayer)
2239         return false;
2240
2241     bool wirelessTarget = [m_avPlayer.get() isExternalPlaybackActive];
2242     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::isCurrentPlaybackTargetWireless(%p) - returning %s", this, boolString(wirelessTarget));
2243     return wirelessTarget;
2244 }
2245
2246 MediaPlayer::WirelessPlaybackTargetType MediaPlayerPrivateAVFoundationObjC::wirelessPlaybackTargetType() const
2247 {
2248     if (!m_avPlayer)
2249         return MediaPlayer::TargetTypeNone;
2250
2251     switch (wkExernalDeviceTypeForPlayer(m_avPlayer.get())) {
2252     case wkExternalPlaybackTypeNone:
2253         return MediaPlayer::TargetTypeNone;
2254     case wkExternalPlaybackTypeAirPlay:
2255         return MediaPlayer::TargetTypeAirPlay;
2256     case wkExternalPlaybackTypeTVOut:
2257         return MediaPlayer::TargetTypeTVOut;
2258     }
2259
2260     ASSERT_NOT_REACHED();
2261     return MediaPlayer::TargetTypeNone;
2262 }
2263
2264 String MediaPlayerPrivateAVFoundationObjC::wirelessPlaybackTargetName() const
2265 {
2266     if (!m_avPlayer)
2267         return emptyString();
2268     
2269     String wirelessTargetName = wkExernalDeviceDisplayNameForPlayer(m_avPlayer.get());
2270     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::wirelessPlaybackTargetName(%p) - returning %s", this, wirelessTargetName.utf8().data());
2271
2272     return wirelessTargetName;
2273 }
2274
2275 bool MediaPlayerPrivateAVFoundationObjC::wirelessVideoPlaybackDisabled() const
2276 {
2277     if (!m_avPlayer)
2278         return !m_allowsWirelessVideoPlayback;
2279     
2280     m_allowsWirelessVideoPlayback = [m_avPlayer.get() allowsExternalPlayback];
2281     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::wirelessVideoPlaybackDisabled(%p) - returning %s", this, boolString(!m_allowsWirelessVideoPlayback));
2282
2283     return !m_allowsWirelessVideoPlayback;
2284 }
2285
2286 void MediaPlayerPrivateAVFoundationObjC::setWirelessVideoPlaybackDisabled(bool disabled)
2287 {
2288     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::setWirelessVideoPlaybackDisabled(%p) - %s", this, boolString(disabled));
2289     m_allowsWirelessVideoPlayback = !disabled;
2290     if (!m_avPlayer)
2291         return;
2292     
2293     [m_avPlayer.get() setAllowsExternalPlayback:!disabled];
2294 }
2295 #endif
2296
2297 void MediaPlayerPrivateAVFoundationObjC::playerItemStatusDidChange(int status)
2298 {
2299     m_cachedItemStatus = status;
2300
2301     updateStates();
2302 }
2303
2304 void MediaPlayerPrivateAVFoundationObjC::playbackLikelyToKeepUpWillChange()
2305 {
2306     m_pendingStatusChanges++;
2307 }
2308
2309 void MediaPlayerPrivateAVFoundationObjC::playbackLikelyToKeepUpDidChange(bool likelyToKeepUp)
2310 {
2311     m_cachedLikelyToKeepUp = likelyToKeepUp;
2312
2313     ASSERT(m_pendingStatusChanges);
2314     if (!--m_pendingStatusChanges)
2315         updateStates();
2316 }
2317
2318 void MediaPlayerPrivateAVFoundationObjC::playbackBufferEmptyWillChange()
2319 {
2320     m_pendingStatusChanges++;
2321 }
2322
2323 void MediaPlayerPrivateAVFoundationObjC::playbackBufferEmptyDidChange(bool bufferEmpty)
2324 {
2325     m_cachedBufferEmpty = bufferEmpty;
2326
2327     ASSERT(m_pendingStatusChanges);
2328     if (!--m_pendingStatusChanges)
2329         updateStates();
2330 }
2331
2332 void MediaPlayerPrivateAVFoundationObjC::playbackBufferFullWillChange()
2333 {
2334     m_pendingStatusChanges++;
2335 }
2336
2337 void MediaPlayerPrivateAVFoundationObjC::playbackBufferFullDidChange(bool bufferFull)
2338 {
2339     m_cachedBufferFull = bufferFull;
2340
2341     ASSERT(m_pendingStatusChanges);
2342     if (!--m_pendingStatusChanges)
2343         updateStates();
2344 }
2345
2346 void MediaPlayerPrivateAVFoundationObjC::seekableTimeRangesDidChange(RetainPtr<NSArray> seekableRanges)
2347 {
2348     m_cachedSeekableRanges = seekableRanges;
2349
2350     seekableTimeRangesChanged();
2351     updateStates();
2352 }
2353
2354 void MediaPlayerPrivateAVFoundationObjC::loadedTimeRangesDidChange(RetainPtr<NSArray> loadedRanges)
2355 {
2356     m_cachedLoadedRanges = loadedRanges;
2357
2358     loadedTimeRangesChanged();
2359     updateStates();
2360 }
2361
2362 void MediaPlayerPrivateAVFoundationObjC::firstFrameAvailableDidChange(bool isReady)
2363 {
2364     m_cachedIsReadyForDisplay = isReady;
2365     if (!hasVideo() && isReady)
2366         tracksChanged();
2367     updateStates();
2368 }
2369
2370 void MediaPlayerPrivateAVFoundationObjC::trackEnabledDidChange(bool)
2371 {
2372     tracksChanged();
2373     updateStates();
2374 }
2375
2376 void MediaPlayerPrivateAVFoundationObjC::setShouldBufferData(bool shouldBuffer)
2377 {
2378     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::shouldBufferData(%p) - %s", this, boolString(shouldBuffer));
2379     if (m_shouldBufferData == shouldBuffer)
2380         return;
2381
2382     m_shouldBufferData = shouldBuffer;
2383     
2384     if (!m_avPlayer)
2385         return;
2386
2387     if (m_shouldBufferData)
2388         [m_avPlayer.get() replaceCurrentItemWithPlayerItem:m_avPlayerItem.get()];
2389     else
2390         [m_avPlayer.get() replaceCurrentItemWithPlayerItem:nil];
2391 }
2392
2393 #if ENABLE(DATACUE_VALUE)
2394 static const AtomicString& metadataType(NSString *avMetadataKeySpace)
2395 {
2396     static NeverDestroyed<const AtomicString> quickTimeUserData("com.apple.quicktime.udta", AtomicString::ConstructFromLiteral);
2397     static NeverDestroyed<const AtomicString> isoUserData("org.mp4ra", AtomicString::ConstructFromLiteral);
2398     static NeverDestroyed<const AtomicString> quickTimeMetadata("com.apple.quicktime.mdta", AtomicString::ConstructFromLiteral);
2399     static NeverDestroyed<const AtomicString> iTunesMetadata("com.apple.itunes", AtomicString::ConstructFromLiteral);
2400     static NeverDestroyed<const AtomicString> id3Metadata("org.id3", AtomicString::ConstructFromLiteral);
2401
2402     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceQuickTimeUserData])
2403         return quickTimeUserData;
2404     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceISOUserData])
2405         return isoUserData;
2406     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceQuickTimeMetadata])
2407         return quickTimeMetadata;
2408     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceiTunes])
2409         return iTunesMetadata;
2410     if ([avMetadataKeySpace isEqualToString:AVMetadataKeySpaceID3])
2411         return id3Metadata;
2412
2413     return emptyAtom;
2414 }
2415 #endif
2416
2417 void MediaPlayerPrivateAVFoundationObjC::metadataDidArrive(RetainPtr<NSArray> metadata, double mediaTime)
2418 {
2419     m_currentMetaData = metadata && ![metadata isKindOfClass:[NSNull class]] ? metadata : nil;
2420
2421     LOG(Media, "MediaPlayerPrivateAVFoundationObjC::metadataDidArrive(%p) - adding %i cues at time %.2f", this, m_currentMetaData ? static_cast<int>([m_currentMetaData.get() count]) : 0, mediaTime);
2422
2423 #if ENABLE(DATACUE_VALUE)
2424     if (seeking())
2425         return;
2426
2427     if (!metadata || [metadata isKindOfClass:[NSNull class]]) {
2428         m_metadataTrack->updatePendingCueEndTimes(mediaTime);
2429         return;
2430     }
2431
2432     if (!m_metadataTrack)
2433         processMetadataTrack();
2434
2435     // Set the duration of all incomplete cues before adding new ones.
2436     double earliesStartTime = std::numeric_limits<double>::infinity();
2437     for (AVMetadataItemType *item in m_currentMetaData.get()) {
2438         double start = CMTimeGetSeconds(item.time);
2439         if (start < earliesStartTime)
2440             earliesStartTime = start;
2441     }
2442     m_metadataTrack->updatePendingCueEndTimes(earliesStartTime);
2443
2444     for (AVMetadataItemType *item in m_currentMetaData.get()) {
2445         double start = CMTimeGetSeconds(item.time);
2446         double end = std::numeric_limits<double>::infinity();
2447         if (CMTIME_IS_VALID(item.duration))
2448             end = start + CMTimeGetSeconds(item.duration);
2449
2450         AtomicString type = nullAtom;
2451         if (item.keySpace)
2452             type = metadataType(item.keySpace);
2453
2454         m_metadataTrack->addDataCue(start, end, SerializedPlatformRepresentationMac::create(item), type);
2455     }
2456 #endif
2457 }
2458
2459 void MediaPlayerPrivateAVFoundationObjC::tracksDidChange(RetainPtr<NSArray> tracks)
2460 {
2461     for (AVPlayerItemTrack *track in m_cachedTracks.get())
2462         [track removeObserver:m_objcObserver.get() forKeyPath:@"enabled"];
2463
2464     m_cachedTracks = tracks;
2465     for (AVPlayerItemTrack *track in m_cachedTracks.get())
2466         [track addObserver:m_objcObserver.get() forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:(void *)MediaPlayerAVFoundationObservationContextPlayerItemTrack];
2467
2468     m_cachedTotalBytes = 0;
2469
2470     tracksChanged();
2471     updateStates();
2472 }
2473
2474 void MediaPlayerPrivateAVFoundationObjC::hasEnabledAudioDidChange(bool hasEnabledAudio)
2475 {
2476     m_cachedHasEnabledAudio = hasEnabledAudio;
2477
2478     tracksChanged();
2479     updateStates();
2480 }
2481
2482 void MediaPlayerPrivateAVFoundationObjC::presentationSizeDidChange(FloatSize size)
2483 {
2484     m_cachedPresentationSize = size;
2485
2486     sizeChanged();
2487     updateStates();
2488 }
2489
2490 void MediaPlayerPrivateAVFoundationObjC::durationDidChange(double duration)
2491 {
2492     m_cachedDuration = duration;
2493
2494     invalidateCachedDuration();
2495 }
2496
2497 void MediaPlayerPrivateAVFoundationObjC::rateDidChange(double rate)
2498 {
2499     m_cachedRate = rate;
2500
2501     updateStates();
2502     rateChanged();
2503 }
2504     
2505 #if ENABLE(IOS_AIRPLAY)
2506 void MediaPlayerPrivateAVFoundationObjC::playbackTargetIsWirelessDidChange()
2507 {
2508     playbackTargetIsWirelessChanged();
2509 }
2510 #endif
2511
2512 NSArray* assetMetadataKeyNames()
2513 {
2514     static NSArray* keys;
2515     if (!keys) {
2516         keys = [[NSArray alloc] initWithObjects:@"duration",
2517                     @"naturalSize",
2518                     @"preferredTransform",
2519                     @"preferredVolume",
2520                     @"preferredRate",
2521                     @"playable",
2522                     @"tracks",
2523                     @"availableMediaCharacteristicsWithMediaSelectionOptions",
2524                    nil];
2525     }
2526     return keys;
2527 }
2528
2529 NSArray* itemKVOProperties()
2530 {
2531     static NSArray* keys;
2532     if (!keys) {
2533         keys = [[NSArray alloc] initWithObjects:@"presentationSize",
2534                 @"status",
2535                 @"asset",
2536                 @"tracks",
2537                 @"seekableTimeRanges",
2538                 @"loadedTimeRanges",
2539                 @"playbackLikelyToKeepUp",
2540                 @"playbackBufferFull",
2541                 @"playbackBufferEmpty",
2542                 @"duration",
2543                 @"hasEnabledAudio",
2544                 @"timedMetadata",
2545                 nil];
2546     }
2547     return keys;
2548 }
2549
2550 NSArray* assetTrackMetadataKeyNames()
2551 {
2552     static NSArray* keys = [[NSArray alloc] initWithObjects:@"totalSampleDataLength", @"mediaType", @"enabled", nil];
2553
2554     return keys;
2555 }
2556
2557 } // namespace WebCore
2558
2559 @implementation WebCoreAVFMovieObserver
2560
2561 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC*)callback
2562 {
2563     self = [super init];
2564     if (!self)
2565         return nil;
2566     m_callback = callback;
2567     return self;
2568 }
2569
2570 - (void)disconnect
2571 {
2572     [NSObject cancelPreviousPerformRequestsWithTarget:self];
2573     m_callback = 0;
2574 }
2575
2576 - (void)metadataLoaded
2577 {
2578     if (!m_callback)
2579         return;
2580
2581     m_callback->scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification::AssetMetadataLoaded);
2582 }
2583
2584 - (void)didEnd:(NSNotification *)unusedNotification
2585 {
2586     UNUSED_PARAM(unusedNotification);
2587     if (!m_callback)
2588         return;
2589     m_callback->scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification::ItemDidPlayToEndTime);
2590 }
2591
2592 - (void)observeValueForKeyPath:keyPath ofObject:(id)object change:(NSDictionary *)change context:(MediaPlayerAVFoundationObservationContext)context
2593 {
2594     UNUSED_PARAM(object);
2595     id newValue = [change valueForKey:NSKeyValueChangeNewKey];
2596
2597     LOG(Media, "WebCoreAVFMovieObserver::observeValueForKeyPath(%p) - keyPath = %s", self, [keyPath UTF8String]);
2598
2599     if (!m_callback)
2600         return;
2601
2602     bool willChange = [[change valueForKey:NSKeyValueChangeNotificationIsPriorKey] boolValue];
2603
2604     WTF::Function<void ()> function;
2605
2606     if (context == MediaPlayerAVFoundationObservationContextAVPlayerLayer) {
2607         if ([keyPath isEqualToString:@"readyForDisplay"])
2608             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::firstFrameAvailableDidChange, m_callback, [newValue boolValue]);
2609     }
2610
2611     if (context == MediaPlayerAVFoundationObservationContextPlayerItemTrack) {
2612         if ([keyPath isEqualToString:@"enabled"])
2613             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::trackEnabledDidChange, m_callback, [newValue boolValue]);
2614     }
2615
2616     if (context == MediaPlayerAVFoundationObservationContextPlayerItem && willChange) {
2617         if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"])
2618             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::playbackLikelyToKeepUpWillChange, m_callback);
2619         else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
2620             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::playbackBufferEmptyWillChange, m_callback);
2621         else if ([keyPath isEqualToString:@"playbackBufferFull"])
2622             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::playbackBufferFullWillChange, m_callback);
2623     }
2624
2625     if (context == MediaPlayerAVFoundationObservationContextPlayerItem && !willChange) {
2626         // A value changed for an AVPlayerItem
2627         if ([keyPath isEqualToString:@"status"])
2628             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::playerItemStatusDidChange, m_callback, [newValue intValue]);
2629         else if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"])
2630             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::playbackLikelyToKeepUpDidChange, m_callback, [newValue boolValue]);
2631         else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
2632             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::playbackBufferEmptyDidChange, m_callback, [newValue boolValue]);
2633         else if ([keyPath isEqualToString:@"playbackBufferFull"])
2634             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::playbackBufferFullDidChange, m_callback, [newValue boolValue]);
2635         else if ([keyPath isEqualToString:@"asset"])
2636             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::setAsset, m_callback, RetainPtr<NSArray>(newValue));
2637         else if ([keyPath isEqualToString:@"loadedTimeRanges"])
2638             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::loadedTimeRangesDidChange, m_callback, RetainPtr<NSArray>(newValue));
2639         else if ([keyPath isEqualToString:@"seekableTimeRanges"])
2640             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::seekableTimeRangesDidChange, m_callback, RetainPtr<NSArray>(newValue));
2641         else if ([keyPath isEqualToString:@"tracks"])
2642             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::tracksDidChange, m_callback, RetainPtr<NSArray>(newValue));
2643         else if ([keyPath isEqualToString:@"hasEnabledAudio"])
2644             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::hasEnabledAudioDidChange, m_callback, [newValue boolValue]);
2645         else if ([keyPath isEqualToString:@"presentationSize"])
2646             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::presentationSizeDidChange, m_callback, FloatSize([newValue sizeValue]));
2647         else if ([keyPath isEqualToString:@"duration"])
2648             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::durationDidChange, m_callback, CMTimeGetSeconds([newValue CMTimeValue]));
2649         else if ([keyPath isEqualToString:@"timedMetadata"] && newValue) {
2650             double now = 0;
2651             CMTime itemTime = [(AVPlayerItemType *)object currentTime];
2652             if (CMTIME_IS_NUMERIC(itemTime))
2653                 now = std::max(narrowPrecisionToFloat(CMTimeGetSeconds(itemTime)), 0.0f);
2654             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::metadataDidArrive, m_callback, RetainPtr<NSArray>(newValue), now);
2655         }
2656     }
2657
2658     if (context == MediaPlayerAVFoundationObservationContextPlayer && !willChange) {
2659         // A value changed for an AVPlayer.
2660         if ([keyPath isEqualToString:@"rate"])
2661             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::rateDidChange, m_callback, [newValue doubleValue]);
2662 #if ENABLE(IOS_AIRPLAY)
2663         else if ([keyPath isEqualToString:@"externalPlaybackActive"])
2664             function = WTF::bind(&MediaPlayerPrivateAVFoundationObjC::playbackTargetIsWirelessDidChange, m_callback);
2665 #endif
2666     }
2667     
2668     if (function.isNull())
2669         return;
2670
2671     auto weakThis = m_callback->createWeakPtr();
2672     m_callback->scheduleMainThreadNotification(MediaPlayerPrivateAVFoundation::Notification([weakThis, function]{
2673         // weakThis and function both refer to the same MediaPlayerPrivateAVFoundationObjC instance. If the WeakPtr has
2674         // been cleared, the underlying object has been destroyed, and it is unsafe to call function().
2675         if (!weakThis)
2676             return;
2677         function();
2678     }));
2679 }
2680
2681 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
2682 - (void)legibleOutput:(id)output didOutputAttributedStrings:(NSArray *)strings nativeSampleBuffers:(NSArray *)nativeSamples forItemTime:(CMTime)itemTime
2683 {
2684     UNUSED_PARAM(output);
2685     UNUSED_PARAM(nativeSamples);
2686
2687     if (!m_callback)
2688         return;
2689
2690     RetainPtr<WebCoreAVFMovieObserver> strongSelf = self;
2691     RetainPtr<NSArray> strongStrings = strings;
2692     callOnMainThread([strongSelf, strongStrings, itemTime] {
2693         MediaPlayerPrivateAVFoundationObjC* callback = strongSelf->m_callback;
2694         if (!callback)
2695             return;
2696         callback->processCue(strongStrings.get(), CMTimeGetSeconds(itemTime));
2697     });
2698 }
2699
2700 - (void)outputSequenceWasFlushed:(id)output
2701 {
2702     UNUSED_PARAM(output);
2703
2704     if (!m_callback)
2705         return;
2706     
2707     RetainPtr<WebCoreAVFMovieObserver> strongSelf = self;
2708     callOnMainThread([strongSelf] {
2709         if (MediaPlayerPrivateAVFoundationObjC* callback = strongSelf->m_callback)
2710             callback->flushCues();
2711     });
2712 }
2713 #endif
2714
2715 @end
2716
2717 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
2718 @implementation WebCoreAVFLoaderDelegate
2719
2720 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC*)callback
2721 {
2722     self = [super init];
2723     if (!self)
2724         return nil;
2725     m_callback = callback;
2726     return self;
2727 }
2728
2729 - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
2730 {
2731     UNUSED_PARAM(resourceLoader);
2732     if (!m_callback)
2733         return NO;
2734
2735     RetainPtr<WebCoreAVFLoaderDelegate> strongSelf = self;
2736     RetainPtr<AVAssetResourceLoadingRequest> strongRequest = loadingRequest;
2737     callOnMainThread([strongSelf, strongRequest] {
2738         MediaPlayerPrivateAVFoundationObjC* callback = strongSelf->m_callback;
2739         if (!callback) {
2740             [strongRequest finishLoadingWithError:nil];
2741             return;
2742         }
2743
2744         if (!callback->shouldWaitForLoadingOfResource(strongRequest.get()))
2745             [strongRequest finishLoadingWithError:nil];
2746     });
2747
2748     return YES;
2749 }
2750
2751 - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForResponseToAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
2752 {
2753     UNUSED_PARAM(resourceLoader);
2754     if (!m_callback)
2755         return NO;
2756
2757     if ([[[challenge protectionSpace] authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
2758         return NO;
2759
2760     RetainPtr<WebCoreAVFLoaderDelegate> strongSelf = self;
2761     RetainPtr<NSURLAuthenticationChallenge> strongChallenge = challenge;
2762     callOnMainThread([strongSelf, strongChallenge] {
2763         MediaPlayerPrivateAVFoundationObjC* callback = strongSelf->m_callback;
2764         if (!callback) {
2765             [[strongChallenge sender] cancelAuthenticationChallenge:strongChallenge.get()];
2766             return;
2767         }
2768
2769         if (!callback->shouldWaitForResponseToAuthenticationChallenge(strongChallenge.get()))
2770             [[strongChallenge sender] cancelAuthenticationChallenge:strongChallenge.get()];
2771     });
2772
2773     return YES;
2774 }
2775
2776 - (void)resourceLoader:(AVAssetResourceLoader *)resourceLoader didCancelLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest
2777 {
2778     UNUSED_PARAM(resourceLoader);
2779     if (!m_callback)
2780         return;
2781
2782     RetainPtr<WebCoreAVFLoaderDelegate> strongSelf = self;
2783     RetainPtr<AVAssetResourceLoadingRequest> strongRequest = loadingRequest;
2784     callOnMainThread([strongSelf, strongRequest] {
2785         MediaPlayerPrivateAVFoundationObjC* callback = strongSelf->m_callback;
2786         if (callback)
2787             callback->didCancelLoadingRequest(strongRequest.get());
2788     });
2789 }
2790
2791 - (void)setCallback:(MediaPlayerPrivateAVFoundationObjC*)callback
2792 {
2793     m_callback = callback;
2794 }
2795 @end
2796 #endif
2797
2798 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
2799 @implementation WebCoreAVFPullDelegate
2800 - (id)initWithCallback:(MediaPlayerPrivateAVFoundationObjC *)callback
2801 {
2802     self = [super init];
2803     if (self)
2804         m_callback = callback;
2805     return self;
2806 }
2807
2808 - (void)setCallback:(MediaPlayerPrivateAVFoundationObjC *)callback
2809 {
2810     m_callback = callback;
2811 }
2812
2813 - (void)outputMediaDataWillChange:(AVPlayerItemVideoOutput *)output
2814 {
2815     if (m_callback)
2816         m_callback->outputMediaDataWillChange(output);
2817 }
2818
2819 - (void)outputSequenceWasFlushed:(AVPlayerItemVideoOutput *)output
2820 {
2821     UNUSED_PARAM(output);
2822     // No-op.
2823 }
2824 @end
2825 #endif
2826
2827 #endif