YSFClients/YSFParrot/YSFParrot.cpp

137 lines
2.8 KiB
C++
Raw Normal View History

2016-05-19 18:36:54 +02:00
/*
2018-01-29 23:07:08 +01:00
* Copyright (C) 2016,2018 by Jonathan Naylor G4KLX
2016-05-19 18:36:54 +02:00
*
* 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 "YSFParrot.h"
#include "Parrot.h"
#include "Network.h"
#include "Version.h"
2016-10-18 08:55:01 +02:00
#include "Thread.h"
2016-06-13 23:53:25 +02:00
#include "Timer.h"
2016-05-19 18:36:54 +02:00
#include <cstdio>
#include <cstdlib>
#include <cstring>
2016-05-19 18:36:54 +02:00
int main(int argc, char** argv)
{
if (argc == 1) {
2018-01-29 23:07:08 +01:00
::fprintf(stderr, "Usage: YSFParrot [-d|--debug] <port>\n");
2016-05-19 18:36:54 +02:00
return 1;
}
2018-01-30 08:09:14 +01:00
unsigned int port = ::atoi(argv[1U]);
2016-05-19 18:36:54 +02:00
if (port == 0U) {
2018-01-30 08:09:14 +01:00
::fprintf(stderr, "YSFParrot: invalid port number - %s\n", argv[1U]);
2016-05-19 18:36:54 +02:00
return 1;
}
2018-01-30 08:09:14 +01:00
CYSFParrot parrot(port);
2016-05-19 18:36:54 +02:00
parrot.run();
return 0;
}
2018-01-30 08:09:14 +01:00
CYSFParrot::CYSFParrot(unsigned int port) :
m_port(port)
2016-05-19 18:36:54 +02:00
{
}
CYSFParrot::~CYSFParrot()
{
}
void CYSFParrot::run()
{
CParrot parrot(180U);
CNetwork network(m_port);
2016-05-19 18:36:54 +02:00
2018-01-29 23:07:08 +01:00
bool ret = network.open();
if (!ret)
2016-05-19 18:36:54 +02:00
return;
CStopWatch stopWatch;
stopWatch.start();
CTimer watchdogTimer(1000U, 0U, 1500U);
CTimer turnaroundTimer(1000U, 2U);
CStopWatch playoutTimer;
unsigned int count = 0U;
bool playing = false;
2018-01-29 23:07:08 +01:00
::fprintf(stdout, "Starting YSFParrot-%s\n", VERSION);
2016-05-19 18:36:54 +02:00
for (;;) {
unsigned char buffer[200U];
unsigned int len = network.read(buffer);
if (len > 0U) {
parrot.write(buffer);
watchdogTimer.start();
2016-06-09 18:35:52 +02:00
if ((buffer[34U] & 0x01U) == 0x01U) {
2016-05-19 18:36:54 +02:00
turnaroundTimer.start();
watchdogTimer.stop();
parrot.end();
}
}
if (turnaroundTimer.isRunning() && turnaroundTimer.hasExpired()) {
if (!playing) {
playoutTimer.start();
playing = true;
count = 0U;
}
// A frame every 100ms
unsigned int wanted = playoutTimer.elapsed() / 100U;
while (count < wanted) {
len = parrot.read(buffer);
if (len > 0U) {
network.write(buffer);
count++;
} else {
2016-06-13 23:53:25 +02:00
parrot.clear();
network.end();
2016-05-19 18:36:54 +02:00
turnaroundTimer.stop();
playing = false;
count = wanted;
}
}
}
unsigned int ms = stopWatch.elapsed();
stopWatch.start();
watchdogTimer.clock(ms);
turnaroundTimer.clock(ms);
if (watchdogTimer.isRunning() && watchdogTimer.hasExpired()) {
turnaroundTimer.start();
watchdogTimer.stop();
parrot.end();
}
2016-10-18 08:55:01 +02:00
if (ms < 5U)
CThread::sleep(5U);
2016-05-19 18:36:54 +02:00
}
network.close();
}