Network live broadcast system implemented using live555 class library - live broadcast H264 file

    Download the latest code of live555, compile and generate the library file of live555: libBasicUsageEnvironment.a libgroupsock.a libliveMedia.a libUsageEnvironment.a , using these four libraries plus the test program that comes with live555, you can easily realize live555 live broadcast. The live broadcast program provided by live555 can only broadcast the video that has been recorded before (different from VOD). code show as below:

/*=============================================================================
      FileName: h264live.c
          Desc: use the lib of live555 to do live
        Author: licaibiao
       Version:
    LastChange: 2017-02-23
*=============================================================================*/
#include "h264live.hh"

UsageEnvironment* approx;
char const* inputFileName = "test.264";
H264VideoStreamFramer* videoSource;
RTPSink* videoSink;

void play(); // forward

int main(int argc, char** argv) {
  // Begin by setting up our usage environment:
  TaskScheduler* scheduler = BasicTaskScheduler::createNew();
  env = BasicUsageEnvironment::createNew(*scheduler);

  // Create 'groupsocks' for RTP and RTCP:
  struct in_addr destinationAddress;
  destinationAddress.s_addr = chooseRandomIPv4SSMAddress(*env);
  // Note: This is a multicast address.  If you wish instead to stream
  // using unicast, then you should use the "testOnDemandRTSPServer"
  // test program - not this test program - as a model.

  const unsigned short rtpPortNum = 18888;
  const unsigned short rtcpPortNum = rtpPortNum+1;
  const unsigned char ttl = 255;

  const Port rtpPort(rtpPortNum);
  const Port rtcpPort(rtcpPortNum);

  Groupsock rtpGroupsock(*env, destinationAddress, rtpPort, ttl);
  rtpGroupsock.multicastSendOnly(); // we're a SSM source
  Groupsock rtcpGroupsock(*env, destinationAddress, rtcpPort, ttl);
  rtcpGroupsock.multicastSendOnly(); // we're a SSM source

  // Create a 'H264 Video RTP' sink from the RTP 'groupsock':
  OutPacketBuffer::maxSize = 100000;
  videoSink = H264VideoRTPSink::createNew(*env, &rtpGroupsock, 96);

  // Create (and start) a 'RTCP instance' for this RTP sink:
  const unsigned estimatedSessionBandwidth = 500; // in kbps; for RTCP b/w share
  const unsigned maxCNAMElen = 100;
  unsigned char CNAME[maxCNAMElen+1];
  gethostname((char*)CNAME, maxCNAMElen);
  CNAME[maxCNAMElen] = '\0'; // just in case
  RTCPInstance* rtcp
  = RTCPInstance::createNew(*env, &rtcpGroupsock,
			    estimatedSessionBandwidth, CNAME,
			    videoSink, NULL /* we're a server */,
			    True /* we're a SSM source */);
  // Note: This starts RTCP running automatically

  RTSPServer* rtspServer = RTSPServer::createNew(*env, 8554);
  if (rtspServer == NULL) {
    *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
    exit(1);
  }
  ServerMediaSession* sms
    = ServerMediaSession::createNew(*env, "testStream", inputFileName,
		   "Session streamed by \"testH264VideoStreamer\"",
					   True /*SSM*/);
  sms->addSubsession(PassiveServerMediaSubsession::createNew(*videoSink, rtcp));
  rtspServer->addServerMediaSession(sms);

  char* url = rtspServer->rtspURL(sms);
  *env << "Play this stream using the URL \"" << url << "\"\n";
  delete[] url;

  // Start the streaming:
  *env << "Beginning streaming...\n";
  play();

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

  return 0; // only to prevent compiler warning
}

void afterPlaying(void* /*clientData*/) {
  *env << "...done reading from file\n";
  videoSink->stopPlaying();
  Medium::close(videoSource);
  // Note that this also closes the input file that this source read from.

  // Start playing once again:
  play();
}

void play() {
  // Open the input file as a 'byte-stream file source':
  ByteStreamFileSource* fileSource
    = ByteStreamFileSource::createNew(*env, inputFileName);
  if (fileSource == NULL) {
    *env << "Unable to open file \"" << inputFileName
         << "\" as a byte-stream file source\n";
    exit(1);
  }

  FramedSource* videoES = fileSource;

  // Create a framer for the Video Elementary Stream:
  videoSource = H264VideoStreamFramer::createNew(*env, videoES);

  // Finally, start playing:
  *env << "Beginning to read from file...\n";
  videoSink->startPlaying(*videoSource, afterPlaying, videoSink);
}

The Makefile is as follows:

INCLUDES 	 = -I./include/usageEnvironment/ -I./include/groupsock/ -I.include/liveMedia/ -I.include/BasicUsageEnvironment
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 = or
LINK 		 =          c++ -o
LINK_OPTS    =     -L. $(LDFLAGS)
CONSOLE_LINK_OPTS = $(LINK_OPTS)

USAGE_ENVIRONMENT_LIB = ./lib/libUsageEnvironment.a
BASIC_USAGE_ENVIRONMENT_LIB = ./lib/libBasicUsageEnvironment.a
LIVEMEDIA_LIB = ./lib/libliveMedia.a
GROUPSOCK_LIB = ./lib/libgroupsock.a
LOCAL_LIBS 	  =  $(LIVEMEDIA_LIB) $(GROUPSOCK_LIB) $(BASIC_USAGE_ENVIRONMENT_LIB) $(USAGE_ENVIRONMENT_LIB)
LIBS          =  $(LOCAL_LIBS)

MEDIA_SERVER_OBJS = h264live.$(OBJ)
APP = h264live


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

h264live: $(MEDIA_SERVER_OBJS) $(LOCAL_LIBS)
	$(LINK)$@ $(CONSOLE_LINK_OPTS) $(MEDIA_SERVER_OBJS) $(LIBS)
	
clean:
	-rm -rf *.$(OBJ) $(APP) core *.core *~ include/*~


The project directory is as follows:

[root@redhat h264live]# tree -L 2
.
├── h264live
├── h264live.cpp
├── h264live.hh
├── h264live.o
├── include
│   ├── basicUsageEnvironment
│   ├── groupsock
│   ├── liveMedia
│   └── usageEnvironment
├── lib
│   ├── libBasicUsageEnvironment.a
│   ├── libgroupsock.a
├── ├── libliveMedia.a
│   └── libUsageEnvironment.a
├── Makefile
└── test.264

Run the h264live file directly, and then open the URL on the client: "rtsp://192.168.0.127:8554/testStream" to watch the live video.


Complete project code: live555 implements h264 file live broadcast project





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324756033&siteId=291194637