REGRESSION (r206025): All YouTube videos play with black bars on all four sides
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / SourceBufferPrivateAVFObjC.h
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 <dispatch/group.h>
33 #include <dispatch/semaphore.h>
34 #include <wtf/Deque.h>
35 #include <wtf/HashMap.h>
36 #include <wtf/MediaTime.h>
37 #include <wtf/OSObjectPtr.h>
38 #include <wtf/RefPtr.h>
39 #include <wtf/RetainPtr.h>
40 #include <wtf/Vector.h>
41 #include <wtf/WeakPtr.h>
42 #include <wtf/text/AtomicString.h>
43
44 OBJC_CLASS AVAsset;
45 OBJC_CLASS AVStreamDataParser;
46 OBJC_CLASS AVSampleBufferAudioRenderer;
47 OBJC_CLASS AVSampleBufferDisplayLayer;
48 OBJC_CLASS NSData;
49 OBJC_CLASS NSError;
50 OBJC_CLASS NSObject;
51 OBJC_CLASS WebAVStreamDataParserListener;
52 OBJC_CLASS WebAVSampleBufferErrorListener;
53
54 typedef struct opaqueCMSampleBuffer *CMSampleBufferRef;
55 typedef const struct opaqueCMFormatDescription *CMFormatDescriptionRef;
56
57 namespace WebCore {
58
59 class CDMSessionMediaSourceAVFObjC;
60 class MediaSourcePrivateAVFObjC;
61 class TimeRanges;
62 class AudioTrackPrivate;
63 class VideoTrackPrivate;
64 class AudioTrackPrivateMediaSourceAVFObjC;
65 class VideoTrackPrivateMediaSourceAVFObjC;
66
67 class SourceBufferPrivateAVFObjCErrorClient {
68 public:
69     virtual ~SourceBufferPrivateAVFObjCErrorClient() { }
70     virtual void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *, bool& shouldIgnore) = 0;
71     virtual void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *, bool& shouldIgnore) = 0;
72 };
73
74 class SourceBufferPrivateAVFObjC final : public SourceBufferPrivate {
75 public:
76     static RefPtr<SourceBufferPrivateAVFObjC> create(MediaSourcePrivateAVFObjC*);
77     virtual ~SourceBufferPrivateAVFObjC();
78
79     void clearMediaSource() { m_mediaSource = nullptr; }
80
81     // AVStreamDataParser delegate methods
82     void didParseStreamDataAsAsset(AVAsset*);
83     void didFailToParseStreamDataWithError(NSError*);
84     void didProvideMediaDataForTrackID(int trackID, CMSampleBufferRef, const String& mediaType, unsigned flags);
85     void didReachEndOfTrackWithTrackID(int trackID, const String& mediaType);
86     void willProvideContentKeyRequestInitializationDataForTrackID(int trackID);
87     void didProvideContentKeyRequestInitializationDataForTrackID(NSData*, int trackID, OSObjectPtr<dispatch_semaphore_t>);
88
89     bool processCodedFrame(int trackID, CMSampleBufferRef, const String& mediaType);
90
91     bool hasVideo() const;
92     bool hasAudio() const;
93
94     void trackDidChangeEnabled(VideoTrackPrivateMediaSourceAVFObjC*);
95     void trackDidChangeEnabled(AudioTrackPrivateMediaSourceAVFObjC*);
96
97     void seekToTime(MediaTime);
98     MediaTime fastSeekTimeForMediaTime(MediaTime, MediaTime negativeThreshold, MediaTime positiveThreshold);
99     FloatSize naturalSize();
100
101     int protectedTrackID() const { return m_protectedTrackID; }
102     AVStreamDataParser* parser() const { return m_parser.get(); }
103     void setCDMSession(CDMSessionMediaSourceAVFObjC*);
104
105     void flush();
106
107     void registerForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient*);
108     void unregisterForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient*);
109     void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *);
110     void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *);
111
112 private:
113     explicit SourceBufferPrivateAVFObjC(MediaSourcePrivateAVFObjC*);
114
115     // SourceBufferPrivate overrides
116     void setClient(SourceBufferPrivateClient*) override;
117     void append(const unsigned char* data, unsigned length) override;
118     void abort() override;
119     void resetParserState() override;
120     void removedFromMediaSource() override;
121     MediaPlayer::ReadyState readyState() const override;
122     void setReadyState(MediaPlayer::ReadyState) override;
123     void flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>>, AtomicString trackID) override;
124     void enqueueSample(PassRefPtr<MediaSample>, AtomicString trackID) override;
125     bool isReadyForMoreSamples(AtomicString trackID) override;
126     void setActive(bool) override;
127     void notifyClientWhenReadyForMoreSamples(AtomicString trackID) override;
128
129     void flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>>, AVSampleBufferAudioRenderer*);
130     void flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>>, AVSampleBufferDisplayLayer*);
131
132     void didBecomeReadyForMoreSamples(int trackID);
133     void appendCompleted();
134     void destroyParser();
135     void destroyRenderers();
136
137     WeakPtr<SourceBufferPrivateAVFObjC> createWeakPtr() { return m_weakFactory.createWeakPtr(); }
138
139     Vector<RefPtr<VideoTrackPrivateMediaSourceAVFObjC>> m_videoTracks;
140     Vector<RefPtr<AudioTrackPrivateMediaSourceAVFObjC>> m_audioTracks;
141     Vector<SourceBufferPrivateAVFObjCErrorClient*> m_errorClients;
142
143     WeakPtrFactory<SourceBufferPrivateAVFObjC> m_weakFactory;
144     WeakPtrFactory<SourceBufferPrivateAVFObjC> m_appendWeakFactory;
145
146     RetainPtr<AVStreamDataParser> m_parser;
147     RetainPtr<AVAsset> m_asset;
148     RetainPtr<AVSampleBufferDisplayLayer> m_displayLayer;
149     HashMap<int, RetainPtr<AVSampleBufferAudioRenderer>> m_audioRenderers;
150     RetainPtr<WebAVStreamDataParserListener> m_delegate;
151     RetainPtr<WebAVSampleBufferErrorListener> m_errorListener;
152     RetainPtr<NSError> m_hdcpError;
153     OSObjectPtr<dispatch_semaphore_t> m_hasSessionSemaphore;
154     OSObjectPtr<dispatch_group_t> m_isAppendingGroup;
155
156     MediaSourcePrivateAVFObjC* m_mediaSource;
157     SourceBufferPrivateClient* m_client;
158     CDMSessionMediaSourceAVFObjC* m_session { nullptr };
159
160     Optional<FloatSize> m_cachedSize;
161     FloatSize m_currentSize;
162     bool m_parsingSucceeded;
163     bool m_parserStateWasReset { false };
164     int m_enabledVideoTrackID;
165     int m_protectedTrackID;
166 };
167
168 }
169
170 #endif // ENABLE(MEDIA_SOURCE) && USE(AVFOUNDATION)
171
172 #endif