Skip to content

Commit

Permalink
Always re-use the same computer ID for AirPlay. Share ID between RAOP…
Browse files Browse the repository at this point in the history
… and AirPlay

Random ID is generated the first time, the following session will always re-use the same ID.
Having AirPlay and RAOP share the same hardware ID makes mythfrontend behave more like an AppleTV: only one device is showing in the list. Either one for audio or one for audio+video, not both at the same time.
  • Loading branch information
jyavenard committed May 2, 2012
1 parent 374e2cd commit f125f53
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
41 changes: 13 additions & 28 deletions mythtv/libs/libmythtv/mythairplayserver.cpp
Expand Up @@ -3,8 +3,6 @@
// race on startup?
// http date format and locale
// GET scrub on iOS 5
// binary plist for non mac
// same mac address for bonjour service and server-info

#include <QTcpSocket>
#include <QNetworkInterface>
Expand All @@ -22,6 +20,7 @@

#include "bonjourregister.h"
#include "mythairplayserver.h"
#include "mythraopdevice.h"

MythAirplayServer* MythAirplayServer::gMythAirplayServer = NULL;
MThread* MythAirplayServer::gMythAirplayServerThread = NULL;
Expand Down Expand Up @@ -353,8 +352,8 @@ void MythAirplayServer::Start(void)
name.append(gCoreContext->GetHostName());
QByteArray type = "_airplay._tcp";
QByteArray txt;
txt.append(26); txt.append("deviceid=00:00:00:00:00:00");
txt.append(13); txt.append("features=0x77");
txt.append(26); txt.append("deviceid="); txt.append(GetMacAddress());
txt.append(14); txt.append("features=0x219");
txt.append(16); txt.append("model=AppleTV2,1");
txt.append(14); txt.append("srcvers=101.28");

Expand Down Expand Up @@ -516,7 +515,7 @@ void MythAirplayServer::HandleResponse(APHTTPRequest *req,
{
content_type = "text/x-apple-plist+xml\r\n";
body = SERVER_INFO;
body.replace("%1", GetMacAddress(socket));
body.replace("%1", GetMacAddress());
LOG(VB_GENERAL, LOG_INFO, body);
}
else if (req->GetURI() == "/scrub")
Expand Down Expand Up @@ -785,34 +784,20 @@ void MythAirplayServer::GetPlayerStatus(bool &playing, float &speed,
duration = state["totalseconds"].toDouble();
}

QString MythAirplayServer::GetMacAddress(QTcpSocket *socket)
QString MythAirplayServer::GetMacAddress()
{
if (!socket)
return "";
QString id = MythRAOPDevice::HardwareId();

QString res("");
QString fallback("");

foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
QString res;
for (int i = 1; i <= id.size(); i++)
{
if (!(interface.flags() & QNetworkInterface::IsLoopBack))
res.append(id[i-1]);
if (i % 2 == 0 && i != id.size())
{
fallback = interface.hardwareAddress();
QList<QNetworkAddressEntry> entries = interface.addressEntries();
foreach (QNetworkAddressEntry entry, entries)
if (entry.ip() == socket->localAddress())
res = fallback;
res.append(':');
}
}

if (res.isEmpty())
{
LOG(VB_GENERAL, LOG_WARNING, LOC + "Using fallback MAC address.");
res = fallback;
}

if (res.isEmpty())
LOG(VB_GENERAL, LOG_ERR, LOC + "Didn't find MAC address.");

QByteArray ba = res.toAscii();
const char *t = ba.constData();
return res;
}
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/mythairplayserver.h
Expand Up @@ -65,7 +65,7 @@ class MTV_PUBLIC MythAirplayServer : public ServerPool
QString eventToString(AirplayEvent event);
void GetPlayerStatus(bool &playing, float &speed, double &position,
double &duration);
QString GetMacAddress(QTcpSocket *socket);
QString GetMacAddress();
void SendReverseEvent(QByteArray &session, AirplayEvent event);

// Globals
Expand Down
21 changes: 19 additions & 2 deletions mythtv/libs/libmythtv/mythraopdevice.cpp
Expand Up @@ -55,10 +55,28 @@ bool MythRAOPDevice::Create(void)
gMythRAOPDeviceThread->start(QThread::LowestPriority);
}


LOG(VB_GENERAL, LOG_INFO, LOC + "Created RAOP device objects.");
return true;
}

QString MythRAOPDevice::HardwareId()
{
QString key = "AirPlayId";
QString id = gCoreContext->GetSetting(key);
int size = id.size();
if (size == 12)
return id;

QByteArray ba;
for (int i = 0; i < RAOP_HARDWARE_ID_SIZE; i++)
ba.append((random() % 80) + 33);
id = ba.toHex();

gCoreContext->SaveSetting(key, id);
return id;
}

void MythRAOPDevice::Cleanup(void)
{
LOG(VB_GENERAL, LOG_INFO, LOC + "Cleaning up.");
Expand All @@ -83,8 +101,7 @@ MythRAOPDevice::MythRAOPDevice()
: ServerPool(), m_name(QString("MythTV")), m_bonjour(NULL), m_valid(false),
m_lock(new QMutex(QMutex::Recursive)), m_setupPort(5000)
{
for (int i = 0; i < RAOP_HARDWARE_ID_SIZE; i++)
m_hardwareId.append((random() % 80) + 33);
m_hardwareId = QByteArray::fromHex(HardwareId().toAscii());
}

MythRAOPDevice::~MythRAOPDevice()
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythtv/mythraopdevice.h
Expand Up @@ -24,6 +24,7 @@ class MTV_PUBLIC MythRAOPDevice : public ServerPool

MythRAOPDevice();
bool NextInAudioQueue(MythRAOPConnection* conn);
static QString HardwareId();

private slots:
void Start();
Expand Down

0 comments on commit f125f53

Please sign in to comment.