1/* 2 * Copyright (C) 2013-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#ifndef SourceBufferPrivateAVFObjC_h 27#define SourceBufferPrivateAVFObjC_h 28 29#if ENABLE(MEDIA_SOURCE) && USE(AVFOUNDATION) 30 31#include "SourceBufferPrivate.h" 32#include <map> 33#include <wtf/Deque.h> 34#include <wtf/HashMap.h> 35#include <wtf/MediaTime.h> 36#include <wtf/RefPtr.h> 37#include <wtf/RetainPtr.h> 38#include <wtf/Vector.h> 39#include <wtf/WeakPtr.h> 40#include <wtf/text/AtomicString.h> 41 42OBJC_CLASS AVAsset; 43OBJC_CLASS AVStreamDataParser; 44OBJC_CLASS AVSampleBufferAudioRenderer; 45OBJC_CLASS AVSampleBufferDisplayLayer; 46OBJC_CLASS NSData; 47OBJC_CLASS NSError; 48OBJC_CLASS NSObject; 49OBJC_CLASS WebAVStreamDataParserListener; 50OBJC_CLASS WebAVSampleBufferErrorListener; 51 52typedef struct opaqueCMSampleBuffer *CMSampleBufferRef; 53typedef const struct opaqueCMFormatDescription *CMFormatDescriptionRef; 54 55namespace WebCore { 56 57class MediaSourcePrivateAVFObjC; 58class TimeRanges; 59class AudioTrackPrivate; 60class VideoTrackPrivate; 61class AudioTrackPrivateMediaSourceAVFObjC; 62class VideoTrackPrivateMediaSourceAVFObjC; 63 64class SourceBufferPrivateAVFObjCErrorClient { 65public: 66 virtual ~SourceBufferPrivateAVFObjCErrorClient() { } 67 virtual void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *) = 0; 68 virtual void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *) = 0; 69}; 70 71class SourceBufferPrivateAVFObjC final : public SourceBufferPrivate { 72public: 73 static RefPtr<SourceBufferPrivateAVFObjC> create(MediaSourcePrivateAVFObjC*); 74 virtual ~SourceBufferPrivateAVFObjC(); 75 76 void clearMediaSource() { m_mediaSource = nullptr; } 77 78 // AVStreamDataParser delegate methods 79 void didParseStreamDataAsAsset(AVAsset*); 80 void didFailToParseStreamDataWithError(NSError*); 81 void didProvideMediaDataForTrackID(int trackID, CMSampleBufferRef, const String& mediaType, unsigned flags); 82 void didReachEndOfTrackWithTrackID(int trackID, const String& mediaType); 83 void didProvideContentKeyRequestInitializationDataForTrackID(NSData*, int trackID); 84 85 bool processCodedFrame(int trackID, CMSampleBufferRef, const String& mediaType); 86 87 bool hasVideo() const; 88 bool hasAudio() const; 89 90 void trackDidChangeEnabled(VideoTrackPrivateMediaSourceAVFObjC*); 91 void trackDidChangeEnabled(AudioTrackPrivateMediaSourceAVFObjC*); 92 93 void seekToTime(MediaTime); 94 MediaTime fastSeekTimeForMediaTime(MediaTime, MediaTime negativeThreshold, MediaTime positiveThreshold); 95 IntSize naturalSize(); 96 97 int protectedTrackID() const { return m_protectedTrackID; } 98 AVStreamDataParser* parser() const { return m_parser.get(); } 99 100 void registerForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient*); 101 void unregisterForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient*); 102 void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *); 103 void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *); 104 105private: 106 explicit SourceBufferPrivateAVFObjC(MediaSourcePrivateAVFObjC*); 107 108 // SourceBufferPrivate overrides 109 virtual void setClient(SourceBufferPrivateClient*) override; 110 virtual void append(const unsigned char* data, unsigned length) override; 111 virtual void abort() override; 112 virtual void removedFromMediaSource() override; 113 virtual MediaPlayer::ReadyState readyState() const override; 114 virtual void setReadyState(MediaPlayer::ReadyState) override; 115 virtual void flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>>, AtomicString trackID) override; 116 virtual void enqueueSample(PassRefPtr<MediaSample>, AtomicString trackID) override; 117 virtual bool isReadyForMoreSamples(AtomicString trackID) override; 118 virtual void setActive(bool) override; 119 virtual void notifyClientWhenReadyForMoreSamples(AtomicString trackID) override; 120 121 void flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>>, AVSampleBufferAudioRenderer*); 122 void flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>>, AVSampleBufferDisplayLayer*); 123 124 void didBecomeReadyForMoreSamples(int trackID); 125 void appendCompleted(); 126 void destroyParser(); 127 void destroyRenderers(); 128 129 WeakPtr<SourceBufferPrivateAVFObjC> createWeakPtr() { return m_weakFactory.createWeakPtr(); } 130 131 Vector<RefPtr<VideoTrackPrivateMediaSourceAVFObjC>> m_videoTracks; 132 Vector<RefPtr<AudioTrackPrivateMediaSourceAVFObjC>> m_audioTracks; 133 Vector<SourceBufferPrivateAVFObjCErrorClient*> m_errorClients; 134 135 WeakPtrFactory<SourceBufferPrivateAVFObjC> m_weakFactory; 136 137 RetainPtr<AVStreamDataParser> m_parser; 138 RetainPtr<AVAsset> m_asset; 139 RetainPtr<AVSampleBufferDisplayLayer> m_displayLayer; 140 std::map<int, RetainPtr<AVSampleBufferAudioRenderer>> m_audioRenderers; 141 RetainPtr<WebAVStreamDataParserListener> m_delegate; 142 RetainPtr<WebAVSampleBufferErrorListener> m_errorListener; 143 144 MediaSourcePrivateAVFObjC* m_mediaSource; 145 SourceBufferPrivateClient* m_client; 146 147 FloatSize m_cachedSize; 148 bool m_parsingSucceeded; 149 int m_enabledVideoTrackID; 150 int m_protectedTrackID; 151}; 152 153} 154 155#endif // ENABLE(MEDIA_SOURCE) && USE(AVFOUNDATION) 156 157#endif 158