Download rtsp.c

1. [代码][C/C++]代码

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

时间: 2025-01-11 23:13:22

Download rtsp.c的相关文章

基于ffmpeg+SDL2 实现简单rtsp播放器

参考资料: 编译参考:         http://blog.chinaunix.net/uid-20718335-id-2980793.html 代码参考:         http://blog.csdn.net/leixiaohua1020/article/details/8652605 实现ffmpeg在window下编译,并基于ffmpeg动态库用测试程序播放本地文件和RTSP视频流 csdn博客插个图片怎么这么麻烦,上篇辛辛苦苦截了那么多图一上传全没了,代码里想变色结果发布了一看全

利用vlc插件将IP摄像头嵌入网页和网页播放RTSP流

1. 描述 最近有一个项目需要将IP摄像机的画面嵌入到web网页中,考虑到减少开发成本,使用vlc插件播放摄像头提供的RTSP流.在videolan wiki的官网详细介绍了关于vlc web plugin的使用方法. 有一点需要注意的是,vlc2.2.0以前的版本,wiki上提供的方法却不再适用.原因是vlc的last一个版本中没有axvlc.cab文件了,最新的的一个在0.9.2版本对应的目录中.而且在IE中还回应为这个cab文件没有签名而无法安装此插件. 2. 解决办法 使用2.2.0以后

分享几个可用的rtsp, http測试url

rtsp://218.204.223.237:554/live/1/0547424F573B085C/gsfp90ef4k0a6iap.sdp rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp http://download.wavetlan.com/SVV/EmbededContent/BlackBerry.3gp 此外假设网速好的话还能够用aarnet(澳大利亚学术研究网),还支持ipv6, 曾经我

分享几个可用的rtsp, http测试url

rtsp://218.204.223.237:554/live/1/0547424F573B085C/gsfp90ef4k0a6iap.sdp rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp http://download.wavetlan.com/SVV/EmbededContent/BlackBerry.3gp 此外如果网速好的话还可以用aarnet(澳大利亚学术研究网),还支持ipv6, 以前我

ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)

原文  http://blog.csdn.net/gubenpeiyuan/article/details/25618177 概要: 前言及鸣谢: 感谢guog先生,快活林高先生,onvif全国交流群的的酷夏先生在开发过程中给予的巨大支持,没有你们的帮助开发过程将异常艰难啊.谢谢了! ONVIF介绍: ONVIF致力于通过全球性的开放接口标准来推进 网络视频 在安防市场的应用,这一接口标准将确保不同厂商生产的网络视频产品具有互通性.2008年11月,论坛正式发布了ONVIF第一版规范——ONVI

rtsp学习----海康RTSP客户端连接深入分析

转载于:http://blog.csdn.net/zhouyongku/article/details/41546789 海康相机RTSP连接代码分析 最近在做海康相机rtsp连接获取音视频的工作,现在介绍一下分析过程和源码. [源码在我上传的共享资料中:http://download.csdn.net/detail/zhouyongku/8203521] 一.基本原理 RTSP客户端去连接服务器的最基本步骤如下: (1)分析url中的端口号,创建一个与服务器[S-PORT]端口号的TCP连接用

网页中插入VLC播放器播放rtsp视频流步骤

1.      仿照http://download.csdn.net/detail/haowenxin123456789/8044245 中步骤: 2.      从http://www.videolan.org/vlc/index.html  中下载 vlc-2.2.1-win32.exe 并安装到D:\\ProgramFiles文件夹下: 3.  运行:vegsvr32  D:\\ProgramFiles\\VideoLAN\\VLC\\axvlc.dll : 4.  test.html 文

Streaming Video with RTSP and RTP

The Code In this lab you will implement a streaming video server and client that communicate using the Real-Time Streaming Protocol (RTSP) and send data using the Real-time Transfer Protocol (RTP). Your task is to implement the RTSP protocol in the c

android-vlc for rtsp build OK

最近研究 rtsp http stream 流获取方式 vlc over live555  是个非常不错的选择,当然了 andorid framework av也是支持rtsp http的,同样不错的选择 1.编译 https://wiki.videolan.org/AndroidCompile 我是ubuntu 12.04 可能是环境安装的一些东西比较好的缘故吧,呃,貌似还有一个必备条件,网络.如果没有这个你会遇到 莫名奇妙的问题的.我以前编译这个就不支持rtsp很费解 直接按照上面的步骤就可