天天看点

live555 实现一个最简单的RTSP服务器及其makefile

myser.cpp 和makefile 放在live\mediaServer文件夹中

myser.cpp

#include <BasicUsageEnvironment.hh>

#include "liveMedia.hh"

static ServerMediaSession* createNewSMS(UsageEnvironment& env, char const* fileName);

int main()

{

    TaskScheduler* scheduler;

    UsageEnvironment* env ;

    RTSPServer* rtspServer;

    ServerMediaSession* sms;

    //创建任务调用器

    scheduler = BasicTaskScheduler::createNew();

    //创建交互环境

    env = BasicUsageEnvironment::createNew(*scheduler);

    //创建RTSP服务器

    rtspServer = RTSPServer::createNew(*env,554);    //使用554端口

    if(rtspServer == NULL)

    {

        rtspServer = RTSPServer::createNew(*env,8554);   //554端口被占用,就使用8554端口

    }

    //打印服务器地址

    *env << "Play streams from this server using the URL\n\t"

         << rtspServer->rtspURLPrefix() << "<filename>.\n";

    //创建session

    sms = createNewSMS(*env, "test.mpg");

    rtspServer->addServerMediaSession(sms);

    //添加其它文件对应的session...

    //进行事件循环

    env->taskScheduler().doEventLoop(); // does not return

    return 0;

}

static ServerMediaSession* createNewSMS(UsageEnvironment& env, char const* fileName)

{

    char const* extension = strrchr(fileName, '.');

    ServerMediaSession* sms = NULL;

    Boolean reuseSource = False;    //不重用source

    sms = ServerMediaSession::createNew(env, fileName);  //创建一个session

    //

    //在session中添加subssesion

    //

    if(strcmp(extension, ".aac") == 0)

    {

        sms->addSubsession(ADTSAudioFileServerMediaSubsession::createNew(env, fileName, reuseSource));

    }

    else if(strcmp(extension, ".264") == 0)

    {

        sms->addSubsession(H264VideoFileServerMediaSubsession::createNew(env, fileName, reuseSource));

    }

    else if(strcmp(extension, ".mpg") == 0)

    {

        MPEG1or2FileServerDemux* demux

          = MPEG1or2FileServerDemux::createNew(env, fileName, reuseSource);

        sms->addSubsession(demux->newVideoServerMediaSubsession());

        sms->addSubsession(demux->newAudioServerMediaSubsession());

    }

    //添加其它媒体格式支持...

    return sms;

}

makefile

INCLUDES = -I../UsageEnvironment/include -I../groupsock/include -I../liveMedia/include -I../BasicUsageEnvironment/include

##### Change the following for your environment: 

COMPILE_OPTS = $(INCLUDES) -I. -O2 -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64

C = c

C_COMPILER = cc

C_FLAGS = $(COMPILE_OPTS)

CPP = cpp

CPLUSPLUS_COMPILER = g++

CPLUSPLUS_FLAGS = $(COMPILE_OPTS) -Wall -DBSD=1

OBJ = o

LINK = g++ -o

LINK_OPTS = -L.

CONSOLE_LINK_OPTS = $(LINK_OPTS)

LIBRARY_LINK = ar cr 

LIBRARY_LINK_OPTS =

LIB_SUFFIX = a

LIBS_FOR_CONSOLE_APPLICATION =

LIBS_FOR_GUI_APPLICATION =

EXE =

##### End of variables to change

MEDIA_SERVER = myser$(EXE)

ALL = $(MEDIA_SERVER)

all: $(ALL)

.$(C).$(OBJ):

$(C_COMPILER) -c $(C_FLAGS) $<       

.$(CPP).$(OBJ):

$(CPLUSPLUS_COMPILER) -c $(CPLUSPLUS_FLAGS) $<

MEDIA_SERVER_OBJS = myser.$(OBJ) 

#myser.$(CPP): liveMedia.hh

USAGE_ENVIRONMENT_DIR = ../UsageEnvironment

USAGE_ENVIRONMENT_LIB = $(USAGE_ENVIRONMENT_DIR)/libUsageEnvironment.$(LIB_SUFFIX)

BASIC_USAGE_ENVIRONMENT_DIR = ../BasicUsageEnvironment

BASIC_USAGE_ENVIRONMENT_LIB = $(BASIC_USAGE_ENVIRONMENT_DIR)/libBasicUsageEnvironment.$(LIB_SUFFIX)

LIVEMEDIA_DIR = ../liveMedia

LIVEMEDIA_LIB = $(LIVEMEDIA_DIR)/libliveMedia.$(LIB_SUFFIX)

GROUPSOCK_DIR = ../groupsock

GROUPSOCK_LIB = $(GROUPSOCK_DIR)/libgroupsock.$(LIB_SUFFIX)

LOCAL_LIBS = $(LIVEMEDIA_LIB) $(GROUPSOCK_LIB) \

$(BASIC_USAGE_ENVIRONMENT_LIB) $(USAGE_ENVIRONMENT_LIB)

LIBS = $(LOCAL_LIBS) $(LIBS_FOR_CONSOLE_APPLICATION)

myser$(EXE): $(MEDIA_SERVER_OBJS) $(LOCAL_LIBS)

$(LINK)[email protected] $(CONSOLE_LINK_OPTS) $(MEDIA_SERVER_OBJS) $(LIBS)

clean:

-rm -rf *.$(OBJ) $(ALL) core *.core *~ include/*~

##### Any additional, platform-specific rules come here: