[MSE][Mac] Add AVSampleBufferAudioRenderer support.
[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, AVSampleBufferAudioRenderer)
63 SOFT_LINK_CLASS(AVFoundation, AVSampleBufferDisplayLayer)
64
65 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaTypeVideo, NSString *)
66 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaTypeAudio, NSString *)
67 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVMediaTypeText, NSString *)
68
69 SOFT_LINK_CONSTANT(CoreMedia, kCMTimeZero, CMTime);
70 SOFT_LINK_CONSTANT(CoreMedia, kCMTimeInvalid, CMTime);
71 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleAttachmentKey_DoNotDisplay, CFStringRef)
72 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleAttachmentKey_NotSync, CFStringRef)
73 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleBufferAttachmentKey_DrainAfterDecoding, CFStringRef)
74 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding, CFStringRef)
75 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleBufferAttachmentKey_EmptyMedia, CFStringRef)
76 SOFT_LINK_CONSTANT(CoreMedia, kCMSampleBufferAttachmentKey_DisplayEmptyMediaImmediately, CFStringRef)
77
78 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicVisual, NSString*)
79 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicAudible, NSString*)
80 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicLegible, NSString*)
81
82 SOFT_LINK(CoreMedia, CMFormatDescriptionGetMediaType, CMMediaType, (CMFormatDescriptionRef desc), (desc))
83 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))
84 SOFT_LINK(CoreMedia, CMSampleBufferCreateCopy, OSStatus, (CFAllocatorRef allocator, CMSampleBufferRef sbuf, CMSampleBufferRef *sbufCopyOut), (allocator, sbuf, sbufCopyOut))
85 SOFT_LINK(CoreMedia, CMSampleBufferCallForEachSample, OSStatus, (CMSampleBufferRef sbuf, OSStatus (*callback)( CMSampleBufferRef sampleBuffer, CMItemCount index, void *refcon), void *refcon), (sbuf, callback, refcon))
86 SOFT_LINK(CoreMedia, CMSampleBufferGetDecodeTimeStamp, CMTime, (CMSampleBufferRef sbuf), (sbuf))
87 SOFT_LINK(CoreMedia, CMSampleBufferGetDuration, CMTime, (CMSampleBufferRef sbuf), (sbuf))
88 SOFT_LINK(CoreMedia, CMSampleBufferGetPresentationTimeStamp, CMTime, (CMSampleBufferRef sbuf), (sbuf))
89 SOFT_LINK(CoreMedia, CMSampleBufferGetSampleAttachmentsArray, CFArrayRef, (CMSampleBufferRef sbuf, Boolean createIfNecessary), (sbuf, createIfNecessary))
90 SOFT_LINK(CoreMedia, CMFormatDescriptionGetMediaSubType, FourCharCode, (CMFormatDescriptionRef desc), (desc))
91 SOFT_LINK(CoreMedia, CMSetAttachment, void, (CMAttachmentBearerRef target, CFStringRef key, CFTypeRef value, CMAttachmentMode attachmentMode), (target, key, value, attachmentMode))
92
93 #define AVMediaTypeVideo getAVMediaTypeVideo()
94 #define AVMediaTypeAudio getAVMediaTypeAudio()
95 #define AVMediaTypeText getAVMediaTypeText()
96 #define kCMTimeZero getkCMTimeZero()
97 #define kCMTimeInvalid getkCMTimeInvalid()
98 #define kCMSampleAttachmentKey_NotSync getkCMSampleAttachmentKey_NotSync()
99 #define kCMSampleAttachmentKey_DoNotDisplay getkCMSampleAttachmentKey_DoNotDisplay()
100 #define kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding getkCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding()
101 #define kCMSampleBufferAttachmentKey_DrainAfterDecoding getkCMSampleBufferAttachmentKey_DrainAfterDecoding()
102 #define kCMSampleBufferAttachmentKey_EmptyMedia getkCMSampleBufferAttachmentKey_EmptyMedia()
103 #define kCMSampleBufferAttachmentKey_DisplayEmptyMediaImmediately getkCMSampleBufferAttachmentKey_DisplayEmptyMediaImmediately()
104
105 #define AVMediaCharacteristicVisual getAVMediaCharacteristicVisual()
106 #define AVMediaCharacteristicAudible getAVMediaCharacteristicAudible()
107 #define AVMediaCharacteristicLegible getAVMediaCharacteristicLegible()
108
109 #pragma mark -
110 #pragma mark AVStreamDataParser
111
112 @interface AVStreamDataParser : NSObject
113 - (void)setDelegate:(id)delegate;
114 - (void)appendStreamData:(NSData *)data;
115 - (void)setShouldProvideMediaData:(BOOL)shouldProvideMediaData forTrackID:(CMPersistentTrackID)trackID;
116 - (BOOL)shouldProvideMediaDataForTrackID:(CMPersistentTrackID)trackID;
117 @end
118
119 #pragma mark -
120 #pragma mark AVSampleBufferAudioRenderer
121
122 #if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1090
123 @interface AVSampleBufferAudioRenderer : NSObject
124 - (NSInteger)status;
125 - (NSError*)error;
126 - (void)enqueueSampleBuffer:(CMSampleBufferRef)sampleBuffer;
127 - (void)flush;
128 - (BOOL)isReadyForMoreMediaData;
129 - (void)requestMediaDataWhenReadyOnQueue:(dispatch_queue_t)queue usingBlock:(void (^)(void))block;
130 - (void)stopRequestingMediaData;
131 @end
132 #endif
133
134 #pragma mark -
135 #pragma mark WebAVStreamDataParserListener
136
137 @interface WebAVStreamDataParserListener : NSObject {
138     WebCore::SourceBufferPrivateAVFObjC* _parent;
139     AVStreamDataParser* _parser;
140 }
141 - (id)initWithParser:(AVStreamDataParser*)parser parent:(WebCore::SourceBufferPrivateAVFObjC*)parent;
142 @end
143
144 @implementation WebAVStreamDataParserListener
145 - (id)initWithParser:(AVStreamDataParser*)parser parent:(WebCore::SourceBufferPrivateAVFObjC*)parent
146 {
147     self = [super init];
148     if (!self)
149         return nil;
150
151     ASSERT(parent);
152     _parent = parent;
153     _parser = parser;
154     [_parser setDelegate:self];
155     return self;
156 }
157
158 - (void)dealloc
159 {
160     [_parser setDelegate:nil];
161     [super dealloc];
162 }
163
164 - (void)streamDataParser:(AVStreamDataParser *)streamDataParser didParseStreamDataAsAsset:(AVAsset *)asset
165 {
166 #if ASSERT_DISABLED
167     UNUSED_PARAM(streamDataParser);
168 #endif
169     ASSERT(streamDataParser == _parser);
170     _parent->didParseStreamDataAsAsset(asset);
171 }
172
173 - (void)streamDataParser:(AVStreamDataParser *)streamDataParser didFailToParseStreamDataWithError:(NSError *)error
174 {
175 #if ASSERT_DISABLED
176     UNUSED_PARAM(streamDataParser);
177 #endif
178     ASSERT(streamDataParser == _parser);
179     _parent->didFailToParseStreamDataWithError(error);
180 }
181
182 - (void)streamDataParser:(AVStreamDataParser *)streamDataParser didProvideMediaData:(CMSampleBufferRef)mediaData forTrackID:(CMPersistentTrackID)trackID mediaType:(NSString *)mediaType flags:(NSUInteger)flags
183 {
184 #if ASSERT_DISABLED
185     UNUSED_PARAM(streamDataParser);
186 #endif
187     ASSERT(streamDataParser == _parser);
188     _parent->didProvideMediaDataForTrackID(trackID, mediaData, mediaType, flags);
189 }
190
191 - (void)streamDataParser:(AVStreamDataParser *)streamDataParser didReachEndOfTrackWithTrackID:(CMPersistentTrackID)trackID mediaType:(NSString *)mediaType
192 {
193 #if ASSERT_DISABLED
194     UNUSED_PARAM(streamDataParser);
195 #endif
196     ASSERT(streamDataParser == _parser);
197     _parent->didReachEndOfTrackWithTrackID(trackID, mediaType);
198 }
199 @end
200
201 namespace WebCore {
202
203 #pragma mark -
204 #pragma mark MediaSampleAVFObjC
205
206 class MediaSampleAVFObjC FINAL : public MediaSample {
207 public:
208     static RefPtr<MediaSampleAVFObjC> create(CMSampleBufferRef sample, int trackID) { return adoptRef(new MediaSampleAVFObjC(sample, trackID)); }
209     virtual ~MediaSampleAVFObjC() { }
210
211     virtual MediaTime presentationTime() const OVERRIDE { return toMediaTime(CMSampleBufferGetPresentationTimeStamp(m_sample.get())); }
212     virtual MediaTime decodeTime() const OVERRIDE { return toMediaTime(CMSampleBufferGetDecodeTimeStamp(m_sample.get())); }
213     virtual MediaTime duration() const OVERRIDE { return toMediaTime(CMSampleBufferGetDuration(m_sample.get())); }
214     virtual AtomicString trackID() const OVERRIDE { return m_id; }
215
216     virtual SampleFlags flags() const OVERRIDE;
217     virtual PlatformSample platformSample() OVERRIDE;
218
219 protected:
220     MediaSampleAVFObjC(CMSampleBufferRef sample, int trackID)
221         : m_sample(sample)
222         , m_id(String::format("%d", trackID))
223     {
224     }
225
226     RetainPtr<CMSampleBufferRef> m_sample;
227     AtomicString m_id;
228 };
229
230 PlatformSample MediaSampleAVFObjC::platformSample()
231 {
232     PlatformSample sample = { PlatformSample::CMSampleBufferType, { .cmSampleBuffer = m_sample.get() } };
233     return sample;
234 }
235
236 static bool CMSampleBufferIsRandomAccess(CMSampleBufferRef sample)
237 {
238     CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sample, false);
239     if (!attachments)
240         return true;
241
242     for (CFIndex i = 0, count = CFArrayGetCount(attachments); i < count; ++i) {
243         CFDictionaryRef attachmentDict = (CFDictionaryRef)CFArrayGetValueAtIndex(attachments, i);
244         if (CFDictionaryContainsKey(attachmentDict, kCMSampleAttachmentKey_NotSync))
245             return false;
246     }
247     return true;
248 }
249
250 MediaSample::SampleFlags MediaSampleAVFObjC::flags() const
251 {
252     int returnValue = MediaSample::None;
253
254     if (CMSampleBufferIsRandomAccess(m_sample.get()))
255         returnValue |= MediaSample::IsSync;
256
257     return SampleFlags(returnValue);
258 }
259
260 #pragma mark -
261 #pragma mark MediaDescriptionAVFObjC
262
263 class MediaDescriptionAVFObjC FINAL : public MediaDescription {
264 public:
265     static RefPtr<MediaDescriptionAVFObjC> create(AVAssetTrack* track) { return adoptRef(new MediaDescriptionAVFObjC(track)); }
266     virtual ~MediaDescriptionAVFObjC() { }
267
268     virtual AtomicString codec() const OVERRIDE { return m_codec; }
269     virtual bool isVideo() const OVERRIDE { return m_isVideo; }
270     virtual bool isAudio() const OVERRIDE { return m_isAudio; }
271     virtual bool isText() const OVERRIDE { return m_isText; }
272     
273 protected:
274     MediaDescriptionAVFObjC(AVAssetTrack* track)
275         : m_isVideo([track hasMediaCharacteristic:AVMediaCharacteristicVisual])
276         , m_isAudio([track hasMediaCharacteristic:AVMediaCharacteristicAudible])
277         , m_isText([track hasMediaCharacteristic:AVMediaCharacteristicLegible])
278     {
279         NSArray* formatDescriptions = [track formatDescriptions];
280         CMFormatDescriptionRef description = [formatDescriptions count] ? (CMFormatDescriptionRef)[formatDescriptions objectAtIndex:0] : 0;
281         if (description) {
282             FourCharCode codec = CMFormatDescriptionGetMediaSubType(description);
283             m_codec = AtomicString(reinterpret_cast<LChar*>(&codec), 4);
284         }
285     }
286
287     AtomicString m_codec;
288     bool m_isVideo;
289     bool m_isAudio;
290     bool m_isText;
291 };
292
293 #pragma mark -
294 #pragma mark SourceBufferPrivateAVFObjC
295
296 RefPtr<SourceBufferPrivateAVFObjC> SourceBufferPrivateAVFObjC::create(MediaSourcePrivateAVFObjC* parent)
297 {
298     return adoptRef(new SourceBufferPrivateAVFObjC(parent));
299 }
300
301 SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC(MediaSourcePrivateAVFObjC* parent)
302     : m_parser(adoptNS([[getAVStreamDataParserClass() alloc] init]))
303     , m_delegate(adoptNS([[WebAVStreamDataParserListener alloc] initWithParser:m_parser.get() parent:this]))
304     , m_mediaSource(parent)
305     , m_client(0)
306     , m_parsingSucceeded(true)
307     , m_enabledVideoTrackID(-1)
308 {
309 }
310
311 SourceBufferPrivateAVFObjC::~SourceBufferPrivateAVFObjC()
312 {
313     destroyRenderers();
314 }
315
316 void SourceBufferPrivateAVFObjC::didParseStreamDataAsAsset(AVAsset* asset)
317 {
318     LOG(Media, "SourceBufferPrivateAVFObjC::didParseStreamDataAsAsset(%p)", this);
319
320     m_asset = asset;
321
322     SourceBufferPrivateClient::InitializationSegment segment;
323     segment.duration = toMediaTime([m_asset duration]);
324
325     for (AVAssetTrack* track in [m_asset tracks]) {
326         if ([track hasMediaCharacteristic:AVMediaCharacteristicVisual]) {
327             SourceBufferPrivateClient::InitializationSegment::VideoTrackInformation info;
328             info.track = VideoTrackPrivateMediaSourceAVFObjC::create(track, this);
329             info.description = MediaDescriptionAVFObjC::create(track);
330             segment.videoTracks.append(info);
331         } else if ([track hasMediaCharacteristic:AVMediaCharacteristicAudible]) {
332             SourceBufferPrivateClient::InitializationSegment::AudioTrackInformation info;
333             info.track = AudioTrackPrivateMediaSourceAVFObjC::create(track, this);
334             info.description = MediaDescriptionAVFObjC::create(track);
335             segment.audioTracks.append(info);
336         }
337
338         // FIXME(125161): Add TextTrack support
339     }
340
341     if (m_client)
342         m_client->sourceBufferPrivateDidReceiveInitializationSegment(this, segment);
343 }
344
345 void SourceBufferPrivateAVFObjC::didFailToParseStreamDataWithError(NSError* error)
346 {
347 #if LOG_DISABLED
348     UNUSED_PARAM(error);
349 #endif
350     LOG(Media, "SourceBufferPrivateAVFObjC::didFailToParseStreamDataWithError(%p) - error:\"%s\"", this, String([error description]).utf8().data());
351
352     m_parsingSucceeded = false;
353 }
354
355 struct ProcessCodedFrameInfo {
356     SourceBufferPrivateAVFObjC* sourceBuffer;
357     int trackID;
358     const String& mediaType;
359 };
360
361 static OSStatus callProcessCodedFrameForEachSample(CMSampleBufferRef sampleBuffer, CMItemCount, void *refcon)
362 {
363     ProcessCodedFrameInfo* info = static_cast<ProcessCodedFrameInfo*>(refcon);
364     return info->sourceBuffer->processCodedFrame(info->trackID, sampleBuffer, info->mediaType) ? noErr : paramErr;
365 }
366
367 void SourceBufferPrivateAVFObjC::didProvideMediaDataForTrackID(int trackID, CMSampleBufferRef sampleBuffer, const String& mediaType, unsigned flags)
368 {
369     UNUSED_PARAM(flags);
370
371     ProcessCodedFrameInfo info = {this, trackID, mediaType};
372     CMSampleBufferCallForEachSample(sampleBuffer, &callProcessCodedFrameForEachSample, &info);
373 }
374
375 bool SourceBufferPrivateAVFObjC::processCodedFrame(int trackID, CMSampleBufferRef sampleBuffer, const String&)
376 {
377     if (m_client)
378         m_client->sourceBufferPrivateDidReceiveSample(this, MediaSampleAVFObjC::create(sampleBuffer, trackID));
379
380     return true;
381 }
382
383 void SourceBufferPrivateAVFObjC::didReachEndOfTrackWithTrackID(int trackID, const String& mediaType)
384 {
385     UNUSED_PARAM(mediaType);
386     UNUSED_PARAM(trackID);
387     notImplemented();
388 }
389
390 void SourceBufferPrivateAVFObjC::setClient(SourceBufferPrivateClient* client)
391 {
392     m_client = client;
393 }
394
395 SourceBufferPrivate::AppendResult SourceBufferPrivateAVFObjC::append(const unsigned char* data, unsigned length)
396 {
397     m_parsingSucceeded = true;
398
399     LOG(Media, "SourceBufferPrivateAVFObjC::append(%p) - data:%p, length:%d", this, data, length);
400     [m_parser appendStreamData:[NSData dataWithBytes:data length:length]];
401
402     if (m_parsingSucceeded && m_mediaSource)
403         m_mediaSource->player()->setLoadingProgresssed(true);
404
405     return m_parsingSucceeded ? AppendSucceeded : ParsingFailed;
406 }
407
408 void SourceBufferPrivateAVFObjC::abort()
409 {
410     notImplemented();
411 }
412
413 void SourceBufferPrivateAVFObjC::destroyRenderers()
414 {
415     if (m_displayLayer) {
416         if (m_mediaSource)
417             m_mediaSource->player()->removeDisplayLayer(m_displayLayer.get());
418         [m_displayLayer flush];
419         [m_displayLayer stopRequestingMediaData];
420         m_displayLayer = nullptr;
421     }
422
423     for (auto it = m_audioRenderers.begin(), end = m_audioRenderers.end(); it != end; ++it) {
424         AVSampleBufferAudioRenderer* renderer = it->second.get();
425         if (m_mediaSource)
426             m_mediaSource->player()->removeAudioRenderer(renderer);
427         [renderer flush];
428         [renderer stopRequestingMediaData];
429     }
430
431     m_audioRenderers.clear();
432 }
433
434 void SourceBufferPrivateAVFObjC::removedFromMediaSource()
435 {
436     destroyRenderers();
437
438     if (m_mediaSource)
439         m_mediaSource->removeSourceBuffer(this);
440 }
441
442 MediaPlayer::ReadyState SourceBufferPrivateAVFObjC::readyState() const
443 {
444     return m_mediaSource ? m_mediaSource->player()->readyState() : MediaPlayer::HaveNothing;
445 }
446
447 void SourceBufferPrivateAVFObjC::setReadyState(MediaPlayer::ReadyState readyState)
448 {
449     if (m_mediaSource)
450         m_mediaSource->player()->setReadyState(readyState);
451 }
452
453 void SourceBufferPrivateAVFObjC::evictCodedFrames()
454 {
455     notImplemented();
456 }
457
458 bool SourceBufferPrivateAVFObjC::isFull()
459 {
460     notImplemented();
461     return false;
462 }
463
464
465 bool SourceBufferPrivateAVFObjC::hasVideo() const
466 {
467     if (!m_client)
468         return false;
469
470     return m_client->sourceBufferPrivateHasVideo(this);
471 }
472
473 bool SourceBufferPrivateAVFObjC::hasAudio() const
474 {
475     if (!m_client)
476         return false;
477
478     return m_client->sourceBufferPrivateHasAudio(this);
479 }
480
481 void SourceBufferPrivateAVFObjC::trackDidChangeEnabled(VideoTrackPrivateMediaSourceAVFObjC* track)
482 {
483     int trackID = track->trackID();
484     if (!track->selected() && m_enabledVideoTrackID == trackID) {
485         m_enabledVideoTrackID = -1;
486         [m_parser setShouldProvideMediaData:NO forTrackID:trackID];
487         if (m_mediaSource)
488             m_mediaSource->player()->removeDisplayLayer(m_displayLayer.get());
489     } else if (track->selected()) {
490         m_enabledVideoTrackID = trackID;
491         [m_parser setShouldProvideMediaData:YES forTrackID:trackID];
492         if (!m_displayLayer) {
493             m_displayLayer = [[getAVSampleBufferDisplayLayerClass() alloc] init];
494             [m_displayLayer requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
495                 didBecomeReadyForMoreSamples(trackID);
496             }];
497         }
498         if (m_mediaSource)
499             m_mediaSource->player()->addDisplayLayer(m_displayLayer.get());
500     }
501 }
502
503 void SourceBufferPrivateAVFObjC::trackDidChangeEnabled(AudioTrackPrivateMediaSourceAVFObjC* track)
504 {
505     int trackID = track->trackID();
506
507     if (!track->enabled()) {
508         AVSampleBufferAudioRenderer* renderer = m_audioRenderers[trackID].get();
509         [m_parser setShouldProvideMediaData:NO forTrackID:trackID];
510         if (m_mediaSource)
511             m_mediaSource->player()->removeAudioRenderer(renderer);
512     } else {
513         [m_parser setShouldProvideMediaData:YES forTrackID:trackID];
514         AVSampleBufferAudioRenderer* renderer;
515         if (!m_audioRenderers.count(trackID)) {
516             renderer = [[getAVSampleBufferAudioRendererClass() alloc] init];
517             [renderer requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
518                 didBecomeReadyForMoreSamples(trackID);
519             }];
520             m_audioRenderers[trackID] = renderer;
521         } else
522             renderer = m_audioRenderers[trackID].get();
523
524         if (m_mediaSource)
525             m_mediaSource->player()->addAudioRenderer(renderer);
526     }
527 }
528
529 static RetainPtr<CMSampleBufferRef> createNonDisplayingCopy(CMSampleBufferRef sampleBuffer)
530 {
531     CMSampleBufferRef newSampleBuffer = 0;
532     CMSampleBufferCreateCopy(kCFAllocatorDefault, sampleBuffer, &newSampleBuffer);
533     if (!newSampleBuffer)
534         return sampleBuffer;
535
536     CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(newSampleBuffer, true);
537     for (CFIndex i = 0; i < CFArrayGetCount(attachmentsArray); ++i) {
538         CFMutableDictionaryRef attachments = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachmentsArray, i);
539         CFDictionarySetValue(attachments, kCMSampleAttachmentKey_DoNotDisplay, kCFBooleanTrue);
540     }
541
542     return adoptCF(newSampleBuffer);
543 }
544
545 void SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>> mediaSamples, AtomicString trackIDString)
546 {
547     int trackID = trackIDString.toInt();
548     LOG(Media, "SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples(%p) samples: %d samples, trackId: %d", this, mediaSamples.size(), trackID);
549
550     if (trackID == m_enabledVideoTrackID)
551         flushAndEnqueueNonDisplayingSamples(mediaSamples, m_displayLayer.get());
552     else if (m_audioRenderers.count(trackID))
553         flushAndEnqueueNonDisplayingSamples(mediaSamples, m_audioRenderers[trackID].get());
554 }
555
556 void SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>> mediaSamples, AVSampleBufferAudioRenderer* renderer)
557 {
558     [renderer flush];
559
560     for (auto it = mediaSamples.begin(), end = mediaSamples.end(); it != end; ++it) {
561         RefPtr<MediaSample>& mediaSample = *it;
562
563         PlatformSample platformSample = mediaSample->platformSample();
564         ASSERT(platformSample.type == PlatformSample::CMSampleBufferType);
565
566         RetainPtr<CMSampleBufferRef> sampleBuffer = createNonDisplayingCopy(platformSample.sample.cmSampleBuffer);
567
568         [renderer enqueueSampleBuffer:sampleBuffer.get()];
569     }
570 }
571
572 void SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>> mediaSamples, AVSampleBufferDisplayLayer* layer)
573 {
574     [layer flush];
575
576     for (auto it = mediaSamples.begin(), end = mediaSamples.end(); it != end; ++it) {
577         RefPtr<MediaSample>& mediaSample = *it;
578
579         PlatformSample platformSample = mediaSample->platformSample();
580         ASSERT(platformSample.type == PlatformSample::CMSampleBufferType);
581
582         RetainPtr<CMSampleBufferRef> sampleBuffer = createNonDisplayingCopy(platformSample.sample.cmSampleBuffer);
583
584         [layer enqueueSampleBuffer:sampleBuffer.get()];
585     }
586
587     if (m_mediaSource)
588         m_mediaSource->player()->setHasAvailableVideoFrame(false);
589 }
590
591 void SourceBufferPrivateAVFObjC::enqueueSample(PassRefPtr<MediaSample> prpMediaSample, AtomicString trackIDString)
592 {
593     int trackID = trackIDString.toInt();
594     if (trackID != m_enabledVideoTrackID && !m_audioRenderers.count(trackID))
595         return;
596
597     RefPtr<MediaSample> mediaSample = prpMediaSample;
598
599     PlatformSample platformSample = mediaSample->platformSample();
600     if (platformSample.type != PlatformSample::CMSampleBufferType)
601         return;
602
603     if (trackID == m_enabledVideoTrackID) {
604         [m_displayLayer enqueueSampleBuffer:platformSample.sample.cmSampleBuffer];
605         if (m_mediaSource)
606             m_mediaSource->player()->setHasAvailableVideoFrame(true);
607     } else
608         [m_audioRenderers[trackID] enqueueSampleBuffer:platformSample.sample.cmSampleBuffer];
609 }
610
611 bool SourceBufferPrivateAVFObjC::isReadyForMoreSamples(AtomicString trackIDString)
612 {
613     int trackID = trackIDString.toInt();
614     if (trackID == m_enabledVideoTrackID)
615         return [m_displayLayer isReadyForMoreMediaData];
616     else if (m_audioRenderers.count(trackID))
617         return [m_audioRenderers[trackID] isReadyForMoreMediaData];
618     else
619         ASSERT_NOT_REACHED();
620
621     return false;
622 }
623
624 void SourceBufferPrivateAVFObjC::setActive(bool isActive)
625 {
626     if (m_mediaSource)
627         m_mediaSource->sourceBufferPrivateDidChangeActiveState(this, isActive);
628 }
629
630 MediaTime SourceBufferPrivateAVFObjC::fastSeekTimeForMediaTime(MediaTime time, MediaTime negativeThreshold, MediaTime positiveThreshold)
631 {
632     if (m_client)
633         return m_client->sourceBufferPrivateFastSeekTimeForMediaTime(this, time, negativeThreshold, positiveThreshold);
634     return time;
635 }
636
637 void SourceBufferPrivateAVFObjC::seekToTime(MediaTime time)
638 {
639     if (m_client)
640         m_client->sourceBufferPrivateSeekToTime(this, time);
641 }
642
643 void SourceBufferPrivateAVFObjC::didBecomeReadyForMoreSamples(int trackID)
644 {
645     if (trackID == m_enabledVideoTrackID)
646         [m_displayLayer stopRequestingMediaData];
647     else if (m_audioRenderers.count(trackID))
648         [m_audioRenderers[trackID] stopRequestingMediaData];
649     else {
650         ASSERT_NOT_REACHED();
651         return;
652     }
653
654     if (m_client)
655         m_client->sourceBufferPrivateDidBecomeReadyForMoreSamples(this, AtomicString::number(trackID));
656 }
657
658 void SourceBufferPrivateAVFObjC::notifyClientWhenReadyForMoreSamples(AtomicString trackIDString)
659 {
660     int trackID = trackIDString.toInt();
661     if (trackID == m_enabledVideoTrackID) {
662         [m_displayLayer requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
663             didBecomeReadyForMoreSamples(trackID);
664         }];
665     } else if (m_audioRenderers.count(trackID)) {
666         [m_audioRenderers[trackID] requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
667             didBecomeReadyForMoreSamples(trackID);
668         }];
669     } else
670         ASSERT_NOT_REACHED();
671 }
672
673 }
674
675 #endif // ENABLE(MEDIA_SOURCE) && USE(AVFOUNDATION)