基于live555实时流服务器解析

14 篇文章 3 订阅
6 篇文章 0 订阅
#define READ_FROM_FILES_SYNCHRONOUSLY 1READ_FROM_FILES_SYNCHRONOUSLY 1
void ByteFrameLiveVideoSource:: doGetNextFrame()
{
	if(fLimitNumBytesToStream && fNumBytesToStream == 0) {
		handleClosure(this);
		return;
	}
//	printf("ByteFrameLiveVideoSource doGetNextFrame 1: \r\n");


#ifdef READ_FROM_FILES_SYNCHRONOUSLY
	 //从编码处获取frame!!!!
	   doGetNextFrameFormEncoder();
#else
	   
	if (!fHaveStartedReading) {
	// Await readable data from the file:
		envir().taskScheduler().turnOnBackgroundReadHandling(GETFRAME_HANDLER_ID,
		(TaskScheduler::BackgroundHandlerProc*)&getFrameableHandler, this);
		fHaveStartedReading = True;
	}
#endif	  
}
	if(fLimitNumBytesToStream && fNumBytesToStream == 0) {
		handleClosure(this);
		return;
	}
//	printf("ByteFrameLiveVideoSource doGetNextFrame 1: \r\n");


#ifdef READ_FROM_FILES_SYNCHRONOUSLY
	 //从编码处获取frame!!!!
	   doGetNextFrameFormEncoder();
#else
	   
	if (!fHaveStartedReading) {
	// Await readable data from the file:
		envir().taskScheduler().turnOnBackgroundReadHandling(GETFRAME_HANDLER_ID,
		(TaskScheduler::BackgroundHandlerProc*)&getFrameableHandler, this);
		fHaveStartedReading = True;
	}
#endif	  
}
void ByteFrameLiveVideoSource:: doGetNextFrameFormEncoder(){

	// Try to read as many bytes as will fit in the buffer provided (or "fPreferredFrameSize" if less)
	
	if (fLimitNumBytesToStream && fNumBytesToStream < (u_int64_t)fMaxSize) {
		fMaxSize = (unsigned)fNumBytesToStream;
	}
	if (fPreferredFrameSize > 0 && fPreferredFrameSize < fMaxSize) {
		fMaxSize = fPreferredFrameSize;
	}

	else srcId =0;

	fFrameSize =0;

	if(getFrame != NULL){			
		//回调函数,外部送流给live555
		fFrameSize = getFrame(chId,srcId,fTo,fMaxSize); 
		if(fFrameSize > fMaxSize){
			///这里非常重要,如果不设置fNumTruncatedBytes这个变量,fTo无法回收
			///说明没有足够的空间存放一帧,丢弃,应该请求一个IDR帧,getFrame请求IDR
			fNumTruncatedBytes = fFrameSize - fMaxSize;
			fFrameSize = fMaxSize;
		}
		else fNumTruncatedBytes = 0;
	}
 
 
	if (fFrameSize == 0) {
		handleClosure(this);
		return;
	} 
 
 
	//fNumBytesToStream -= fFrameSize;
	// Set the 'presentation time':
	if (fPlayTimePerFrame > 0 && fPreferredFrameSize > 0) {
	if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
		// This is the first frame, so use the current time:
		gettimeofday(&fPresentationTime, NULL);
	} else {
		// Increment by the play time of the previous data:
		unsigned uSeconds = fPresentationTime.tv_usec + fLastPlayTime;
		fPresentationTime.tv_sec += uSeconds/1000000;
		fPresentationTime.tv_usec = uSeconds%1000000;
	}
 
 
		// Remember the play time of this data:
		fLastPlayTime = (fPlayTimePerFrame*fFrameSize)/fPreferredFrameSize;
		fDurationInMicroseconds = fLastPlayTime;
	} else {
		// We don't know a specific play time duration for this data,
		// so just record the current time as being the 'presentation time':
		gettimeofday(&fPresentationTime, NULL);
	}
 
 
	// Inform the reader that he has data:
#ifdef READ_FROM_FILES_SYNCHRONOUSLY
	// To avoid possible infinite recursion, we need to return to the event loop to do this:
	nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
	(TaskFunc*)FramedSource::afterGetting, this);
#else
	// Because the file read was done from the event loop, we can call the
	// 'after getting' function directly, without risk of infinite recursion:
	FramedSource::afterGetting(this);
#endif
}
	

重写 H264LiveVideoServerMediaSubsession 由 H264FileServerMediaSubsession修改,H264FileServerMediaSubsession createsource 来自ByteFile。。。source

H264LiveVideoServerMediaSubsession来自ByteFrameLiveVideoSource

关键代码,两个回调函数,tempCB和stopCb,tempCb填充ByteFrameLiveVideoSource::getFrame,stopCb用于RTSP接受到teardown命令执行的一个函数!

 

FramedSource* H264LiveVideoServerMediaSubsession ::
	createNewStreamSource(unsigned clientSessionId,unsigned & estBitrate){
	  estBitrate = 500; // kbps, estimate

  // Create the video source:
  // if(tempCb != NULL) cout<<"create new stream source------------------>"<<endl; 

  ByteFrameLiveVideoSource* liveVideoSource = ByteFrameLiveVideoSource::createNew(envir(),tempCb,stopCb,chId,srcId);
//  printf("createNewStreamSource--------------====> %#x\n",liveVideoSource);

  if(liveVideoSource !=NULL){
  	cout<< "create liveVideoSource OK \n";
  	return H264VideoStreamFramer::createNew(envir(), liveVideoSource);
  }
  else return NULL;
}

 

 

 

 

 

最后接口阶段

class liveVideoRTSPServer: public RTSPServerSupportingHTTPStreaming {
public:
  static liveVideoRTSPServer* createNew(Port ourPort,UserAuthenticationDatabase* authDatabase,
  	GetFrameCB cb,StopPlayCB stopCb,unsigned reclamationTestSeconds = 65);

protected:
  liveVideoRTSPServer(UsageEnvironment& env, int ourSocket, Port ourPort,
		    UserAuthenticationDatabase* authDatabase, unsigned reclamationTestSeconds);
  // called only by createNew();
  virtual ~liveVideoRTSPServer();

protected: // redefined virtual functions
  virtual ServerMediaSession* lookupServerMediaSession(char const* streamName);
private:
	GetFrameCB readFreamCb;
	StopPlayCB stopPlayCb;
public:
	static UsageEnvironment* s_env;
	static UsageEnvironment* getEnv();
};
lookupServerMediaSession这个可以定义自己的访问规则,我这里定义了RTSP://IP:554/ch0/main 即可访问主码流。ch0对应通道0,main主码流,sub子码流
 

C接口:

 

 

void* liveVideoServerStart(myGetFrameCB cb,myStopPlayCB stopCb){
	
	RTSPServer* rtspServer;
	portNumBits rtspServerPortNum = 554;
  // Begin by setting up our usage environment:
	rtspServer = liveVideoRTSPServer::createNew(rtspServerPortNum,NULL,(GetFrameCB)cb,(StopPlayCB)stopCb);

	if (rtspServer == NULL) {

    	rtspServerPortNum = 8554;
    	rtspServer = liveVideoRTSPServer::createNew(rtspServerPortNum, NULL,(GetFrameCB)cb,(StopPlayCB)stopCb);
  	}
	if (rtspServer == NULL) {
    	*liveVideoRTSPServer::getEnv() << "Failed to create RTSP server: " <<liveVideoRTSPServer::getEnv()->getResultMsg() << "\n";
    	exit(1);
  	}
	
	char* urlPrefix = rtspServer->rtspURLPrefix();
	fprintf(stdout, "use like this:%s", urlPrefix);
	fprintf(stdout, "channel/srcch \n");
	
  	if (rtspServer->setUpTunnelingOverHTTP(80) || rtspServer->setUpTunnelingOverHTTP(8000) || rtspServer->setUpTunnelingOverHTTP(8080)) {
    	*liveVideoRTSPServer::getEnv() << "(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling, or for HTTP live streaming (for indexed Transport Stream files only).)\n";
  	} 
	else {
    	*liveVideoRTSPServer::getEnv() << "(RTSP-over-HTTP tunneling is not available.)\n";
 	}

	liveVideoRTSPServer::getEnv()->taskScheduler().doEventLoop(); // does not return
	return NULL;
}

定义自己的访问规则!

static ServerMediaSession* createNewSMS(UsageEnvironment& env,
					char const* streamName, GetFrameCB cb,StopPlayCB stopCb) {
  // Use the file name extension to determine the type of "ServerMediaSession":
  int chId,SrcId;
  int i;
  ServerMediaSession* sms = NULL;
  Boolean const reuseSource = False;
  char const* extension = strrchr(streamName, '/');
  char const* pstr = streamName;
  char chStr[10]={0} ;
  //pstr = streamName;
  if (extension == NULL) return NULL;
  for(i=0;i<strlen(streamName);i++){
  	if(*pstr == '/'){
		break;	
	}
	chStr[i] = *pstr;
	pstr++;
  }
  chStr[i]='\0';
  if(strcmp(chStr,"ch0")){
  	chId =0;
  }else if(strcmp(chStr,"ch1")){
  	chId =1;
  }else return NULL;
  
 
  if (strcmp(extension, "/main") == 0) {
  	SrcId = 0;	
  } else if (strcmp(extension, "/sub") == 0){
  	SrcId = 1;
  }else 
  	return NULL;
  cout<<"create H264LiveVideoServerMediaSubsession"<<endl;
  NEW_SMS("H.264 Video");
  OutPacketBuffer::maxSize = 1920*1080*3/2; //6000000;//HIGH*WIDTH *3 /2 -> YUV4:2:0 // allow for some possibly large H.264 frames
  sms->addSubsession(H264LiveVideoServerMediaSubsession::createNew(env,cb, stopCb,chId,SrcId, reuseSource));

  return sms;
}

 

VLC 观看:

 

总结:

经过修改可以完成编码器-live555 RTSP推送码流,VLC观看!

存在实时性相对较差,开多个卡,偶尔有画面花屏。待修改!


 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

john_liqinghan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值