rtsp.c: new example
authorJim Hollinger <hollinger.jim@gmail.com>
Wed, 10 Aug 2011 08:54:53 +0000 (10:54 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 10 Aug 2011 08:54:53 +0000 (10:54 +0200)
Code from
http://code.google.com/p/rtsprequest/source/browse/trunk/rtsprequest.cpp

docs/examples/rtsp.c [new file with mode: 0644]

diff --git a/docs/examples/rtsp.c b/docs/examples/rtsp.c
new file mode 100644 (file)
index 0000000..2fe912c
--- /dev/null
@@ -0,0 +1,253 @@
+/*\r
+ * Copyright (c) 2011, Jim Hollinger\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *   * Redistributions of source code must retain the above copyright\r
+ *     notice, this list of conditions and the following disclaimer.\r
+ *   * Redistributions in binary form must reproduce the above copyright\r
+ *     notice, this list of conditions and the following disclaimer in the\r
+ *     documentation and/or other materials provided with the distribution.\r
+ *   * Neither the name of Jim Hollinger nor the names of its contributors\r
+ *     may be used to endorse or promote products derived from this\r
+ *     software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+\r
+#if defined (WIN32)\r
+#  include <conio.h>  // _getch()\r
+#else\r
+#  include <termios.h>\r
+#  include <unistd.h>\r
+\r
+  int _getch(void) {\r
+    struct termios oldt, newt;\r
+    tcgetattr( STDIN_FILENO, &oldt );\r
+    newt = oldt;\r
+    newt.c_lflag &= ~( ICANON | ECHO );\r
+    tcsetattr( STDIN_FILENO, TCSANOW, &newt );\r
+    int ch = getchar();\r
+    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );\r
+    return ch;\r
+  }\r
+#endif\r
+\r
+#include <curl/curl.h>\r
+\r
+#define VERSION_STR  "V1.0"\r
+\r
+// error handling macros\r
+#define my_curl_easy_setopt(A, B, C) \\r
+  if ((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \\r
+    fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", #A, #B, #C, res);\r
+\r
+#define my_curl_easy_perform(A) \\r
+  if ((res = curl_easy_perform((A))) != CURLE_OK) \\r
+    fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res);\r
+\r
+\r
+// send RTSP OPTIONS request\r
+void rtsp_options(CURL *curl, const char *uri) {\r
+    CURLcode res = CURLE_OK;\r
+    printf("\nRTSP: OPTIONS %s\n", uri);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_OPTIONS);\r
+    my_curl_easy_perform(curl);\r
+}\r
+\r
+\r
+// send RTSP DESCRIBE request and write sdp response to a file\r
+void rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename) {\r
+    CURLcode res = CURLE_OK;\r
+    printf("\nRTSP: DESCRIBE %s\n", uri);\r
+    FILE *sdp_fp = fopen(sdp_filename, "wt");\r
+    if (sdp_fp == NULL) {\r
+        fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename);\r
+        sdp_fp = stdout;\r
+    } else {\r
+        printf("Writing SDP to '%s'\n", sdp_filename);\r
+    }\r
+    my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE);\r
+    my_curl_easy_perform(curl);\r
+    my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);\r
+    if (sdp_fp != stdout) {\r
+        fclose(sdp_fp);\r
+    }\r
+}\r
+\r
+\r
+// send RTSP SETUP request\r
+void rtsp_setup(CURL *curl, const char *uri, const char *transport) {\r
+    CURLcode res = CURLE_OK;\r
+    printf("\nRTSP: SETUP %s\n", uri);\r
+    printf("      TRANSPORT %s\n", transport);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP);\r
+    my_curl_easy_perform(curl);\r
+}\r
+\r
+\r
+// send RTSP PLAY request\r
+void rtsp_play(CURL *curl, const char *uri, const char *range) {\r
+    CURLcode res = CURLE_OK;\r
+    printf("\nRTSP: PLAY %s\n", uri);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);\r
+    my_curl_easy_setopt(curl, CURLOPT_RANGE, range);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);\r
+    my_curl_easy_perform(curl);\r
+}\r
+\r
+\r
+// send RTSP TEARDOWN request\r
+void rtsp_teardown(CURL *curl, const char *uri) {\r
+    CURLcode res = CURLE_OK;\r
+    printf("\nRTSP: TEARDOWN %s\n", uri);\r
+    my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_TEARDOWN);\r
+    my_curl_easy_perform(curl);\r
+}\r
+\r
+\r
+// convert url into an sdp filename\r
+void get_sdp_filename(const char *url, char *sdp_filename) {\r
+    strcpy(sdp_filename, "video.sdp");\r
+    const char *s = strrchr(url, '/');\r
+    if (s != NULL) {\r
+        s++;\r
+        if (s[0] != '\0') {\r
+            sprintf(sdp_filename, "%s.sdp", s);\r
+        }\r
+    }\r
+}\r
+\r
+\r
+// scan sdp file for media control attribute\r
+void get_media_control_attribute(const char *sdp_filename, char *control) {\r
+    control[0] = '\0';\r
+    int max_len = 256;\r
+    char *s = new char[max_len];\r
+    FILE *sdp_fp = fopen(sdp_filename, "rt");\r
+    if (sdp_fp != NULL) {\r
+        while (fgets(s, max_len - 2, sdp_fp) != NULL) {\r
+            sscanf(s, " a = control: %s", control);\r
+        }\r
+        fclose(sdp_fp);\r
+    }\r
+    delete []s;\r
+}\r
+\r
+\r
+// main app\r
+int main(int argc, char * const argv[]) {\r
+    const char *transport = "RTP/AVP;unicast;client_port=1234-1235";  // UDP           \r
+//    const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235";  // TCP\r
+    const char *range = "0.000-";\r
+    int rc = EXIT_SUCCESS;\r
+\r
+    printf("\nRTSP request %s\n", VERSION_STR);\r
+    printf("    Project web site: http://code.google.com/p/rtsprequest/\n");\r
+    printf("    Requires cURL V7.20 or greater\n\n");\r
+\r
+    // check command line\r
+    char *basename = NULL;\r
+    if ((argc != 2) && (argc != 3)) {\r
+        basename = strrchr(argv[0], '/');\r
+        if (basename == NULL) {\r
+            basename = strrchr(argv[0], '\\');\r
+        }\r
+        if (basename == NULL) {\r
+            basename = argv[0];\r
+        } else {\r
+            basename++;\r
+        }\r
+        printf("Usage:   %s url [transport]\n", basename);\r
+        printf("         url of video server\n");\r
+        printf("         transport (optional) specifier for media stream protocol\n");\r
+        printf("         default transport: %s\n", transport);\r
+        printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", basename);\r
+        rc = EXIT_FAILURE;\r
+    } else {\r
+        const char *url = argv[1];\r
+        char *uri = new char[strlen(url) + 32];\r
+        char *sdp_filename = new char[strlen(url) + 32];\r
+        char *control = new char[strlen(url) + 32];\r
+        get_sdp_filename(url, sdp_filename);\r
+        if (argc == 3) {\r
+            transport = argv[2];\r
+        }\r
+\r
+        // initialize curl\r
+        CURLcode res = CURLE_OK;\r
+        res = curl_global_init(CURL_GLOBAL_ALL);\r
+        if (res == CURLE_OK) {\r
+            curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);\r
+            fprintf(stderr, "    cURL V%s loaded\n", data->version);\r
+\r
+            // initialize this curl session\r
+            CURL *curl = curl_easy_init();\r
+            if (curl != NULL) {\r
+                my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);\r
+                my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);\r
+                my_curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout);\r
+                my_curl_easy_setopt(curl, CURLOPT_URL, url);\r
+\r
+                // request server options\r
+                sprintf(uri, "%s", url);\r
+                rtsp_options(curl, uri);\r
+\r
+                // request session description and write response to sdp file\r
+                rtsp_describe(curl, uri, sdp_filename);\r
+\r
+                // get media control attribute from sdp file\r
+                get_media_control_attribute(sdp_filename, control);\r
+\r
+                // setup media stream\r
+                sprintf(uri, "%s/%s", url, control);\r
+                rtsp_setup(curl, uri, transport);\r
+\r
+                // start playing media stream\r
+                sprintf(uri, "%s/", url);\r
+                rtsp_play(curl, uri, range);\r
+                printf("Playing video, press any key to stop ...");\r
+                _getch();\r
+                printf("\n");\r
+\r
+                // teardown session\r
+                rtsp_teardown(curl, uri);\r
+\r
+                // cleanup\r
+                curl_easy_cleanup(curl);\r
+                curl = NULL;\r
+            } else {\r
+                fprintf(stderr, "curl_easy_init() failed\n");\r
+            }\r
+            curl_global_cleanup();\r
+        } else {\r
+            fprintf(stderr, "curl_global_init(%s) failed\n", "CURL_GLOBAL_ALL", res);\r
+        }\r
+        delete []control;\r
+        delete []sdp_filename;\r
+        delete []uri;\r
+    }\r
+\r
+    return rc;\r
+}\r