From bd373dc09ce0ee8a511c16c843ff4f3bf10bd6f2 Mon Sep 17 00:00:00 2001 From: Stijn Van der Borght Date: Mon, 6 Apr 2020 23:51:32 +0100 Subject: [PATCH] code review changes: use NOMINMAX hack, audio backend android only, move windows time defs to time_defs.hpp --- CMakeLists.txt | 7 ++--- client/snapclient.cpp | 3 +- client/stream.cpp | 8 +++-- common/message/message.hpp | 29 ----------------- common/time_defs.hpp | 64 ++++++++++++++++++-------------------- 5 files changed, 40 insertions(+), 71 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 28f3ae4e..1f34e2e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -207,12 +207,9 @@ if(WIN32) find_package(FLAC REQUIRED) find_package(Ogg REQUIRED) find_package(Vorbis REQUIRED) - find_package(Opus) - if(OPUS_FOUND) - add_definitions("-DHAS_OPUS") - endif() + find_package(Opus REQUIRED) add_definitions(-DNTDDI_VERSION=0x06020000 -D_WIN32_WINNT=0x0602 -DWINVER=0x0602 -DWINDOWS -DWIN32_LEAN_AND_MEAN -DUNICODE -D_UNICODE -D_CRT_SECURE_NO_WARNINGS ) - add_definitions(-DHAS_OGG -DHAS_VORBIS -DHAS_FLAC -DHAS_VORBIS_ENC -DHAS_WASAPI) + add_definitions(-DHAS_OGG -DHAS_VORBIS -DHAS_FLAC -DHAS_VORBIS_ENC -DHAS_WASAPI -DHAS_OPUS) endif() add_subdirectory(common) diff --git a/client/snapclient.cpp b/client/snapclient.cpp index dc5c99aa..2f500c6b 100644 --- a/client/snapclient.cpp +++ b/client/snapclient.cpp @@ -111,7 +111,9 @@ int main(int argc, char** argv) /*auto latencyValue =*/op.add>("", "latency", "latency of the PCM device", 0, &settings.player.latency); /*auto instanceValue =*/op.add>("i", "instance", "instance id", 1, &settings.instance); /*auto hostIdValue =*/op.add>("", "hostID", "unique host id", "", &settings.host_id); +#ifdef ANDROID op.add>("", "player", "audio backend", "", &settings.player.player_name); +#endif #ifdef HAS_SOXR auto sample_format = op.add>("", "sampleformat", "resample audio stream to ::", ""); #endif @@ -241,7 +243,6 @@ int main(int argc, char** argv) #endif #ifdef HAS_WASAPI - settings.player.player_name = "wasapi"; if (wasapi_mode->is_set()) { settings.player.wasapi_mode = (strcmp(wasapi_mode->value().c_str(), "exclusive") == 0) ? diff --git a/client/stream.cpp b/client/stream.cpp index 7973bed7..eefd280f 100644 --- a/client/stream.cpp +++ b/client/stream.cpp @@ -16,6 +16,8 @@ along with this program. If not, see . ***/ +#define NOMINMAX + #include "stream.hpp" #include "common/aixlog.hpp" #include "time_provider.hpp" @@ -390,7 +392,7 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT // and can play 30ms of the stream uint32_t silent_frames = static_cast(-chunk_->format.nsRate() * std::chrono::duration_cast(age).count()); bool result = (silent_frames <= frames); - silent_frames = (std::min)(silent_frames, frames); + silent_frames = std::min(silent_frames, frames); LOG(DEBUG, LOG_TAG) << "Silent frames: " << silent_frames << ", frames: " << frames << ", age: " << std::chrono::duration_cast(age).count() / 1000. << "\n"; getSilentPlayerChunk(outputBuffer, silent_frames); @@ -453,7 +455,7 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT if ((cs::usec(shortMedian_) > kCorrectionBegin) && (cs::usec(miniMedian) > cs::usec(50)) && (cs::usec(age) > cs::usec(50))) { double rate = (shortMedian_ / 100) * 0.00005; - rate = 1.0 - (std::min)(rate, 0.0005); + rate = 1.0 - std::min(rate, 0.0005); // LOG(INFO, LOG_TAG) << "Rate: " << rate << "\n"; // we are late (age > 0), this means we are not playing fast enough // => the real sample rate seems to be lower, we have to drop some frames @@ -462,7 +464,7 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT else if ((cs::usec(shortMedian_) < -kCorrectionBegin) && (cs::usec(miniMedian) < -cs::usec(50)) && (cs::usec(age) < -cs::usec(50))) { double rate = (-shortMedian_ / 100) * 0.00005; - rate = 1.0 + (std::min)(rate, 0.0005); + rate = 1.0 + std::min(rate, 0.0005); // LOG(INFO, LOG_TAG) << "Rate: " << rate << "\n"; // we are early (age > 0), this means we are playing too fast // => the real sample rate seems to be higher, we have to insert some frames diff --git a/common/message/message.hpp b/common/message/message.hpp index edd40260..0d117069 100644 --- a/common/message/message.hpp +++ b/common/message/message.hpp @@ -30,31 +30,6 @@ #endif #include -#ifdef WINDOWS // Implementation from http://stackoverflow.com/a/26085827/2510022 -#include -#include -#include - -inline static int gettimeofday(struct timeval* tp, struct timezone* tzp) -{ - // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's - static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL); - - SYSTEMTIME system_time; - FILETIME file_time; - uint64_t time; - - GetSystemTime(&system_time); - SystemTimeToFileTime(&system_time, &file_time); - time = ((uint64_t)file_time.dwLowDateTime); - time += ((uint64_t)file_time.dwHighDateTime) << 32; - - tp->tv_sec = (long)((time - EPOCH) / 10000000L); - tp->tv_usec = (long)(system_time.wMilliseconds * 1000); - return 0; -} -#endif - /* template > class vectorwrapbuf : public std::basic_streambuf @@ -96,11 +71,7 @@ struct tv tv() { timeval t; -#ifdef WINDOWS - gettimeofday(&t, NULL); -#else chronos::steadytimeofday(&t); -#endif sec = t.tv_sec; usec = t.tv_usec; } diff --git a/common/time_defs.hpp b/common/time_defs.hpp index 8de958b8..ea47ce5a 100644 --- a/common/time_defs.hpp +++ b/common/time_defs.hpp @@ -29,7 +29,8 @@ #ifndef WINDOWS #include #else // from the GNU C library implementation of sys/time.h - +#include +#include #include #define timersub(a, b, result) \ @@ -45,37 +46,6 @@ } while (0) #define CLOCK_MONOTONIC 42 // discarded on windows plaforms - -// from http://stackoverflow.com/a/38212960/2510022 -#define BILLION (1E9) - -static BOOL g_first_time = 1; -static LARGE_INTEGER g_counts_per_sec; - -inline static int clock_gettime(int dummy, struct timespec* ct) -{ - LARGE_INTEGER count; - - if (g_first_time) - { - g_first_time = 0; - - if (0 == QueryPerformanceFrequency(&g_counts_per_sec)) - { - g_counts_per_sec.QuadPart = 0; - } - } - - if ((NULL == ct) || (g_counts_per_sec.QuadPart <= 0) || (0 == QueryPerformanceCounter(&count))) - { - return -1; - } - - ct->tv_sec = count.QuadPart / g_counts_per_sec.QuadPart; - ct->tv_nsec = ((count.QuadPart % g_counts_per_sec.QuadPart) * BILLION) / g_counts_per_sec.QuadPart; - - return 0; -} #endif namespace chronos @@ -101,9 +71,35 @@ inline static void timeofday(struct timeval* tv) tv->tv_usec = microsecs.count() % 1000000; } +#ifdef WINDOWS +// Implementation from http://stackoverflow.com/a/26085827/2510022 +inline static int gettimeofday(struct timeval* tp, struct timezone* tzp) +{ + // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's + static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL); + + SYSTEMTIME system_time; + FILETIME file_time; + uint64_t time; + + GetSystemTime(&system_time); + SystemTimeToFileTime(&system_time, &file_time); + time = ((uint64_t)file_time.dwLowDateTime); + time += ((uint64_t)file_time.dwHighDateTime) << 32; + + tp->tv_sec = (long)((time - EPOCH) / 10000000L); + tp->tv_usec = (long)(system_time.wMilliseconds * 1000); + return 0; +} +#endif + inline static void steadytimeofday(struct timeval* tv) { +#ifndef WINDOWS timeofday(tv); +#else + gettimeofday(tv, NULL); +#endif } inline static void systemtimeofday(struct timeval* tv) @@ -142,13 +138,15 @@ inline static void addUs(timeval& tv, int us) inline static long getTickCount() { -#ifdef MACOS +#if defined (MACOS) clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); return mts.tv_sec * 1000 + mts.tv_nsec / 1000000; +#elif defined(WINDOWS) + return getTickCount(); #else struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now);