Unreviewed, rolling out r140420.
[WebKit-https.git] / Source / WebCore / platform / graphics / gstreamer / GStreamerVersioning.cpp
1 /*
2  * Copyright (C) 2012 Igalia, S.L.
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Library General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2 of the License, or (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Library General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Library General Public License
15  *  along with this library; see the file COPYING.LIB.  If not, write to
16  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  *  Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include "GStreamerVersioning.h"
23
24 #if USE(GSTREAMER)
25 #include "IntSize.h"
26 #include <wtf/UnusedParam.h>
27
28 #ifdef GST_API_VERSION_1
29 #include <gst/audio/audio.h>
30 #else
31 #include <gst/audio/multichannel.h>
32 #endif
33
34 void webkitGstObjectRefSink(GstObject* gstObject)
35 {
36 #ifdef GST_API_VERSION_1
37     gst_object_ref_sink(gstObject);
38 #else
39     gst_object_ref(gstObject);
40     gst_object_sink(gstObject);
41 #endif
42 }
43
44 GstPad* webkitGstGhostPadFromStaticTemplate(GstStaticPadTemplate* staticPadTemplate, const gchar* name, GstPad* target)
45 {
46     GstPad* pad;
47     GstPadTemplate* padTemplate = gst_static_pad_template_get(staticPadTemplate);
48
49     if (target)
50         pad = gst_ghost_pad_new_from_template(name, target, padTemplate);
51     else
52         pad = gst_ghost_pad_new_no_target_from_template(name, padTemplate);
53
54 #ifdef GST_API_VERSION_1
55     gst_object_unref(padTemplate);
56 #endif
57
58     return pad;
59 }
60
61 GRefPtr<GstCaps> webkitGstGetPadCaps(GstPad* pad)
62 {
63     if (!pad)
64         return 0;
65
66 #ifdef GST_API_VERSION_1
67     return adoptGRef(gst_pad_get_current_caps(pad)); // gst_pad_get_current_caps return a new reference.
68 #else
69     return GST_PAD_CAPS(pad);
70 #endif
71 }
72
73 #if ENABLE(VIDEO)
74 bool getVideoSizeAndFormatFromCaps(GstCaps* caps, WebCore::IntSize& size, GstVideoFormat& format, int& pixelAspectRatioNumerator, int& pixelAspectRatioDenominator, int& stride)
75 {
76 #ifdef GST_API_VERSION_1
77     GstVideoInfo info;
78
79     if (!gst_caps_is_fixed(caps) || !gst_video_info_from_caps(&info, caps))
80         return false;
81
82     format = GST_VIDEO_INFO_FORMAT(&info);
83     size.setWidth(GST_VIDEO_INFO_WIDTH(&info));
84     size.setHeight(GST_VIDEO_INFO_HEIGHT(&info));
85     pixelAspectRatioNumerator = GST_VIDEO_INFO_PAR_N(&info);
86     pixelAspectRatioDenominator = GST_VIDEO_INFO_PAR_D(&info);
87     stride = GST_VIDEO_INFO_PLANE_STRIDE(&info, 0);
88 #else
89     gint width, height;
90     if (!GST_IS_CAPS(caps) || !gst_caps_is_fixed(caps)
91         || !gst_video_format_parse_caps(caps, &format, &width, &height)
92         || !gst_video_parse_caps_pixel_aspect_ratio(caps, &pixelAspectRatioNumerator,
93                                                     &pixelAspectRatioDenominator))
94         return false;
95     size.setWidth(width);
96     size.setHeight(height);
97     stride = size.width() * 4;
98 #endif
99
100     return true;
101 }
102 #endif
103
104 GstBuffer* createGstBuffer(GstBuffer* buffer)
105 {
106 #ifndef GST_API_VERSION_1
107     GstBuffer* newBuffer = gst_buffer_try_new_and_alloc(GST_BUFFER_SIZE(buffer));
108 #else
109     gsize bufferSize = gst_buffer_get_size(buffer);
110     GstBuffer* newBuffer = gst_buffer_new_and_alloc(bufferSize);
111 #endif
112
113     if (!newBuffer)
114         return 0;
115
116 #ifndef GST_API_VERSION_1
117     gst_buffer_copy_metadata(newBuffer, buffer, static_cast<GstBufferCopyFlags>(GST_BUFFER_COPY_ALL));
118 #else
119     gst_buffer_copy_into(newBuffer, buffer, static_cast<GstBufferCopyFlags>(GST_BUFFER_COPY_METADATA), 0, bufferSize);
120 #endif
121     return newBuffer;
122 }
123
124 void setGstElementClassMetadata(GstElementClass* elementClass, const char* name, const char* longName, const char* description, const char* author)
125 {
126 #ifdef GST_API_VERSION_1
127     gst_element_class_set_metadata(elementClass, name, longName, description, author);
128 #else
129     gst_element_class_set_details_simple(elementClass, name, longName, description, author);
130 #endif
131 }
132
133 bool gstObjectIsFloating(GstObject* gstObject)
134 {
135 #ifdef GST_API_VERSION_1
136     return g_object_is_floating(G_OBJECT(gstObject));
137 #else
138     return GST_OBJECT_IS_FLOATING(gstObject);
139 #endif
140 }
141
142 void notifyGstTagsOnPad(GstElement* element, GstPad* pad, GstTagList* tags)
143 {
144 #ifdef GST_API_VERSION_1
145     UNUSED_PARAM(element);
146     gst_pad_push_event(GST_PAD_CAST(pad), gst_event_new_tag(tags));
147 #else
148     gst_element_found_tags_for_pad(element, pad, tags);
149 #endif
150 }
151
152 #if ENABLE(WEB_AUDIO)
153 GstCaps* getGstAudioCaps(int channels, float sampleRate)
154 {
155 #ifdef GST_API_VERSION_1
156     return gst_caps_new_simple("audio/x-raw", "rate", G_TYPE_INT, static_cast<int>(sampleRate),
157         "channels", G_TYPE_INT, channels,
158         "format", G_TYPE_STRING, gst_audio_format_to_string(GST_AUDIO_FORMAT_F32),
159         "layout", G_TYPE_STRING, "interleaved", NULL);
160 #else
161     return gst_caps_new_simple("audio/x-raw-float", "rate", G_TYPE_INT, static_cast<int>(sampleRate),
162         "channels", G_TYPE_INT, channels,
163         "endianness", G_TYPE_INT, G_BYTE_ORDER,
164         "width", G_TYPE_INT, 32, NULL);
165 #endif
166 }
167 #endif
168
169 #endif // USE(GSTREAMER)