live555实现264直播

代码可以参考这篇文章


http://blog.csdn.net/firehood_/article/details/16844397

作者抽丝剥茧,用最简洁的方式实现了rtsp server。

代码需要稍微修改,保证读文件是按顺序的:

bool CRTSPStream::SendH264File(const char *pFileName)
{
	if(pFileName == NULL)
	{
		return false;
	}
	FILE *fp = fopen(pFileName, "rb");  
	if(!fp)  
	{  
		printf("[RTSPStream] error:open file %s failed!",pFileName);
	}  
	fseek(fp, 0, SEEK_SET);

	unsigned char *buffer  = new unsigned char[FILEBUFSIZE];
	int pos = 0;
	while(1)
	{
		int readlen = fread(buffer, sizeof(unsigned char), FILEBUFSIZE, fp);

		if(readlen<=0)
		{
			perror("readlen \n");
			break;
		}
printf("readlen:%d,pos:%d\n",readlen,pos);
		//readlen+=pos;
pos += readlen;
		int writelen = SendH264Data(buffer,readlen);
	/*	if(writelen<=0)
		{
			printf("writelen  %d\n",writelen);
			perror("a\n");
			break;
		}
		*/
		//memcpy(buffer,buffer+writelen,readlen-writelen);
		//memcpy(buffer,buffer+writelen,readlen-writelen);
		//pos = readlen-writelen;
	//	printf("writelen:%d,readlen-writelen:%d\n",writelen,pos);

		mSleep(25);
	}
	fclose(fp);
	delete[] buffer;
	return true;
}
int main(int argc, char** argv) {
	// Begin by setting up our usage environment:
	TaskScheduler* scheduler = BasicTaskScheduler::createNew();
	UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
	OutPacketBuffer::maxSize = 100000;

	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

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

修改/liveMedia/StreamParser.cpp中的#define BANK_SIZE 150000,改为2400000

live555MediaServer需要加入:OutPacketBuffer::maxSize = 100000; 以此保证接收端的数据不溢出


更改Makefile,用于生成live555MediaServer和main(测试代码)

INCLUDES = -I../UsageEnvironment/include -I../groupsock/include -I../liveMedia/include -I../BasicUsageEnvironment/include -I ./
# Default library filename suffixes for each library that we link with.  The "config.*" file might redefine these later.
libliveMedia_LIB_SUFFIX = $(LIB_SUFFIX)
libBasicUsageEnvironment_LIB_SUFFIX = $(LIB_SUFFIX)
libUsageEnvironment_LIB_SUFFIX = $(LIB_SUFFIX)
libgroupsock_LIB_SUFFIX = $(LIB_SUFFIX)
##### 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) $(CPPFLAGS) $(CFLAGS)
CPP =   cpp
CPLUSPLUS_COMPILER = c++
CPLUSPLUS_FLAGS = $(COMPILE_OPTS) -Wall -DBSD=1 $(CPPFLAGS) $(CXXFLAGS)
OBJ =   o
LINK =   c++ -o
LINK_OPTS =  -L. $(LDFLAGS)
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 = live555MediaServer$(EXE)
#MEDIA_SERVER = main$(EXE)
PREFIX = /usr/local
ALL = $(MEDIA_SERVER)
all: $(ALL)

.$(C).$(OBJ):
 $(C_COMPILER) -c $(C_FLAGS) $<
.$(CPP).$(OBJ):
 $(CPLUSPLUS_COMPILER) -c $(CPLUSPLUS_FLAGS) $<

MEDIA_SERVER_OBJS = live555MediaServer.$(OBJ) DynamicRTSPServer.$(OBJ)  WW_H264VideoServerMediaSubsession.$(OBJ) WW_H264VideoSource.$(OBJ)
#MEDIA_SERVER_OBJS = main.$(OBJ) RTSPStream.$(OBJ) DynamicRTSPServer.$(OBJ)  WW_H264VideoServerMediaSubsession.$(OBJ) WW_H264VideoSource.$(OBJ)

live555MediaServer.$(CPP):  DynamicRTSPServer.hh version.hh
main.$(CPP): RTSPStream.hh DynamicRTSPServer.hh version.hh
DynamicRTSPServer.$(CPP):  DynamicRTSPServer.hh
WW_H264VideoServerMediaSubsession.$(CPP):  WW_H264VideoServerMediaSubsession.hh
WW_H264VideoSource.$(CPP):       WW_H264VideoSource.hh
USAGE_ENVIRONMENT_DIR = ../UsageEnvironment
USAGE_ENVIRONMENT_LIB = $(USAGE_ENVIRONMENT_DIR)/libUsageEnvironment.$(libUsageEnvironment_LIB_SUFFIX)
BASIC_USAGE_ENVIRONMENT_DIR = ../BasicUsageEnvironment
BASIC_USAGE_ENVIRONMENT_LIB = $(BASIC_USAGE_ENVIRONMENT_DIR)/libBasicUsageEnvironment.$(libBasicUsageEnvironment_LIB_SUFFIX)
LIVEMEDIA_DIR = ../liveMedia
LIVEMEDIA_LIB = $(LIVEMEDIA_DIR)/libliveMedia.$(libliveMedia_LIB_SUFFIX)
GROUPSOCK_DIR = ../groupsock
GROUPSOCK_LIB = $(GROUPSOCK_DIR)/libgroupsock.$(libgroupsock_LIB_SUFFIX)
LOCAL_LIBS = $(LIVEMEDIA_LIB) $(GROUPSOCK_LIB) \
  $(BASIC_USAGE_ENVIRONMENT_LIB) $(USAGE_ENVIRONMENT_LIB)
LIBS =   $(LOCAL_LIBS) $(LIBS_FOR_CONSOLE_APPLICATION)

live555MediaServer$(EXE): $(MEDIA_SERVER_OBJS) $(LOCAL_LIBS)
 $(LINK)$@ $(CONSOLE_LINK_OPTS) $(MEDIA_SERVER_OBJS) $(LIBS)
#main$(EXE): $(MEDIA_SERVER_OBJS) $(LOCAL_LIBS)
# $(LINK)$@ $(CONSOLE_LINK_OPTS) $(MEDIA_SERVER_OBJS) $(LIBS)
clean:
 -rm -rf *.$(OBJ) $(ALL) core *.core *~ include/*~

install: $(MEDIA_SERVER)
   install -d $(DESTDIR)$(PREFIX)/bin
   install -m 755 $(MEDIA_SERVER) $(DESTDIR)$(PREFIX)/bin

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


编译后运行成功



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值