[WPE][GTK] Bump minimum versions of GLib, GTK, libsoup, ATK, GStreamer, and Cairo
[WebKit-https.git] / Source / WebCore / platform / mediastream / gstreamer / GStreamerVideoCapturer.cpp
1 /*
2  * Copyright (C) 2018 Metrological Group B.V.
3  * Author: Thibault Saunier <tsaunier@igalia.com>
4  * Author: Alejandro G. Castro  <alex@igalia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * aint with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23
24 #if ENABLE(MEDIA_STREAM) && USE(LIBWEBRTC) && USE(GSTREAMER)
25 #include "GStreamerVideoCapturer.h"
26
27 namespace WebCore {
28
29 GStreamerVideoCapturer::GStreamerVideoCapturer(GStreamerCaptureDevice device)
30     : GStreamerCapturer(device, adoptGRef(gst_caps_new_empty_simple("video/x-raw")))
31 {
32 }
33
34 GStreamerVideoCapturer::GStreamerVideoCapturer(const char* sourceFactory)
35     : GStreamerCapturer(sourceFactory, adoptGRef(gst_caps_new_empty_simple("video/x-raw")))
36 {
37 }
38
39 GstElement* GStreamerVideoCapturer::createConverter()
40 {
41     // https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/issues/97#note_56575
42     auto converter = gst_parse_bin_from_description("videoscale ! videoconvert ! videorate drop-only=1 average-period=1", TRUE, nullptr);
43     ASSERT(converter);
44
45     return converter;
46 }
47
48 GstVideoInfo GStreamerVideoCapturer::getBestFormat()
49 {
50     GRefPtr<GstCaps> caps = adoptGRef(gst_caps_fixate(gst_device_get_caps(m_device.get())));
51     GstVideoInfo info;
52     gst_video_info_from_caps(&info, caps.get());
53
54     return info;
55 }
56
57 bool GStreamerVideoCapturer::setSize(int width, int height)
58 {
59     if (!width || !height)
60         return false;
61
62     m_caps = adoptGRef(gst_caps_copy(m_caps.get()));
63     gst_caps_set_simple(m_caps.get(), "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, nullptr);
64
65     if (!m_capsfilter)
66         return false;
67
68     g_object_set(m_capsfilter.get(), "caps", m_caps.get(), nullptr);
69
70     return true;
71 }
72
73 bool GStreamerVideoCapturer::setFrameRate(double frameRate)
74 {
75     int numerator, denominator;
76
77     gst_util_double_to_fraction(frameRate, &numerator, &denominator);
78
79     if (numerator < -G_MAXINT) {
80         GST_INFO_OBJECT(m_pipeline.get(), "Framerate %f not allowed", frameRate);
81         return false;
82     }
83
84     if (!numerator) {
85         GST_INFO_OBJECT(m_pipeline.get(), "Do not force variable framerate");
86         return false;
87     }
88
89     m_caps = adoptGRef(gst_caps_copy(m_caps.get()));
90     gst_caps_set_simple(m_caps.get(), "framerate", GST_TYPE_FRACTION, numerator, denominator, nullptr);
91
92     if (!m_capsfilter)
93         return false;
94
95     g_object_set(m_capsfilter.get(), "caps", m_caps.get(), nullptr);
96
97     return true;
98 }
99
100 } // namespace WebCore
101
102 #endif // ENABLE(MEDIA_STREAM) && USE(LIBWEBRTC) && USE(GSTREAMER)