1
0
mirror of https://github.com/rofafor/vdr-plugin-satip.git synced 2023-10-10 13:37:42 +02:00

Added cSatipRtsp().

This commit is contained in:
Rolf Ahrenberg 2014-11-09 12:51:52 +02:00
parent 72a5ad34fb
commit 2f723e0f66
7 changed files with 440 additions and 294 deletions

View File

@ -88,7 +88,7 @@ all-redirect: all
### The object files (add further files here): ### The object files (add further files here):
OBJS = $(PLUGIN).o common.o config.o device.o discover.o param.o \ OBJS = $(PLUGIN).o common.o config.o device.o discover.o param.o rtsp.o \
sectionfilter.o server.o setup.o socket.o statistics.o tuner.o sectionfilter.o server.o setup.o socket.o statistics.o tuner.o
### The main target: ### The main target:

268
rtsp.c Normal file
View File

@ -0,0 +1,268 @@
/*
* rtsp.c: SAT>IP plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include "common.h"
#include "rtsp.h"
cSatipRtsp::cSatipRtsp(cSatipTunerIf &tunerP)
: tunerM(&tunerP),
tunerIdM(tunerM ? tunerM->GetId() : -1),
handleM(curl_easy_init()),
headerListM(NULL)
{
if (handleM) {
CURLcode res = CURLE_OK;
#ifdef DEBUG
// Verbose output
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_VERBOSE, 1L);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_DEBUGFUNCTION, cSatipRtsp::DebugCallback);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_DEBUGDATA, this);
#endif
// No progress meter and no signaling
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_NOPROGRESS, 1L);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_NOSIGNAL, 1L);
// Set timeouts
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_TIMEOUT_MS, (long)eConnectTimeoutMs);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_CONNECTTIMEOUT_MS, (long)eConnectTimeoutMs);
// Set user-agent
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_USERAGENT, *cString::sprintf("vdr-%s/%s (device %d)", PLUGIN_NAME_I18N, VERSION, tunerIdM));
}
}
cSatipRtsp::~cSatipRtsp()
{
if (handleM) {
// Cleanup curl stuff
if (headerListM) {
curl_slist_free_all(headerListM);
headerListM = NULL;
}
curl_easy_cleanup(handleM);
handleM = NULL;
}
}
size_t cSatipRtsp::HeaderCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP)
{
cSatipRtsp *obj = reinterpret_cast<cSatipRtsp *>(dataP);
size_t len = sizeP * nmembP;
//debug("cSatipRtsp::%s(%zu)", __FUNCTION__, len);
char *s, *p = (char *)ptrP;
char *r = strtok_r(p, "\r\n", &s);
while (obj && obj->tunerM && r) {
//debug("cSatipRtsp::%s(%zu): %s", __FUNCTION__, len, r);
r = skipspace(r);
if (strstr(r, "com.ses.streamID")) {
int streamid = -1;
if (sscanf(r, "com.ses.streamID:%11d", &streamid) == 1)
obj->tunerM->SetStreamId(streamid);
}
else if (strstr(r, "Session:")) {
int timeout = -1;
char *session = NULL;
if (sscanf(r, "Session:%m[^;];timeout=%11d", &session, &timeout) == 2)
obj->tunerM->SetSessionTimeout(skipspace(session), timeout * 1000);
else if (sscanf(r, "Session:%m[^;]", &session) == 1)
obj->tunerM->SetSessionTimeout(skipspace(session), -1);
FREE_POINTER(session);
}
r = strtok_r(NULL, "\r\n", &s);
}
return len;
}
size_t cSatipRtsp::WriteCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP)
{
cSatipRtsp *obj = reinterpret_cast<cSatipRtsp *>(dataP);
size_t len = sizeP * nmembP;
//debug("cSatipRtsp::%s(%zu)", __FUNCTION__, len);
if (obj && obj->tunerM && (len > 0)) {
char *data = strndup((char*)ptrP, len);
obj->tunerM->ParseReceptionParameters(data);
FREE_POINTER(data);
}
return len;
}
int cSatipRtsp::DebugCallback(CURL *handleP, curl_infotype typeP, char *dataP, size_t sizeP, void *userPtrP)
{
cSatipRtsp *obj = reinterpret_cast<cSatipRtsp *>(userPtrP);
if (obj && obj->tunerM) {
switch (typeP) {
case CURLINFO_TEXT:
debug("cSatipTuner::%s(%d): RTSP INFO %.*s", __FUNCTION__, obj->tunerM->GetId(), (int)sizeP, dataP);
break;
case CURLINFO_HEADER_IN:
debug("cSatipTuner::%s(%d): RTSP HEAD <<< %.*s", __FUNCTION__, obj->tunerM->GetId(), (int)sizeP, dataP);
break;
case CURLINFO_HEADER_OUT:
debug("cSatipTuner::%s(%d): RTSP HEAD >>>\n%.*s", __FUNCTION__, obj->tunerM->GetId(), (int)sizeP, dataP);
break;
case CURLINFO_DATA_IN:
debug("cSatipTuner::%s(%d): RTSP DATA <<< %.*s", __FUNCTION__, obj->tunerM->GetId(), (int)sizeP, dataP);
break;
case CURLINFO_DATA_OUT:
debug("cSatipTuner::%s(%d): RTSP DATA >>>\n%.*s", __FUNCTION__, obj->tunerM->GetId(), (int)sizeP, dataP);
break;
default:
break;
}
}
return 0;
}
cString cSatipRtsp::RtspUnescapeString(const char *strP)
{
debug("cSatipRtsp::%s(%d): str=%s", __FUNCTION__, tunerIdM, strP);
if (handleM) {
char *p = curl_easy_unescape(handleM, strP, 0, NULL);
cString s = p;
curl_free(p);
return s;
}
return cString(strP);
}
bool cSatipRtsp::Options(const char *uriP)
{
debug("cSatipRtsp::%s(%d): uri=%s", __FUNCTION__, tunerIdM, uriP);
if (handleM && !isempty(uriP)) {
CURLcode res = CURLE_OK;
if (!strstr(uriP, "?"))
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_URL, uriP);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, uriP);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS);
SATIP_CURL_EASY_PERFORM(handleM);
return ValidateLatestResponse();
}
return false;
}
bool cSatipRtsp::Setup(const char *uriP, int rtpPortP, int rtcpPortP)
{
debug("cSatipRtsp::%s(%d): uri=%s rtp=%d rtcp=%d", __FUNCTION__, tunerIdM, uriP, rtpPortP, rtcpPortP);
if (handleM && !isempty(uriP)) {
CURLcode res = CURLE_OK;
cString transport = cString::sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpPortP, rtcpPortP);
// Setup media stream
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, uriP);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_TRANSPORT, *transport);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
// Set header callback for catching the session and timeout
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_HEADERFUNCTION, cSatipRtsp::HeaderCallback);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEHEADER, this);
SATIP_CURL_EASY_PERFORM(handleM);
// Session id is now known - disable header parsing
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_HEADERFUNCTION, NULL);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEHEADER, NULL);
return ValidateLatestResponse();
}
return false;
}
bool cSatipRtsp::SetSession(const char *sessionP)
{
debug("cSatipRtsp::%s(%d): session=%s", __FUNCTION__, tunerIdM, sessionP);
if (handleM) {
CURLcode res = CURLE_OK;
debug("cSatipRtsp::%s(%d): session id quirk enabled", __FUNCTION__, tunerIdM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_SESSION_ID, sessionP);
}
return true;
}
bool cSatipRtsp::Describe(const char *uriP)
{
debug("cSatipRtsp::%s(%d): uri=%s", __FUNCTION__, tunerIdM, uriP);
if (handleM && !isempty(uriP)) {
CURLcode res = CURLE_OK;
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, uriP);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEFUNCTION, cSatipRtsp::WriteCallback);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEDATA, this);
SATIP_CURL_EASY_PERFORM(handleM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEFUNCTION, NULL);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEDATA, NULL);
return ValidateLatestResponse();
}
return false;
}
bool cSatipRtsp::Play(const char *uriP)
{
debug("cSatipRtsp::%s(%d): uri=%s", __FUNCTION__, tunerIdM, uriP);
if (handleM && !isempty(uriP)) {
CURLcode res = CURLE_OK;
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, uriP);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY);
SATIP_CURL_EASY_PERFORM(handleM);
return ValidateLatestResponse();
}
return false;
}
bool cSatipRtsp::Teardown(const char *uriP)
{
debug("cSatipRtsp::%s(%d): uri=%s", __FUNCTION__, tunerIdM, uriP);
if (handleM && !isempty(uriP)) {
CURLcode res = CURLE_OK;
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, uriP);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN);
SATIP_CURL_EASY_PERFORM(handleM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_CLIENT_CSEQ, 1L);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_SESSION_ID, NULL);
return ValidateLatestResponse();
}
return false;
}
bool cSatipRtsp::ValidateLatestResponse(void)
{
debug("cSatipRtsp::%s(%d)", __FUNCTION__, tunerIdM);
if (handleM) {
long rc = 0;
CURLcode res = CURLE_OK;
SATIP_CURL_EASY_GETINFO(handleM, CURLINFO_RESPONSE_CODE, &rc);
if (rc == 200)
return true;
else if (rc != 0)
error("Tuner detected invalid status code %ld [device %d]", rc, tunerIdM);
}
return false;
}

54
rtsp.h Normal file
View File

@ -0,0 +1,54 @@
/*
* rtsp.h: SAT>IP plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#ifndef __SATIP_RTSP_H
#define __SATIP_RTSP_H
#include <curl/curl.h>
#include <curl/easy.h>
#ifndef CURLOPT_RTSPHEADER
#error "libcurl is missing required RTSP support"
#endif
#include "tunerif.h"
class cSatipRtsp {
private:
static size_t HeaderCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP);
static size_t WriteCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP);
static int DebugCallback(CURL *handleP, curl_infotype typeP, char *dataP, size_t sizeP, void *userPtrP);
enum {
eConnectTimeoutMs = 1500, // in milliseconds
};
cSatipTunerIf* tunerM;
int tunerIdM;
CURL *handleM;
struct curl_slist *headerListM;
bool ValidateLatestResponse(void);
// to prevent copy constructor and assignment
cSatipRtsp(const cSatipRtsp&);
cSatipRtsp& operator=(const cSatipRtsp&);
public:
cSatipRtsp(cSatipTunerIf &tunerP);
virtual ~cSatipRtsp();
cString RtspUnescapeString(const char *strP);
bool Options(const char *uriP);
bool Setup(const char *uriP, int rtpPortP, int rtcpPortP);
bool SetSession(const char *sessionP);
bool Describe(const char *uriP);
bool Play(const char *uriP);
bool Teardown(const char *uriP);
};
#endif // __SATIP_RTSP_H

View File

@ -38,7 +38,6 @@ cSatipSocket::~cSatipSocket()
bool cSatipSocket::Open(const int portP) bool cSatipSocket::Open(const int portP)
{ {
debug("cSatipSocket::%s(%d)", __FUNCTION__, portP);
// Bind to the socket if it is not active already // Bind to the socket if it is not active already
if (socketDescM < 0) { if (socketDescM < 0) {
socklen_t len = sizeof(sockAddrM); socklen_t len = sizeof(sockAddrM);
@ -64,12 +63,13 @@ bool cSatipSocket::Open(const int portP)
"getsockname()", Close(), return false); "getsockname()", Close(), return false);
socketPortM = ntohs(sockAddrM.sin_port); socketPortM = ntohs(sockAddrM.sin_port);
} }
debug("cSatipSocket::%s(%d): socketPort=%d", __FUNCTION__, portP, socketPortM);
return true; return true;
} }
void cSatipSocket::Close(void) void cSatipSocket::Close(void)
{ {
debug("cSatipSocket::%s()", __FUNCTION__); debug("cSatipSocket::%s(%d)", __FUNCTION__, socketPortM);
// Check if socket exists // Check if socket exists
if (socketDescM >= 0) { if (socketDescM >= 0) {
close(socketDescM); close(socketDescM);

336
tuner.c
View File

@ -14,7 +14,9 @@ cSatipTuner::cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP)
: cThread("SAT>IP tuner"), : cThread("SAT>IP tuner"),
sleepM(), sleepM(),
deviceM(&deviceP), deviceM(&deviceP),
deviceIdM(deviceM ? deviceM->GetId() : -1),
packetBufferLenM(packetLenP), packetBufferLenM(packetLenP),
rtspM(new cSatipRtsp(*this)),
rtpSocketM(new cSatipSocket()), rtpSocketM(new cSatipSocket()),
rtcpSocketM(new cSatipSocket()), rtcpSocketM(new cSatipSocket()),
streamAddrM(""), streamAddrM(""),
@ -22,8 +24,6 @@ cSatipTuner::cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP)
currentServerM(NULL), currentServerM(NULL),
nextServerM(NULL), nextServerM(NULL),
mutexM(), mutexM(),
handleM(NULL),
headerListM(NULL),
keepAliveM(), keepAliveM(),
statusUpdateM(), statusUpdateM(),
pidUpdateCacheM(), pidUpdateCacheM(),
@ -39,110 +39,52 @@ cSatipTuner::cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP)
delPidsM(), delPidsM(),
pidsM() pidsM()
{ {
debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, packetBufferLenM, deviceM->GetId()); debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, packetBufferLenM, deviceIdM);
// Allocate packet buffer // Allocate packet buffer
packetBufferM = MALLOC(unsigned char, packetBufferLenM); packetBufferM = MALLOC(unsigned char, packetBufferLenM);
if (packetBufferM) if (packetBufferM)
memset(packetBufferM, 0, packetBufferLenM); memset(packetBufferM, 0, packetBufferLenM);
else else
error("MALLOC() failed for packet buffer [device %d]", deviceM->GetId()); error("MALLOC() failed for packet buffer [device %d]", deviceIdM);
// Open sockets
int i = 100;
while (i-- > 0) {
if (rtpSocketM->Open() && rtcpSocketM->Open(rtpSocketM->Port() + 1))
break;
rtpSocketM->Close();
rtcpSocketM->Close();
}
if ((rtpSocketM->Port() <= 0) || (rtcpSocketM->Port() <= 0)) {
error("Cannot open required RTP/RTCP ports [device %d]", deviceIdM);
}
// Start thread // Start thread
Start(); Start();
} }
cSatipTuner::~cSatipTuner() cSatipTuner::~cSatipTuner()
{ {
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
// Stop thread // Stop thread
sleepM.Signal(); sleepM.Signal();
if (Running()) if (Running())
Cancel(3); Cancel(3);
Close(); Close();
// Close the listening sockets
rtpSocketM->Close();
rtcpSocketM->Close();
// Free allocated memory // Free allocated memory
free(packetBufferM); free(packetBufferM);
DELETENULL(rtcpSocketM); DELETENULL(rtcpSocketM);
DELETENULL(rtpSocketM); DELETENULL(rtpSocketM);
} }
size_t cSatipTuner::HeaderCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP)
{
cSatipTuner *obj = reinterpret_cast<cSatipTuner *>(dataP);
size_t len = sizeP * nmembP;
//debug("cSatipTuner::%s(%zu)", __FUNCTION__, len);
char *s, *p = (char *)ptrP;
char *r = strtok_r(p, "\r\n", &s);
while (obj && r) {
//debug("cSatipTuner::%s(%zu): %s", __FUNCTION__, len, r);
r = skipspace(r);
if (strstr(r, "com.ses.streamID")) {
int streamid = -1;
if (sscanf(r, "com.ses.streamID:%11d", &streamid) == 1)
obj->SetStreamId(streamid);
}
else if (strstr(r, "Session:")) {
int timeout = -1;
char *session = NULL;
if (sscanf(r, "Session:%m[^;];timeout=%11d", &session, &timeout) == 2)
obj->SetSessionTimeout(skipspace(session), timeout * 1000);
else if (sscanf(r, "Session:%m[^;]", &session) == 1)
obj->SetSessionTimeout(skipspace(session));
FREE_POINTER(session);
}
r = strtok_r(NULL, "\r\n", &s);
}
return len;
}
size_t cSatipTuner::DataCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP)
{
cSatipTuner *obj = reinterpret_cast<cSatipTuner *>(dataP);
size_t len = sizeP * nmembP;
//debug("cSatipTuner::%s(%zu)", __FUNCTION__, len);
if (obj && (len > 0)) {
char *data = strndup((char*)ptrP, len);
obj->ParseReceptionParameters(data);
FREE_POINTER(data);
}
return len;
}
int cSatipTuner::DebugCallback(CURL *handleP, curl_infotype typeP, char *dataP, size_t sizeP, void *userPtrP)
{
cSatipTuner *obj = reinterpret_cast<cSatipTuner *>(userPtrP);
if (obj) {
switch (typeP) {
case CURLINFO_TEXT:
debug("cSatipTuner::%s(%d): RTSP INFO %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
break;
case CURLINFO_HEADER_IN:
debug("cSatipTuner::%s(%d): RTSP HEAD <<< %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
break;
case CURLINFO_HEADER_OUT:
debug("cSatipTuner::%s(%d): RTSP HEAD >>> %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
break;
case CURLINFO_DATA_IN:
debug("cSatipTuner::%s(%d): RTSP DATA <<< %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
break;
case CURLINFO_DATA_OUT:
debug("cSatipTuner::%s(%d): RTSP DATA >>> %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
break;
default:
break;
}
}
return 0;
}
void cSatipTuner::Action(void) void cSatipTuner::Action(void)
{ {
debug("cSatipTuner::%s(): entering [device %d]", __FUNCTION__, deviceM->GetId()); debug("cSatipTuner::%s(): entering [device %d]", __FUNCTION__, deviceIdM);
cTimeMs timeout(eReConnectTimeoutMs); cTimeMs timeout(eReConnectTimeoutMs);
// Increase priority // Increase priority
SetPriority(-1); SetPriority(-1);
@ -190,126 +132,45 @@ void cSatipTuner::Action(void)
sleepM.Wait(10); // to avoid busy loop and reduce cpu load sleepM.Wait(10); // to avoid busy loop and reduce cpu load
} }
} }
debug("cSatipTuner::%s(): exiting [device %d]", __FUNCTION__, deviceM->GetId()); debug("cSatipTuner::%s(): exiting [device %d]", __FUNCTION__, deviceIdM);
} }
bool cSatipTuner::Open(void) bool cSatipTuner::Open(void)
{ {
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return Connect(); return Connect();
} }
bool cSatipTuner::Close(void) bool cSatipTuner::Close(void)
{ {
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return Disconnect(); return Disconnect();
} }
bool cSatipTuner::Connect(void) bool cSatipTuner::Connect(void)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
// Initialize the curl session
if (!handleM)
handleM = curl_easy_init();
if (handleM && !isempty(*streamAddrM)) {
cString uri, control, transport, range;
CURLcode res = CURLE_OK;
if (!isempty(*streamAddrM)) {
cString connectionUri = cString::sprintf("rtsp://%s", *streamAddrM);
cString uri = cString::sprintf("%s/?%s", *connectionUri, *streamParamM);
// Just retune // Just retune
if (tunedM && (streamIdM >= 0)) { if (tunedM && (streamIdM >= 0)) {
debug("cSatipTuner::%s(): retune [device %d]", __FUNCTION__, deviceM->GetId()); debug("cSatipTuner::%s(): retune [device %d]", __FUNCTION__, deviceIdM);
keepAliveM.Set(0); keepAliveM.Set(0);
KeepAlive(); KeepAlive();
// Flush any old content // Flush any old content
if (rtpSocketM) if (rtpSocketM)
rtpSocketM->Flush(); rtpSocketM->Flush();
openedM = rtspM->Setup(*uri, rtpSocketM->Port(), rtcpSocketM->Port());
uri = cString::sprintf("rtsp://%s/?%s", *streamAddrM, *streamParamM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
transport = cString::sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpSocketM->Port(), rtcpSocketM->Port());
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_TRANSPORT, *transport);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
SATIP_CURL_EASY_PERFORM(handleM);
openedM = true;
return openedM; return openedM;
} }
#ifdef DEBUG
// Verbose output
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_VERBOSE, 1L);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_DEBUGFUNCTION, cSatipTuner::DebugCallback);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_DEBUGDATA, this);
#endif
// No progress meter and no signaling
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_NOPROGRESS, 1L);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_NOSIGNAL, 1L);
// Set timeouts
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_TIMEOUT_MS, (long)eConnectTimeoutMs);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_CONNECTTIMEOUT_MS, (long)eConnectTimeoutMs);
// Set user-agent
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_USERAGENT, *cString::sprintf("vdr-%s/%s (device %d)", PLUGIN_NAME_I18N, VERSION, deviceM->GetId()));
// Set URL
char *p = curl_easy_unescape(handleM, *streamAddrM, 0, NULL);
streamAddrM = p;
curl_free(p);
uri = cString::sprintf("rtsp://%s/", *streamAddrM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_URL, *uri);
// Open sockets
int i = 100;
while (i-- > 0) {
if (rtpSocketM->Open() && rtcpSocketM->Open(rtpSocketM->Port() + 1))
break;
rtpSocketM->Close();
rtcpSocketM->Close();
}
if ((rtpSocketM->Port() <= 0) || (rtcpSocketM->Port() <= 0)) {
error("Cannot open required RTP/RTCP ports [device %d]", deviceM->GetId());
openedM = false;
return openedM;
}
// Request server options
keepAliveM.Set(timeoutM); keepAliveM.Set(timeoutM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri); openedM = rtspM->Options(*connectionUri) && rtspM->Setup(*uri, rtpSocketM->Port(), rtcpSocketM->Port());
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS); if (openedM) {
SATIP_CURL_EASY_PERFORM(handleM); if (nextServerM && nextServerM->Quirk(cSatipServer::eSatipQuirkSessionId))
if (!ValidateLatestResponse()) { rtspM->SetSession(SkipZeroes(*sessionM));
openedM = false;
return openedM;
}
// Setup media stream: "&pids=all" for the whole mux
uri = cString::sprintf("rtsp://%s/?%s", *streamAddrM, *streamParamM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
transport = cString::sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpSocketM->Port(), rtcpSocketM->Port());
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_TRANSPORT, *transport);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
// Set header callback for catching the session and timeout
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_HEADERFUNCTION, cSatipTuner::HeaderCallback);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEHEADER, this);
SATIP_CURL_EASY_PERFORM(handleM);
// Session id is now known - disable header parsing
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_HEADERFUNCTION, NULL);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEHEADER, NULL);
if (nextServerM && nextServerM->Quirk(cSatipServer::eSatipQuirkSessionId) && !isempty(*sessionM) && startswith(*sessionM, "0")) {
debug("cSatipTuner::%s(): session id quirk [device %d]", __FUNCTION__, deviceM->GetId());
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_SESSION_ID, SkipZeroes(*sessionM));
}
if (!ValidateLatestResponse()) {
openedM = false;
return openedM;
}
// Start playing
tunedM = true; tunedM = true;
UpdatePids(true); UpdatePids(true);
if (nextServerM) { if (nextServerM) {
@ -317,7 +178,8 @@ bool cSatipTuner::Connect(void)
currentServerM = nextServerM; currentServerM = nextServerM;
nextServerM = NULL; nextServerM = NULL;
} }
openedM = true; }
return openedM; return openedM;
} }
@ -328,35 +190,14 @@ bool cSatipTuner::Connect(void)
bool cSatipTuner::Disconnect(void) bool cSatipTuner::Disconnect(void)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
openedM = false; openedM = false;
// Terminate curl session if (!isempty(*streamAddrM)) {
if (handleM) {
// Teardown rtsp session
if (!isempty(*streamAddrM) && streamIdM >= 0) {
CURLcode res = CURLE_OK;
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM); cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri); rtspM->Teardown(*uri);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN);
SATIP_CURL_EASY_PERFORM(handleM);
ValidateLatestResponse();
} }
// Cleanup curl stuff
if (headerListM) {
curl_slist_free_all(headerListM);
headerListM = NULL;
}
curl_easy_cleanup(handleM);
handleM = NULL;
}
// Close the listening sockets
rtpSocketM->Close();
rtcpSocketM->Close();
// Reset signal parameters // Reset signal parameters
hasLockM = false; hasLockM = false;
signalStrengthM = -1; signalStrengthM = -1;
@ -374,25 +215,9 @@ bool cSatipTuner::Disconnect(void)
return true; return true;
} }
bool cSatipTuner::ValidateLatestResponse(void)
{
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
if (handleM) {
long rc = 0;
CURLcode res = CURLE_OK;
SATIP_CURL_EASY_GETINFO(handleM, CURLINFO_RESPONSE_CODE, &rc);
if (rc == 200)
return true;
else if (rc != 0)
error("Tuner detected invalid status code %ld [device %d]", rc, deviceM->GetId());
}
return false;
}
void cSatipTuner::ParseReceptionParameters(const char *paramP) void cSatipTuner::ParseReceptionParameters(const char *paramP)
{ {
//debug("cSatipTuner::%s(%s) [device %d]", __FUNCTION__, paramP, deviceM->GetId()); //debug("cSatipTuner::%s(%s) [device %d]", __FUNCTION__, paramP, deviceIdM);
// DVB-S2: // DVB-S2:
// ver=<major>.<minor>;src=<srcID>;tuner=<feID>,<level>,<lock>,<quality>,<frequency>,<polarisation>,<system>,<type>,<pilots>,<roll_off>,<symbol_rate>,<fec_inner>;pids=<pid0>,...,<pidn> // ver=<major>.<minor>;src=<srcID>;tuner=<feID>,<level>,<lock>,<quality>,<frequency>,<polarisation>,<system>,<type>,<pilots>,<roll_off>,<symbol_rate>,<fec_inner>;pids=<pid0>,...,<pidn>
// DVB-T2: // DVB-T2:
@ -439,27 +264,33 @@ void cSatipTuner::ParseReceptionParameters(const char *paramP)
void cSatipTuner::SetStreamId(int streamIdP) void cSatipTuner::SetStreamId(int streamIdP)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, streamIdP, deviceM->GetId()); debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, streamIdP, deviceIdM);
streamIdM = streamIdP; streamIdM = streamIdP;
} }
void cSatipTuner::SetSessionTimeout(const char *sessionP, int timeoutP) void cSatipTuner::SetSessionTimeout(const char *sessionP, int timeoutP)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, sessionP, timeoutP, deviceM->GetId()); debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, sessionP, timeoutP, deviceIdM);
sessionM = sessionP; sessionM = sessionP;
timeoutM = (timeoutP > eMinKeepAliveIntervalMs) ? timeoutP : eMinKeepAliveIntervalMs; timeoutM = (timeoutP > eMinKeepAliveIntervalMs) ? timeoutP : eMinKeepAliveIntervalMs;
} }
int cSatipTuner::GetId(void)
{
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return deviceIdM;
}
bool cSatipTuner::SetSource(cSatipServer *serverP, const char *parameterP, const int indexP) bool cSatipTuner::SetSource(cSatipServer *serverP, const char *parameterP, const int indexP)
{ {
debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, parameterP, indexP, deviceM->GetId()); debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, parameterP, indexP, deviceIdM);
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
if (serverP) { if (serverP) {
nextServerM = cSatipDiscover::GetInstance()->GetServer(serverP); nextServerM = cSatipDiscover::GetInstance()->GetServer(serverP);
if (nextServerM && !isempty(nextServerM->Address()) && !isempty(parameterP)) { if (nextServerM && !isempty(nextServerM->Address()) && !isempty(parameterP)) {
// Update stream address and parameter // Update stream address and parameter
streamAddrM = nextServerM->Address(); streamAddrM = rtspM->RtspUnescapeString(nextServerM->Address());
streamParamM = parameterP; streamParamM = parameterP;
// Reconnect // Reconnect
Connect(); Connect();
@ -472,7 +303,7 @@ bool cSatipTuner::SetSource(cSatipServer *serverP, const char *parameterP, const
bool cSatipTuner::SetPid(int pidP, int typeP, bool onP) bool cSatipTuner::SetPid(int pidP, int typeP, bool onP)
{ {
//debug("cSatipTuner::%s(%d, %d, %d) [device %d]", __FUNCTION__, pidP, typeP, onP, deviceM->GetId()); //debug("cSatipTuner::%s(%d, %d, %d) [device %d]", __FUNCTION__, pidP, typeP, onP, deviceIdM);
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
if (onP) { if (onP) {
pidsM.AddPid(pidP); pidsM.AddPid(pidP);
@ -492,8 +323,7 @@ bool cSatipTuner::UpdatePids(bool forceP)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
if (((forceP && pidsM.Size()) || (pidUpdateCacheM.TimedOut() && (addPidsM.Size() || delPidsM.Size()))) && if (((forceP && pidsM.Size()) || (pidUpdateCacheM.TimedOut() && (addPidsM.Size() || delPidsM.Size()))) &&
tunedM && handleM && !isempty(*streamAddrM) && (streamIdM > 0)) { tunedM && !isempty(*streamAddrM) && (streamIdM > 0)) {
CURLcode res = CURLE_OK;
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM); cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
bool usedummy = !!(currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkPlayPids)); bool usedummy = !!(currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkPlayPids));
if (forceP || usedummy) { if (forceP || usedummy) {
@ -517,19 +347,13 @@ bool cSatipTuner::UpdatePids(bool forceP)
uri = cString::sprintf("%s%d%s", *uri, delPidsM[i], (i == (delPidsM.Size() - 1)) ? "" : ","); uri = cString::sprintf("%s%d%s", *uri, delPidsM[i], (i == (delPidsM.Size() - 1)) ? "" : ",");
} }
} }
//debug("cSatipTuner::%s(): %s [device %d]", __FUNCTION__, *uri, deviceM->GetId()); if (rtspM->Play(*uri)) {
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY);
SATIP_CURL_EASY_PERFORM(handleM);
if (ValidateLatestResponse()) {
addPidsM.Clear(); addPidsM.Clear();
delPidsM.Clear(); delPidsM.Clear();
}
else
Disconnect();
return true; return true;
} }
Disconnect();
}
return false; return false;
} }
@ -537,21 +361,14 @@ bool cSatipTuner::UpdatePids(bool forceP)
bool cSatipTuner::KeepAlive(void) bool cSatipTuner::KeepAlive(void)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
if (tunedM && handleM && keepAliveM.TimedOut()) { if (tunedM && keepAliveM.TimedOut()) {
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
CURLcode res = CURLE_OK;
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM); cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
if (rtspM->Options(*uri)) {
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS);
SATIP_CURL_EASY_PERFORM(handleM);
if (ValidateLatestResponse())
keepAliveM.Set(timeoutM); keepAliveM.Set(timeoutM);
else
Disconnect();
return true; return true;
} }
Disconnect();
}
return false; return false;
} }
@ -559,55 +376,44 @@ bool cSatipTuner::KeepAlive(void)
bool cSatipTuner::ReadReceptionStatus(void) bool cSatipTuner::ReadReceptionStatus(void)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
if (tunedM && handleM && !pidsM.Size() && statusUpdateM.TimedOut() ) { if (tunedM && !pidsM.Size() && statusUpdateM.TimedOut() ) {
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
CURLcode res = CURLE_OK;
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM); cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
if (rtspM->Describe(*uri)) {
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEFUNCTION, cSatipTuner::DataCallback);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEDATA, this);
SATIP_CURL_EASY_PERFORM(handleM);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEFUNCTION, NULL);
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEDATA, NULL);
if (ValidateLatestResponse())
statusUpdateM.Set(eStatusUpdateTimeoutMs); statusUpdateM.Set(eStatusUpdateTimeoutMs);
else
Disconnect();
return true; return true;
} }
Disconnect();
}
return false; return false;
} }
int cSatipTuner::SignalStrength(void) int cSatipTuner::SignalStrength(void)
{ {
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); //debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return signalStrengthM; return signalStrengthM;
} }
int cSatipTuner::SignalQuality(void) int cSatipTuner::SignalQuality(void)
{ {
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); //debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return signalQualityM; return signalQualityM;
} }
bool cSatipTuner::HasLock(void) bool cSatipTuner::HasLock(void)
{ {
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); //debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return tunedM && hasLockM; return tunedM && hasLockM;
} }
cString cSatipTuner::GetSignalStatus(void) cString cSatipTuner::GetSignalStatus(void)
{ {
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); //debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return cString::sprintf("lock=%d strength=%d quality=%d", HasLock(), SignalStrength(), SignalQuality()); return cString::sprintf("lock=%d strength=%d quality=%d", HasLock(), SignalStrength(), SignalQuality());
} }
cString cSatipTuner::GetInformation(void) cString cSatipTuner::GetInformation(void)
{ {
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId()); //debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return tunedM ? cString::sprintf("rtsp://%s/?%s [stream=%d]", *streamAddrM, *streamParamM, streamIdM) : "connection failed"; return tunedM ? cString::sprintf("rtsp://%s/?%s [stream=%d]", *streamAddrM, *streamParamM, streamIdM) : "connection failed";
} }

29
tuner.h
View File

@ -8,17 +8,11 @@
#ifndef __SATIP_TUNER_H #ifndef __SATIP_TUNER_H
#define __SATIP_TUNER_H #define __SATIP_TUNER_H
#include <curl/curl.h>
#include <curl/easy.h>
#ifndef CURLOPT_RTSPHEADER
#error "libcurl is missing required RTSP support"
#endif
#include <vdr/thread.h> #include <vdr/thread.h>
#include <vdr/tools.h> #include <vdr/tools.h>
#include "deviceif.h" #include "deviceif.h"
#include "rtsp.h"
#include "server.h" #include "server.h"
#include "statistics.h" #include "statistics.h"
#include "socket.h" #include "socket.h"
@ -49,7 +43,7 @@ public:
} }
}; };
class cSatipTuner : public cThread, public cSatipTunerStatistics { class cSatipTuner : public cThread, public cSatipTunerStatistics, public cSatipTunerIf {
private: private:
enum { enum {
eDummyPid = 100, eDummyPid = 100,
@ -62,14 +56,12 @@ private:
eMinKeepAliveIntervalMs = 30000 // in milliseconds eMinKeepAliveIntervalMs = 30000 // in milliseconds
}; };
static size_t HeaderCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP);
static size_t DataCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP);
static int DebugCallback(CURL *handleP, curl_infotype typeP, char *dataP, size_t sizeP, void *userPtrP);
cCondWait sleepM; cCondWait sleepM;
cSatipDeviceIf* deviceM; cSatipDeviceIf* deviceM;
int deviceIdM;
unsigned char* packetBufferM; unsigned char* packetBufferM;
unsigned int packetBufferLenM; unsigned int packetBufferLenM;
cSatipRtsp *rtspM;
cSatipSocket *rtpSocketM; cSatipSocket *rtpSocketM;
cSatipSocket *rtcpSocketM; cSatipSocket *rtcpSocketM;
cString streamAddrM; cString streamAddrM;
@ -77,8 +69,6 @@ private:
cSatipServer *currentServerM; cSatipServer *currentServerM;
cSatipServer *nextServerM; cSatipServer *nextServerM;
cMutex mutexM; cMutex mutexM;
CURL *handleM;
struct curl_slist *headerListM;
cTimeMs keepAliveM; cTimeMs keepAliveM;
cTimeMs statusUpdateM; cTimeMs statusUpdateM;
cTimeMs signalInfoCacheM; cTimeMs signalInfoCacheM;
@ -97,10 +87,6 @@ private:
bool Connect(void); bool Connect(void);
bool Disconnect(void); bool Disconnect(void);
bool ValidateLatestResponse(void);
void ParseReceptionParameters(const char *paramP);
void SetStreamId(int streamIdP);
void SetSessionTimeout(const char *sessionP, int timeoutP = 0);
bool KeepAlive(void); bool KeepAlive(void);
bool ReadReceptionStatus(void); bool ReadReceptionStatus(void);
bool UpdateSignalInfoCache(void); bool UpdateSignalInfoCache(void);
@ -122,6 +108,13 @@ public:
bool HasLock(void); bool HasLock(void);
cString GetSignalStatus(void); cString GetSignalStatus(void);
cString GetInformation(void); cString GetInformation(void);
// for internal tuner interface
public:
virtual void ParseReceptionParameters(const char *paramP);
virtual void SetStreamId(int streamIdP);
virtual void SetSessionTimeout(const char *sessionP, int timeoutP);
virtual int GetId(void);
}; };
#endif // __SATIP_TUNER_H #endif // __SATIP_TUNER_H

25
tunerif.h Normal file
View File

@ -0,0 +1,25 @@
/*
* tunerif.h: SAT>IP plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#ifndef __SATIP_TUNERIF_H
#define __SATIP_TUNERIF_H
class cSatipTunerIf {
public:
cSatipTunerIf() {}
virtual ~cSatipTunerIf() {}
virtual void ParseReceptionParameters(const char *paramP) = 0;
virtual void SetStreamId(int streamIdP) = 0;
virtual void SetSessionTimeout(const char *sessionP, int timeoutP) = 0;
virtual int GetId(void) = 0;
private:
cSatipTunerIf(const cSatipTunerIf&);
cSatipTunerIf& operator=(const cSatipTunerIf&);
};
#endif // __SATIP_TUNERIF_H