1 | /* |
2 | * Copyright (C) 2018 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. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
16 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
17 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
18 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
19 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
22 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
23 | */ |
24 | |
25 | #pragma once |
26 | |
27 | #if ENABLE(MEDIA_STREAM) |
28 | |
29 | #include "ActiveDOMObject.h" |
30 | #include "EventTarget.h" |
31 | #include "MediaStream.h" |
32 | #include "MediaStreamTrackPrivate.h" |
33 | #include <wtf/UniqueRef.h> |
34 | |
35 | namespace WebCore { |
36 | |
37 | class Blob; |
38 | class Document; |
39 | class MediaRecorderPrivate; |
40 | |
41 | typedef std::unique_ptr<MediaRecorderPrivate>(*creatorFunction)(); |
42 | |
43 | class MediaRecorder final |
44 | : public ActiveDOMObject |
45 | , public RefCounted<MediaRecorder> |
46 | , public EventTargetWithInlineData |
47 | , public CanMakeWeakPtr<MediaRecorder> |
48 | , private MediaStream::Observer |
49 | , private MediaStreamTrackPrivate::Observer { |
50 | WTF_MAKE_ISO_ALLOCATED(MediaRecorder); |
51 | public: |
52 | enum class RecordingState { Inactive, Recording, Paused }; |
53 | |
54 | struct Options { |
55 | String mimeType; |
56 | unsigned audioBitsPerSecond; |
57 | unsigned videoBitsPerSecond; |
58 | unsigned bitsPerSecond; |
59 | }; |
60 | |
61 | ~MediaRecorder(); |
62 | |
63 | static ExceptionOr<Ref<MediaRecorder>> create(Document&, Ref<MediaStream>&&, Options&& = { }); |
64 | |
65 | WEBCORE_EXPORT static void setCustomPrivateRecorderCreator(creatorFunction); |
66 | |
67 | RecordingState state() const { return m_state; } |
68 | |
69 | using RefCounted::ref; |
70 | using RefCounted::deref; |
71 | |
72 | ExceptionOr<void> startRecording(Optional<int>); |
73 | ExceptionOr<void> stopRecording(); |
74 | |
75 | private: |
76 | MediaRecorder(Document&, Ref<MediaStream>&&, std::unique_ptr<MediaRecorderPrivate>&&, Options&& = { }); |
77 | |
78 | static std::unique_ptr<MediaRecorderPrivate> getPrivateImpl(const MediaStreamPrivate&); |
79 | |
80 | Ref<Blob> createRecordingDataBlob(); |
81 | |
82 | // EventTarget |
83 | void refEventTarget() final { ref(); } |
84 | void derefEventTarget() final { deref(); } |
85 | EventTargetInterface eventTargetInterface() const final { return MediaRecorderEventTargetInterfaceType; } |
86 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); } |
87 | |
88 | // ActiveDOMObject API. |
89 | void stop() final; |
90 | const char* activeDOMObjectName() const final; |
91 | bool canSuspendForDocumentSuspension() const final; |
92 | |
93 | void stopRecordingInternal(); |
94 | |
95 | // MediaStream::Observer |
96 | void didAddOrRemoveTrack() final; |
97 | |
98 | // MediaStreamTrackPrivate::Observer |
99 | void trackEnded(MediaStreamTrackPrivate&) final; |
100 | void trackMutedChanged(MediaStreamTrackPrivate&) final { }; |
101 | void trackSettingsChanged(MediaStreamTrackPrivate&) final { }; |
102 | void trackEnabledChanged(MediaStreamTrackPrivate&) final { }; |
103 | void sampleBufferUpdated(MediaStreamTrackPrivate&, MediaSample&) final; |
104 | void audioSamplesAvailable(MediaStreamTrackPrivate&, const MediaTime&, const PlatformAudioData&, const AudioStreamDescription&, size_t) final; |
105 | |
106 | void scheduleDeferredTask(Function<void()>&&); |
107 | |
108 | static creatorFunction m_customCreator; |
109 | |
110 | Options m_options; |
111 | Ref<MediaStream> m_stream; |
112 | std::unique_ptr<MediaRecorderPrivate> m_private; |
113 | RecordingState m_state { RecordingState::Inactive }; |
114 | Vector<Ref<MediaStreamTrackPrivate>> m_tracks; |
115 | |
116 | bool m_isActive { true }; |
117 | }; |
118 | |
119 | } // namespace WebCore |
120 | |
121 | #endif // ENABLE(MEDIA_STREAM) |
122 | |