[MSE] Add per-track signalling between SourceBuffer and SourceBufferPrivate.
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / SourceBufferPrivateAVFObjC.mm
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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 COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "config.h"
27 #import "SourceBufferPrivateAVFObjC.h"
28
29 #if ENABLE(MEDIA_SOURCE) && USE(AVFOUNDATION)
30
31 #import "ExceptionCodePlaceholder.h"
32 #import "Logging.h"
33 #import "MediaDescription.h"
34 #import "MediaPlayerPrivateMediaSourceAVFObjC.h"
35 #import "MediaSample.h"
36 #import "MediaSourcePrivateAVFObjC.h"
37 #import "MediaTimeMac.h"
38 #import "NotImplemented.h"
39 #import "SoftLinking.h"
40 #import "SourceBufferPrivateClient.h"
41 #import "TimeRanges.h"
42 #import "AudioTrackPrivateMediaSourceAVFObjC.h"
43 #import "VideoTrackPrivateMediaSourceAVFObjC.h"
44 #import "InbandTextTrackPrivateAVFObjC.h"
45 #import <AVFoundation/AVAssetTrack.h>
46 #import <AVFoundation/AVSampleBufferDisplayLayer.h>
47 #import <objc/runtime.h>
48 #import <wtf/text/AtomicString.h>
49 #import <wtf/text/CString.h>
50 #import <wtf/HashCountedSet.h>
51 #import <wtf/WeakPtr.h>
52 #import <map>
53
54 #pragma mark -
55 #pragma mark Soft Linking
56
57 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
58 SOFT_LINK_FRAMEWORK_OPTIONAL(CoreMedia)
59
60 SOFT_LINK_CLASS(AVFoundation, AVAssetTrack)
61 SOFT_LINK_CLASS(AVFoundation, AVStreamDataParser)
62 SOFT_LINK_CLASS(AVFoundation, AVSampleBufferDisplayLayer)
63
64 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaTypeVideo, NSString *)
65 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaTypeAudio, NSString *)
66 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaTypeText, NSString *)
67
68 SOFT_LINK_CONSTANT(CoreMedia, kCMTimeZero, CMTime);
69 SOFT_LINK_CONSTANT(CoreMedia, kCMTimeInvalid, CMTime);
70 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleAttachmentKey_DoNotDisplay, CFStringRef)
71 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleAttachmentKey_NotSync, CFStringRef)
72 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleBufferAttachmentKey_DrainAfterDecoding, CFStringRef)
73 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding, CFStringRef)
74 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleBufferAttachmentKey_EmptyMedia, CFStringRef)
75 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleBufferAttachmentKey_DisplayEmptyMediaImmediately, CFStringRef)
76
77 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicVisual, NSString*)
78 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicAudible, NSString*)
79 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicLegible, NSString*)
80
81 SOFT_LINK(CoreMedia, CMFormatDescriptionGetMediaType, CMMediaType, (CMFormatDescriptionRef desc), (desc))
82 SOFT_LINK(CoreMedia, CMSampleBufferCreate, OSStatus, (CFAllocatorRef allocator, CMBlockBufferRef dataBuffer, Boolean dataReady, CMSampleBufferMakeDataReadyCallback makeDataReadyCallback, void *makeDataReadyRefcon, CMFormatDescriptionRef formatDescription, CMItemCount numSamples, CMItemCount numSampleTimingEntries, const CMSampleTimingInfo *sampleTimingArray, CMItemCount numSampleSizeEntries, const size_t *sampleSizeArray, CMSampleBufferRef *sBufOut), (allocator, dataBuffer, dataReady, makeDataReadyCallback, makeDataReadyRefcon, formatDescription, numSamples, numSampleTimingEntries, sampleTimingArray, numSampleSizeEntries, sampleSizeArray, sBufOut))
83 SOFT_LINK(CoreMedia, CMSampleBufferCreateCopy, OSStatus, (CFAllocatorRef allocator, CMSampleBufferRef sbuf, CMSampleBufferRef *sbufCopyOut), (allocator, sbuf, sbufCopyOut))
84 SOFT_LINK(CoreMedia, CMSampleBufferCallForEachSample, OSStatus, (CMSampleBufferRef sbuf, OSStatus (*callback)( CMSampleBufferRef sampleBuffer, CMItemCount index, void *refcon), void *refcon), (sbuf, callback, refcon))
85 SOFT_LINK(CoreMedia, CMSampleBufferGetDecodeTimeStamp, CMTime, (CMSampleBufferRef sbuf), (sbuf))
86 SOFT_LINK(CoreMedia, CMSampleBufferGetDuration, CMTime, (CMSampleBufferRef sbuf), (sbuf))
87 SOFT_LINK(CoreMedia, CMSampleBufferGetPresentationTimeStamp, CMTime, (CMSampleBufferRef sbuf), (sbuf))
88 SOFT_LINK(CoreMedia, CMSampleBufferGetSampleAttachmentsArray, CFArrayRef, (CMSampleBufferRef sbuf, Boolean createIfNecessary), (sbuf, createIfNecessary))
89 SOFT_LINK(CoreMedia, CMFormatDescriptionGetMediaSubType, FourCharCode, (CMFormatDescriptionRef desc), (desc))
90 SOFT_LINK(CoreMedia, CMSetAttachment, void, (CMAttachmentBearerRef target, CFStringRef key, CFTypeRef value, CMAttachmentMode attachmentMode), (target, key, value, attachmentMode))
91
92 #define AVMediaTypeVideo getAVMediaTypeVideo()
93 #define AVMediaTypeAudio getAVMediaTypeAudio()
94 #define AVMediaTypeText getAVMediaTypeText()
95 #define kCMTimeZero getkCMTimeZero()
96 #define kCMTimeInvalid getkCMTimeInvalid()
97 #define kCMSampleAttachmentKey_NotSync getkCMSampleAttachmentKey_NotSync()
98 #define kCMSampleAttachmentKey_DoNotDisplay getkCMSampleAttachmentKey_DoNotDisplay()
99 #define kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding getkCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding()
100 #define kCMSampleBufferAttachmentKey_DrainAfterDecoding getkCMSampleBufferAttachmentKey_DrainAfterDecoding()
101 #define kCMSampleBufferAttachmentKey_EmptyMedia getkCMSampleBufferAttachmentKey_EmptyMedia()
102 #define kCMSampleBufferAttachmentKey_DisplayEmptyMediaImmediately getkCMSampleBufferAttachmentKey_DisplayEmptyMediaImmediately()
103
104 #define AVMediaCharacteristicVisual getAVMediaCharacteristicVisual()
105 #define AVMediaCharacteristicAudible getAVMediaCharacteristicAudible()
106 #define AVMediaCharacteristicLegible getAVMediaCharacteristicLegible()
107
108 #pragma mark -
109 #pragma mark AVStreamDataParser
110 @class AVStreamDataParserInternal;
111
112 NS_CLASS_AVAILABLE(TBD, TBD)
113 @interface AVStreamDataParser : NSObject
114 - (void)setDelegate:(id)delegate;
115 - (void)appendStreamData:(NSData *)data;
116 - (void)setShouldProvideMediaData:(BOOL)shouldProvideMediaData forTrackID:(CMPersistentTrackID)trackID;
117 - (BOOL)shouldProvideMediaDataForTrackID:(CMPersistentTrackID)trackID;
118 @end
119
120 #pragma mark -
121 #pragma mark WebAVStreamDataParserListener
122
123 @interface WebAVStreamDataParserListener : NSObject {
124     WebCore::SourceBufferPrivateAVFObjC* _parent;
125     AVStreamDataParser* _parser;
126 }
127 - (id)initWithParser:(AVStreamDataParser*)parser parent:(WebCore::SourceBufferPrivateAVFObjC*)parent;
128 @end
129
130 @implementation WebAVStreamDataParserListener
131 - (id)initWithParser:(AVStreamDataParser*)parser parent:(WebCore::SourceBufferPrivateAVFObjC*)parent
132 {
133     self = [super init];
134     if (!self)
135         return nil;
136
137     ASSERT(parent);
138     _parent = parent;
139     _parser = parser;
140     [_parser setDelegate:self];
141     return self;
142 }
143
144 - (void)dealloc
145 {
146     [_parser setDelegate:nil];
147     [super dealloc];
148 }
149
150 - (void)streamDataParser:(AVStreamDataParser *)streamDataParser didParseStreamDataAsAsset:(AVAsset *)asset
151 {
152 #if ASSERT_DISABLED
153     UNUSED_PARAM(streamDataParser);
154 #endif
155     ASSERT(streamDataParser == _parser);
156     _parent->didParseStreamDataAsAsset(asset);
157 }
158
159 - (void)streamDataParser:(AVStreamDataParser *)streamDataParser didFailToParseStreamDataWithError:(NSError *)error
160 {
161 #if ASSERT_DISABLED
162     UNUSED_PARAM(streamDataParser);
163 #endif
164     ASSERT(streamDataParser == _parser);
165     _parent->didFailToParseStreamDataWithError(error);
166 }
167
168 - (void)streamDataParser:(AVStreamDataParser *)streamDataParser didProvideMediaData:(CMSampleBufferRef)mediaData forTrackID:(CMPersistentTrackID)trackID mediaType:(NSString *)mediaType flags:(NSUInteger)flags
169 {
170 #if ASSERT_DISABLED
171     UNUSED_PARAM(streamDataParser);
172 #endif
173     ASSERT(streamDataParser == _parser);
174     _parent->didProvideMediaDataForTrackID(trackID, mediaData, mediaType, flags);
175 }
176
177 - (void)streamDataParser:(AVStreamDataParser *)streamDataParser didReachEndOfTrackWithTrackID:(CMPersistentTrackID)trackID mediaType:(NSString *)mediaType
178 {
179 #if ASSERT_DISABLED
180     UNUSED_PARAM(streamDataParser);
181 #endif
182     ASSERT(streamDataParser == _parser);
183     _parent->didReachEndOfTrackWithTrackID(trackID, mediaType);
184 }
185 @end
186
187 namespace WebCore {
188
189 #pragma mark -
190 #pragma mark MediaSampleAVFObjC
191
192 class MediaSampleAVFObjC FINAL : public MediaSample {
193 public:
194     static RefPtr<MediaSampleAVFObjC> create(CMSampleBufferRef sample, int trackID) { return adoptRef(new MediaSampleAVFObjC(sample, trackID)); }
195     virtual ~MediaSampleAVFObjC() { }
196
197     virtual MediaTime presentationTime() const OVERRIDE { return toMediaTime(CMSampleBufferGetPresentationTimeStamp(m_sample.get())); }
198     virtual MediaTime decodeTime() const OVERRIDE { return toMediaTime(CMSampleBufferGetDecodeTimeStamp(m_sample.get())); }
199     virtual MediaTime duration() const OVERRIDE { return toMediaTime(CMSampleBufferGetDuration(m_sample.get())); }
200     virtual AtomicString trackID() const OVERRIDE { return m_id; }
201
202     virtual SampleFlags flags() const OVERRIDE;
203     virtual PlatformSample platformSample() OVERRIDE;
204
205 protected:
206     MediaSampleAVFObjC(CMSampleBufferRef sample, int trackID)
207         : m_sample(sample)
208         , m_id(String::format("%d", trackID))
209     {
210     }
211
212     RetainPtr<CMSampleBufferRef> m_sample;
213     AtomicString m_id;
214 };
215
216 PlatformSample MediaSampleAVFObjC::platformSample()
217 {
218     PlatformSample sample = { PlatformSample::CMSampleBufferType, { .cmSampleBuffer = m_sample.get() } };
219     return sample;
220 }
221
222 static bool CMSampleBufferIsRandomAccess(CMSampleBufferRef sample)
223 {
224     CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sample, false);
225     if (!attachments)
226         return true;
227
228     for (CFIndex i = 0, count = CFArrayGetCount(attachments); i < count; ++i) {
229         CFDictionaryRef attachmentDict = (CFDictionaryRef)CFArrayGetValueAtIndex(attachments, i);
230         if (CFDictionaryContainsKey(attachmentDict, kCMSampleAttachmentKey_NotSync))
231             return false;
232     }
233     return true;
234 }
235
236 MediaSample::SampleFlags MediaSampleAVFObjC::flags() const
237 {
238     int returnValue = MediaSample::None;
239
240     if (CMSampleBufferIsRandomAccess(m_sample.get()))
241         returnValue |= MediaSample::IsSync;
242
243     return SampleFlags(returnValue);
244 }
245
246 #pragma mark -
247 #pragma mark MediaDescriptionAVFObjC
248
249 class MediaDescriptionAVFObjC FINAL : public MediaDescription {
250 public:
251     static RefPtr<MediaDescriptionAVFObjC> create(AVAssetTrack* track) { return adoptRef(new MediaDescriptionAVFObjC(track)); }
252     virtual ~MediaDescriptionAVFObjC() { }
253
254     virtual AtomicString codec() const OVERRIDE { return m_codec; }
255     virtual bool isVideo() const OVERRIDE { return m_isVideo; }
256     virtual bool isAudio() const OVERRIDE { return m_isAudio; }
257     virtual bool isText() const OVERRIDE { return m_isText; }
258     
259 protected:
260     MediaDescriptionAVFObjC(AVAssetTrack* track)
261         : m_isVideo([track hasMediaCharacteristic:AVMediaCharacteristicVisual])
262         , m_isAudio([track hasMediaCharacteristic:AVMediaCharacteristicAudible])
263         , m_isText([track hasMediaCharacteristic:AVMediaCharacteristicLegible])
264     {
265         NSArray* formatDescriptions = [track formatDescriptions];
266         CMFormatDescriptionRef description = [formatDescriptions count] ? (CMFormatDescriptionRef)[formatDescriptions objectAtIndex:0] : 0;
267         if (description) {
268             FourCharCode codec = CMFormatDescriptionGetMediaSubType(description);
269             m_codec = AtomicString(reinterpret_cast<LChar*>(&codec), 4);
270         }
271     }
272
273     AtomicString m_codec;
274     bool m_isVideo;
275     bool m_isAudio;
276     bool m_isText;
277 };
278
279 #pragma mark -
280 #pragma mark SourceBufferPrivateAVFObjC
281
282 RefPtr<SourceBufferPrivateAVFObjC> SourceBufferPrivateAVFObjC::create(MediaSourcePrivateAVFObjC* parent)
283 {
284     return adoptRef(new SourceBufferPrivateAVFObjC(parent));
285 }
286
287 SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC(MediaSourcePrivateAVFObjC* parent)
288     : m_parser(adoptNS([[getAVStreamDataParserClass() alloc] init]))
289     , m_delegate(adoptNS([[WebAVStreamDataParserListener alloc] initWithParser:m_parser.get() parent:this]))
290     , m_mediaSource(parent)
291     , m_client(0)
292     , m_parsingSucceeded(true)
293 {
294 }
295
296 SourceBufferPrivateAVFObjC::~SourceBufferPrivateAVFObjC()
297 {
298     if (m_displayLayer) {
299         if (m_mediaSource)
300             m_mediaSource->player()->removeDisplayLayer(m_displayLayer.get());
301         [m_displayLayer flushAndRemoveImage];
302         [m_displayLayer stopRequestingMediaData];
303         m_displayLayer = nullptr;
304     }
305 }
306
307 void SourceBufferPrivateAVFObjC::didParseStreamDataAsAsset(AVAsset* asset)
308 {
309     LOG(Media, "SourceBufferPrivateAVFObjC::didParseStreamDataAsAsset(%p)", this);
310
311     m_asset = asset;
312
313     SourceBufferPrivateClient::InitializationSegment segment;
314     segment.duration = toMediaTime([m_asset duration]);
315
316     for (AVAssetTrack* track in [m_asset tracks]) {
317         if ([track hasMediaCharacteristic:AVMediaCharacteristicVisual]) {
318             SourceBufferPrivateClient::InitializationSegment::VideoTrackInformation info;
319             info.track = VideoTrackPrivateMediaSourceAVFObjC::create(track, this);
320             info.description = MediaDescriptionAVFObjC::create(track);
321             segment.videoTracks.append(info);
322         } else if ([track hasMediaCharacteristic:AVMediaCharacteristicAudible]) {
323             SourceBufferPrivateClient::InitializationSegment::AudioTrackInformation info;
324             info.track = AudioTrackPrivateMediaSourceAVFObjC::create(track, this);
325             info.description = MediaDescriptionAVFObjC::create(track);
326             segment.audioTracks.append(info);
327         }
328
329         // FIXME(125161): Add TextTrack support
330     }
331
332     if (m_client)
333         m_client->sourceBufferPrivateDidReceiveInitializationSegment(this, segment);
334 }
335
336 void SourceBufferPrivateAVFObjC::didFailToParseStreamDataWithError(NSError* error)
337 {
338 #if LOG_DISABLED
339     UNUSED_PARAM(error);
340 #endif
341     LOG(Media, "SourceBufferPrivateAVFObjC::didFailToParseStreamDataWithError(%p) - error:\"%s\"", this, String([error description]).utf8().data());
342
343     m_parsingSucceeded = false;
344 }
345
346 struct ProcessCodedFrameInfo {
347     SourceBufferPrivateAVFObjC* sourceBuffer;
348     int trackID;
349     const String& mediaType;
350 };
351
352 static OSStatus callProcessCodedFrameForEachSample(CMSampleBufferRef sampleBuffer, CMItemCount, void *refcon)
353 {
354     ProcessCodedFrameInfo* info = static_cast<ProcessCodedFrameInfo*>(refcon);
355     return info->sourceBuffer->processCodedFrame(info->trackID, sampleBuffer, info->mediaType);
356 }
357
358 void SourceBufferPrivateAVFObjC::didProvideMediaDataForTrackID(int trackID, CMSampleBufferRef sampleBuffer, const String& mediaType, unsigned flags)
359 {
360     UNUSED_PARAM(flags);
361
362     ProcessCodedFrameInfo info = {this, trackID, mediaType};
363     CMSampleBufferCallForEachSample(sampleBuffer, &callProcessCodedFrameForEachSample, &info);
364 }
365
366 bool SourceBufferPrivateAVFObjC::processCodedFrame(int trackID, CMSampleBufferRef sampleBuffer, const String&)
367 {
368     if (m_client)
369         m_client->sourceBufferPrivateDidReceiveSample(this, MediaSampleAVFObjC::create(sampleBuffer, trackID));
370
371     return true;
372 }
373
374 void SourceBufferPrivateAVFObjC::didReachEndOfTrackWithTrackID(int trackID, const String& mediaType)
375 {
376     UNUSED_PARAM(mediaType);
377     UNUSED_PARAM(trackID);
378     notImplemented();
379 }
380
381 void SourceBufferPrivateAVFObjC::setClient(SourceBufferPrivateClient* client)
382 {
383     m_client = client;
384 }
385
386 SourceBufferPrivate::AppendResult SourceBufferPrivateAVFObjC::append(const unsigned char* data, unsigned length)
387 {
388     m_parsingSucceeded = true;
389
390     LOG(Media, "SourceBufferPrivateAVFObjC::append(%p) - data:%p, length:%d", this, data, length);
391     [m_parser appendStreamData:[NSData dataWithBytes:data length:length]];
392
393     if (m_parsingSucceeded && m_mediaSource)
394         m_mediaSource->player()->setLoadingProgresssed(true);
395
396     return m_parsingSucceeded ? AppendSucceeded : ParsingFailed;
397 }
398
399 void SourceBufferPrivateAVFObjC::abort()
400 {
401     notImplemented();
402 }
403
404 void SourceBufferPrivateAVFObjC::removedFromMediaSource()
405 {
406     if (m_displayLayer) {
407         if (m_mediaSource)
408             m_mediaSource->player()->removeDisplayLayer(m_displayLayer.get());
409         [m_displayLayer flush];
410         [m_displayLayer stopRequestingMediaData];
411         m_displayLayer = nullptr;
412     }
413
414     if (m_mediaSource)
415         m_mediaSource->removeSourceBuffer(this);
416 }
417
418 MediaPlayer::ReadyState SourceBufferPrivateAVFObjC::readyState() const
419 {
420     return m_mediaSource ? m_mediaSource->player()->readyState() : MediaPlayer::HaveNothing;
421 }
422
423 void SourceBufferPrivateAVFObjC::setReadyState(MediaPlayer::ReadyState readyState)
424 {
425     if (m_mediaSource)
426         m_mediaSource->player()->setReadyState(readyState);
427 }
428
429 void SourceBufferPrivateAVFObjC::evictCodedFrames()
430 {
431     notImplemented();
432 }
433
434 bool SourceBufferPrivateAVFObjC::isFull()
435 {
436     notImplemented();
437     return false;
438 }
439
440
441 bool SourceBufferPrivateAVFObjC::hasVideo() const
442 {
443     if (!m_client)
444         return false;
445
446     return m_client->sourceBufferPrivateHasVideo(this);
447 }
448
449 bool SourceBufferPrivateAVFObjC::hasAudio() const
450 {
451     if (!m_client)
452         return false;
453
454     return m_client->sourceBufferPrivateHasAudio(this);
455 }
456
457 void SourceBufferPrivateAVFObjC::trackDidChangeEnabled(VideoTrackPrivateMediaSourceAVFObjC* track)
458 {
459     int trackID = track->trackID();
460     if (!track->selected() && m_enabledVideoTrackID == trackID) {
461         m_enabledVideoTrackID = -1;
462         [m_parser setShouldProvideMediaData:NO forTrackID:trackID];
463         if (m_mediaSource)
464             m_mediaSource->player()->removeDisplayLayer(m_displayLayer.get());
465     } else if (track->selected()) {
466         m_enabledVideoTrackID = trackID;
467         [m_parser setShouldProvideMediaData:YES forTrackID:trackID];
468         if (!m_displayLayer) {
469             m_displayLayer = [[getAVSampleBufferDisplayLayerClass() alloc] init];
470             [m_displayLayer requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
471                 if (m_client)
472                     m_client->sourceBufferPrivateDidBecomeReadyForMoreSamples(this, AtomicString::number(trackID));
473             }];
474             if (m_mediaSource)
475                 m_mediaSource->player()->addDisplayLayer(m_displayLayer.get());
476         }
477     }
478 }
479
480 void SourceBufferPrivateAVFObjC::trackDidChangeEnabled(AudioTrackPrivateMediaSourceAVFObjC*)
481 {
482     // No-op.
483 }
484
485 static RetainPtr<CMSampleBufferRef> createNonDisplayingCopy(CMSampleBufferRef sampleBuffer)
486 {
487     CMSampleBufferRef newSampleBuffer = 0;
488     CMSampleBufferCreateCopy(kCFAllocatorDefault, sampleBuffer, &newSampleBuffer);
489     if (!newSampleBuffer)
490         return sampleBuffer;
491
492     CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(newSampleBuffer, true);
493     for (CFIndex i = 0; i < CFArrayGetCount(attachmentsArray); ++i) {
494         CFMutableDictionaryRef attachments = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachmentsArray, i);
495         CFDictionarySetValue(attachments, kCMSampleAttachmentKey_DoNotDisplay, kCFBooleanTrue);
496     }
497
498     return adoptCF(newSampleBuffer);
499 }
500
501 void SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>> mediaSamples, AtomicString trackID)
502 {
503     if (trackID.toInt() != m_enabledVideoTrackID)
504         return;
505
506     LOG(Media, "SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples(%p) samples: %d samples, trackId: %d", this, mediaSamples.size(), trackID.toInt());
507
508     [m_displayLayer flush];
509
510     for (auto it = mediaSamples.begin(), end = mediaSamples.end(); it != end; ++it) {
511         RefPtr<MediaSample>& mediaSample = *it;
512
513         PlatformSample platformSample = mediaSample->platformSample();
514         ASSERT(platformSample.type == PlatformSample::CMSampleBufferType);
515
516         RetainPtr<CMSampleBufferRef> sampleBuffer = createNonDisplayingCopy(platformSample.sample.cmSampleBuffer);
517
518         [m_displayLayer enqueueSampleBuffer:sampleBuffer.get()];
519     }
520
521     if (m_mediaSource)
522         m_mediaSource->player()->setHasAvailableVideoFrame(false);
523 }
524
525 void SourceBufferPrivateAVFObjC::enqueueSample(PassRefPtr<MediaSample> prpMediaSample, AtomicString trackID)
526 {
527     if (trackID.toInt() != m_enabledVideoTrackID)
528         return;
529
530     RefPtr<MediaSample> mediaSample = prpMediaSample;
531
532     PlatformSample platformSample = mediaSample->platformSample();
533     if (platformSample.type != PlatformSample::CMSampleBufferType)
534         return;
535
536     [m_displayLayer enqueueSampleBuffer:platformSample.sample.cmSampleBuffer];
537     if (m_mediaSource)
538         m_mediaSource->player()->setHasAvailableVideoFrame(true);
539 }
540
541 bool SourceBufferPrivateAVFObjC::isReadyForMoreSamples(AtomicString trackID)
542 {
543     UNUSED_PARAM(trackID);
544     return [m_displayLayer isReadyForMoreMediaData];
545 }
546
547 void SourceBufferPrivateAVFObjC::setActive(bool isActive)
548 {
549     if (m_mediaSource)
550         m_mediaSource->sourceBufferPrivateDidChangeActiveState(this, isActive);
551 }
552
553 MediaTime SourceBufferPrivateAVFObjC::fastSeekTimeForMediaTime(MediaTime time, MediaTime negativeThreshold, MediaTime positiveThreshold)
554 {
555     if (m_client)
556         return m_client->sourceBufferPrivateFastSeekTimeForMediaTime(this, time, negativeThreshold, positiveThreshold);
557     return time;
558 }
559
560 void SourceBufferPrivateAVFObjC::seekToTime(MediaTime time)
561 {
562     if (m_client)
563         m_client->sourceBufferPrivateSeekToTime(this, time);
564 }
565
566 }
567
568 #endif // ENABLE(MEDIA_SOURCE) && USE(AVFOUNDATION)