`

live555 源代码简单分析1:主程序

 
阅读更多

live555是使用十分广泛的开源流媒体服务器,之前也看过其他人写的live555的学习笔记,在这里自己简单总结下。

live555源代码有以下几个明显的特点:

1.头文件是.hh后缀的,但没觉得和.h后缀的有什么不同

2.采用了面向对象的程序设计思路,里面各种对象

 

好了,不罗嗦,使用vc2010打开live555的vc工程,看到live555源代码结构如下:

 

 

源代码由5个工程构成(4个库和一个主程序):

libUsageEnvironment.lib;libliveMedia.lib;libgroupsock.lib;libBasicUsageEnvironment.lib;以及live555MediaServer

这里我们只分析live555MediaServer这个主程序,其实代码量并不大,主要有两个CPP:DynamicRTSPServer.cpp和live555MediaServer.cpp

程序的main()在live555MediaServer.cpp中,在main()中调用了DynamicRTSPServer中的类

 

不废话,直接贴上有注释的源码

live555MediaServer.cpp:

#include <BasicUsageEnvironment.hh>
#include "DynamicRTSPServer.hh"
#include "version.hh"

int main(int argc, char** argv) {
  // Begin by setting up our usage environment:
  // TaskScheduler用于任务计划
  TaskScheduler* scheduler = BasicTaskScheduler::createNew();
  // UsageEnvironment用于输出
  UsageEnvironment* 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 <username>, <password> that you wish to allow
  // access to the server.
#endif

  //建立 RTSP server.  使用默认端口 (554),
  // and then with the alternative port number (8554):
  RTSPServer* rtspServer;
  portNumBits rtspServerPortNum = 554;
  //创建 RTSPServer实例
  rtspServer = DynamicRTSPServer::createNew(*env, rtspServerPortNum, authDB);
  if (rtspServer == NULL) {
    rtspServerPortNum = 8554;
    rtspServer = DynamicRTSPServer::createNew(*env, rtspServerPortNum, authDB);
  }
  if (rtspServer == NULL) {
    *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
    exit(1);
  }
  //用到了运算符重载
  *env << "LIVE555 Media Server\n";
  *env << "\tversion " << MEDIA_SERVER_VERSION_STRING
       << " (LIVE555 Streaming Media library version "
       << LIVEMEDIA_LIBRARY_VERSION_STRING << ").\n";

  char* urlPrefix = rtspServer->rtspURLPrefix();
  *env << "Play streams from this server using the URL\n\t"
       << urlPrefix << "<filename>\nwhere <filename> is a file present in the current directory.\n";
  *env << "Each file's type is inferred from its name suffix:\n";
  *env << "\t\".aac\" => an AAC Audio (ADTS format) file\n";
  *env << "\t\".amr\" => an AMR Audio file\n";
  *env << "\t\".m4e\" => a MPEG-4 Video Elementary Stream file\n";
  *env << "\t\".dv\" => a DV Video file\n";
  *env << "\t\".mp3\" => a MPEG-1 or 2 Audio file\n";
  *env << "\t\".mpg\" => a MPEG-1 or 2 Program Stream (audio+video) file\n";
  *env << "\t\".ts\" => a MPEG Transport Stream file\n";
  *env << "\t\t(a \".tsx\" index file - if present - provides server 'trick play' support)\n";
  *env << "\t\".wav\" => a WAV Audio file\n";
  *env << "See http://www.live555.com/mediaServer/ for additional documentation.\n";

  // 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 << "(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling.)\n";
  } else {
    *env << "(RTSP-over-HTTP tunneling is not available.)\n";
  }
  //进入一个永久的循环
  env->taskScheduler().doEventLoop(); // does not return

  return 0; // only to prevent compiler warning
}


DynamicRTSPServer.cpp:

#include "DynamicRTSPServer.hh"
#include <liveMedia.hh>
#include <string.h>

DynamicRTSPServer*
DynamicRTSPServer::createNew(UsageEnvironment& env, Port ourPort,
			     UserAuthenticationDatabase* authDatabase,
			     unsigned reclamationTestSeconds) {
  int ourSocket = -1;

  do {
	//建立TCP socket(socket(),bind(),listen()...)
    int ourSocket = setUpOurSocket(env, ourPort);
    if (ourSocket == -1) break;

    return new DynamicRTSPServer(env, ourSocket, ourPort, authDatabase, reclamationTestSeconds);
  } while (0);

  if (ourSocket != -1) ::closeSocket(ourSocket);
  return NULL;
}

DynamicRTSPServer::DynamicRTSPServer(UsageEnvironment& env, int ourSocket,
				     Port ourPort,
				     UserAuthenticationDatabase* authDatabase, unsigned reclamationTestSeconds)
  : RTSPServer(env, ourSocket, ourPort, authDatabase, reclamationTestSeconds) {
}

DynamicRTSPServer::~DynamicRTSPServer() {
}

static ServerMediaSession* createNewSMS(UsageEnvironment& env,
					char const* fileName, FILE* fid); // forward



//查找ServerMediaSession(对应服务器上一个媒体文件,,或设备),如果没有的话就创建一个
//streamName例:A.avi
ServerMediaSession*
DynamicRTSPServer::lookupServerMediaSession(char const* streamName) {
  // First, check whether the specified "streamName" exists as a local file:
  FILE* fid = fopen(streamName, "rb");
  //如果返回文件指针不为空,则文件存在
  Boolean fileExists = fid != NULL;

  // Next, check whether we already have a "ServerMediaSession" for this file:
  //看看是否有这个ServerMediaSession
  ServerMediaSession* sms = RTSPServer::lookupServerMediaSession(streamName);
  Boolean smsExists = sms != NULL;

  // Handle the four possibilities for "fileExists" and "smsExists":
  //文件没了,ServerMediaSession有,删之
  if (!fileExists) {
    if (smsExists) {
      // "sms" was created for a file that no longer exists. Remove it:
      removeServerMediaSession(sms);
    }
    return NULL;
  } else {
	//文件有,ServerMediaSession无,加之
    if (!smsExists) {
      // Create a new "ServerMediaSession" object for streaming from the named file.
      sms = createNewSMS(envir(), streamName, fid);
      addServerMediaSession(sms);
    }
    fclose(fid);
    return sms;
  }
}

#define NEW_SMS(description) do {\
char const* descStr = description\
    ", streamed by the LIVE555 Media Server";\
sms = ServerMediaSession::createNew(env, fileName, fileName, descStr);\
} while(0)


//创建一个ServerMediaSession
static ServerMediaSession* createNewSMS(UsageEnvironment& env,
					char const* fileName, FILE* /*fid*/) {
  // Use the file name extension to determine the type of "ServerMediaSession":
	//获取扩展名,以“.”开始。不严密,万一文件名有多个点?
  char const* extension = strrchr(fileName, '.');
  if (extension == NULL) return NULL;

  ServerMediaSession* sms = NULL;
  Boolean const reuseSource = False;
  if (strcmp(extension, ".aac") == 0) {
    // Assumed to be an AAC Audio (ADTS format) file:
	// 调用ServerMediaSession::createNew()
	//还会调用MediaSubsession
    NEW_SMS("AAC Audio");
    sms->addSubsession(ADTSAudioFileServerMediaSubsession::createNew(env, fileName, reuseSource));
  } else if (strcmp(extension, ".amr") == 0) {
    // Assumed to be an AMR Audio file:
    NEW_SMS("AMR Audio");
    sms->addSubsession(AMRAudioFileServerMediaSubsession::createNew(env, fileName, reuseSource));
  } else if (strcmp(extension, ".m4e") == 0) {
    // Assumed to be a MPEG-4 Video Elementary Stream file:
    NEW_SMS("MPEG-4 Video");
    sms->addSubsession(MPEG4VideoFileServerMediaSubsession::createNew(env, fileName, reuseSource));
  } else if (strcmp(extension, ".mp3") == 0) {
    // Assumed to be a MPEG-1 or 2 Audio file:
    NEW_SMS("MPEG-1 or 2 Audio");
    // To stream using 'ADUs' rather than raw MP3 frames, uncomment the following:
//#define STREAM_USING_ADUS 1
    // To also reorder ADUs before streaming, uncomment the following:
//#define INTERLEAVE_ADUS 1
    // (For more information about ADUs and interleaving,
    //  see <http://www.live555.com/rtp-mp3/>)
    Boolean useADUs = False;
    Interleaving* interleaving = NULL;
#ifdef STREAM_USING_ADUS
    useADUs = True;
#ifdef INTERLEAVE_ADUS
    unsigned char interleaveCycle[] = {0,2,1,3}; // or choose your own...
    unsigned const interleaveCycleSize
      = (sizeof interleaveCycle)/(sizeof (unsigned char));
    interleaving = new Interleaving(interleaveCycleSize, interleaveCycle);
#endif
#endif
    sms->addSubsession(MP3AudioFileServerMediaSubsession::createNew(env, fileName, reuseSource, useADUs, interleaving));
  } else if (strcmp(extension, ".mpg") == 0) {
    // Assumed to be a MPEG-1 or 2 Program Stream (audio+video) file:
    NEW_SMS("MPEG-1 or 2 Program Stream");
    MPEG1or2FileServerDemux* demux
      = MPEG1or2FileServerDemux::createNew(env, fileName, reuseSource);
    sms->addSubsession(demux->newVideoServerMediaSubsession());
    sms->addSubsession(demux->newAudioServerMediaSubsession());
  } else if (strcmp(extension, ".ts") == 0) {
    // Assumed to be a MPEG Transport Stream file:
    // Use an index file name that's the same as the TS file name, except with ".tsx":
    unsigned indexFileNameLen = strlen(fileName) + 2; // allow for trailing "x\0"
    char* indexFileName = new char[indexFileNameLen];
    sprintf(indexFileName, "%sx", fileName);
    NEW_SMS("MPEG Transport Stream");
    sms->addSubsession(MPEG2TransportFileServerMediaSubsession::createNew(env, fileName, indexFileName, reuseSource));
    delete[] indexFileName;
  } else if (strcmp(extension, ".wav") == 0) {
    // Assumed to be a WAV Audio file:
    NEW_SMS("WAV Audio Stream");
    // To convert 16-bit PCM data to 8-bit u-law, prior to streaming,
    // change the following to True:
    Boolean convertToULaw = False;
    sms->addSubsession(WAVAudioFileServerMediaSubsession::createNew(env, fileName, reuseSource, convertToULaw));
  } else if (strcmp(extension, ".dv") == 0) {
    // Assumed to be a DV Video file
    // First, make sure that the RTPSinks' buffers will be large enough to handle the huge size of DV frames (as big as 288000).
    OutPacketBuffer::maxSize = 300000;

    NEW_SMS("DV Video");
    sms->addSubsession(DVVideoFileServerMediaSubsession::createNew(env, fileName, reuseSource));
  }

  return sms;
}


live555源代码(VC6):http://download.csdn.net/detail/leixiaohua1020/6374387

分享到:
评论

相关推荐

    RTSP服务器实例live555源代码分析.doc

    在这个文档中,我们将深入分析Live555源代码,了解RTSP服务器如何建立连接以及处理DESCRIBE请求。 1. RTSP连接的建立过程: - 首先,通过RTSPServer类创建RTSP服务器。RTSPServer内部包含RTSPClientSession类,...

    live555服务器端代码

    这个文件可能包含主程序、配置文件、以及可能的测试脚本,帮助用户理解和实践Live555的功能。 总的来说,Live555服务器端代码是一个强大的工具,它使得开发和调试流媒体服务器变得简单。通过学习和利用这个源代码,...

    live555 Qt工程

    【压缩包子文件的文件名称列表】"livePro"可能是项目的主程序文件或者是包含整个项目源代码的文件夹。通常在这样的工程中,会包含`.cpp`和`.h`文件,这些是C++源代码和头文件,用于定义类和函数;可能还有`.pro`文件...

    live555在mips嵌入平台上的移植

    总结,将Live555移植到MIPS嵌入式平台涉及了多个关键步骤,包括编译环境的配置、源代码调整、编译构建以及最终的应用测试。通过上述过程,可以实现在资源受限的MIPS设备上运行实时流媒体应用,满足特定场景下的通信...

    live555 在window下的lib编译工程

    接着,编辑项目主文件(通常是`main.cpp`),添加一些简单的测试代码,比如初始化live555环境、创建会话等,以验证编译后的库是否正常工作。例如: ```cpp #include "liveMedia.hh" #include "GroupsockHelper.hh" ...

    运用live555编译后的库实现demo调用例子

    在"live555配置后源码工程"的压缩包中,你应该找到了编译后的Live555库及其相关的源代码示例。这些示例代码是用于演示如何使用Live555库来创建一个基本的RTSP服务器。RTSP服务器的主要任务是接收客户端的请求,如...

    基于live555实现H264实时流RTSP发送(VS2010)

    1. **编译Live555库**:首先,需要下载并编译Live555源代码,确保其与Visual Studio 2010兼容。这通常涉及到配置预处理器宏、设置编译选项以及链接器设置。 2. **设置H264编码器**:H264是一种高效的视频编码标准,...

    在vs2008上建立工程编译live555(20130703),适合调试

    在“新建项目”对话框中,选择“Win32控制台应用程序”,输入项目名称(如“Live555Server”),然后点击“确定”。 步骤三:配置工程设置 在接下来的向导中,选择“空项目”,确保“生成DLL”选项未被勾选。点击...

    简易抓包程序源代码c++

    本资源是一个使用C++编写的简易抓包程序的源代码,旨在帮助开发者更好地理解网络数据包的结构,并进行学习与实践。以下是该程序可能涉及的关键知识点及详细解释: 1. **网络层协议解析**:抓包程序的核心功能是对...

    live.2018.01.29.tar.gz|live.2018.01.29.tar.gz

    该压缩包中的"live"文件夹包含了完整的Live555源代码,包括头文件、源文件和相关的构建脚本。在使用之前,我们需要先解压文件,然后进入解压后的目录进行编译。编译过程通常包括以下步骤: 1. **配置环境**:确保你...

    屏幕RTSP服务,使用VS2013开发,采用live555做服务,ffmpeg进行抓屏和编解码

    `ScreenServer`可能是一个源代码文件夹,包含该项目的主要源代码,如服务器的主程序和相关模块。`Release`目录则存储了编译后的可执行文件和其他相关输出,供用户运行和测试。而`ffmpeg`和`live555`目录可能分别包含...

    live555-master

    这个名为"live555-master"的压缩包包含了Live555项目的源代码和项目文件,允许开发者深入理解其内部工作原理并进行定制化开发。 Live555的核心功能是支持多种实时通信协议,包括Real-time Transport Protocol (RTP)...

    rtsp-video-server:基于Live555和FFmpeg的RTSP视频流服务器实现

    源代码文件,如服务器的主程序和Live555及FFmpeg的接口实现;可能还有配置文件和测试脚本。 通过rtsp-video-server,我们可以学习如何整合Live555和FFmpeg来构建一个高效的RTSP服务器,包括: 1. 实现RTSP服务器的...

    C++源代码的简单实时评分应用程序.zip

    项目中的关键文件“Simple Live Scoring App using C++”很可能是包含主程序逻辑的源代码文件。在C++中,我们可以使用标准库如`iostream`来处理输入输出,`vector`来动态存储和管理数据,以及`algorithm`来执行排序...

    live555 + vs2015

    值得注意的是,live555源码中的"code"目录下的"live"子目录,正是包含了核心的源代码文件。这部分代码实现了对流媒体协议的解析和处理,开发者可以通过阅读和理解这部分源码,深入了解实时流媒体的工作原理,并进行...

    SNMP监控软件源代码 基于WinPcap

    "pktdump_ex.c"很可能是主程序源代码,实现了数据包捕获和解析的功能。".dsp"和".dsw"是Visual Studio的项目文件,用于构建和管理C++项目。开发者可以使用这些文件来编译和调试SNMP监控软件。 在深入源代码之前,你...

    libpcap源代码分析及网络数据包过滤机制

    1. **主入口函数**:程序通常以简单的框架形式组织,如上面给出的例子所示。 2. **设备管理**:通过`pcap_lookupdev()`函数实现设备的查询,`pcap_open_live()`用于打开指定设备并配置捕获参数。 3. **过滤器管理**...

    计算机网络高级软件编程技术第6章 Tracert程序源代码

    在第6章“计算机网络高级软件编程技术”中,Tracert程序源代码的分析与实现是核心内容,这将深入探讨网络协议、ICMP(Internet Control Message Protocol)以及如何利用这些技术进行网络故障排查。 首先,Tracert的...

    Windows+Phone+7+完美开发征程源代码

    《Windows Phone 7 完美开发征程源代码详解》 Windows Phone 7(简称WP7)是微软在移动设备上推出的操作系统,旨在为开发者提供一个全新的平台来创建丰富的、引人入胜的应用程序。本资源集合名为"Windows Phone 7 ...

    网络嗅探器Sniffer 源代码.zip

    《网络嗅探器Sniffer:深入理解源代码》 网络嗅探器,也称为网络抓包工具,是一种用于捕获网络上数据包的软件。在本文中,我们将深入探讨一个基于VC++实现的网络嗅探器——Sniffer的源代码。通过对`Sniffer`源代码...

Global site tag (gtag.js) - Google Analytics