2016-10-03 16:57:14 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 by Jonathan Naylor G4KLX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StopWatch.h"
|
|
|
|
#include "P25Parrot.h"
|
|
|
|
#include "Parrot.h"
|
|
|
|
#include "Network.h"
|
|
|
|
#include "Version.h"
|
|
|
|
#include "Timer.h"
|
2016-10-04 12:54:58 -04:00
|
|
|
#include "Log.h"
|
2016-10-03 16:57:14 -04:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2016-10-04 12:54:58 -04:00
|
|
|
#include <cstring>
|
2016-10-03 16:57:14 -04:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc == 1) {
|
2016-10-04 12:54:58 -04:00
|
|
|
::fprintf(stderr, "Usage: P25Parrot [-d|--debug] <port>\n");
|
2016-10-03 16:57:14 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-04 12:54:58 -04:00
|
|
|
unsigned int n = 1U;
|
|
|
|
|
|
|
|
bool debug = false;
|
|
|
|
if (::strcmp(argv[1], "-d") == 0 || ::strcmp(argv[1], "--debug") == 0) {
|
|
|
|
debug = true;
|
|
|
|
n = 2U;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int port = ::atoi(argv[n]);
|
2016-10-03 16:57:14 -04:00
|
|
|
if (port == 0U) {
|
2016-10-04 12:54:58 -04:00
|
|
|
::fprintf(stderr, "P25Parrot: invalid port number - %s\n", argv[n]);
|
2016-10-03 16:57:14 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-04 12:54:58 -04:00
|
|
|
CP25Parrot parrot(port, debug);
|
2016-10-03 16:57:14 -04:00
|
|
|
parrot.run();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 12:54:58 -04:00
|
|
|
CP25Parrot::CP25Parrot(unsigned int port, bool debug) :
|
|
|
|
m_port(port),
|
|
|
|
m_debug(debug)
|
2016-10-03 16:57:14 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CP25Parrot::~CP25Parrot()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CP25Parrot::run()
|
|
|
|
{
|
2016-10-04 12:54:58 -04:00
|
|
|
bool ret = ::LogInitialise(".", "P25Parrot", m_debug ? 1U : 2U, m_debug ? 1U : 2U);
|
|
|
|
if (!ret) {
|
|
|
|
::fprintf(stderr, "P25Parrot: unable to open the log file\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-03 16:57:14 -04:00
|
|
|
CParrot parrot(180U);
|
2016-10-04 12:54:58 -04:00
|
|
|
CNetwork network(m_port);
|
2016-10-03 16:57:14 -04:00
|
|
|
|
2016-10-04 12:54:58 -04:00
|
|
|
ret = network.open();
|
|
|
|
if (!ret) {
|
|
|
|
::LogFinalise();
|
2016-10-03 16:57:14 -04:00
|
|
|
return;
|
2016-10-04 12:54:58 -04:00
|
|
|
}
|
2016-10-03 16:57:14 -04:00
|
|
|
|
|
|
|
CStopWatch stopWatch;
|
|
|
|
stopWatch.start();
|
|
|
|
|
|
|
|
CTimer watchdogTimer(1000U, 0U, 1500U);
|
|
|
|
CTimer turnaroundTimer(1000U, 2U);
|
|
|
|
|
|
|
|
CStopWatch playoutTimer;
|
|
|
|
unsigned int count = 0U;
|
|
|
|
bool playing = false;
|
|
|
|
|
2016-10-04 12:54:58 -04:00
|
|
|
LogInfo("Starting P25Parrot-%s", VERSION);
|
2016-10-03 16:57:14 -04:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
unsigned char buffer[200U];
|
|
|
|
|
|
|
|
unsigned int len = network.read(buffer);
|
|
|
|
if (len > 0U) {
|
|
|
|
parrot.write(buffer, len);
|
|
|
|
watchdogTimer.start();
|
|
|
|
|
|
|
|
if ((buffer[0U] == 0x6AU && buffer[15U] == 0x00U) || (buffer[0U] == 0x73U && buffer[15U] == 0x00U)) {
|
2016-10-04 12:54:58 -04:00
|
|
|
LogDebug("Received end of transmission");
|
2016-10-03 16:57:14 -04:00
|
|
|
turnaroundTimer.start();
|
|
|
|
watchdogTimer.stop();
|
|
|
|
parrot.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (turnaroundTimer.isRunning() && turnaroundTimer.hasExpired()) {
|
|
|
|
if (!playing) {
|
|
|
|
playoutTimer.start();
|
|
|
|
playing = true;
|
|
|
|
count = 0U;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A frame every 20ms
|
|
|
|
unsigned int wanted = playoutTimer.elapsed() / 20U;
|
|
|
|
while (count < wanted) {
|
|
|
|
len = parrot.read(buffer);
|
|
|
|
if (len > 0U) {
|
|
|
|
network.write(buffer, len);
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
parrot.clear();
|
|
|
|
network.end();
|
|
|
|
turnaroundTimer.stop();
|
|
|
|
playing = false;
|
|
|
|
count = wanted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ms = stopWatch.elapsed();
|
|
|
|
stopWatch.start();
|
|
|
|
|
|
|
|
network.clock(ms);
|
|
|
|
watchdogTimer.clock(ms);
|
|
|
|
turnaroundTimer.clock(ms);
|
|
|
|
|
|
|
|
if (watchdogTimer.isRunning() && watchdogTimer.hasExpired()) {
|
2016-10-04 12:54:58 -04:00
|
|
|
LogDebug("Network watchdog has expired");
|
2016-10-03 16:57:14 -04:00
|
|
|
turnaroundTimer.start();
|
|
|
|
watchdogTimer.stop();
|
|
|
|
parrot.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ms < 5U) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
::Sleep(5UL); // 5ms
|
|
|
|
#else
|
|
|
|
::usleep(5000); // 5ms
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
network.close();
|
2016-10-04 12:54:58 -04:00
|
|
|
|
|
|
|
::LogFinalise();
|
2016-10-03 16:57:14 -04:00
|
|
|
}
|