682
785
RTSPTransport * transport)
788
GstPad *outpad = NULL;
789
GstPadTemplate *template;
686
790
GstStateChangeReturn ret;
793
const gchar *mime, *manager;
689
796
src = stream->parent;
691
if (!(stream->rtpdec = gst_element_factory_make ("rtpdec", NULL)))
694
/* we manage this element */
695
gst_rtspsrc_add_element (src, stream->rtpdec);
698
gst_element_set_state (stream->rtpdec,
699
GST_STATE_PAUSED)) != GST_STATE_CHANGE_SUCCESS)
700
goto start_rtpdec_failure;
702
stream->rtpdecrtp = gst_element_get_pad (stream->rtpdec, "sinkrtp");
703
stream->rtpdecrtcp = gst_element_get_pad (stream->rtpdec, "sinkrtcp");
798
GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
800
s = gst_caps_get_structure (stream->caps, 0);
802
if ((res = rtsp_transport_get_mime (transport->trans, &mime)) < 0)
807
GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
808
/* configure the final mime type */
809
gst_structure_set_name (s, mime);
811
if ((res = rtsp_transport_get_manager (transport->trans, &manager)) < 0)
815
GST_DEBUG_OBJECT (src, "using manager %s", manager);
816
/* FIXME, the session manager needs to be shared with all the streams */
817
if (!(stream->sess = gst_element_factory_make (manager, NULL)))
820
/* we manage this element */
821
gst_bin_add (GST_BIN_CAST (src), stream->sess);
823
ret = gst_element_set_state (stream->sess, GST_STATE_PAUSED);
824
if (ret != GST_STATE_CHANGE_SUCCESS)
825
goto start_session_failure;
827
stream->channelpad[0] = gst_element_get_pad (stream->sess, "sinkrtp");
828
stream->channelpad[1] = gst_element_get_pad (stream->sess, "sinkrtcp");
705
831
if (transport->lower_transport == RTSP_LOWER_TRANS_TCP) {
706
834
/* configure for interleaved delivery, nothing needs to be done
707
835
* here, the loop function will call the chain functions of the
708
* rtp session manager. */
836
* session manager. */
837
stream->channel[0] = transport->interleaved.min;
838
stream->channel[1] = transport->interleaved.max;
839
GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
840
stream->channel[0], stream->channel[1]);
842
/* we can remove the allocated UDP ports now */
843
for (i = 0; i < 2; i++) {
844
if (stream->udpsrc[i]) {
845
gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
846
gst_object_unref (stream->udpsrc[i]);
847
stream->udpsrc[i] = NULL;
850
/* no session manager, send data to srcpad directly */
851
if (!stream->channelpad[0]) {
852
GST_DEBUG_OBJECT (src, "no manager, creating pad");
854
name = g_strdup_printf ("stream%d", stream->id);
855
template = gst_static_pad_template_get (&rtptemplate);
856
stream->srcpad = gst_pad_new_from_template (template, name);
857
gst_object_unref (template);
860
gst_pad_use_fixed_caps (stream->srcpad);
861
gst_pad_set_caps (stream->srcpad, stream->caps);
863
stream->channelpad[0] = gst_object_ref (stream->srcpad);
864
gst_pad_set_active (stream->srcpad, TRUE);
865
gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
867
GST_DEBUG_OBJECT (src, "using manager source pad");
868
outpad = gst_element_get_pad (stream->sess, "srcrtp");
710
/* configure for UDP delivery, we need to connect the udp pads to
711
* the rtp session plugin. */
712
pad = gst_element_get_pad (stream->rtpsrc, "src");
713
gst_pad_link (pad, stream->rtpdecrtp);
714
gst_object_unref (pad);
716
pad = gst_element_get_pad (stream->rtcpsrc, "src");
717
gst_pad_link (pad, stream->rtpdecrtcp);
718
gst_object_unref (pad);
721
pad = gst_element_get_pad (stream->rtpdec, "srcrtp");
722
name = g_strdup_printf ("rtp_stream%d", stream->id);
723
gst_element_add_pad (GST_ELEMENT_CAST (src), gst_ghost_pad_new (name, pad));
725
gst_object_unref (pad);
871
/* multicast was selected, create UDP sources and join the multicast
873
if (transport->lower_transport == RTSP_LOWER_TRANS_UDP_MCAST) {
876
GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
878
/* creating UDP source */
879
if (transport->port.min != -1) {
880
uri = g_strdup_printf ("udp://%s:%d", transport->destination,
881
transport->port.min);
882
stream->udpsrc[0] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
884
if (stream->udpsrc[0] == NULL)
888
gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
891
/* creating another UDP source */
892
if (transport->port.max != -1) {
893
uri = g_strdup_printf ("udp://%s:%d", transport->destination,
894
transport->port.max);
895
stream->udpsrc[1] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
897
if (stream->udpsrc[1] == NULL)
900
gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
904
/* we manage the UDP elements now. For unicast, the UDP sources where
905
* allocated in the stream when we suggested a transport. */
906
if (stream->udpsrc[0]) {
909
gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
911
GST_DEBUG_OBJECT (src, "setting up UDP source");
914
g_object_set (G_OBJECT (stream->udpsrc[0]), "caps", stream->caps, NULL);
916
/* configure a timeout on the UDP port */
917
g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", src->timeout,
920
if (stream->channelpad[0]) {
921
GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
922
/* configure for UDP delivery, we need to connect the UDP pads to
923
* the session plugin. */
924
pad = gst_element_get_pad (stream->udpsrc[0], "src");
925
gst_pad_link (pad, stream->channelpad[0]);
926
gst_object_unref (pad);
928
outpad = gst_element_get_pad (stream->sess, "srcrtp");
930
GST_DEBUG_OBJECT (src, "using UDP src pad as output");
931
outpad = gst_element_get_pad (stream->udpsrc[0], "src");
935
if (stream->udpsrc[1]) {
936
gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
938
if (stream->channelpad[1]) {
941
GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
943
pad = gst_element_get_pad (stream->udpsrc[1], "src");
944
gst_pad_link (pad, stream->channelpad[1]);
945
gst_object_unref (pad);
950
if (outpad && !stream->srcpad) {
951
GST_DEBUG_OBJECT (src, "creating ghostpad");
953
gst_pad_use_fixed_caps (outpad);
954
gst_pad_set_caps (outpad, stream->caps);
956
/* create ghostpad */
957
name = g_strdup_printf ("stream%d", stream->id);
958
template = gst_static_pad_template_get (&rtptemplate);
959
stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
960
gst_object_unref (template);
963
gst_object_unref (outpad);
966
gst_pad_set_active (stream->srcpad, TRUE);
967
gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
970
stream->last_ret = GST_FLOW_OK;
977
GST_DEBUG_OBJECT (src, "unknown transport");
982
GST_DEBUG_OBJECT (src, "cannot get a session manager");
731
GST_DEBUG ("no rtpdec element found");
987
GST_DEBUG_OBJECT (src, "no rtpdec element found");
734
start_rtpdec_failure:
990
start_session_failure:
736
GST_DEBUG ("could not start RTP session");
992
GST_DEBUG_OBJECT (src, "could not start session");
742
find_stream (GstRTSPStream * stream, gconstpointer a)
998
find_stream_by_channel (GstRTSPStream * stream, gconstpointer a)
744
1000
gint channel = GPOINTER_TO_INT (a);
746
if (stream->rtpchannel == channel || stream->rtcpchannel == channel)
1002
if (stream->channel[0] == channel || stream->channel[1] == channel)
753
gst_rtspsrc_loop (GstRTSPSrc * src)
1008
static GstFlowReturn
1009
gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
1014
/* store the value */
1015
stream->last_ret = ret;
1017
/* if it's success we can return the value right away */
1018
if (GST_FLOW_IS_SUCCESS (ret))
1021
/* any other error that is not-linked can be returned right
1023
if (ret != GST_FLOW_NOT_LINKED)
1026
/* only return NOT_LINKED if all other pads returned NOT_LINKED */
1027
for (streams = src->streams; streams; streams = g_list_next (streams)) {
1028
GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
1030
ret = ostream->last_ret;
1031
/* some other return value (must be SUCCESS but we can return
1032
* other values as well) */
1033
if (ret != GST_FLOW_NOT_LINKED)
1036
/* if we get here, all other pads were unlinked and we return
1037
* NOT_LINKED then */
1043
gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
1047
for (streams = src->streams; streams; streams = g_list_next (streams)) {
1048
GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
1050
/* only pads that have a connection to the outside world */
1051
if (ostream->srcpad == NULL)
1054
if (ostream->channelpad[0]) {
1055
gst_event_ref (event);
1056
if (GST_PAD_IS_SRC (ostream->channelpad[0]))
1057
gst_pad_push_event (ostream->channelpad[0], event);
1059
gst_pad_send_event (ostream->channelpad[0], event);
1062
if (ostream->channelpad[1]) {
1063
gst_event_ref (event);
1064
if (GST_PAD_IS_SRC (ostream->channelpad[1]))
1065
gst_pad_push_event (ostream->channelpad[1], event);
1067
gst_pad_send_event (ostream->channelpad[1], event);
1070
gst_event_unref (event);
1074
gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
755
1076
RTSPMessage response = { 0 };
795
1125
goto unknown_stream;
797
1127
/* and chain buffer to internal element */
801
buf = gst_buffer_new_and_alloc (size);
802
memcpy (GST_BUFFER_DATA (buf), data, size);
804
if (gst_pad_chain (outpad, buf) != GST_FLOW_OK)
1128
rtsp_message_steal_body (&response, &data, &size);
1130
/* strip the trailing \0 */
1133
buf = gst_buffer_new ();
1134
GST_BUFFER_DATA (buf) = data;
1135
GST_BUFFER_MALLOCDATA (buf) = data;
1136
GST_BUFFER_SIZE (buf) = size;
1138
/* don't need message anymore */
1139
rtsp_message_unset (&response);
1142
gst_buffer_set_caps (buf, caps);
1144
GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
1147
/* chain to the peer pad */
1148
if (GST_PAD_IS_SINK (outpad))
1149
ret = gst_pad_chain (outpad, buf);
1151
ret = gst_pad_push (outpad, buf);
1153
/* combine all stream flows */
1154
ret = gst_rtspsrc_combine_flows (src, stream, ret);
1155
if (ret != GST_FLOW_OK)
1163
GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
1164
rtsp_message_unset (&response);
814
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
815
("Could not receive message."), (NULL));
817
gst_pad_push_event (src->srcpad, gst_event_new (GST_EVENT_EOS));
1169
gchar *str = rtsp_strresult (res);
1171
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
1172
("Could not receive message. (%s)", str));
1176
rtsp_message_dump (&response);
1178
rtsp_message_unset (&response);
1179
ret = GST_FLOW_UNEXPECTED;
819
1180
goto need_pause;
1184
GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
1185
("Short message received."));
1186
rtsp_message_unset (&response);
823
gst_task_pause (src->task);
1191
const gchar *reason = gst_flow_get_name (ret);
1193
GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1194
src->running = FALSE;
1195
gst_task_pause (src->task);
1196
if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1197
if (ret == GST_FLOW_UNEXPECTED) {
1198
/* perform EOS logic */
1199
if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1200
gst_element_post_message (GST_ELEMENT_CAST (src),
1201
gst_message_new_segment_done (GST_OBJECT_CAST (src),
1202
src->segment.format, src->segment.last_stop));
1204
gst_rtspsrc_push_event (src, gst_event_new_eos ());
1207
/* for fatal errors we post an error message, post the error
1208
* first so the app knows about the error first. */
1209
GST_ELEMENT_ERROR (src, STREAM, FAILED,
1210
("Internal data flow error."),
1211
("streaming task paused, reason %s (%d)", reason, ret));
1212
gst_rtspsrc_push_event (src, gst_event_new_eos ());
1220
gst_rtspsrc_loop_udp (GstRTSPSrc * src)
1222
gboolean restart = FALSE;
1224
GST_OBJECT_LOCK (src);
1225
if (src->loop_cmd == CMD_STOP)
1228
while (src->loop_cmd == CMD_WAIT) {
1229
GST_DEBUG_OBJECT (src, "waiting");
1230
GST_RTSP_LOOP_WAIT (src);
1231
GST_DEBUG_OBJECT (src, "waiting done");
1232
if (src->loop_cmd == CMD_STOP)
1235
if (src->loop_cmd == CMD_RECONNECT) {
1236
/* FIXME, when we get here we have to reconnect using tcp */
1237
src->loop_cmd = CMD_WAIT;
1240
GST_OBJECT_UNLOCK (src);
1243
gst_rtspsrc_pause (src);
1246
/* stop task, we cannot join as this would deadlock */
1247
gst_task_stop (src->task);
1248
/* and free the task so that _close will not stop/join it again. */
1249
gst_object_unref (GST_OBJECT (src->task));
1252
gst_rtspsrc_close (src);
1254
/* see if we have TCP left to try */
1255
if (src->cur_protocols & RTSP_LOWER_TRANS_TCP) {
1258
/* We post a warning message now to inform the user
1259
* that nothing happened. It's most likely a firewall thing. */
1260
GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
1261
("Could not receive any UDP packets for %.4f seconds, maybe your "
1262
"firewall is blocking it. Retrying using a TCP connection.",
1263
(gdouble) src->timeout / 1000000));
1264
/* we can try only TCP now */
1265
src->cur_protocols = RTSP_LOWER_TRANS_TCP;
1267
pos = strstr (src->location, "://");
1271
url = g_strdup_printf ("rtspt://%s", pos + 3);
1273
gst_element_post_message (GST_ELEMENT_CAST (src),
1274
gst_message_new_element (GST_OBJECT_CAST (src),
1275
gst_structure_new ("redirect",
1276
"new-location", G_TYPE_STRING, url, NULL)));
1279
src->cur_protocols = 0;
1280
/* no transport possible, post an error and stop */
1281
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
1282
("Could not connect to server, no protocols left"));
1290
GST_OBJECT_UNLOCK (src);
1291
src->running = FALSE;
1292
gst_task_pause (src->task);
1297
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
1298
("Could not redirect, location %s is invalid", src->location));
1304
gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd)
1306
GST_OBJECT_LOCK (src);
1307
src->loop_cmd = cmd;
1308
GST_RTSP_LOOP_SIGNAL (src);
1309
GST_OBJECT_UNLOCK (src);
1313
gst_rtspsrc_loop (GstRTSPSrc * src)
1315
if (src->interleaved)
1316
gst_rtspsrc_loop_interleaved (src);
1318
gst_rtspsrc_loop_udp (src);
1322
gst_rtspsrc_handle_request (GstRTSPSrc * src, RTSPMessage * request)
1324
RTSPMessage response = { 0 };
1327
res = rtsp_message_init_response (&response, RTSP_STS_OK, "OK", request);
1332
rtsp_message_dump (&response);
1334
if ((res = rtsp_connection_send (src->connection, &response)) < 0)
1342
gchar *str = rtsp_strresult (res);
1344
GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
1345
("Could not send message. (%s)", str));
1353
* @src: the rtsp source
1354
* @request: must point to a valid request
1355
* @response: must point to an empty #RTSPMessage
1357
* send @request and retrieve the response in @response. optionally @code can be
1358
* non-NULL in which case it will contain the status code of the response.
1360
* If This function returns TRUE, @response will contain a valid response
1361
* message that should be cleaned with rtsp_message_unset() after usage.
1363
* If @code is NULL, this function will return FALSE (with an invalid @response
1364
* message) if the response code was not 200 (OK).
1366
* Returns: TRUE if the processing was successful.
829
1369
gst_rtspsrc_send (GstRTSPSrc * src, RTSPMessage * request,
830
1370
RTSPMessage * response, RTSPStatusCode * code)
1373
RTSPStatusCode thecode;
1374
gchar *content_base = NULL;
1376
if (src->extension && src->extension->before_send)
1377
src->extension->before_send (src->extension, request);
835
1380
rtsp_message_dump (request);
837
1382
if ((res = rtsp_connection_send (src->connection, request)) < 0)
838
1383
goto send_error;
840
1386
if ((res = rtsp_connection_receive (src->connection, response)) < 0)
841
1387
goto receive_error;
844
*code = response->type_data.response.code;
848
1390
rtsp_message_dump (response);
1392
switch (response->type) {
1393
case RTSP_MESSAGE_REQUEST:
1394
/* FIXME, handle server request, reply with OK, for now */
1395
if ((res = gst_rtspsrc_handle_request (src, response)) < 0)
1396
goto handle_request_failed;
1398
case RTSP_MESSAGE_RESPONSE:
1399
/* ok, a response is good */
1402
case RTSP_MESSAGE_DATA:
1403
/* get next response */
850
if (response->type_data.response.code != RTSP_STS_OK)
1407
thecode = response->type_data.response.code;
1408
/* if the caller wanted the result code, we store it. Else we check if it's
1412
else if (thecode != RTSP_STS_OK)
851
1413
goto error_response;
1415
/* store new content base if any */
1416
rtsp_message_get_header (response, RTSP_HDR_CONTENT_BASE, &content_base);
1418
g_free (src->content_base);
1419
src->content_base = g_strdup (content_base);
1422
if (src->extension && src->extension->after_send)
1423
src->extension->after_send (src->extension, request, response);
857
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
858
("Could not send message."), (NULL));
1430
gchar *str = rtsp_strresult (res);
1432
GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
1433
("Could not send message. (%s)", str));
863
GST_ELEMENT_ERROR (src, RESOURCE, READ,
864
("Could not receive message."), (NULL));
1439
gchar *str = rtsp_strresult (res);
1441
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
1442
("Could not receive message. (%s)", str));
1446
handle_request_failed:
1448
/* ERROR was posted */
869
GST_ELEMENT_ERROR (src, RESOURCE, READ, ("Got error response: %d (%s).",
870
response->type_data.response.code,
871
response->type_data.response.reason), (NULL));
1453
switch (response->type_data.response.code) {
1454
case RTSP_STS_NOT_FOUND:
1455
GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
1456
response->type_data.response.reason));
1459
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
1460
("Got error response: %d (%s).", response->type_data.response.code,
1461
response->type_data.response.reason));
1464
/* we return FALSE so we should unset the response ourselves */
1465
rtsp_message_unset (response);
1470
/* parse the response and collect all the supported methods. We need this
1471
* information so that we don't try to send an unsupported request to the
1475
gst_rtspsrc_parse_methods (GstRTSPSrc * src, RTSPMessage * response)
1477
gchar *respoptions = NULL;
1481
/* clear supported methods */
1484
/* Try Allow Header first */
1485
rtsp_message_get_header (response, RTSP_HDR_ALLOW, &respoptions);
1487
/* Then maybe Public Header... */
1488
rtsp_message_get_header (response, RTSP_HDR_PUBLIC, &respoptions);
1490
/* this field is not required, assume the server supports
1491
* DESCRIBE, SETUP and PLAY */
1492
GST_DEBUG_OBJECT (src, "could not get OPTIONS");
1493
src->methods = RTSP_DESCRIBE | RTSP_SETUP | RTSP_PLAY | RTSP_PAUSE;
1497
/* If we get here, the server gave a list of supported methods, parse
1498
* them here. The string is like:
1500
* OPTIONS, DESCRIBE, ANNOUNCE, PLAY, SETUP, ...
1502
options = g_strsplit (respoptions, ",", 0);
1504
for (i = 0; options[i]; i++) {
1508
stripped = g_strstrip (options[i]);
1509
method = rtsp_find_method (stripped);
1511
/* keep bitfield of supported methods */
1513
src->methods |= method;
1515
g_strfreev (options);
1517
/* we need describe and setup */
1518
if (!(src->methods & RTSP_DESCRIBE))
1520
if (!(src->methods & RTSP_SETUP))
1529
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
1530
("Server does not support DESCRIBE."));
1535
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
1536
("Server does not support SETUP."));
1542
gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
1543
RTSPLowerTrans protocols, gchar ** transports)
1549
if (src->extension && src->extension->get_transports)
1551
src->extension->get_transports (src->extension, protocols,
1555
/* extension listed transports, use those */
1556
if (*transports != NULL)
1559
result = g_strdup ("");
1560
if (protocols & RTSP_LOWER_TRANS_UDP) {
1563
GST_DEBUG_OBJECT (src, "adding UDP unicast");
1566
g_strconcat (result, "RTP/AVP/UDP;unicast;client_port=%%u1-%%u2", NULL);
1570
if (protocols & RTSP_LOWER_TRANS_UDP_MCAST) {
1573
GST_DEBUG_OBJECT (src, "adding UDP multicast");
1575
/* we don't have to allocate any UDP ports yet, if the selected transport
1576
* turns out to be multicast we can create them and join the multicast
1577
* group indicated in the transport reply */
1578
new = g_strconcat (result, result[0] ? "," : "",
1579
"RTP/AVP/UDP;multicast", NULL);
1583
if (protocols & RTSP_LOWER_TRANS_TCP) {
1586
GST_DEBUG_OBJECT (src, "adding TCP");
1588
new = g_strconcat (result, result[0] ? "," : "",
1589
"RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2", NULL);
1593
*transports = result;
1605
gst_rtspsrc_configure_transports (GstRTSPStream * stream, gchar ** transports)
1608
gint nr_udp, nr_int;
1610
gint rtpport = 0, rtcpport = 0;
1613
src = stream->parent;
1615
/* find number of placeholders first */
1616
if (strstr (*transports, "%%i2"))
1618
else if (strstr (*transports, "%%i1"))
1623
if (strstr (*transports, "%%u2"))
1625
else if (strstr (*transports, "%%u1"))
1630
if (nr_udp == 0 && nr_int == 0)
1634
if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
1638
str = g_string_new ("");
1640
while ((next = strstr (p, "%%"))) {
1641
g_string_append_len (str, p, next - p);
1642
if (next[2] == 'u') {
1644
g_string_append_printf (str, "%d", rtpport);
1645
else if (next[3] == '2')
1646
g_string_append_printf (str, "%d", rtcpport);
1648
if (next[2] == 'i') {
1650
g_string_append_printf (str, "%d", src->free_channel);
1651
else if (next[3] == '2')
1652
g_string_append_printf (str, "%d", src->free_channel + 1);
1658
g_free (*transports);
1659
*transports = g_string_free (str, FALSE);
877
1672
gst_rtspsrc_open (GstRTSPSrc * src)
881
1675
RTSPMessage request = { 0 };
882
1676
RTSPMessage response = { 0 };
885
1680
SDPMessage sdp = { 0 };
886
GstRTSPProto protocols;
889
GST_DEBUG ("parsing url...");
890
if ((res = rtsp_url_parse (src->location, &url)) < 0)
893
/* open connection */
894
GST_DEBUG ("opening connection...");
895
if ((res = rtsp_connection_open (url, &src->connection)) < 0)
1681
RTSPLowerTrans protocols;
1682
GstRTSPStream *stream = NULL;
1683
gchar *respcont = NULL;
1685
/* reset our state */
1686
src->free_channel = 0;
1687
src->interleaved = FALSE;
1688
gst_segment_init (&src->segment, GST_FORMAT_TIME);
1690
/* can't continue without a valid url */
1691
if (G_UNLIKELY (src->url == NULL))
1694
/* create connection */
1695
GST_DEBUG_OBJECT (src, "creating connection (%s)...", src->location);
1696
if ((res = rtsp_connection_create (src->url, &src->connection)) < 0)
1697
goto could_not_create;
1700
GST_DEBUG_OBJECT (src, "connecting (%s)...", src->location);
1701
if ((res = rtsp_connection_connect (src->connection)) < 0)
1702
goto could_not_connect;
898
1704
/* create OPTIONS */
899
GST_DEBUG ("create options...");
901
rtsp_message_init_request (RTSP_OPTIONS, src->location,
1705
GST_DEBUG_OBJECT (src, "create options...");
1706
res = rtsp_message_init_request (&request, RTSP_OPTIONS, src->location);
903
1708
goto create_request_failed;
905
1710
/* send OPTIONS */
906
GST_DEBUG ("send options...");
1711
GST_DEBUG_OBJECT (src, "send options...");
907
1712
if (!gst_rtspsrc_send (src, &request, &response, NULL))
908
1713
goto send_error;
911
gchar *respoptions = NULL;
915
/* Try Allow Header first */
916
rtsp_message_get_header (&response, RTSP_HDR_ALLOW, &respoptions);
918
/* Then maybe Public Header... */
919
rtsp_message_get_header (&response, RTSP_HDR_PUBLIC, &respoptions);
921
/* this field is not required, assume the server supports
922
* DESCRIBE and SETUP*/
923
GST_DEBUG_OBJECT (src, "could not get OPTIONS");
924
src->options = RTSP_DESCRIBE | RTSP_SETUP;
930
options = g_strsplit (respoptions, ",", 0);
937
stripped = g_strdup (options[i]);
938
stripped = g_strstrip (stripped);
939
method = rtsp_find_method (stripped);
942
/* keep bitfield of supported methods */
944
src->options |= method;
947
g_strfreev (options);
950
/* we need describe and setup */
951
if (!(src->options & RTSP_DESCRIBE))
953
if (!(src->options & RTSP_SETUP))
1716
if (!gst_rtspsrc_parse_methods (src, &response))
957
1719
/* create DESCRIBE */
958
GST_DEBUG ("create describe...");
960
rtsp_message_init_request (RTSP_DESCRIBE, src->location,
1720
GST_DEBUG_OBJECT (src, "create describe...");
1721
res = rtsp_message_init_request (&request, RTSP_DESCRIBE, src->location);
962
1723
goto create_request_failed;
963
/* we accept SDP for now */
1725
/* we only accept SDP for now */
964
1726
rtsp_message_add_header (&request, RTSP_HDR_ACCEPT, "application/sdp");
1728
/* prepare global stream caps properties */
1730
gst_structure_remove_all_fields (src->props);
1732
src->props = gst_structure_empty_new ("RTSP Properties");
966
1734
/* send DESCRIBE */
967
GST_DEBUG ("send describe...");
1735
GST_DEBUG_OBJECT (src, "send describe...");
968
1736
if (!gst_rtspsrc_send (src, &request, &response, NULL))
969
1737
goto send_error;
971
1739
/* check if reply is SDP */
973
gchar *respcont = NULL;
975
rtsp_message_get_header (&response, RTSP_HDR_CONTENT_TYPE, &respcont);
976
/* could not be set but since the request returned OK, we assume it
977
* was SDP, else check it. */
979
if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
980
goto wrong_content_type;
1740
rtsp_message_get_header (&response, RTSP_HDR_CONTENT_TYPE, &respcont);
1741
/* could not be set but since the request returned OK, we assume it
1742
* was SDP, else check it. */
1744
if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
1745
goto wrong_content_type;
1748
/* get message body and parse as SDP */
985
1749
rtsp_message_get_body (&response, &data, &size);
987
GST_DEBUG ("parse sdp...");
1751
GST_DEBUG_OBJECT (src, "parse SDP...");
988
1752
sdp_message_init (&sdp);
989
1753
sdp_message_parse_buffer (data, size, &sdp);
992
1756
sdp_message_dump (&sdp);
994
/* we allow all configured protocols */
995
protocols = src->protocols;
1758
if (src->extension && src->extension->parse_sdp)
1759
src->extension->parse_sdp (src->extension, &sdp);
1761
/* we initially allow all configured lower transports. based on the URL
1762
* transports and the replies from the server we narrow them down. */
1763
protocols = src->url->transports & src->cur_protocols;
996
1765
/* setup streams */
1000
for (i = 0; i < sdp_message_medias_len (&sdp); i++) {
1005
GstRTSPStream *stream;
1007
media = sdp_message_get_media (&sdp, i);
1009
stream = gst_rtspsrc_create_stream (src);
1011
GST_DEBUG ("setup media %d", i);
1012
control_url = sdp_media_get_attribute_val (media, "control");
1013
if (control_url == NULL) {
1014
GST_DEBUG ("no control url found, skipping stream");
1018
/* check absolute/relative URL */
1019
/* FIXME, what if the control_url starts with a '/' or a non rtsp: protocol? */
1020
if (g_str_has_prefix (control_url, "rtsp://")) {
1021
setup_url = g_strdup (control_url);
1023
setup_url = g_strdup_printf ("%s/%s", src->location, control_url);
1026
GST_DEBUG ("setup %s", setup_url);
1027
/* create SETUP request */
1029
rtsp_message_init_request (RTSP_SETUP, setup_url,
1032
goto create_request_failed;
1036
transports = g_strdup ("");
1037
if (protocols & GST_RTSP_PROTO_UDP_UNICAST) {
1039
gint rtpport, rtcpport;
1042
/* allocate two udp ports */
1043
if (!gst_rtspsrc_stream_setup_rtp (stream, media, &rtpport, &rtcpport))
1044
goto setup_rtp_failed;
1046
trxparams = g_strdup_printf ("client_port=%d-%d", rtpport, rtcpport);
1047
new = g_strconcat (transports, "RTP/AVP/UDP;unicast;", trxparams, NULL);
1049
g_free (transports);
1052
if (protocols & GST_RTSP_PROTO_UDP_MULTICAST) {
1056
g_strconcat (transports, transports[0] ? "," : "",
1057
"RTP/AVP/UDP;multicast", NULL);
1058
g_free (transports);
1061
if (protocols & GST_RTSP_PROTO_TCP) {
1065
g_strconcat (transports, transports[0] ? "," : "", "RTP/AVP/TCP",
1067
g_free (transports);
1071
/* select transport, copy is made when adding to header */
1072
rtsp_message_add_header (&request, RTSP_HDR_TRANSPORT, transports);
1073
g_free (transports);
1075
if (!gst_rtspsrc_send (src, &request, &response, NULL))
1078
/* parse response transport */
1080
gchar *resptrans = NULL;
1081
RTSPTransport transport = { 0 };
1083
rtsp_message_get_header (&response, RTSP_HDR_TRANSPORT, &resptrans);
1087
/* parse transport */
1088
rtsp_transport_parse (resptrans, &transport);
1089
/* update allowed transports for other streams */
1090
if (transport.lower_transport == RTSP_LOWER_TRANS_TCP) {
1091
protocols = GST_RTSP_PROTO_TCP;
1766
n_streams = sdp_message_medias_len (&sdp);
1767
for (i = 0; i < n_streams; i++) {
1770
/* create stream from the media, can never return NULL */
1771
stream = gst_rtspsrc_create_stream (src, &sdp, i);
1773
/* see if we need to configure this stream */
1774
if (src->extension && src->extension->configure_stream) {
1775
if (!src->extension->configure_stream (src->extension, stream)) {
1776
GST_DEBUG_OBJECT (src, "skipping stream %d, disabled by extension", i);
1781
/* merge/overwrite global caps */
1786
s = gst_caps_get_structure (stream->caps, 0);
1788
num = gst_structure_n_fields (src->props);
1789
for (j = 0; j < num; j++) {
1793
name = gst_structure_nth_field_name (src->props, j);
1794
val = gst_structure_get_value (src->props, name);
1795
gst_structure_set_value (s, name, val);
1797
GST_DEBUG_OBJECT (src, "copied %s", name);
1801
/* skip setup if we have no URL for it */
1802
if (stream->setup_url == NULL) {
1803
GST_DEBUG_OBJECT (src, "skipping stream %d, no setup", i);
1807
GST_DEBUG_OBJECT (src, "doing setup of stream %d with %s", i,
1810
/* create SETUP request */
1811
res = rtsp_message_init_request (&request, RTSP_SETUP, stream->setup_url);
1813
goto create_request_failed;
1815
/* create a string with all the transports */
1816
res = gst_rtspsrc_create_transports_string (src, protocols, &transports);
1818
goto setup_transport_failed;
1820
/* replace placeholders with real values */
1821
res = gst_rtspsrc_configure_transports (stream, &transports);
1823
goto setup_transport_failed;
1825
/* select transport, copy is made when adding to header */
1826
rtsp_message_add_header (&request, RTSP_HDR_TRANSPORT, transports);
1827
g_free (transports);
1829
if (!gst_rtspsrc_send (src, &request, &response, NULL))
1832
/* parse response transport */
1834
gchar *resptrans = NULL;
1835
RTSPTransport transport = { 0 };
1837
rtsp_message_get_header (&response, RTSP_HDR_TRANSPORT, &resptrans);
1841
/* parse transport */
1842
if (rtsp_transport_parse (resptrans, &transport) != RTSP_OK)
1845
/* update allowed transports for other streams. once the transport of
1846
* one stream has been determined, we make sure that all other streams
1847
* are configured in the same way */
1848
switch (transport.lower_transport) {
1849
case RTSP_LOWER_TRANS_TCP:
1850
GST_DEBUG_OBJECT (src, "stream %d as TCP interleaved", i);
1851
protocols = RTSP_LOWER_TRANS_TCP;
1092
1852
src->interleaved = TRUE;
1094
if (transport.multicast) {
1095
/* disable unicast */
1096
protocols = GST_RTSP_PROTO_UDP_MULTICAST;
1098
/* disable multicast */
1099
protocols = GST_RTSP_PROTO_UDP_UNICAST;
1853
/* update free channels */
1855
MAX (transport.interleaved.min, src->free_channel);
1857
MAX (transport.interleaved.max, src->free_channel);
1858
src->free_channel++;
1860
case RTSP_LOWER_TRANS_UDP_MCAST:
1861
/* only allow multicast for other streams */
1862
GST_DEBUG_OBJECT (src, "stream %d as UDP multicast", i);
1863
protocols = RTSP_LOWER_TRANS_UDP_MCAST;
1865
case RTSP_LOWER_TRANS_UDP:
1866
/* only allow unicast for other streams */
1867
GST_DEBUG_OBJECT (src, "stream %d as UDP unicast", i);
1868
protocols = RTSP_LOWER_TRANS_UDP;
1871
GST_DEBUG_OBJECT (src, "stream %d unknown transport %d", i,
1872
transport.lower_transport);
1876
if (!stream->container || !src->interleaved) {
1102
1877
/* now configure the stream with the transport */
1103
1878
if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
1104
GST_DEBUG ("could not configure stream transport, skipping stream");
1879
GST_DEBUG_OBJECT (src,
1880
"could not configure stream %d transport, skipping stream", i);
1106
/* clean up our transport struct */
1107
rtsp_transport_init (&transport);
1884
/* clean up our transport struct */
1885
rtsp_transport_init (&transport);
1888
if (src->extension && src->extension->stream_select)
1889
src->extension->stream_select (src->extension);
1891
/* if we got here all was configured. We have dynamic pads so we notify that
1893
gst_element_no_more_pads (GST_ELEMENT_CAST (src));
1895
/* clean up any messages */
1896
rtsp_message_unset (&request);
1897
rtsp_message_unset (&response);
1116
GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1117
("Not a valid RTSP url."), (NULL));
1122
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE,
1123
("Could not open connection."), (NULL));
1904
GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
1905
("No valid RTSP URL was provided"));
1910
gchar *str = rtsp_strresult (res);
1912
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
1913
("Could not create connection. (%s)", str));
1919
gchar *str = rtsp_strresult (res);
1921
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
1922
("Could not connect to server. (%s)", str));
1126
1926
create_request_failed:
1128
GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1129
("Could not create request."), (NULL));
1928
gchar *str = rtsp_strresult (res);
1930
GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
1931
("Could not create request. (%s)", str));
1134
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
1135
("Could not send message."), (NULL));
1140
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
1141
("Server does not support DESCRIBE."), (NULL));
1146
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
1147
("Server does not support SETUP."), (NULL));
1937
gchar *str = rtsp_strresult (res);
1939
GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
1940
("Could not send message. (%s)", str));
1946
/* error was posted */
1150
1949
wrong_content_type:
1152
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
1153
("Server does not support SDP."), (NULL));
1951
GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1952
("Server does not support SDP, got %s.", respcont));
1955
setup_transport_failed:
1158
GST_ELEMENT_ERROR (src, RESOURCE, WRITE, ("Could not setup rtp."), (NULL));
1957
GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1958
("Could not setup transport."));
1163
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
1164
("Server did not select transport."), (NULL));
1963
GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
1964
("Server did not select transport."));
1969
rtsp_message_unset (&request);
1970
rtsp_message_unset (&response);