[WTF] Add makeUnique<T>, which ensures T is fast-allocated, makeUnique / makeUniqueWi...
[WebKit-https.git] / Source / WebCore / platform / mediarecorder / MediaRecorderPrivateAVFImpl.cpp
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
26 #include "config.h"
27 #include "MediaRecorderPrivateAVFImpl.h"
28
29 #if ENABLE(MEDIA_STREAM)
30
31 #include "AudioStreamDescription.h"
32 #include "MediaSample.h"
33 #include "MediaStreamPrivate.h"
34 #include "SharedBuffer.h"
35 #include "WebAudioBufferList.h"
36
37 namespace WebCore {
38
39 std::unique_ptr<MediaRecorderPrivateAVFImpl> MediaRecorderPrivateAVFImpl::create(const MediaStreamPrivate& stream)
40 {
41     // FIXME: we will need to implement support for multiple audio/video tracks
42     // Currently we only choose the first track as the recorded track.
43     // FIXME: We would better to throw an exception to JavaScript if writer creation fails.
44
45     String audioTrackId;
46     String videoTrackId;
47     const MediaStreamTrackPrivate* audioTrack { nullptr };
48     const MediaStreamTrackPrivate* videoTrack { nullptr };
49     for (auto& track : stream.tracks()) {
50         if (!track->enabled() || track->ended())
51             continue;
52         switch (track->type()) {
53         case RealtimeMediaSource::Type::Video: {
54             auto& settings = track->settings();
55             if (!videoTrack && settings.supportsWidth() && settings.supportsHeight()) {
56                 videoTrack = track.get();
57                 videoTrackId = videoTrack->id();
58             }
59             break;
60         }
61         case RealtimeMediaSource::Type::Audio:
62             if (!audioTrack) {
63                 audioTrack = track.get();
64                 audioTrackId = audioTrack->id();
65             }
66             break;
67         case RealtimeMediaSource::Type::None:
68             break;
69         }
70     }
71     auto writer = MediaRecorderPrivateWriter::create(audioTrack, videoTrack);
72     if (!writer)
73         return nullptr;
74
75     return makeUnique<MediaRecorderPrivateAVFImpl>(writer.releaseNonNull(), WTFMove(audioTrackId), WTFMove(videoTrackId));
76 }
77
78 MediaRecorderPrivateAVFImpl::MediaRecorderPrivateAVFImpl(Ref<MediaRecorderPrivateWriter>&& writer, String&& audioTrackId, String&& videoTrackId)
79     : m_writer(WTFMove(writer))
80     , m_recordedAudioTrackID(WTFMove(audioTrackId))
81     , m_recordedVideoTrackID(WTFMove(videoTrackId))
82 {
83 }
84
85 void MediaRecorderPrivateAVFImpl::sampleBufferUpdated(MediaStreamTrackPrivate& track, MediaSample& sampleBuffer)
86 {
87     if (track.id() != m_recordedVideoTrackID)
88         return;
89     m_writer->appendVideoSampleBuffer(sampleBuffer.platformSample().sample.cmSampleBuffer);
90 }
91
92 void MediaRecorderPrivateAVFImpl::audioSamplesAvailable(MediaStreamTrackPrivate& track, const WTF::MediaTime& mediaTime, const PlatformAudioData& data, const AudioStreamDescription& description, size_t sampleCount)
93 {
94     if (track.id() != m_recordedAudioTrackID)
95         return;
96     ASSERT(is<WebAudioBufferList>(data));
97     ASSERT(description.platformDescription().type == PlatformDescription::CAAudioStreamBasicType);
98     m_writer->appendAudioSampleBuffer(data, description, mediaTime, sampleCount);
99 }
100
101 void MediaRecorderPrivateAVFImpl::stopRecording()
102 {
103     m_writer->stopRecording();
104 }
105
106 RefPtr<SharedBuffer> MediaRecorderPrivateAVFImpl::fetchData()
107 {
108     return m_writer->fetchData();
109 }
110
111 const String& MediaRecorderPrivateAVFImpl::mimeType()
112 {
113     static NeverDestroyed<const String> mp4MimeType(MAKE_STATIC_STRING_IMPL("video/mp4"));
114     // FIXME: we will need to support more MIME types.
115     return mp4MimeType;
116 }
117
118 } // namespace WebCore
119
120 #endif // ENABLE(MEDIA_STREAM)