H264 Live streaming RTSP sent under Windows using live555

Source: Internet
Author: User
Tags fread

The text, such as its name, recently in the project required to use the RTSP protocol forwarding processing H264 video data to the previous layer of clients, the environment is Windows VS2013, so on a variety of Baidu Google to find code. Results in the use of live555 to do a relatively simple conclusion and also sad to find that the online people posted the code is basically Linux above. After modifying two copies to apply to Windows invalid, again caught Baidu Google's endless search. Anyway, finally solved, so posted code to share with you, hoping to give me a similar demand for children's shoes a little inspiration, but also hope to have a high finger is one of the problems.

The use of live555 for RTSP playback is basically done by modifying the demo of the local file it gives. But because its demo package is relatively deep, it would be awkward to directly modify the code in his fread to make a memory copy for real-time transmission. This article refers to some of the code on the Web, custom a class that inherits from H264videofileservermediasubsession to handle, and defines a class that inherits from Framedsource to do the memory copy operation, This class is also the difference between the reader and the real-time flow of the critical point.

Assorted said a lot of breath, the following post code it. If you feel the need or lazy to build live555 environment can also be in the final link to download the project (Environment for VS2013), if your VS version is appropriate to run directly.

Main file (program entry)

#include "h264livevideoservermediasubssion.hh" #include "h264framedlivesource.hh" #include "livemedia.hh" #include " Basicusageenvironment.hh "#define BUFSIZE 1024*200static void Announcestream (rtspserver* rtspserver, servermediasession* Sms,char const* streamname)//Display RTSP connection information {char* URL = rtspserver->rtspurl (SMS); usageenvironment& env = Rtspserver->envir (); env <<streamName<< "\ n"; env << "Play this stream Using the URL \ "" << url << "\" \ n ";d elete[] URL;} int main (int argc, char** argv) {//Set environment usageenvironment* env; Boolean Reusefirstsource = false;//If "True" then the other access client sees the same video stream as the first client, otherwise the other client will be re-played taskscheduler* scheduler = Basictaskscheduler::createnew (); env = Basicusageenvironment::createnew (*scheduler);// Create the RTSP server userauthenticationdatabase* authdb = NULL; rtspserver* rtspserver = rtspserver::createnew (*env, 8554, authdb); if (rtspserver = = NULL) {*env << Failed to creat E RTSP server: "<< env->getresultmsg () <<" \ n "; exit (1);} Char const* descriptionstring= "Session streamed by \" Testondemandrtspserver\ "";//Analog live stream send correlation variable int datasize;//data area length unsigned char* databuf;//Data area pointer databuf = (unsigned char*) malloc (1024*1024), bool DOSENT;//RTSP send flag bit, true then send, otherwise exit// Copy the 1M data from the file into memory as a real-time network transfer memory simulation, if the real-time network transmission should be a dual-threaded structure, remember to add a thread lock here//In addition to the real-time transmission of the data copy should occur in the H264framedlivesource file, So here is just a pointer down from the past to give it file *pf;fopen_s (&AMP;PF, "test.264", "RB"), Fread (Databuf, 1, BUFSIZE, pf);d atasize = bufsize;dosent = True;fclose (PF);//The above section is basically the same as the demo provided by the live555, except for the part of the analog network transmission, and the following is modified to the form of the network transport, which overrides the first parameter of the addsubsession for the related file Char const * Streamname = "h264esvideotest"; servermediasession* SMS = servermediasession::createnew (*env, Streamname, streamname,descriptionstring);sms-> Addsubsession (H264livevideoservermediasubssion::createnew (*env, Reusefirstsource, &datasize, databuf,& dosent));//Modify the H264livevideoservermediasubssionrtspserver->addservermediasession (SMS) for your own implementation; Announcestream ( Rtspserver, SMS, streamname);//Prompts the user to enter connection information Env->taskscheduler (). Doeventloop (); Loop waitConnect free (DATABUF);//release memory return 0;} 

Custom H264videofileservermediasubsessionclass

H264videofileservermediasubsession.hh

 #ifndef _h264_live_video_server_media_subsession_hh#define _h264_live_video_server_ Media_subsession_hh#include "H264VIDEOFILESERVERMEDIASUBSESSION.HH" class H264livevideoservermediasubssion:public h264videofileservermediasubsession {public:static h264livevideoservermediasubssion* createNew (UsageEnvironment & Env, Boolean reusefirstsource, int *datasize, unsigned char* databuf, bool *dosent);p rotected://We ' re a virtual B ASE Classh264livevideoservermediasubssion (usageenvironment& env, Boolean reusefirstsource, int *datasize, unsigned char* databuf, bool *dosent); ~h264livevideoservermediasubssion ();p rotected://redefined virtual functionsframedsource* createnewstreamsource (unsigned clientsessionid,unsigned& estbitrate);p Ublic:char Ffilename[100];int *server_datasize;//data area size pointer unsigned char* server_databuf;//data area pointer bool *server_dosent;//send flag};# endif 
