continue to work on HLS using memory buffers

This commit is contained in:
mpromonet 2016-04-17 16:39:07 +00:00
parent 16ac9205d4
commit 8a593ab42f
3 changed files with 246 additions and 246 deletions

View File

@ -10,11 +10,13 @@
#ifndef SERVER_MEDIA_SUBSESSION
#define SERVER_MEDIA_SUBSESSION
#include <sys/stat.h>
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <map>
// live555
#include <liveMedia.hh>
@ -87,17 +89,18 @@ class UnicastServerMediaSubsession : public OnDemandServerMediaSubsession , publ
// -----------------------------------------
// ServerMediaSubsession for HLS
// -----------------------------------------
class HLSSink : public MediaSink
class HLSServerMediaSubsession : public UnicastServerMediaSubsession
{
public:
static HLSSink* createNew(UsageEnvironment& env, unsigned int bufferSize)
class HLSSink : public MediaSink
{
return new HLSSink(env, bufferSize);
public:
static HLSSink* createNew(UsageEnvironment& env, unsigned int bufferSize, unsigned int sliceDuration)
{
return new HLSSink(env, bufferSize, sliceDuration);
}
protected:
HLSSink(UsageEnvironment& env, unsigned bufferSize) : MediaSink(env), m_bufferSize(bufferSize), m_slice(0), m_firstslice(0)
HLSSink(UsageEnvironment& env, unsigned bufferSize, unsigned int sliceDuration) : MediaSink(env), m_bufferSize(bufferSize), m_refTime(0), m_sliceDuration(sliceDuration)
{
m_buffer = new unsigned char[m_bufferSize];
}
@ -124,7 +127,7 @@ class HLSSink : public MediaSink
static void afterGettingFrame(void* clientData, unsigned frameSize,
unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned /*durationInMicroseconds*/)
unsigned durationInMicroseconds)
{
HLSSink* sink = (HLSSink*)clientData;
sink->afterGettingFrame(frameSize, numTruncatedBytes, presentationTime);
@ -135,44 +138,87 @@ class HLSSink : public MediaSink
if (numTruncatedBytes > 0)
{
envir() << "FileSink::afterGettingFrame(): The input frame data was too large for our buffer size \n";
// realloc a bigger buffer
m_bufferSize += numTruncatedBytes;
delete[] m_buffer;
m_buffer = new unsigned char[m_bufferSize];
}
if (m_os.is_open())
else
{
if (m_slice != (presentationTime.tv_sec/10))
// append buffer to slice buffer
if (m_refTime == 0)
{
m_os.close();
m_refTime = presentationTime.tv_sec;
}
}
if (!m_os.is_open())
unsigned int slice = (presentationTime.tv_sec-m_refTime)/m_sliceDuration;
std::string& outputBuffer = m_outputBuffers[slice];
outputBuffer.append((const char*)m_buffer, frameSize);
// remove old buffers
while (m_outputBuffers.size()>5)
{
m_slice = presentationTime.tv_sec/10;
if (m_firstslice == 0)
{
m_firstslice = m_slice;
m_outputBuffers.erase(m_outputBuffers.begin());
}
std::ostringstream os;
os << m_slice << ".ts";
m_os.open(os.str().c_str());
}
if (m_os.is_open())
{
m_os.write((char*)m_buffer, frameSize);
}
continuePlaying();
}
public:
unsigned int getHLSBufferSize(unsigned int slice)
{
unsigned int size = 0;
std::map<unsigned int,std::string>::iterator it = m_outputBuffers.find(slice);
if (it != m_outputBuffers.end())
{
size = it->second.size();
}
return size;
}
const char* getHLSBuffer(unsigned int slice)
{
const char* content = NULL;
std::map<unsigned int,std::string>::iterator it = m_outputBuffers.find(slice);
if (it != m_outputBuffers.end())
{
content = it->second.c_str();
}
return content;
}
unsigned int firstTime()
{
unsigned int firstTime = 0;
if (m_outputBuffers.size() != 0)
{
firstTime = m_outputBuffers.begin()->first;
}
return firstTime*m_sliceDuration;
}
unsigned int duration()
{
unsigned int duration = 0;
if (m_outputBuffers.size() != 0)
{
duration = m_outputBuffers.rbegin()->first - m_outputBuffers.begin()->first;
}
return (duration)*m_sliceDuration;
}
unsigned int getSliceDuration()
{
return m_sliceDuration;
}
private:
unsigned char * m_buffer;
unsigned int m_bufferSize;
std::ofstream m_os;
public:
unsigned int m_slice;
unsigned int m_firstslice;
};
std::map<unsigned int,std::string> m_outputBuffers;
unsigned int m_refTime;
unsigned int m_sliceDuration;
};
class HLSServerMediaSubsession : public OnDemandServerMediaSubsession , public BaseServerMediaSubsession
{
public:
static HLSServerMediaSubsession* createNew(UsageEnvironment& env, StreamReplicator* replicator, const std::string& format)
{
@ -181,57 +227,48 @@ class HLSServerMediaSubsession : public OnDemandServerMediaSubsession , public B
protected:
HLSServerMediaSubsession(UsageEnvironment& env, StreamReplicator* replicator, const std::string& format)
: OnDemandServerMediaSubsession(env, False), BaseServerMediaSubsession(replicator), m_format(format)
: UnicastServerMediaSubsession(env, replicator, format)
{
// Create a source
FramedSource* source = replicator->createStreamReplica();
FramedSource* videoSource = createSource(env, source, format);
// Start Playing the Sink
m_videoSink = HLSSink::createNew(env, 65535);
m_videoSink->startPlaying(*videoSource, NULL, NULL);
// Start Playing the HLS Sink
m_hlsSink = HLSSink::createNew(env, OutPacketBuffer::maxSize, 10);
m_hlsSink->startPlaying(*videoSource, NULL, NULL);
}
virtual FramedSource* createNewStreamSource(unsigned clientSessionId, unsigned& estBitrate)
virtual ~HLSServerMediaSubsession()
{
FramedSource* source = m_replicator->createStreamReplica();
return createSource(envir(), source, m_format);
Medium::close(m_hlsSink);
}
virtual RTPSink* createNewRTPSink(Groupsock* rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource* inputSource)
virtual float getCurrentNPT(void* streamToken)
{
return createSink(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic, m_format);
return (m_hlsSink->firstTime());
}
virtual char const* getAuxSDPLine(RTPSink* rtpSink,FramedSource* inputSource);
virtual float duration() const {
std::cout << "duration " << (m_videoSink->m_slice - m_videoSink->m_firstslice)*10 << std::endl;
return (m_videoSink->m_slice - m_videoSink->m_firstslice)*10;
virtual float duration() const
{
return (m_hlsSink->duration());
}
virtual void seekStream(unsigned clientSessionId, void* streamToken, double& seekNPT, double streamDuration, u_int64_t& numBytes)
{
m_slice = seekNPT / 10;
seekNPT = m_slice*10;
std::ostringstream os;
os << m_slice+m_videoSink->m_firstslice << ".ts";
struct stat sb;
int statResult = stat(os.str().c_str(), &sb);
if (statResult == 0)
{
numBytes = sb.st_size;
}
m_slice = seekNPT / m_hlsSink->getSliceDuration();
seekNPT = m_slice * m_hlsSink->getSliceDuration();
numBytes = m_hlsSink->getHLSBufferSize(m_slice);
std::cout << "seek seekNPT:" << seekNPT << " slice:" << m_slice << " numBytes:" << numBytes << std::endl;
}
virtual FramedSource* getStreamSource(void* streamToken)
{
std::ostringstream os;
os << m_slice+m_videoSink->m_firstslice << ".ts";
return ByteStreamFileSource::createNew(envir(), os.str().c_str());
unsigned int size = m_hlsSink->getHLSBufferSize(m_slice);
u_int8_t* content = new u_int8_t[size];
memcpy(content, m_hlsSink->getHLSBuffer(m_slice), size);
return ByteStreamMemoryBufferSource::createNew(envir(), content, size);
}
protected:
const std::string m_format;
unsigned int m_slice;
HLSSink * m_videoSink;
HLSSink * m_hlsSink;
};
#endif

View File

@ -145,10 +145,3 @@ char const* UnicastServerMediaSubsession::getAuxSDPLine(RTPSink* rtpSink,FramedS
return this->getAuxLine(dynamic_cast<V4L2DeviceSource*>(m_replicator->inputSource()), rtpSink->rtpPayloadType());
}
// -----------------------------------------
// ServerMediaSubsession for Unicast
// -----------------------------------------
char const* HLSServerMediaSubsession::getAuxSDPLine(RTPSink* rtpSink,FramedSource* inputSource)
{
return this->getAuxLine(dynamic_cast<V4L2DeviceSource*>(m_replicator->inputSource()), rtpSink->rtpPayloadType());
}

View File

@ -13,20 +13,17 @@
** -------------------------------------------------------------------------*/
#include <iostream>
#include <sstream>
#include "RTSPServer.hh"
#include "RTSPCommon.hh"
#include <sys/stat.h>
#include <time.h>
#ifndef _BYTE_STREAM_MEMORY_BUFFER_SOURCE_HH
#include "ByteStreamMemoryBufferSource.hh"
#endif
#ifndef _TCP_STREAM_SINK_HH
#include "TCPStreamSink.hh"
#endif
// -----------------------------------------
// RTSP server supporting live HLS
// -----------------------------------------
class HLSServer : public RTSPServer
{
@ -34,24 +31,59 @@ class HLSServer : public RTSPServer
{
public:
HLSClientConnection(RTSPServer& ourServer, int clientSocket, struct sockaddr_in clientAddr)
: RTSPServer::RTSPClientConnection(ourServer, clientSocket, clientAddr), fClientSessionId(0), fStreamSource(NULL), fPlaylistSource(NULL), fTCPSink(NULL) {
: RTSPServer::RTSPClientConnection(ourServer, clientSocket, clientAddr), fClientSessionId(0), fTCPSink(NULL) {
}
~HLSClientConnection() {
if (fTCPSink != NULL) fTCPSink->stopPlaying();
Medium::close(fPlaylistSource);
Medium::close(fStreamSource);
if (fTCPSink != NULL)
{
fTCPSink->stopPlaying();
Medium::close(fTCPSink);
}
}
private:
void sendHeader(const char* contentType, unsigned int contentLength)
{
// Construct our response:
snprintf((char*)fResponseBuffer, sizeof fResponseBuffer,
"HTTP/1.1 200 OK\r\n"
"%s"
"Server: LIVE555 Streaming Media v%s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"\r\n",
dateHeader(),
LIVEMEDIA_LIBRARY_VERSION_STRING,
contentType,
contentLength);
// Send the response header
send(fClientOutputSocket, (char const*)fResponseBuffer, strlen((char*)fResponseBuffer), 0);
fResponseBuffer[0] = '\0'; // We've already sent the response. This tells the calling code not to send it again.
}
void streamSource(FramedSource* source)
{
if (fTCPSink != NULL)
{
fTCPSink->stopPlaying();
Medium::close(fTCPSink);
fTCPSink = NULL;
}
if (source != NULL)
{
fTCPSink = TCPStreamSink::createNew(envir(), fClientOutputSocket);
fTCPSink->startPlaying(*source, afterStreaming, this);
}
}
void sendPlayList(char const* urlSuffix)
{
// First, make sure that the named file exists, and is streamable:
ServerMediaSession* session = fOurServer.lookupServerMediaSession(urlSuffix);
if (session == NULL) {
std::cout << "============no session==============" << std::endl;
handleHTTPCmd_notFound();
return;
}
@ -59,77 +91,40 @@ class HLSServer : public RTSPServer
// To be able to construct a playlist for the requested file, we need to know its duration:
float duration = session->duration();
if (duration <= 0.0) {
std::cout << "============no duration==============" << std::endl;
handleHTTPCmd_notSupported();
return;
}
// Now, construct the playlist. It will consist of a prefix, one or more media file specifications, and a suffix:
unsigned const maxIntLen = 10; // >= the maximum possible strlen() of an integer in the playlist
char const* const playlistPrefixFmt =
"#EXTM3U\r\n"
"#EXT-X-ALLOW-CACHE:YES\r\n"
"#EXT-X-MEDIA-SEQUENCE:%d\r\n"
"#EXT-X-TARGETDURATION:%d\r\n";
unsigned const playlistPrefixFmt_maxLen = strlen(playlistPrefixFmt) + maxIntLen;
char const* const playlistMediaFileSpecFmt =
"#EXTINF:%d,\r\n"
"%s?segment=%d,%d\r\n";
unsigned const playlistMediaFileSpecFmt_maxLen = strlen(playlistMediaFileSpecFmt) + maxIntLen + strlen(urlSuffix) + 2*maxIntLen;
// Figure out the 'target duration' that will produce a playlist that will fit in our response buffer. (But make it at least 10s.)
unsigned const playlistMaxSize = 10000;
unsigned const mediaFileSpecsMaxSize = playlistMaxSize - (playlistPrefixFmt_maxLen /*+ playlistSuffixFmt_maxLen*/);
unsigned const maxNumMediaFileSpecs = mediaFileSpecsMaxSize/playlistMediaFileSpecFmt_maxLen;
unsigned targetDuration = (unsigned)(duration/maxNumMediaFileSpecs + 1);
if (targetDuration < 10) targetDuration = 10;
unsigned int startTime = 0;
char* playlist = new char[playlistMaxSize];
char* s = playlist;
sprintf(s, playlistPrefixFmt, startTime,targetDuration);
s += strlen(s);
unsigned durSoFar = startTime;
while (1) {
unsigned dur = targetDuration < duration ? targetDuration : (unsigned)duration;
duration -= dur;
sprintf(s, playlistMediaFileSpecFmt, dur, urlSuffix, durSoFar, dur);
s += strlen(s);
if (duration < 1.0) break;
durSoFar += dur;
ServerMediaSubsessionIterator iter(*session);
ServerMediaSubsession* subsession = iter.next();
if (subsession == NULL) {
handleHTTPCmd_notSupported();
return;
}
unsigned playlistLen = s - playlist;
unsigned int startTime = subsession->getCurrentNPT(NULL);
unsigned sliceDuration = 10;
std::ostringstream os;
os << "#EXTM3U\r\n"
<< "#EXT-X-ALLOW-CACHE:YES\r\n"
<< "#EXT-X-MEDIA-SEQUENCE:" << startTime << "\r\n"
<< "#EXT-X-TARGETDURATION:" << sliceDuration << "\r\n";
// Construct our response:
snprintf((char*)fResponseBuffer, sizeof fResponseBuffer,
"HTTP/1.1 200 OK\r\n"
"%s"
"Server: LIVE555 Streaming Media v%s\r\n"
"Content-Length: %d\r\n"
"Content-Type: application/vnd.apple.mpegurl\r\n"
"\r\n",
dateHeader(),
LIVEMEDIA_LIBRARY_VERSION_STRING,
playlistLen);
// Send the response header now, because we're about to add more data (the playlist):
send(fClientOutputSocket, (char const*)fResponseBuffer, strlen((char*)fResponseBuffer), 0);
fResponseBuffer[0] = '\0'; // We've already sent the response. This tells the calling code not to send it again.
// Then, send the playlist. Because it's large, we don't do so using "send()", because that might not send it all at once.
// Instead, we stream the playlist over the TCP socket:
if (fPlaylistSource != NULL) { // sanity check
if (fTCPSink != NULL) fTCPSink->stopPlaying();
Medium::close(fPlaylistSource);
for (unsigned int slice=0; slice*sliceDuration<duration; slice++)
{
os << "#EXTINF:" << sliceDuration << ",\r\n";
os << urlSuffix << "?segment=" << (startTime+slice*sliceDuration) << "\r\n";
}
fPlaylistSource = ByteStreamMemoryBufferSource::createNew(envir(), (u_int8_t*)playlist, playlistLen);
if (fTCPSink == NULL) fTCPSink = TCPStreamSink::createNew(envir(), fClientOutputSocket);
fTCPSink->startPlaying(*fPlaylistSource, afterStreaming, this);
const std::string& playList(os.str());
// send response header
this->sendHeader("application/vnd.apple.mpegurl", playList.size());
// stream body
u_int8_t* playListBuffer = new u_int8_t[playList.size()];
memcpy(playListBuffer, playList.c_str(), playList.size());
this->streamSource(ByteStreamMemoryBufferSource::createNew(envir(), playListBuffer, playList.size()));
}
void handleHTTPCmd_StreamingGET(char const* urlSuffix, char const* /*fullRequestStr*/) {
@ -138,8 +133,8 @@ class HLSServer : public RTSPServer
do {
char const* questionMarkPos = strrchr(urlSuffix, '?');
if (questionMarkPos == NULL) break;
unsigned offsetInSeconds, durationInSeconds;
if (sscanf(questionMarkPos, "?segment=%u,%u", &offsetInSeconds, &durationInSeconds) != 2) break;
unsigned offsetInSeconds;
if (sscanf(questionMarkPos, "?segment=%u", &offsetInSeconds) != 1) break;
char* streamName = strDup(urlSuffix);
streamName[questionMarkPos-urlSuffix] = '\0';
@ -173,42 +168,21 @@ class HLSServer : public RTSPServer
// Seek the stream source to the desired place, with the desired duration, and (as a side effect) get the number of bytes:
double dOffsetInSeconds = (double)offsetInSeconds;
u_int64_t numBytes;
subsession->seekStream(fClientSessionId, streamToken, dOffsetInSeconds, (double)durationInSeconds, numBytes);
unsigned numTSBytesToStream = (unsigned)numBytes;
std::cout << "numTSBytesToStream:" << numTSBytesToStream << std::endl;
u_int64_t numBytes = 0;
subsession->seekStream(fClientSessionId, streamToken, dOffsetInSeconds, 0.0, numBytes);
if (numTSBytesToStream == 0) {
if (numBytes == 0) {
// For some reason, we do not know the size of the requested range. We can't handle this request:
handleHTTPCmd_notSupported();
break;
}
// Construct our response:
snprintf((char*)fResponseBuffer, sizeof fResponseBuffer,
"HTTP/1.1 200 OK\r\n"
"%s"
"Server: LIVE555 Streaming Media v%s\r\n"
"Content-Length: %d\r\n"
"Content-Type: text/plain; charset=ISO-8859-1\r\n"
"\r\n",
dateHeader(),
LIVEMEDIA_LIBRARY_VERSION_STRING,
numTSBytesToStream);
// Send the response now, because we're about to add more data (from the source):
send(fClientOutputSocket, (char const*)fResponseBuffer, strlen((char*)fResponseBuffer), 0);
fResponseBuffer[0] = '\0'; // We've already sent the response. This tells the calling code not to send it again.
// send response header
this->sendHeader("video/mp2t", numBytes);
// stream body
this->streamSource(subsession->getStreamSource(streamToken));
// Ask the media source to deliver - to the TCP sink - the desired data:
if (fStreamSource != NULL) { // sanity check
if (fTCPSink != NULL) fTCPSink->stopPlaying();
Medium::close(fStreamSource);
}
fStreamSource = subsession->getStreamSource(streamToken);
if (fStreamSource != NULL) {
if (fTCPSink == NULL) fTCPSink = TCPStreamSink::createNew(envir(), fClientOutputSocket);
fTCPSink->startPlaying(*fStreamSource, afterStreaming, this);
}
} while(0);
delete[] streamName;
@ -218,8 +192,8 @@ class HLSServer : public RTSPServer
this->sendPlayList(urlSuffix);
}
static void afterStreaming(void* clientData) {
std::cout << "afterStreaming" << std::endl;
static void afterStreaming(void* clientData)
{
HLSServer::HLSClientConnection* clientConnection = (HLSServer::HLSClientConnection*)clientData;
// Arrange to delete the 'client connection' object:
if (clientConnection->fRecursionCount > 0) {
@ -227,13 +201,11 @@ class HLSServer : public RTSPServer
clientConnection->fIsActive = False; // will cause the object to get deleted at the end of handling the request
} else {
// We're no longer handling a request; delete the object now:
// delete clientConnection;
delete clientConnection;
}
}
private:
u_int32_t fClientSessionId;
FramedSource* fStreamSource;
ByteStreamMemoryBufferSource* fPlaylistSource;
TCPStreamSink* fTCPSink;
};
@ -241,7 +213,6 @@ class HLSServer : public RTSPServer
static HLSServer* createNew(UsageEnvironment& env, Port rtspPort, UserAuthenticationDatabase* authDatabase, unsigned reclamationTestSeconds) {
int ourSocket = setUpOurSocket(env, rtspPort);
if (ourSocket == -1) return NULL;
return new HLSServer(env, ourSocket, rtspPort, authDatabase, reclamationTestSeconds);
}
@ -249,9 +220,6 @@ class HLSServer : public RTSPServer
: RTSPServer(env, ourSocket, rtspPort, authDatabase, reclamationTestSeconds) {
}
virtual ~HLSServer() {
}
RTSPServer::RTSPClientConnection* createNewClientConnection(int clientSocket, struct sockaddr_in clientAddr) {
return new HLSClientConnection(*this, clientSocket, clientAddr);
}
@ -562,11 +530,13 @@ int main(int argc, char** argv)
}
// Create Unicast Session
addSession(rtspServer, baseUrl+url, UnicastServerMediaSubsession::createNew(*env,replicator,rtpFormat));
if (muxTS)
{
addSession(rtspServer, "hls", HLSServerMediaSubsession::createNew(*env,replicator,rtpFormat));
addSession(rtspServer, baseUrl+url, HLSServerMediaSubsession::createNew(*env,replicator,rtpFormat));
}
else
{
addSession(rtspServer, baseUrl+url, UnicastServerMediaSubsession::createNew(*env,replicator,rtpFormat));
}
}
if (out)