2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "rtsp-media.h"
22 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
24 static void gst_rtsp_media_finalize (GObject * obj);
27 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
29 GObjectClass *gobject_class;
31 gobject_class = G_OBJECT_CLASS (klass);
33 gobject_class->finalize = gst_rtsp_media_finalize;
37 gst_rtsp_media_init (GstRTSPMedia * media)
39 media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
43 gst_rtsp_media_stream_free (GstRTSPMediaStream *stream)
48 gst_rtsp_media_finalize (GObject * obj)
53 media = GST_RTSP_MEDIA (obj);
55 g_free (media->location);
56 gst_rtsp_url_free (media->url);
58 for (i = 0; i < media->streams->len; i++) {
59 GstRTSPMediaStream *stream;
61 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
63 gst_rtsp_media_stream_free (stream);
65 g_array_free (media->streams, TRUE);
67 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
72 * @location: the URL of the media
74 * Create a new #GstRTSPMedia instance.
76 * Returns: a new #GstRTSPMedia object or %NULL when location did not contain a
77 * valid or understood URL.
80 gst_rtsp_media_new (const gchar *location)
87 if (gst_rtsp_url_parse (location, &url) != GST_RTSP_OK)
90 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
91 result->location = g_strdup (location);
104 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
107 gst_caps_unref (stream->caps);
108 if ((stream->caps = GST_PAD_CAPS (pad)))
109 gst_caps_ref (stream->caps);
114 * STREAMING CONFIGURATION
116 * gst_rtsp_media_prepare:
117 * @media: a #GstRTSPMedia
118 * @bin: the parent bin to create the elements in.
120 * Prepare the media object so that it creates its streams. Implementations
121 * should crate the needed gstreamer elements and add them to @bin. No state
122 * changes should be performed on them yet.
124 * One or more GstRTSPMediaStream objects should be added to @media with the
125 * srcpad member set to a source pad that produces buffer of type
128 * Returns: %TRUE if the media could be prepared.
131 gst_rtsp_media_prepare (GstRTSPMedia *media, GstBin *bin)
133 GstRTSPMediaStream *stream;
134 GstElement *pay, *element;
138 /* if we're already prepared we must exit */
139 g_return_val_if_fail (media->prepared == FALSE, FALSE);
141 g_print ("%s\n", media->url->abspath);
143 if (g_str_has_prefix (media->url->abspath, "/camera")) {
145 element = gst_parse_launch ("( "
146 "v4l2src ! video/x-raw-yuv,width=352,height=288,framerate=15/1 ! "
147 "queue ! videorate ! ffmpegcolorspace ! "
148 "x264enc bitrate=300 ! rtph264pay name=pay0 pt=96 "
149 "alsasrc ! audio/x-raw-int,rate=8000 ! queue ! "
150 "amrnbenc ! rtpamrpay name=pay1 pt=97 "
153 else if (g_str_has_prefix (media->url->abspath, "/h264")) {
155 element = gst_parse_launch ("( uridecodebin "
156 "uri=file:///home/cschalle/Videos/mi2.avi ! "
157 "x264enc bitrate=300 ! rtph264pay name=pay0 )", NULL);
159 else if (g_str_has_prefix (media->url->abspath, "/theora")) {
160 /* transcode theora */
161 element = gst_parse_launch ("( uridecodebin "
162 "uri=file:///home/wim/data/mi2.avi ! "
163 "theoraenc ! rtptheorapay name=pay0 )", NULL);
165 else if (g_str_has_prefix (media->url->abspath, "/macpclinux")) {
167 element = gst_parse_launch ("( filesrc "
168 "location=/home/cschalle/Videos/mac_pc_linux_2.ogg ! oggdemux name=d ! "
169 "queue ! theoraparse ! rtptheorapay name=pay0 "
170 "d. ! queue ! vorbisparse ! rtpvorbispay name=pay1 )", NULL);
172 else if (g_str_has_prefix (media->url->abspath, "/rtspproxy")) {
173 /* proxy RTSP transcode */
174 element = gst_parse_launch ("( uridecodebin "
175 "uri=rtsp://ia300135.us.archive.org:554/0/items/uncovered_interviews/uncovered_interviews_3_256kb.mp4 ! "
176 "x264enc bitrate=1800 ! rtph264pay name=pay0 )", NULL);
178 else if (g_str_has_prefix (media->url->abspath, "/httpproxy")) {
179 /* proxy HTTP transcode */
180 element = gst_parse_launch ("( uridecodebin "
181 "uri=http://movies.apple.com/movies/fox/maxpayne/maxpayne-tlre_h480.mov name=d "
182 "d. ! queue ! x264enc bitrate=1800 ! rtph264pay name=pay0 pt=96 "
183 "d. ! queue ! faac ! rtpmp4gpay name=pay1 pt=97 )", NULL);
188 gst_bin_add (bin, element);
193 name = g_strdup_printf ("pay%d", i);
195 if (!(pay = gst_bin_get_by_name (GST_BIN (element), name))) {
200 /* create the stream */
201 stream = g_new0 (GstRTSPMediaStream, 1);
202 stream->media = media;
203 stream->element = element;
204 stream->payloader = pay;
205 stream->idx = media->streams->len;
207 pad = gst_element_get_static_pad (pay, "src");
209 stream->srcpad = gst_ghost_pad_new (name, pad);
210 gst_element_add_pad (stream->element, stream->srcpad);
212 stream->caps_sig = g_signal_connect (pad, "notify::caps", (GCallback) caps_notify, stream);
213 gst_object_unref (pad);
216 g_array_append_val (media->streams, stream);
217 gst_object_unref (pay);
222 media->prepared = TRUE;
228 * gst_rtsp_media_n_streams:
229 * @media: a #GstRTSPMedia
231 * Get the number of streams in this media.
233 * Returns: The number of streams.
236 gst_rtsp_media_n_streams (GstRTSPMedia *media)
238 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
239 g_return_val_if_fail (media->prepared, 0);
241 return media->streams->len;
245 * gst_rtsp_media_get_stream:
246 * @media: a #GstRTSPMedia
247 * @idx: the stream index
249 * Retrieve the stream with index @idx from @media.
251 * Returns: the #GstRTSPMediaStream at index @idx.
254 gst_rtsp_media_get_stream (GstRTSPMedia *media, guint idx)
256 GstRTSPMediaStream *res;
258 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
259 g_return_val_if_fail (media->prepared, 0);
260 g_return_val_if_fail (idx < media->streams->len, NULL);
262 res = g_array_index (media->streams, GstRTSPMediaStream *, idx);