v4l2rtspserver/inc/ServerMediaSubsession.h

275 lines
8.5 KiB
C
Raw Normal View History

2014-07-20 14:02:14 +00:00
/* ---------------------------------------------------------------------------
** This software is in the public domain, furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** ServerMediaSubsession.h
**
** -------------------------------------------------------------------------*/
#ifndef SERVER_MEDIA_SUBSESSION
#define SERVER_MEDIA_SUBSESSION
#include <sys/stat.h>
2014-07-20 14:02:14 +00:00
#include <string>
2015-03-01 22:28:17 +00:00
#include <iomanip>
#include <iostream>
2016-04-10 15:58:03 +00:00
#include <fstream>
#include <map>
2014-07-20 14:02:14 +00:00
// live555
#include <liveMedia.hh>
// forward declaration
class V4L2DeviceSource;
2014-07-20 14:02:14 +00:00
// ---------------------------------
// BaseServerMediaSubsession
// ---------------------------------
class BaseServerMediaSubsession
{
public:
BaseServerMediaSubsession(StreamReplicator* replicator): m_replicator(replicator) {};
public:
static FramedSource* createSource(UsageEnvironment& env, FramedSource * videoES, const std::string& format);
static RTPSink* createSink(UsageEnvironment& env, Groupsock * rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, const std::string& format);
char const* getAuxLine(V4L2DeviceSource* source,unsigned char rtpPayloadType);
2014-07-20 14:02:14 +00:00
protected:
StreamReplicator* m_replicator;
};
// -----------------------------------------
// ServerMediaSubsession for Multicast
// -----------------------------------------
class MulticastServerMediaSubsession : public PassiveServerMediaSubsession , public BaseServerMediaSubsession
{
public:
static MulticastServerMediaSubsession* createNew(UsageEnvironment& env
, struct in_addr destinationAddress
, Port rtpPortNum, Port rtcpPortNum
, int ttl
, StreamReplicator* replicator
, const std::string& format);
2014-07-20 14:02:14 +00:00
protected:
MulticastServerMediaSubsession(StreamReplicator* replicator, RTPSink* rtpSink, RTCPInstance* rtcpInstance)
: PassiveServerMediaSubsession(*rtpSink, rtcpInstance), BaseServerMediaSubsession(replicator), m_rtpSink(rtpSink) {};
virtual char const* sdpLines() ;
virtual char const* getAuxSDPLine(RTPSink* rtpSink,FramedSource* inputSource);
protected:
RTPSink* m_rtpSink;
std::string m_SDPLines;
};
// -----------------------------------------
// ServerMediaSubsession for Unicast
// -----------------------------------------
class UnicastServerMediaSubsession : public OnDemandServerMediaSubsession , public BaseServerMediaSubsession
{
public:
static UnicastServerMediaSubsession* createNew(UsageEnvironment& env, StreamReplicator* replicator, const std::string& format);
2014-07-20 14:02:14 +00:00
protected:
UnicastServerMediaSubsession(UsageEnvironment& env, StreamReplicator* replicator, const std::string& format)
2014-07-20 14:02:14 +00:00
: OnDemandServerMediaSubsession(env, False), BaseServerMediaSubsession(replicator), m_format(format) {};
virtual FramedSource* createNewStreamSource(unsigned clientSessionId, unsigned& estBitrate);
virtual RTPSink* createNewRTPSink(Groupsock* rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource* inputSource);
2016-04-10 15:58:03 +00:00
virtual char const* getAuxSDPLine(RTPSink* rtpSink,FramedSource* inputSource);
protected:
const std::string m_format;
};
// -----------------------------------------
// ServerMediaSubsession for HLS
// -----------------------------------------
class HLSServerMediaSubsession : public UnicastServerMediaSubsession
2016-04-10 15:58:03 +00:00
{
class HLSSink : public MediaSink
{
public:
static HLSSink* createNew(UsageEnvironment& env, unsigned int bufferSize, unsigned int sliceDuration)
{
return new HLSSink(env, bufferSize, sliceDuration);
}
protected:
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];
}
2016-04-10 15:58:03 +00:00
virtual ~HLSSink()
{
delete[] m_buffer;
}
2016-04-10 15:58:03 +00:00
virtual Boolean continuePlaying()
2016-04-10 15:58:03 +00:00
{
Boolean ret = False;
if (fSource != NULL)
{
fSource->getNextFrame(m_buffer, m_bufferSize,
afterGettingFrame, this,
onSourceClosure, this);
ret = True;
}
return ret;
2016-04-10 15:58:03 +00:00
}
static void afterGettingFrame(void* clientData, unsigned frameSize,
unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds)
{
HLSSink* sink = (HLSSink*)clientData;
sink->afterGettingFrame(frameSize, numTruncatedBytes, presentationTime);
}
2016-04-10 15:58:03 +00:00
void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime)
2016-04-10 15:58:03 +00:00
{
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];
}
else
{
// append buffer to slice buffer
if (m_refTime == 0)
{
m_refTime = presentationTime.tv_sec;
}
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()>3)
{
m_outputBuffers.erase(m_outputBuffers.begin());
}
}
continuePlaying();
2016-04-10 15:58:03 +00:00
}
public:
unsigned int getHLSBufferSize(unsigned int slice)
2016-04-10 15:58:03 +00:00
{
unsigned int size = 0;
std::map<unsigned int,std::string>::iterator it = m_outputBuffers.find(slice);
if (it != m_outputBuffers.end())
2016-04-10 15:58:03 +00:00
{
size = it->second.size();
2016-04-10 15:58:03 +00:00
}
return size;
2016-04-10 15:58:03 +00:00
}
const char* getHLSBuffer(unsigned int slice)
2016-04-10 15:58:03 +00:00
{
const char* content = NULL;
std::map<unsigned int,std::string>::iterator it = m_outputBuffers.find(slice);
if (it != m_outputBuffers.end())
2016-04-15 18:26:54 +00:00
{
content = it->second.c_str();
2016-04-15 18:26:54 +00:00
}
return content;
2016-04-10 15:58:03 +00:00
}
unsigned int firstTime()
2016-04-10 15:58:03 +00:00
{
unsigned int firstTime = 0;
if (m_outputBuffers.size() != 0)
{
firstTime = m_outputBuffers.begin()->first;
}
return firstTime*m_sliceDuration;
2016-04-10 15:58:03 +00:00
}
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::map<unsigned int,std::string> m_outputBuffers;
unsigned int m_refTime;
unsigned int m_sliceDuration;
};
2016-04-10 15:58:03 +00:00
public:
static HLSServerMediaSubsession* createNew(UsageEnvironment& env, StreamReplicator* replicator, const std::string& format, unsigned int sliceDuration)
2016-04-10 15:58:03 +00:00
{
return new HLSServerMediaSubsession(env, replicator, format, sliceDuration);
2016-04-10 15:58:03 +00:00
}
protected:
HLSServerMediaSubsession(UsageEnvironment& env, StreamReplicator* replicator, const std::string& format, unsigned int sliceDuration)
2016-04-20 16:17:12 +00:00
: UnicastServerMediaSubsession(env, replicator, format), m_slice(0)
2016-04-10 15:58:03 +00:00
{
// Create a source
FramedSource* source = replicator->createStreamReplica();
FramedSource* videoSource = createSource(env, source, format);
// Start Playing the HLS Sink
m_hlsSink = HLSSink::createNew(env, OutPacketBuffer::maxSize, sliceDuration);
m_hlsSink->startPlaying(*videoSource, NULL, NULL);
2016-04-10 15:58:03 +00:00
}
virtual ~HLSServerMediaSubsession()
2016-04-10 15:58:03 +00:00
{
Medium::close(m_hlsSink);
}
virtual float getCurrentNPT(void* streamToken)
2016-04-10 15:58:03 +00:00
{
return (m_hlsSink->firstTime());
2016-04-10 15:58:03 +00:00
}
virtual float duration() const
{
return (m_hlsSink->duration());
2016-04-15 18:26:54 +00:00
}
2016-04-10 15:58:03 +00:00
virtual void seekStream(unsigned clientSessionId, void* streamToken, double& seekNPT, double streamDuration, u_int64_t& numBytes)
{
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;
2016-04-10 15:58:03 +00:00
}
virtual FramedSource* getStreamSource(void* streamToken)
{
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);
2016-04-10 15:58:03 +00:00
}
2014-07-20 14:02:14 +00:00
protected:
2016-04-10 15:58:03 +00:00
unsigned int m_slice;
HLSSink * m_hlsSink;
2014-07-20 14:02:14 +00:00
};
#endif