command-line-formatter: Stop uselessly looping over options
[platform/upstream/gstreamer.git] / ges / ges-audio-track.c
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: gesaudiotrack
22  * @title: GESAudioTrack
23  * @short_description: A standard #GESTrack for raw audio
24  *
25  * A #GESAudioTrack is a default audio #GESTrack, with a
26  * #GES_TRACK_TYPE_AUDIO #GESTrack:track-type and "audio/x-raw(ANY)"
27  * #GESTrack:caps.
28  *
29  * By default, an audio track will have its #GESTrack:restriction-caps
30  * set to "audio/x-raw" with the following properties:
31  *
32  * - format: "S32LE"
33  * - channels: 2
34  * - rate: 44100
35  * - layout: "interleaved"
36  *
37  * These fields are needed for negotiation purposes, but you can change
38  * their values if you wish. It is advised that you do so using
39  * ges_track_update_restriction_caps() with new values for the fields you
40  * wish to change, and any additional fields you may want to add. Unlike
41  * using ges_track_set_restriction_caps(), this will ensure that these
42  * default fields will at least have some value set.
43  */
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include "ges-internal.h"
49 #include "ges-smart-adder.h"
50 #include "ges-audio-track.h"
51
52 #define DEFAULT_CAPS "audio/x-raw"
53
54 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
55 #define DEFAULT_RESTRICTION_CAPS "audio/x-raw, format=S32LE, channels=2, "\
56   "rate=44100, layout=interleaved"
57 #else
58 #define DEFAULT_RESTRICTION_CAPS "audio/x-raw, format=S32BE, channels=2, "\
59   "rate=44100, layout=interleaved"
60 #endif
61
62 struct _GESAudioTrackPrivate
63 {
64   gpointer nothing;
65 };
66
67 G_DEFINE_TYPE_WITH_PRIVATE (GESAudioTrack, ges_audio_track, GES_TYPE_TRACK);
68
69 /****************************************************
70  *              Private methods and utils           *
71  ****************************************************/
72 static void
73 _sync_capsfilter_with_track (GESTrack * track, GstElement * capsfilter)
74 {
75   GstCaps *restriction, *caps;
76   gint rate;
77   GstStructure *structure;
78
79   g_object_get (track, "restriction-caps", &restriction, NULL);
80   if (restriction == NULL)
81     return;
82
83   if (gst_caps_get_size (restriction) == 0)
84     goto done;
85
86   structure = gst_caps_get_structure (restriction, 0);
87   if (!gst_structure_get_int (structure, "rate", &rate))
88     goto done;
89
90   caps = gst_caps_new_simple ("audio/x-raw", "rate", G_TYPE_INT, rate, NULL);
91
92   g_object_set (capsfilter, "caps", caps, NULL);
93   gst_caps_unref (caps);
94
95 done:
96   gst_caps_unref (restriction);
97 }
98
99 static void
100 _track_restriction_changed_cb (GESTrack * track, GParamSpec * arg G_GNUC_UNUSED,
101     GstElement * capsfilter)
102 {
103   _sync_capsfilter_with_track (track, capsfilter);
104 }
105
106 static void
107 _weak_notify_cb (GESTrack * track, GstElement * capsfilter)
108 {
109   g_signal_handlers_disconnect_by_func (track,
110       (GCallback) _track_restriction_changed_cb, capsfilter);
111 }
112
113 static GstElement *
114 create_element_for_raw_audio_gap (GESTrack * track)
115 {
116   GstElement *bin;
117   GstElement *capsfilter;
118
119   bin = gst_parse_bin_from_description
120       ("audiotestsrc wave=silence name=src ! audioconvert ! audioresample ! audioconvert ! capsfilter name=gapfilter caps=audio/x-raw",
121       TRUE, NULL);
122
123   capsfilter = gst_bin_get_by_name (GST_BIN (bin), "gapfilter");
124   g_object_weak_ref (G_OBJECT (capsfilter), (GWeakNotify) _weak_notify_cb,
125       track);
126   g_signal_connect (track, "notify::restriction-caps",
127       (GCallback) _track_restriction_changed_cb, capsfilter);
128
129   _sync_capsfilter_with_track (track, capsfilter);
130
131   gst_object_unref (capsfilter);
132
133   return bin;
134 }
135
136
137 /****************************************************
138  *              GObject vmethods implementations    *
139  ****************************************************/
140
141 static void
142 ges_audio_track_init (GESAudioTrack * self)
143 {
144   self->priv = ges_audio_track_get_instance_private (self);
145 }
146
147 static void
148 ges_audio_track_finalize (GObject * object)
149 {
150   /* TODO: Add deinitalization code here */
151
152   G_OBJECT_CLASS (ges_audio_track_parent_class)->finalize (object);
153 }
154
155 static void
156 ges_audio_track_class_init (GESAudioTrackClass * klass)
157 {
158   GObjectClass *object_class = G_OBJECT_CLASS (klass);
159 /*   GESTrackClass *parent_class = GES_TRACK_CLASS (klass);
160  */
161
162   object_class->finalize = ges_audio_track_finalize;
163
164   GES_TRACK_CLASS (klass)->get_mixing_element = ges_smart_adder_new;
165 }
166
167 /****************************************************
168  *              API implementation                  *
169  ****************************************************/
170 /**
171  * ges_audio_track_new:
172  *
173  * Creates a new audio track, with a #GES_TRACK_TYPE_AUDIO
174  * #GESTrack:track-type, "audio/x-raw(ANY)" #GESTrack:caps, and
175  * "audio/x-raw" #GESTrack:restriction-caps with the properties:
176  *
177  * - format: "S32LE"
178  * - channels: 2
179  * - rate: 44100
180  * - layout: "interleaved"
181  *
182  * You should use ges_track_update_restriction_caps() if you wish to
183  * modify these fields, or add additional ones.
184  *
185  * Returns: (transfer floating): The newly created audio track.
186  */
187 GESAudioTrack *
188 ges_audio_track_new (void)
189 {
190   GESAudioTrack *ret;
191   GstCaps *caps = gst_caps_from_string (DEFAULT_CAPS);
192   GstCaps *restriction_caps = gst_caps_from_string (DEFAULT_RESTRICTION_CAPS);
193
194   ret = g_object_new (GES_TYPE_AUDIO_TRACK, "caps", caps,
195       "track-type", GES_TRACK_TYPE_AUDIO, NULL);
196
197   ges_track_set_create_element_for_gap_func (GES_TRACK (ret),
198       create_element_for_raw_audio_gap);
199
200   ges_track_set_restriction_caps (GES_TRACK (ret), restriction_caps);
201
202   gst_caps_unref (caps);
203   gst_caps_unref (restriction_caps);
204
205   return ret;
206 }