#include "PL_RTSPServer.h"
|
|
#include <liveMedia.hh>
|
#include <BasicUsageEnvironment.hh>
|
|
#include "FFmpegRTSPServer/IEncoder.h"
|
#include "FFmpegRTSPServer/LiveRTSPServer.h"
|
#include "FFmpegRTSPServer/FFmpegH264Source.h"
|
#include "FFmpegRTSPServer/LiveServerMediaSubsession.h"
|
|
class MyEncoderStub;
|
|
struct RTSPServer_Internal
|
{
|
uint8_t* buffer;
|
size_t buffSize;
|
|
bool payError;
|
pthread_t live_daemon_thid;
|
pthread_mutex_t* frame_mutex;
|
bool live_daemon_running;
|
|
MESAI::LiveRTSPServer * server;
|
MyEncoderStub * encoderStub;
|
|
RTSPServer_Internal() :
|
buffer(nullptr), buffSize(0),
|
payError(true), live_daemon_thid(0), frame_mutex(new pthread_mutex_t), live_daemon_running(false),
|
server(nullptr), encoderStub(nullptr)
|
{
|
pthread_mutex_init(frame_mutex, NULL);
|
}
|
|
~RTSPServer_Internal()
|
{
|
if (frame_mutex != nullptr)
|
{
|
pthread_mutex_destroy(frame_mutex);
|
delete frame_mutex;
|
frame_mutex = nullptr;
|
}
|
}
|
|
void reset()
|
{
|
buffer = nullptr;
|
buffSize = 0;
|
|
payError = true;
|
|
if (frame_mutex != nullptr)
|
{
|
pthread_mutex_destroy(frame_mutex);
|
delete frame_mutex;
|
frame_mutex = nullptr;
|
}
|
|
frame_mutex = new pthread_mutex_t;
|
pthread_mutex_init(frame_mutex, NULL);
|
|
live_daemon_thid = 0;
|
live_daemon_running = false;
|
|
server = nullptr;
|
encoderStub = nullptr;
|
}
|
};
|
|
class MyEncoderStub : public MESAI::IEncoder
|
{
|
public:
|
MyEncoderStub(RTSPServer_Internal& _in) : in(_in)
|
{
|
}
|
|
virtual ~MyEncoderStub()
|
{
|
}
|
|
virtual void setCallbackFunctionFrameIsReady(std::function<void()> func)
|
{
|
onFrame = func;
|
}
|
|
virtual char GetFrame(u_int8_t** FrameBuffer, unsigned int *FrameSize)
|
{
|
if (in.buffer != nullptr && in.buffSize > 0)
|
{
|
*FrameBuffer = in.buffer;
|
*FrameSize = in.buffSize;
|
|
printf("send frame size=%u\n", in.buffSize);
|
|
in.buffer = nullptr;
|
in.buffSize = 0;
|
|
return 1;
|
}
|
else
|
{
|
ReleaseFrame();
|
return 0;
|
}
|
}
|
|
virtual char ReleaseFrame()
|
{
|
int ret = pthread_mutex_unlock(in.frame_mutex);
|
if(ret != 0)
|
{
|
printf("pthread_mutex_unlock frame_mutex: %s/n", strerror(ret));
|
return 0;
|
}
|
|
return 1;
|
}
|
|
void deliverFrame()
|
{
|
// write frame buffer of RTSPServer_Internal::buffer
|
onFrame();
|
|
int ret = pthread_mutex_lock(in.frame_mutex);
|
if(ret != 0)
|
{
|
printf("pthread_mutex_lock frame_mutex: %s/n", strerror(ret));
|
return;
|
}
|
}
|
|
private:
|
RTSPServer_Internal& in;
|
std::function<void()> onFrame;
|
};
|
|
PipeLineElem* create_PL_RTSPServer()
|
{
|
return new PL_RTSPServer;
|
}
|
|
PL_RTSPServer::PL_RTSPServer() : internal(new RTSPServer_Internal)
|
{
|
}
|
|
PL_RTSPServer::~PL_RTSPServer()
|
{
|
delete (RTSPServer_Internal*)internal;
|
internal = nullptr;
|
}
|
|
static void* live_daemon_thd(void* arg)
|
{
|
RTSPServer_Internal* in = (RTSPServer_Internal*)arg;
|
|
MyEncoderStub encoder(*in);
|
in->encoderStub = &encoder;
|
in->server = new MESAI::LiveRTSPServer(&encoder, 8554, 8080);
|
|
in->live_daemon_running = true;
|
in->server->run(); // does not return
|
in->encoderStub = nullptr;
|
in->live_daemon_running = false;
|
}
|
|
bool PL_RTSPServer::init(void* args)
|
{
|
RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
|
in->reset();
|
|
int ret = pthread_create(&(in->live_daemon_thid), NULL, live_daemon_thd, in);
|
if(ret != 0)
|
{
|
printf("pthread_create: %s/n", strerror(ret));
|
return false;
|
}
|
|
return true;
|
}
|
|
void PL_RTSPServer::finit()
|
{
|
RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
|
|
pthread_join(in->live_daemon_thid, NULL);
|
}
|
|
bool PL_RTSPServer::pay(const PipeMaterial& pm)
|
{
|
RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
|
|
if (pm.buffer == nullptr || pm.buffSize <= 0)
|
return false;
|
|
in->buffer = pm.buffer;
|
in->buffSize = pm.buffSize;
|
|
if (in->encoderStub == nullptr)
|
return false;
|
|
in->encoderStub->deliverFrame();
|
return true;
|
}
|
|
bool PL_RTSPServer::gain(PipeMaterial& pm)
|
{
|
RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
|
|
pm.buffer = nullptr;
|
pm.buffSize = 0;
|
pm.former = this;
|
return true;
|
}
|