#include "PL_RTSPServer.h"
|
#include "MaterialBuffer.h"
|
#include "logger.h"
|
|
#include <liveMedia/liveMedia.hh>
|
#include <BasicUsageEnvironment/BasicUsageEnvironment.hh>
|
|
#include "FFmpegRTSPServer/IEncoder.h"
|
#include "FFmpegRTSPServer/LiveRTSPServer.h"
|
#include "FFmpegRTSPServer/H264FramedSource.h"
|
#include "FFmpegRTSPServer/LiveServerMediaSubsession.h"
|
#include "PreAllocBufferQueue.h"
|
#include "MediaHelper.h"
|
|
struct RTSPServer_Internal
|
{
|
RTSPServerConfig config;
|
|
pthread_t live_daemon_thid;
|
bool live_daemon_running;
|
|
MESAI::LiveRTSPServer* server;
|
|
PreAllocBufferQueue* frameQueue;
|
pthread_mutex_t* queue_mutex;
|
pthread_mutex_t* queue_empty_mutex;
|
|
bool auxLineSet;
|
|
RTSPServer_Internal() :
|
config(),
|
live_daemon_thid(0), live_daemon_running(false),
|
server(nullptr),
|
frameQueue(nullptr), queue_mutex(new pthread_mutex_t), queue_empty_mutex(new pthread_mutex_t), //#todo from config
|
auxLineSet(false)
|
{
|
pthread_mutex_init(queue_mutex, NULL);
|
}
|
|
~RTSPServer_Internal()
|
{
|
reset();
|
}
|
|
void reset()
|
{
|
RTSPServerConfig _config;
|
config =_config;
|
|
if (frameQueue != nullptr)
|
{
|
delete frameQueue;
|
frameQueue = nullptr;
|
}
|
|
if (queue_mutex != nullptr)
|
{
|
pthread_mutex_destroy(queue_mutex);
|
delete queue_mutex;
|
queue_mutex = nullptr;
|
}
|
|
queue_mutex = new pthread_mutex_t;
|
pthread_mutex_init(queue_mutex, NULL);
|
|
if (queue_empty_mutex != nullptr)
|
{
|
pthread_mutex_destroy(queue_empty_mutex);
|
delete queue_empty_mutex;
|
queue_empty_mutex = nullptr;
|
}
|
|
queue_empty_mutex = new pthread_mutex_t;
|
pthread_mutex_init(queue_empty_mutex, NULL);
|
|
live_daemon_thid = 0;
|
live_daemon_running = false;
|
|
server = nullptr; //#todo delete
|
|
auxLineSet = false;
|
}
|
};
|
|
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;
|
}
|
|
struct DeliverFrameCallback
|
{
|
RTSPServer_Internal* in;
|
PreAllocBufferQueue::Buffer* lastBuffer;
|
|
DeliverFrameCallback(RTSPServer_Internal* _in)
|
: in(_in) , lastBuffer(nullptr)
|
{
|
}
|
|
~DeliverFrameCallback()
|
{
|
if (lastBuffer != nullptr)
|
{
|
in->frameQueue->Release(lastBuffer);
|
lastBuffer = nullptr;
|
}
|
}
|
|
static bool deliverFrame(void* args, uint8_t*& buffer, size_t& buffSize, timeval& pts)
|
{
|
DeliverFrameCallback* _this = (DeliverFrameCallback*)args;
|
|
if (_this->in->frameQueue->Empty())
|
{
|
int ret = pthread_mutex_lock(_this->in->queue_empty_mutex);
|
if (ret != 0)
|
{
|
LOG_WARN << "pthread_mutex_lock queue_empty_mutex, ret=" << ret << std::endl;
|
}
|
}
|
|
ScopeLocker<pthread_mutex_t>(_this->in->queue_mutex);
|
|
if (_this->lastBuffer != nullptr)
|
{
|
// this can not happen
|
_this->in->frameQueue->Release(_this->lastBuffer);
|
_this->lastBuffer = nullptr;
|
}
|
|
_this->lastBuffer = _this->in->frameQueue->Dequeue();
|
if (_this->lastBuffer == nullptr)
|
return false;
|
|
buffer = _this->lastBuffer->buffer;
|
buffSize = _this->lastBuffer->buffSize;
|
|
LOG_INFO << "DeliverFrameCallback buffSize=" << buffSize << LOG_ENDL;
|
//static size_t f = 0;
|
//static FILE *pFile = fopen("/data/bb.264", "wb");
|
//fwrite(buffer, sizeof(char), buffSize, pFile);
|
//if (++f > 30){
|
// fclose(pFile);
|
// exit(0);
|
//}
|
|
gettimeofday(&pts, NULL);
|
return (_this->lastBuffer != nullptr);
|
}
|
|
static void releaseFrame(void* args)
|
{
|
DeliverFrameCallback* _this = (DeliverFrameCallback*)args;
|
|
if (_this->lastBuffer != nullptr)
|
{
|
ScopeLocker<pthread_mutex_t>(_this->in->queue_mutex);
|
_this->in->frameQueue->Release(_this->lastBuffer);
|
_this->lastBuffer = nullptr;
|
}
|
}
|
};
|
|
static void* live_daemon_thd(void* arg)
|
{
|
RTSPServer_Internal* in = (RTSPServer_Internal*)arg;
|
|
in->server = new MESAI::LiveRTSPServer(nullptr, 8554, 8080);
|
|
in->server->init();
|
|
MESAI::H264FramedSource::FrameCallbacks cbs;
|
cbs.args = new DeliverFrameCallback(in);//#todo delete
|
cbs.deliverFrameCallback = DeliverFrameCallback::deliverFrame;
|
cbs.releaseFrameCallback = DeliverFrameCallback::releaseFrame;
|
in->server->framedSource = new MESAI::H264FramedSource(*in->server->env, cbs);
|
|
in->live_daemon_running = true;
|
in->server->run(); // does not return
|
//#todo delete framedSource
|
in->live_daemon_running = false;
|
}
|
|
bool PL_RTSPServer::init(void* args)
|
{
|
RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
|
|
if (args)
|
{
|
RTSPServerConfig* config = (RTSPServerConfig*)args;
|
in->config = *config;
|
}
|
|
PreAllocBufferQueue::Config qcfg;
|
qcfg.multithreadSafe = false;
|
qcfg.fullQueueDropFront = true;
|
qcfg.fullQueueSync = false;
|
qcfg.count = 32;
|
qcfg.maxBuffSize = 100000;
|
in->frameQueue = new PreAllocBufferQueue(qcfg);
|
|
int ret = pthread_create(&(in->live_daemon_thid), NULL, live_daemon_thd, in);
|
if(ret != 0)
|
{
|
LOG_ERROR << "pthread_create: " << strerror(ret) << std::endl;
|
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)
|
return false;
|
|
if (pm.type != PipeMaterial::PMT_FRAME)
|
{
|
LOG_ERROR << "PL_RTSPServer::pay only support PMT_FRAME" << std::endl;
|
return false;
|
}
|
|
if (!in->auxLineSet)
|
{
|
std::string spsStr(this->manager->get_param(PLGP_ENC_SPS_B64));
|
std::string ppsStr(this->manager->get_param(PLGP_ENC_PPS_B64));
|
|
if (!spsStr.empty() && !ppsStr.empty())
|
{
|
MESAI::H264FramedSource* framedSource = dynamic_cast<MESAI::H264FramedSource*>(in->server->framedSource);
|
framedSource->spsBase64 = spsStr;
|
framedSource->ppsBase64 = ppsStr;
|
|
in->auxLineSet = true;
|
}
|
}
|
|
MB_Frame* frame = (MB_Frame*)pm.buffer;
|
if (frame->buffer == nullptr || frame->buffSize == 0)
|
return false;
|
|
ScopeLocker<pthread_mutex_t>(in->queue_mutex);
|
//if (in->frameQueue->Full())
|
// LOG_WARN << "PL_RTSPServer::pay may lost data" << std::endl;
|
|
PreAllocBufferQueue::Buffer* qbuff = in->frameQueue->Enqueue();
|
if (qbuff == nullptr)
|
{
|
LOG_WARN << "PL_RTSPServer::pay may lost data size=" << frame->buffSize << std::endl;
|
int ret = pthread_mutex_unlock(in->queue_empty_mutex);
|
if (ret != 0)
|
{
|
LOG_WARN << "pthread_mutex_unlock queue_empty_mutex, ret=" << ret << std::endl;
|
}
|
return false;
|
}
|
|
memcpy(qbuff->buffer, frame->buffer, frame->buffSize);
|
qbuff->buffSize = frame->buffSize;
|
|
//static size_t f = 0;
|
//static FILE *pFile = fopen("/data/aa.264", "wb");
|
//fwrite(qbuff->buffer, sizeof(char), frame->buffSize, pFile);
|
//if (++f > 400){
|
// fclose(pFile);
|
// exit(0);
|
//}
|
|
int ret = pthread_mutex_unlock(in->queue_empty_mutex);
|
if (ret != 0)
|
{
|
LOG_WARN << "pthread_mutex_unlock queue_empty_mutex, ret=" << ret << std::endl;
|
}
|
return true;
|
}
|
|
bool PL_RTSPServer::gain(PipeMaterial& pm)
|
{
|
RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
|
|
pm.type = PipeMaterial::PMT_NONE;
|
pm.buffer = nullptr;
|
pm.buffSize = 0;
|
pm.former = this;
|
return true;
|
}
|