Live555: RtspServer 示例

概述

live555官方地址:http://www.live555.com/liveMedia/public/。该地址下有live555的工程包(比如:live.2019.10.20.tar.gz),以及h264/h265的测试流文件。本文以live.2019.10.20.tar.gz为例,介绍live555 rtspserver示例代码。

live555工程的目录结构如下所示,其中testProgs则为测试用例,本文将以OnDemandServerMediaSubsession.cpp为例,解说示例如何运行,以及代码结构。

├── BasicUsageEnvironment
│   └── include
├── groupsock
│   └── include
├── liveMedia
│   └── include
├── mediaServer
├── proxyServer
├── testProgs
├── UsageEnvironment
│   └── include
└── WindowsAudioInputDevice

运行示例

1、编译live555工程,确认testProgs中的源文件被编译。
2、执行OnDemandServerMediaSubsession,在执行该程序的目录下必须存在test.h264的码流文件。比如笔者在userdata目录下存在test.h264,则可直接在userdata目录下运行OnDemandServerMediaSubsession。完整的步骤和log如下所示:

[root@rk3328:/userdata]# ls test.264 -al
-rwxrwxrwx 1 root root 87402 Oct 17 06:56 test.264
[root@rk3328:/userdata]# 
[root@rk3328:/userdata]# /usr/bin/testOnDemandRTSPServer

"mpeg4ESVideoTest" stream, from the file "test.m4e"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/mpeg4ESVideoTest"

"h264ESVideoTest" stream, from the file "test.264"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/h264ESVideoTest"

"h265ESVideoTest" stream, from the file "test.265"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/h265ESVideoTest"

"mpeg1or2AudioVideoTest" stream, from the file "test.mpg"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/mpeg1or2AudioVideoTest"

"mpeg1or2ESVideoTest" stream, from the file "testv.mpg"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/mpeg1or2ESVideoTest"

"mp3AudioTest" stream, from the file "test.mp3"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/mp3AudioTest"

"wavAudioTest" stream, from the file "test.wav"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/wavAudioTest"

"amrAudioTest" stream, from the file "test.amr"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/amrAudioTest"

"vobTest" stream, from the file "test.vob"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/vobTest"

"mpeg2TransportStreamTest" stream, from the file "test.ts"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/mpeg2TransportStreamTest"

"aacAudioTest" stream, from the file "test.aac"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/aacAudioTest"

"dvVideoTest" stream, from the file "test.dv"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/dvVideoTest"

"ac3AudioTest" stream, from the file "test.ac3"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/ac3AudioTest"

"matroskaFileTest" stream, from the file "test.mkv"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/matroskaFileTest"

"webmFileTest" stream, from the file "test.webm"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/webmFileTest"

"oggFileTest" stream, from the file "test.ogg"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/oggFileTest"

"opusFileTest" stream, from the file "test.opus"
Play this stream using the URL "rtsp://xxx.xxx.xxx.xxx:8554/opusFileTest"

"mpeg2TransportStreamFromUDPSourceTest" stream, from a UDP Transport Stream input source 
        (IP multicast address xxx.255.xxx.xxx, port 1234)
Play this stream using the URL "rtsp://xxx.xxx.xxx.45:8554/mpeg2TransportStreamFromUDPSourceTest"

(We use port 80 for optional RTSP-over-HTTP tunneling.)

3、确保运行live555示例机器与VLC播放器机器网络互通。然后使用VLC播放器选择上述H264对应的流地址,即可播放。
Live555: RtspServer 示例_第1张图片

代码介绍

代码主体结构很简单,main函数实现如下:

int main(int argc, char** argv) {
     
  //Live555 提供的接口,这两行必须要创建。
  // Begin by setting up our usage environment:
  TaskScheduler* scheduler = BasicTaskScheduler::createNew();
  env = BasicUsageEnvironment::createNew(*scheduler);

  UserAuthenticationDatabase* authDB = NULL;
#ifdef ACCESS_CONTROL
  // To implement client access control to the RTSP server, do the following:
  authDB = new UserAuthenticationDatabase;
  authDB->addUserRecord("username1", "password1"); // replace these with real strings
  // Repeat the above with each ,  that you wish to allow
  // access to the server.
#endif

  // Create the RTSP server:
  RTSPServer* rtspServer = RTSPServer::createNew(*env, 8554, authDB);
  if (rtspServer == NULL) {
     
    *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
    exit(1);
  }

..........

  // A H.264 video elementary stream:
  {
     
    // rtsp流名称。
    char const* streamName = "h264ESVideoTest";
    // 输入h264码流文件名称。
    char const* inputFileName = "test.264";
    // 创建ServerMediaSessio,对应一个rtsp网络地址
    ServerMediaSession* sms
      = ServerMediaSession::createNew(*env, streamName, streamName,
				      descriptionString);
	// 创建一个H264的MediaSubSession,并加入到sms中,
	// 一个MediaSubSession对应一个媒体资源。
    sms->addSubsession(H264VideoFileServerMediaSubsession
		       ::createNew(*env, inputFileName, reuseFirstSource));
    // 加入RtspServer即可完成。
    rtspServer->addServerMediaSession(sms);

    announceStream(rtspServer, sms, streamName, inputFileName);
  }

..........

  // Also, attempt to create a HTTP server for RTSP-over-HTTP tunneling.
  // Try first with the default HTTP port (80), and then with the alternative HTTP
  // port numbers (8000 and 8080).

  if (rtspServer->setUpTunnelingOverHTTP(80) || rtspServer->setUpTunnelingOverHTTP(8000) || rtspServer->setUpTunnelingOverHTTP(8080)) {
     
    *env << "\n(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling.)\n";
  } else {
     
    *env << "\n(RTSP-over-HTTP tunneling is not available.)\n";
  }

  // 姑且理解为mainLoop
  env->taskScheduler().doEventLoop(); // does not return

  return 0; // only to prevent compiler warning
}

【 总结】:从上面代码结构来看,创建一个rtsp server非常简单。H264VideoFileServerMediaSubsession为Live555预先实现好的H264媒体文件类,我们只需要调用该类并传入文件名即可完成RTSP server的推流。

你可能感兴趣的:(Live555,live555,rtspserver,h264)