1/*
2 * Copyright (C) 2017 Igalia S.L. All rights reserved.
3 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted, provided that the following conditions
7 * are required to be met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. AND ITS CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27
28#include "config.h"
29
30#if USE(LIBWEBRTC) && USE(GSTREAMER)
31#include "RealtimeOutgoingVideoSourceLibWebRTC.h"
32
33#include "GStreamerVideoFrameLibWebRTC.h"
34#include "MediaSampleGStreamer.h"
35
36namespace WebCore {
37
38Ref<RealtimeOutgoingVideoSource> RealtimeOutgoingVideoSource::create(Ref<MediaStreamTrackPrivate>&& videoSource)
39{
40 return RealtimeOutgoingVideoSourceLibWebRTC::create(WTFMove(videoSource));
41}
42
43Ref<RealtimeOutgoingVideoSourceLibWebRTC> RealtimeOutgoingVideoSourceLibWebRTC::create(Ref<MediaStreamTrackPrivate>&& videoSource)
44{
45 return adoptRef(*new RealtimeOutgoingVideoSourceLibWebRTC(WTFMove(videoSource)));
46}
47
48RealtimeOutgoingVideoSourceLibWebRTC::RealtimeOutgoingVideoSourceLibWebRTC(Ref<MediaStreamTrackPrivate>&& videoSource)
49 : RealtimeOutgoingVideoSource(WTFMove(videoSource))
50{
51}
52
53void RealtimeOutgoingVideoSourceLibWebRTC::sampleBufferUpdated(MediaStreamTrackPrivate&, MediaSample& sample)
54{
55 if (isSilenced())
56 return;
57
58 switch (sample.videoRotation()) {
59 case MediaSample::VideoRotation::None:
60 m_currentRotation = webrtc::kVideoRotation_0;
61 break;
62 case MediaSample::VideoRotation::UpsideDown:
63 m_currentRotation = webrtc::kVideoRotation_180;
64 break;
65 case MediaSample::VideoRotation::Right:
66 m_currentRotation = webrtc::kVideoRotation_90;
67 break;
68 case MediaSample::VideoRotation::Left:
69 m_currentRotation = webrtc::kVideoRotation_270;
70 break;
71 }
72
73 ASSERT(sample.platformSample().type == PlatformSample::GStreamerSampleType);
74 auto& mediaSample = static_cast<MediaSampleGStreamer&>(sample);
75 auto frameBuffer(GStreamerVideoFrameLibWebRTC::create(gst_sample_ref(mediaSample.platformSample().sample.gstSample)));
76
77 sendFrame(WTFMove(frameBuffer));
78}
79
80rtc::scoped_refptr<webrtc::VideoFrameBuffer> RealtimeOutgoingVideoSourceLibWebRTC::createBlackFrame(size_t width, size_t height)
81{
82 GstVideoInfo info;
83
84 gst_video_info_set_format(&info, GST_VIDEO_FORMAT_RGB, width, height);
85
86 GRefPtr<GstBuffer> buffer = adoptGRef(gst_buffer_new_allocate(nullptr, info.size, nullptr));
87 GRefPtr<GstCaps> caps = adoptGRef(gst_video_info_to_caps(&info));
88
89 auto map = GstMappedBuffer::create(buffer.get(), GST_MAP_WRITE);
90 memset(map->data(), 0, info.size);
91
92 return GStreamerVideoFrameLibWebRTC::create(gst_sample_new(buffer.get(), caps.get(), NULL, NULL));
93}
94
95} // namespace WebCore
96
97#endif // USE(LIBWEBRTC)
98