H264VideoFileServerMediaSubsession.cpp

#include "h264livevideoservermediasubssion.hh" #include "h264framedlivesource.hh" #include " H264videostreamframer.hh "h264livevideoservermediasubssion* h264livevideoservermediasubssion::createnew ( usageenvironment& env, Boolean reusefirstsource, int *datasize, unsigned char* databuf, bool *dosent) {return new H264 Livevideoservermediasubssion (env, Reusefirstsource, DataSize, Databuf, dosent);} H264livevideoservermediasubssion::h264livevideoservermediasubssion (usageenvironment& env, Boolean Reusefirstsource, int *datasize, unsigned char* databuf, bool *dosent): H264videofileservermediasubsession (env, Ffilename, Reusefirstsource)//h264videofileservermediasubsession is not the file we need to modify,//But we're going to use it to initialize our function,//So give an empty array in it { server_datasize = datasize;//data area size pointer server_databuf = databuf;//DATA area pointer server_dosent = dosent;//Send Flag} H264livevideoservermediasubssion::~h264livevideoservermediasubssion () {}framedsource* H264livevideoservermediasubssion::createnewstreamsource (unsigned clientsessionid, unsigned&amP Estbitrate) {/* remain to do:assign estbitrate */estbitrate = $,//kbps, estimate//create video source h264framedlivesource* LiveSou Rce = H264framedlivesource::createnew (EnviR (), Server_datasize, Server_databuf, server_dosent); if (LiveSource = = NULL) {return NULL;} Create a framer for the Video Elementary Stream:return h264videostreamframer::createnew (EnviR (), liveSource);}

Custom H264framedlivesourceclass

H264framedlivesource.hh

#ifndef _h264framedlivesource_hh#define _h264framedlivesource_hh#include <framedsource.hh>class H264framedlivesource:public framedsource{public:static h264framedlivesource* CreateNew (usageenvironment& env, int *datasize, unsigned char*  databuf, bool *dosent, unsigned preferredframesize = 0, unsigned playtimeperframe = 0);p Rotected:h264framedlivesource (usageenvironment& env, int *datasize, unsigned char*  databuf, bool *dosent, Unsigned preferredframesize, unsigned playtimeperframe); ~h264framedlivesource ();p rivate:virtual void Dogetnextframe (); int transportdata (unsigned char* to, unsigned maxSize);p rotected:int *framed_datasize;//data area size pointer unsigned char * framed_databuf;//data area pointer bool *framed_dosent;//send flag int readbufsize;//record read data area size int bufsizel;//record data area size}; #endif
H264FramedLiveSource.cpp

#include "h264framedlivesource.hh" H264framedlivesource::h264framedlivesource (usageenvironment& env, int * DataSize, unsigned char* databuf, bool *dosent, unsigned preferredframesize, unsigned playtimeperframe): Framedsource (en V) {framed_datasize = datasize;//data area size pointer framed_databuf = databuf;//DATA area pointer framed_dosent = dosent;//Send Flag}  h264framedlivesource* h264framedlivesource::createnew (usageenvironment& env, int *datasize, unsigned char* DATABUF, bool *dosent, unsigned preferredframesize, unsigned playtimeperframe) {h264framedlivesource* Newsource = new H264framedlivesource (env, DataSize, Databuf, dosent, Preferredframesize, playtimeperframe); return newSource;} H264framedlivesource::~h264framedlivesource () {}void h264framedlivesource::d ogetnextframe () {if (*Framed_dosent = = True) {*framed_dosent = False;bufsizel = *framed_datasize;readbufsize = 0;fframesize = fmaxsize;memcpy (FTo, Framed_ Databuf + readbufsize, fframesize); Readbufsize + = fframesize;} Else{if (Bufsizel-readbufsize>fmaxsizE) {fframesize = fmaxsize;memcpy (fTo, Framed_databuf + readbufsize, fframesize); Readbufsize + = fframesize;} else{memcpy (FTo, Framed_databuf + readbufsize, bufsizel-readbufsize); *framed_dosent = True;}} Nexttask () = EnviR (). TaskScheduler (). Scheduledelayedtask (0, (taskfunc*) framedsource::aftergetting, this);// Represents a delay of 0 seconds before executing the aftergetting function return;}

Project: Click to open link



H264 Live streaming RTSP sent under Windows using live555

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.