Some minor fixes.

This commit is contained in:
Rolf Ahrenberg 2007-09-12 21:55:57 +00:00
parent 6511d87fe6
commit 9131cd0e48
2 changed files with 29 additions and 22 deletions

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * See the README file for copyright information and how to reach the author.
* *
* $Id: streamer.c,v 1.7 2007/09/12 21:14:51 rahrenbe Exp $ * $Id: streamer.c,v 1.8 2007/09/12 21:55:57 rahrenbe Exp $
*/ */
#include <sys/types.h> #include <sys/types.h>
@ -19,10 +19,10 @@
#include "common.h" #include "common.h"
#include "streamer.h" #include "streamer.h"
cIptvStreamer::cIptvStreamer(cRingBufferLinear* BufferPtr, cMutex* Mutex) cIptvStreamer::cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex)
: cThread("IPTV streamer"), : cThread("IPTV streamer"),
dataPort(1234), dataPort(1234),
pRingBuffer(BufferPtr), pRingBuffer(Buffer),
bufferSize(TS_SIZE * 7), bufferSize(TS_SIZE * 7),
mutex(Mutex), mutex(Mutex),
socketActive(false), socketActive(false),
@ -58,7 +58,8 @@ void cIptvStreamer::Action()
debug("cIptvStreamer::Action(): Entering\n"); debug("cIptvStreamer::Action(): Entering\n");
while (Running()) { while (Running()) {
socklen_t addrlen = sizeof(sa); socklen_t addrlen = sizeof(sa);
int length = recvfrom(socketDesc, pReceiveBuffer, bufferSize, 0, (struct sockaddr *)&sa, &addrlen); int length = recvfrom(socketDesc, pReceiveBuffer, bufferSize, 0,
(struct sockaddr *)&sa, &addrlen);
mutex->Lock(); mutex->Lock();
int p = pRingBuffer->Put(pReceiveBuffer, bufferSize); int p = pRingBuffer->Put(pReceiveBuffer, bufferSize);
if (p != length && Running()) { if (p != length && Running()) {
@ -87,6 +88,7 @@ bool cIptvStreamer::CheckAndCreateSocket(const int port)
if (socketDesc < 0) { if (socketDesc < 0) {
char tmp[64]; char tmp[64];
error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
} }
int yes = 1; int yes = 1;
@ -96,6 +98,8 @@ bool cIptvStreamer::CheckAndCreateSocket(const int port)
sizeof(yes)) < 0) { sizeof(yes)) < 0) {
char tmp[64]; char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
close(socketDesc);
return false;
} }
memset(&sa, '\0', sizeof(sa)); memset(&sa, '\0', sizeof(sa));
@ -107,6 +111,7 @@ bool cIptvStreamer::CheckAndCreateSocket(const int port)
if (err < 0) { if (err < 0) {
char tmp[64]; char tmp[64];
error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp)));
close(socketDesc);
return false; return false;
} }
@ -138,43 +143,49 @@ bool cIptvStreamer::Activate()
} }
if (mcastActive) { if (mcastActive) {
debug("cIptvStreamer::Activate(): Already active!\n"); debug("cIptvStreamer::Activate(): Already active\n");
return true; return true;
} }
// Ensure that socket is valid // Ensure that socket is valid
CheckAndCreateSocket(dataPort); CheckAndCreateSocket(dataPort);
// Start thread
if (!Running())
Start();
// Join a new multicast group // Join a new multicast group
mreq.imr_multiaddr.s_addr = inet_addr(stream); mreq.imr_multiaddr.s_addr = inet_addr(stream);
mreq.imr_interface.s_addr = htonl(INADDR_ANY); mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq));
if (err < 0) { if (err < 0) {
char tmp[64]; char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false; return false;
} }
// Start thread
if (!Running())
Start();
mcastActive = true; mcastActive = true;
return true; return true;
} }
bool cIptvStreamer::Deactivate() bool cIptvStreamer::Deactivate()
{ {
debug("cIptvStreamer::Deactivate()\n");
if (stream && mcastActive) {
debug("cIptvStreamer::Deactivate(): stream = %s\n", stream); debug("cIptvStreamer::Deactivate(): stream = %s\n", stream);
// Stop thread
if (Running())
Cancel(3);
if (stream && mcastActive) {
struct ip_mreq mreq; struct ip_mreq mreq;
debug("cIptvStreamer::Deactivate(): Deactivating\n");
mreq.imr_multiaddr.s_addr = inet_addr(stream); mreq.imr_multiaddr.s_addr = inet_addr(stream);
mreq.imr_interface.s_addr = htonl(INADDR_ANY); mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq,
sizeof(mreq));
if (err < 0) { if (err < 0) {
char tmp[64]; char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
@ -185,18 +196,14 @@ bool cIptvStreamer::Deactivate()
mcastActive = false; mcastActive = false;
} }
// Stop thread
if (Running())
Cancel(3);
return true; return true;
} }
bool cIptvStreamer::SetStream(const char* address, const int port, const char* protocol) bool cIptvStreamer::SetStream(const char* address, const int port, const char* protocol)
{ {
debug("cIptvStreamer::SetChannel(): %s://%s:%d\n", protocol, address, port); debug("cIptvStreamer::SetStream(): %s://%s:%d\n", protocol, address, port);
// De-activate the reception if it is running currently. Otherwise the // Deactivate the reception if it is running currently. Otherwise the
// reception stream is overwritten and cannot be un-set after this // reception stream is overwritten and cannot be un-set after this
Deactivate(); Deactivate();

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * See the README file for copyright information and how to reach the author.
* *
* $Id: streamer.h,v 1.3 2007/09/12 21:14:51 rahrenbe Exp $ * $Id: streamer.h,v 1.4 2007/09/12 21:55:57 rahrenbe Exp $
*/ */
#ifndef __IPTV_STREAMER_H #ifndef __IPTV_STREAMER_H
@ -32,7 +32,7 @@ private:
public: public:
cIptvStreamer(); cIptvStreamer();
cIptvStreamer(cRingBufferLinear* BufferPtr, cMutex* Mutex); cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex);
virtual ~cIptvStreamer(); virtual ~cIptvStreamer();
virtual void Action(); virtual void Action();
bool SetStream(const char* address, const int port, const char* protocol); bool SetStream(const char* address, const int port, const char* protocol);