From 9131cd0e4853bcd9cffc02a37fd07e06428e06e5 Mon Sep 17 00:00:00 2001 From: Rolf Ahrenberg Date: Wed, 12 Sep 2007 21:55:57 +0000 Subject: [PATCH] Some minor fixes. --- streamer.c | 47 +++++++++++++++++++++++++++-------------------- streamer.h | 4 ++-- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/streamer.c b/streamer.c index 952b4d0..98fd377 100644 --- a/streamer.c +++ b/streamer.c @@ -3,7 +3,7 @@ * * 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 @@ -19,10 +19,10 @@ #include "common.h" #include "streamer.h" -cIptvStreamer::cIptvStreamer(cRingBufferLinear* BufferPtr, cMutex* Mutex) +cIptvStreamer::cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex) : cThread("IPTV streamer"), dataPort(1234), - pRingBuffer(BufferPtr), + pRingBuffer(Buffer), bufferSize(TS_SIZE * 7), mutex(Mutex), socketActive(false), @@ -58,7 +58,8 @@ void cIptvStreamer::Action() debug("cIptvStreamer::Action(): Entering\n"); while (Running()) { 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(); int p = pRingBuffer->Put(pReceiveBuffer, bufferSize); if (p != length && Running()) { @@ -87,6 +88,7 @@ bool cIptvStreamer::CheckAndCreateSocket(const int port) if (socketDesc < 0) { char tmp[64]; error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp))); + return false; } int yes = 1; @@ -96,6 +98,8 @@ bool cIptvStreamer::CheckAndCreateSocket(const int port) sizeof(yes)) < 0) { char tmp[64]; error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); + close(socketDesc); + return false; } memset(&sa, '\0', sizeof(sa)); @@ -107,6 +111,7 @@ bool cIptvStreamer::CheckAndCreateSocket(const int port) if (err < 0) { char tmp[64]; error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp))); + close(socketDesc); return false; } @@ -138,43 +143,49 @@ bool cIptvStreamer::Activate() } if (mcastActive) { - debug("cIptvStreamer::Activate(): Already active!\n"); + debug("cIptvStreamer::Activate(): Already active\n"); return true; } // Ensure that socket is valid CheckAndCreateSocket(dataPort); - // Start thread - if (!Running()) - Start(); - // Join a new multicast group mreq.imr_multiaddr.s_addr = inet_addr(stream); 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) { char tmp[64]; error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); return false; } + // Start thread + if (!Running()) + Start(); + mcastActive = true; return true; } 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; + debug("cIptvStreamer::Deactivate(): Deactivating\n"); mreq.imr_multiaddr.s_addr = inet_addr(stream); 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) { char tmp[64]; error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); @@ -185,18 +196,14 @@ bool cIptvStreamer::Deactivate() mcastActive = false; } - // Stop thread - if (Running()) - Cancel(3); - return true; } 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 Deactivate(); diff --git a/streamer.h b/streamer.h index baef2f0..75455fc 100644 --- a/streamer.h +++ b/streamer.h @@ -3,7 +3,7 @@ * * 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 @@ -32,7 +32,7 @@ private: public: cIptvStreamer(); - cIptvStreamer(cRingBufferLinear* BufferPtr, cMutex* Mutex); + cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex); virtual ~cIptvStreamer(); virtual void Action(); bool SetStream(const char* address, const int port, const char* protocol);