Analysis of live555 media library's own file stream read and write, analysis of live555

Source: Internet
Author: User
Tags fread

Analysis of live555 media library's own file stream read and write, analysis of live555

The first two articles in the blog briefly introduced some of live555's introductory knowledge and the general workflow framework.

The following is the implementation of the Code. If the sub-class implemented by myself is to implement file stream playback.

It mainly implements two sub-classes: FramedSource and FileServerMediaSubsession.

Subsession to create a task, the Source obtains the video Source data, and then the subsession creates a new rtpsink to send the video data to the client.

The implementation file is as follows:

Header file


Cpp File


The main. cpp file is used to create the rtsp Service and add the session to the linked list.

The specific implementation is as follows:

/** =================================================== ========================================================== =========** Filename: main. cpp ** Description: Initialize the live555 working environment, establish the RTSPServer, and add the Subsession ** Version: 1.0 * Created: September 07, 2015 * Revision: none * Compiler: gcc ** Author: max_min _, * Organization: ** ===================================================== ========================================================== =========*/# include <stdi O. h> # include "BasicUsageEnvironment. hh "# include" RTSPServer. hh "# include" demoh1_mediasubsession. h "int main (int argc, char * argv []) {printf (" live555 stream start \ n "); // Begin by setting up the live555 usage environment TaskScheduler * schedew = basictaskschedew: createNew (); required * env = Signature: createNew (* schedew); UserAuthenticationDatabase * authDB = NULL; # if ACCESS _ CONTROL // authDB = new UserAuthenticationDatabase; authDB-> addUserRecord (argv [1], argv [2]); # endif RTSPServer * rtspServer = NULL; portNumBits rtspServerPortNum = 554; // rtsp port // create RTSP Service rtspServer = RTSPServer: createNew (* env, rtspServerPortNum, authDB); if (rtspServer = NULL) {* env <"create RTSPServer Failed:" <env-> getResultMsg () <"\ n"; return 0;} const char * decription = "Session Test By live555 Stream "; // H264 Subsession const char * streamName =" hsf-_streaming "; const char * inputName =" tc10.264 "; ServerMediaSession * sms = ServerMediaSession: createNew (* env, streamName, streamName, decription); // Add the self-derived subclass MediaSubsession class and add it to ServerMediaSession // when a client link is established, will call the server's lookup to find the next streamName subsessionsms-> addSubsession (demoh‑mediasubsession: createNew (* env, inputName, fals E); rtspServer-> addServerMediaSession (sms); char * url = rtspServer-> rtspURL (sms); * env <"URL: "<url <" \ n "; // loop and not come back ~ Env-> taskScheduler (). doEventLoop (); return 0 ;}

Implementation of FramedSoucre subclass:

# Include <stdio. h> # include "demoh‑framesource. h "demoh1_framesource: demoh1_framesource (struct & env, const char * fileName, unsigned int preferredFrameSize, unsigned int playTimePerFrame): FramedSource (env) {// ready for the source data; // open the streaming media file. In Real-Time Streaming, This is the preparation work before the stream is sent. fp = fopen (fileName, "rb");} demohworkflow framesource ::~ Destroy () {} demohsf-framesource * metadata: createNew (struct & env, const char * fileName, unsigned preferredFrameSize, unsigned playTimePerFrame) {return new evaluate (env, fileName, preferredFrameSize, playTimePerFrame);}/* Get the total length of the FILE to be read. live555 has a limit on the length of each data transmission */long filesize (FILE * stream) {long curpos, length; curpos = ftell (stream); fseek (stream, 0L, SEEK_END); length = ftell (stream); fseek (stream, curpos, SEEK_SET); return length;} void demoh#framesource :: doGetNextFrame () {// determine whether the maximum value is exceeded. Separate if (filesize (fp)> fMaxSize) {fFrameSize = fread (fTo, 1, fMaxSize, fp );} else {fFrameSize = fread (fTo, 1, filesize (fp), fp); fseek (fp, 0, SEEK_SET);} nextTask () = envir (). taskScheduler (). scheduleDelayedTask (0, (TaskFunc *) FramedSource: afterGetting, this); // It indicates that the afterGetting function is executed after a delay of 0 seconds}
Implementation of the FileServerMediaSubsession subclass:

# Include "demoh1_mediasubsession. h "# include" demoh‑framesource. h "# include" hsf-videostreamframer. hh "# include" hsf-videortpsink. hh "condition: condition (UsageEnvironment & env, const char * fileName, bool reuseFirstSource): FileServerMediaSubsession (env, fileName, reuseFirstSource) {// pass the required file name strcpy (fFileName, fileName);} demoh1_mediasubsession ::~ Callback () {} inclu* DemoH264MediaSubsession: createNew (UsageEnvironment & env, const char * fileName, bool reuseFirstSource) {optional * sms = new response (env, fileName, reuseFirstSource ); return sms;} FramedSource * DemoH264MediaSubsession: createNewStreamSource (unsigned clientsessionId, unsigned & estBitrate) {estBitrate = 1000; // create the required source, When a real-time stream is created later, demoh‑framesource * source = demoh‑framesource: createNew (envir (), fFileName); if (source = NULL) {envir () <"new source failed! \ N ";} return metadata: createNew (envir (), source);} RTPSink * metadata: createNewRTPSink (Groupsock * rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource * inputSource) {// create rtpSink // that is, the Source consumer return hsf-videortpsink: createNew (envir (), rtpGroupsock, rtpPayloadTypeIfDynamic );}

Download the entire source code from here: live555 implements file stream reading (which also contains the h264 file I downloaded from the live55 official website)

It mainly inherits the FramedSource and FileServerMediaSubsession classes, and then implements its own source acquisition and session processing.

It is one of the tasks I run:


Later, we will continue to update how to obtain and send Real-Time Streaming Data to the client. This is also a feature that many embedded devices often need to implement. At that time, c may need to implement rtsp by itself ~~~

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.