1 /* GStreamer Editing Services
2  * Copyright (C) <2013> Thibault Saunier <thibault.saunier@collabora.com>
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
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /**
21  * SECTION: gesvideotrack
22  * @title: GESVideoTrack
23  * @short_description: A standard GESTrack for raw video
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include "ges-video-track.h"
30 #include "ges-smart-video-mixer.h"
31 
32 struct _GESVideoTrackPrivate
33 {
34   gpointer nothing;
35 };
36 
37 G_DEFINE_TYPE_WITH_PRIVATE (GESVideoTrack, ges_video_track, GES_TYPE_TRACK);
38 
39 static void
_sync_capsfilter_with_track(GESTrack * track,GstElement * capsfilter)40 _sync_capsfilter_with_track (GESTrack * track, GstElement * capsfilter)
41 {
42   GstCaps *restriction, *caps;
43   gint fps_n, fps_d;
44   GstStructure *structure;
45 
46   g_object_get (track, "restriction-caps", &restriction, NULL);
47   if (restriction == NULL)
48     return;
49 
50   if (gst_caps_get_size (restriction) == 0)
51     goto done;
52 
53   structure = gst_caps_get_structure (restriction, 0);
54   if (!gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d))
55     goto done;
56 
57   caps =
58       gst_caps_new_simple ("video/x-raw", "framerate", GST_TYPE_FRACTION, fps_n,
59       fps_d, NULL);
60 
61   g_object_set (capsfilter, "caps", caps, NULL);
62   gst_caps_unref (caps);
63 
64 done:
65   gst_caps_unref (restriction);
66 }
67 
68 static void
_track_restriction_changed_cb(GESTrack * track,GParamSpec * arg G_GNUC_UNUSED,GstElement * capsfilter)69 _track_restriction_changed_cb (GESTrack * track, GParamSpec * arg G_GNUC_UNUSED,
70     GstElement * capsfilter)
71 {
72   _sync_capsfilter_with_track (track, capsfilter);
73 }
74 
75 static void
_weak_notify_cb(GESTrack * track,GstElement * capsfilter)76 _weak_notify_cb (GESTrack * track, GstElement * capsfilter)
77 {
78   g_signal_handlers_disconnect_by_func (track,
79       (GCallback) _track_restriction_changed_cb, capsfilter);
80 }
81 
82 static GstElement *
create_element_for_raw_video_gap(GESTrack * track)83 create_element_for_raw_video_gap (GESTrack * track)
84 {
85   GstElement *bin;
86   GstElement *capsfilter;
87 
88   bin = gst_parse_bin_from_description
89       ("videotestsrc pattern=2 name=src ! videorate ! capsfilter name=gapfilter caps=video/x-raw",
90       TRUE, NULL);
91 
92   capsfilter = gst_bin_get_by_name (GST_BIN (bin), "gapfilter");
93   g_object_weak_ref (G_OBJECT (capsfilter), (GWeakNotify) _weak_notify_cb,
94       track);
95   g_signal_connect (track, "notify::restriction-caps",
96       (GCallback) _track_restriction_changed_cb, capsfilter);
97 
98   _sync_capsfilter_with_track (track, capsfilter);
99 
100   gst_object_unref (capsfilter);
101 
102   return bin;
103 }
104 
105 static void
ges_video_track_init(GESVideoTrack * ges_video_track)106 ges_video_track_init (GESVideoTrack * ges_video_track)
107 {
108 /*   GESVideoTrackPrivate *priv = GES_VIDEO_TRACK_GET_PRIVATE (ges_video_track);
109  */
110 
111   /* TODO: Add initialization code here */
112 }
113 
114 static void
ges_video_track_finalize(GObject * object)115 ges_video_track_finalize (GObject * object)
116 {
117   /* TODO: Add deinitalization code here */
118 
119   G_OBJECT_CLASS (ges_video_track_parent_class)->finalize (object);
120 }
121 
122 static void
ges_video_track_class_init(GESVideoTrackClass * klass)123 ges_video_track_class_init (GESVideoTrackClass * klass)
124 {
125   GObjectClass *object_class = G_OBJECT_CLASS (klass);
126 /*   GESTrackClass *parent_class = GES_TRACK_CLASS (klass);
127  */
128 
129   object_class->finalize = ges_video_track_finalize;
130 
131   GES_TRACK_CLASS (klass)->get_mixing_element = ges_smart_mixer_new;
132 }
133 
134 /**
135  * ges_video_track_new:
136  *
137  * Creates a new #GESVideoTrack of type #GES_TRACK_TYPE_VIDEO and with generic
138  * raw video caps ("video/x-raw");
139  *
140  * Returns: (transfer floating): A new #GESTrack.
141  */
142 GESVideoTrack *
ges_video_track_new(void)143 ges_video_track_new (void)
144 {
145   GESVideoTrack *ret;
146   GstCaps *caps = gst_caps_new_empty_simple ("video/x-raw");
147 
148   ret = g_object_new (GES_TYPE_VIDEO_TRACK, "track-type", GES_TRACK_TYPE_VIDEO,
149       "caps", caps, NULL);
150 
151   ges_track_set_create_element_for_gap_func (GES_TRACK (ret),
152       create_element_for_raw_video_gap);
153 
154   gst_caps_unref (caps);
155 
156   return ret;
157 }
158