From 9e5b98875f4430473ea3ffebb804cea69008d8fb Mon Sep 17 00:00:00 2001
From: Stefan Becker <chemobejk@gmail.com>
Date: Fri, 13 Dec 2013 22:12:04 +0200
Subject: [PATCH] Fixes #10667. Bind to multicast address.

If multiple servers multicast content on the same port we can't receive
it on multiple recorders if bind to QHostAddress::Any. If we bind to the
multicast address we don't have this problem. Problem discovered and
patched by Niels Ole Kirkeby.

Note: This also adds error handling for when a bind fails.
---
 .../libs/libmythtv/recorders/iptvstreamhandler.cpp | 28 ++++++++++++++--------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/mythtv/libs/libmythtv/recorders/iptvstreamhandler.cpp b/mythtv/libs/libmythtv/recorders/iptvstreamhandler.cpp
index 432d248..9fb74eb 100644
--- a/mythtv/libs/libmythtv/recorders/iptvstreamhandler.cpp
+++ b/mythtv/libs/libmythtv/recorders/iptvstreamhandler.cpp
@@ -167,6 +167,7 @@ void IPTVStreamHandler::run(void)
             "", 0);
     }
 
+    bool error = false;
     for (uint i = 0; i < IPTV_SOCKET_COUNT; i++)
     {
         QUrl url = tuning.GetURL(i);
@@ -210,10 +211,14 @@ void IPTVStreamHandler::run(void)
         m_sockets[i]->setSocketDescriptor(
             fd, QAbstractSocket::UnconnectedState, QIODevice::ReadOnly);
 
-        m_sockets[i]->bind(QHostAddress::Any, url.port());
-
         QHostAddress dest_addr(tuning.GetURL(i).host());
 
+        if (!m_sockets[i]->bind(dest_addr, url.port()))
+        {
+            LOG(VB_GENERAL, LOG_ERR, LOC + "Binding to port failed.");
+            error = true;
+        }
+
         if (dest_addr != QHostAddress::Any)
         {
             //m_sockets[i]->joinMulticastGroup(dest_addr); // needs Qt 4.8
@@ -235,15 +240,18 @@ void IPTVStreamHandler::run(void)
         if (!url.userInfo().isEmpty())
             m_sender[i] = QHostAddress(url.userInfo());
     }
-    if (m_use_rtp_streaming)
-        m_buffer = new RTPPacketBuffer(tuning.GetBitrate(0));
-    else
-        m_buffer = new UDPPacketBuffer(tuning.GetBitrate(0));
-    m_write_helper = new IPTVStreamHandlerWriteHelper(this);
-    m_write_helper->Start();
 
-    bool error = false;
-    if (rtsp)
+    if (!error)
+    {
+        if (m_use_rtp_streaming)
+            m_buffer = new RTPPacketBuffer(tuning.GetBitrate(0));
+        else
+            m_buffer = new UDPPacketBuffer(tuning.GetBitrate(0));
+        m_write_helper = new IPTVStreamHandlerWriteHelper(this);
+        m_write_helper->Start();
+    }
+
+    if (!error && rtsp)
     {
         // Start Streaming
         if (!rtsp->Setup(m_sockets[0]->localPort(),
-- 
1.8.4.2

