1/*
2 * Copyright (C) 2020 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''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "MediaRecorderPrivate.h"
28
29#if PLATFORM(COCOA) && ENABLE(GPU_PROCESS)
30
31#include "GPUProcessConnection.h"
32#include "RemoteMediaRecorderMessages.h"
33#include "RemoteMediaRecordersMessages.h"
34#include "WebProcess.h"
35#include <WebCore/CARingBuffer.h>
36#include <WebCore/MediaStreamPrivate.h>
37#include <WebCore/MediaStreamTrackPrivate.h>
38#include <WebCore/RemoteVideoSample.h>
39#include <WebCore/SharedBuffer.h>
40#include <WebCore/WebAudioBufferList.h>
41
42using namespace WebCore;
43
44namespace WebKit {
45
46MediaRecorderPrivate::MediaRecorderPrivate(const MediaStreamPrivate& stream)
47 : m_identifier(MediaRecorderIdentifier::generate())
48 , m_connection(WebProcess::singleton().ensureGPUProcessConnection().connection())
49{
50 // FIXME: we will need to implement support for multiple audio/video tracks
51 // Currently we only choose the first track as the recorded track.
52 // FIXME: We would better to throw an exception to JavaScript if writer creation fails.
53
54 const MediaStreamTrackPrivate* audioTrack { nullptr };
55 const MediaStreamTrackPrivate* videoTrack { nullptr };
56 for (auto& track : stream.tracks()) {
57 if (!track->enabled() || track->ended())
58 continue;
59 switch (track->type()) {
60 case RealtimeMediaSource::Type::Video: {
61 auto& settings = track->settings();
62 if (!videoTrack && settings.supportsWidth() && settings.supportsHeight()) {
63 videoTrack = track.get();
64 m_recordedVideoTrackID = videoTrack->id();
65 }
66 break;
67 }
68 case RealtimeMediaSource::Type::Audio:
69 if (!audioTrack) {
70 m_ringBuffer = makeUnique<CARingBuffer>(makeUniqueRef<SharedRingBufferStorage>(this));
71 audioTrack = track.get();
72 m_recordedAudioTrackID = audioTrack->id();
73 }
74 break;
75 case RealtimeMediaSource::Type::None:
76 break;
77 }
78 }
79 m_connection->sendWithAsyncReply(Messages::RemoteMediaRecorders::CreateRecorder { m_identifier, !!audioTrack, videoTrack ? videoTrack->settings().width() : 0, videoTrack ? videoTrack->settings().height() : 0 }, [this, weakThis = makeWeakPtr(this)](auto&& exception) {
80 if (!weakThis || !exception)
81 return;
82 m_errorCallback(Exception { exception->code, WTFMove(exception->message) });
83 }, 0);
84}
85
86MediaRecorderPrivate::~MediaRecorderPrivate()
87{
88 m_connection->send(Messages::RemoteMediaRecorders::ReleaseRecorder { m_identifier }, 0);
89}
90
91void MediaRecorderPrivate::sampleBufferUpdated(const WebCore::MediaStreamTrackPrivate& track, WebCore::MediaSample& sample)
92{
93 if (track.id() != m_recordedVideoTrackID)
94 return;
95 if (auto remoteSample = RemoteVideoSample::create(sample))
96 m_connection->send(Messages::RemoteMediaRecorder::VideoSampleAvailable { WTFMove(*remoteSample) }, m_identifier);
97}
98
99void MediaRecorderPrivate::audioSamplesAvailable(const WebCore::MediaStreamTrackPrivate& track, const MediaTime& time, const PlatformAudioData& audioData, const AudioStreamDescription& description, size_t numberOfFrames)
100{
101 if (track.id() != m_recordedAudioTrackID)
102 return;
103
104 if (m_description != description) {
105 ASSERT(description.platformDescription().type == PlatformDescription::CAAudioStreamBasicType);
106 m_description = *WTF::get<const AudioStreamBasicDescription*>(description.platformDescription().description);
107
108 // Allocate a ring buffer large enough to contain 2 seconds of audio.
109 m_numberOfFrames = m_description.sampleRate() * 2;
110 m_ringBuffer->allocate(m_description.streamDescription(), m_numberOfFrames);
111 }
112
113 ASSERT(is<WebAudioBufferList>(audioData));
114 m_ringBuffer->store(downcast<WebAudioBufferList>(audioData).list(), numberOfFrames, time.timeValue());
115 uint64_t startFrame;
116 uint64_t endFrame;
117 m_ringBuffer->getCurrentFrameBounds(startFrame, endFrame);
118 m_connection->send(Messages::RemoteMediaRecorder::AudioSamplesAvailable { time, numberOfFrames, startFrame, endFrame }, m_identifier);
119}
120
121void MediaRecorderPrivate::storageChanged(SharedMemory* storage)
122{
123 SharedMemory::Handle handle;
124 if (storage)
125 storage->createHandle(handle, SharedMemory::Protection::ReadOnly);
126 m_connection->send(Messages::RemoteMediaRecorder::AudioSamplesStorageChanged { handle, m_description, static_cast<uint64_t>(m_numberOfFrames) }, m_identifier);
127}
128
129void MediaRecorderPrivate::fetchData(CompletionHandler<void(RefPtr<WebCore::SharedBuffer>&&, const String& mimeType)>&& completionHandler)
130{
131 m_connection->sendWithAsyncReply(Messages::RemoteMediaRecorder::FetchData { }, [completionHandler = WTFMove(completionHandler)](auto&& data, auto&& mimeType) mutable {
132 RefPtr<SharedBuffer> buffer;
133 if (!data.size())
134 buffer = SharedBuffer::create(data.data(), data.size());
135 completionHandler(WTFMove(buffer), mimeType);
136 }, m_identifier);
137}
138
139void MediaRecorderPrivate::stopRecording()
140{
141 m_connection->send(Messages::RemoteMediaRecorder::StopRecording { }, m_identifier);
142}
143
144}
145
146#endif // PLATFORM(COCOA) && ENABLE(GPU_PROCESS)