diff --git a/server/streamreader/librespot_stream.cpp b/server/streamreader/librespot_stream.cpp index 89238bca..557dd061 100644 --- a/server/streamreader/librespot_stream.cpp +++ b/server/streamreader/librespot_stream.cpp @@ -1,6 +1,6 @@ /*** This file is part of snapcast - Copyright (C) 2014-2021 Johannes Pohl + Copyright (C) 2014-2022 Johannes Pohl 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 @@ -26,8 +26,6 @@ #include "common/utils/file_utils.hpp" #include "common/utils/string_utils.hpp" -// standard headers -#include using namespace std; @@ -178,16 +176,15 @@ void LibrespotStream::onStderrMsg(const std::string& line) // non patched: // [2021-06-04T07:20:47Z INFO librespot_playback::player] (310573 ms) loaded // info!("Track \"{}\" loaded", track.name); - // std::cerr << line << "\n"; - smatch m; - static regex re_patched("metadata:(.*)"); - static regex re_track_loaded(R"( <(.*)> \((.*) ms\) loaded)"); - // Parse the patched version - if (regex_search(line, m, re_patched)) + size_t title_pos = 0; + size_t ms_pos = 0; + size_t n = 0; + if (((n = line.find("metadata:")) != std::string::npos)) { + std::string metadata = line.substr(n + 9); // Patched version - LOG(INFO, LOG_TAG) << "metadata: <" << m[1] << ">\n"; - json j = json::parse(m[1].str()); + LOG(INFO, LOG_TAG) << "metadata: <" << metadata << ">\n"; + json j = json::parse(metadata); Metadata meta; meta.artist = std::vector{j["ARTIST"].get()}; meta.title = j["TITLE"].get(); @@ -196,12 +193,17 @@ void LibrespotStream::onStderrMsg(const std::string& line) properties.metadata = std::move(meta); setProperties(properties); } - else if (regex_search(line, m, re_track_loaded)) + else if (((title_pos = line.find("<")) != std::string::npos) && ((n = line.find(">", title_pos)) != std::string::npos) && + ((ms_pos = line.find("(", n)) != std::string::npos) && ((n = line.find("ms) loaded", ms_pos)) != std::string::npos)) { - LOG(INFO, LOG_TAG) << "metadata: <" << m[1] << ">\n"; + title_pos += 1; + std::string title = line.substr(title_pos, line.find(">", title_pos) - title_pos); + LOG(INFO, LOG_TAG) << "metadata: <" << title << ">\n"; + ms_pos += 1; + std::string ms = line.substr(ms_pos, n - ms_pos - 1); Metadata meta; - meta.title = string(m[1]); - meta.duration = cpt::stod(m[2]) / 1000.; + meta.title = title; + meta.duration = cpt::stod(ms) / 1000.; meta.art_data = {SPOTIFY_LOGO, "svg"}; Properties properties; properties.metadata = std::move(meta); diff --git a/test/test_main.cpp b/test/test_main.cpp index 875e83f9..2eaad1c0 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -259,6 +259,38 @@ TEST_CASE("Librespot") } +TEST_CASE("Librespot2") +{ + std::string line = "[2021-06-04T07:20:47Z INFO librespot_playback::player] (310573 ms) loaded"; + size_t n = 0; + size_t title_pos = 0; + size_t ms_pos = 0; + if (((title_pos = line.find("<")) != std::string::npos) && ((n = line.find(">", title_pos)) != std::string::npos) && + ((ms_pos = line.find("(", n)) != std::string::npos) && ((n = line.find("ms) loaded", ms_pos)) != std::string::npos)) + { + title_pos += 1; + std::string title = line.substr(title_pos, line.find(">", title_pos) - title_pos); + REQUIRE(title == "Tunnel"); + ms_pos += 1; + std::string ms = line.substr(ms_pos, n - ms_pos - 1); + REQUIRE(ms == "310573"); + } + + line = "[2021-06-04T07:20:47Z INFO librespot_playback::player] metadata:{\"ARTIST\":\"artist\",\"TITLE\":\"title\"}"; + n = 0; + if (((n = line.find("metadata:")) != std::string::npos)) + { + std::string meta = line.substr(n + 9); + REQUIRE(meta == "{\"ARTIST\":\"artist\",\"TITLE\":\"title\"}"); + json j = json::parse(meta); + std::string artist = j["ARTIST"].get(); + REQUIRE(artist == "artist"); + std::string title = j["TITLE"].get(); + REQUIRE(title == title); + } +} + + // TEST_CASE("Librespot2") // { // std::vector @@ -288,11 +320,28 @@ TEST_CASE("Librespot") // R"xx([2022-06-27T20:45:00Z DEBUG librespot_core::session] Session[0] strong=3 weak=2)xx", // R"xx([2022-06-27T20:45:00Z INFO librespot_core::session] Country: "NZ")xx", // R"xx([2022-06-27T20:45:00Z DEBUG librespot_core::mercury] subscribed uri=hm://remote/user/REDACTED/ count=0)xx", -// R"xx([2022-06-27T20:45:00Z DEBUG librespot_connect::spirc] kMessageTypeNotify "REDACTED’s MacBook Air" 77fff453997b1fd01bfe33e60acca5b91948f3df 652808319 1656362700156 kPlayStatusStop)xx", -// R"xx([2022-06-27T20:45:00Z DEBUG librespot_connect::spirc] kMessageTypeNotify "REDACTED" 73d40aa67ebaebab0f887eae39a3dca1a29a68f0 652808319 1656362700156 kPlayStatusStop)xx", -// R"xx([2022-06-27T20:45:00Z DEBUG librespot_connect::spirc] kMessageTypeLoad "REDACTED MacBook Air" 77fff453997b1fd01bfe33e60acca5b91948f3df 652808631 1656362700156 kPlayStatusPause)xx", -// R"xx([2022-06-27T20:45:00Z DEBUG librespot_connect::spirc] State: context_uri: "spotify:playlist:6C0FrjLUZZ99ovwSXwt0DE" index: 10 position_ms: 126126 status: kPlayStatusPause position_measured_at: 1656362700511 context_description: "" shuffle: false repeat: false playing_from_fallback: true row: 0 playing_track_index: 10 track {gid: "\315i\201.\177BC\316\223\302h!|\203\264\024"} track {gid: "y\336[{\220\237C\022\210\300\032_\246\276Eu"} track {gid: "\323.H^\265\265A\313\265\'\205\220\272*\341\030"} track {gid: ")\224E\314)\357GX\201\277\311l\277\364`$"} track {gid: "\310#\223\310\254\376H\026\214\3600\r\201\000R\313"} track {gid: "\223\227#\3447\354M\322\234\222\355\242Kpq)"} track {gid: "\326\326\235\t\320\244K<\231\336\032V\240d%\031"} track {gid: "\007\204\252o\022\303N\200\214\377\221\310EW7\230"} track {gid: "\327\307\336\265koLt\2507a9\364\306}j"} track {gid: ")\221-\034\316`J\303\241\362\020\335\221\034:\320"} track {gid: "M\332\300\341\260\264M\340\216\237[\3313<\221 "} track {gid: "\202q\273\177i\352J.\212\222\335\303\002a!z"} track {gid: ",\356\372\\\215D@j\203\366\022\021&\336\237\000"} track {gid: "<\031\235{\305\320O-\264a\331=\273\236\365\220"} track {gid: "\316[\002\222\3704J*\2157V\336\344\266\351\314"} track {gid: "1+\360\n;\212Nb\235%\2476xL\257\265"} track {gid: "j\207K\250f\034\306RcH"} track {gid: ";wr\260\264\234E>\207\200\301n9\261\022w"} track {gid: "\010\226\300\377BDD\377\215\030\376\013j\311\262*"} track {gid: "\377\272\345m\324\232N\321\203\253I\016I\321H\315"} track {gid: "\313\336\321\034\306RcH"} +// track {gid: ";wr\260\264\234E>\207\200\301n9\261\022w"} track {gid: "\010\226\300\377BDD\377\215\030\376\013j\311\262*"} track {gid: +// "\377\272\345m\324\232N\321\203\253I\016I\321H\315"} track {gid: "\313\336\321 (187661 ms) loaded)xx", // R"xx([2022-06-27T20:45:03Z TRACE librespot_connect::spirc] Sending status to server: [kPlayStatusPause])xx", // R"xx([2022-06-27T20:45:03Z TRACE librespot_connect::spirc] ==> kPlayStatusPause)xx", -// R"xx([2022-06-30T15:25:37Z DEBUG librespot_connect::spirc] State: context_uri: "spotify:show:4bGWMQA1ANGTgcysDo14aX" index: 10 position_ms: 3130170 status: kPlayStatusPause position_measured_at: 1656602737490 context_description: "" shuffle: false repeat: false playing_from_fallback: true row: 0 playing_track_index: 10 track {uri: "spotify:episode:7luHEaQvEf2GA241SqaoIg"} track {uri: "spotify:episode:29LOC9vr1vWakVpWBwuF7n"} track {uri: "spotify:episode:2MS5phBmztGepq6yft07XA"} track {uri: "spotify:episode:1kqqpitr713tPbjzPhCCbf"} track {uri: "spotify:episode:59PP5P8bF17KnzueM9DUH5"} track {uri: "spotify:episode:4nxFKKH8UPXrYUhbD2G0Db"} track {uri: "spotify:episode:4CU6IsMhkflwdQcXyXPxrs"} track {uri: "spotify:episode:0VfUuKQWnttO6XsffakAnF"} track {uri: "spotify:episode:5ZNj0vDZJOYTHRpaCJExCl"} track {uri: "spotify:episode:3SV9ap9wMxpDKHaeEv0kuc"} track {uri: "spotify:episode:7oe8eW41Vrh2xJAUOQRsvF"})xx"}; +// R"xx([2022-06-30T15:25:37Z DEBUG librespot_connect::spirc] State: context_uri: "spotify:show:4bGWMQA1ANGTgcysDo14aX" index: 10 position_ms: +// 3130170 status: kPlayStatusPause position_measured_at: 1656602737490 context_description: "" shuffle: false repeat: false playing_from_fallback: +// true row: 0 playing_track_index: 10 track {uri: "spotify:episode:7luHEaQvEf2GA241SqaoIg"} track {uri: "spotify:episode:29LOC9vr1vWakVpWBwuF7n"} +// track {uri: "spotify:episode:2MS5phBmztGepq6yft07XA"} track {uri: "spotify:episode:1kqqpitr713tPbjzPhCCbf"} track {uri: +// "spotify:episode:59PP5P8bF17KnzueM9DUH5"} track {uri: "spotify:episode:4nxFKKH8UPXrYUhbD2G0Db"} track {uri: +// "spotify:episode:4CU6IsMhkflwdQcXyXPxrs"} track {uri: "spotify:episode:0VfUuKQWnttO6XsffakAnF"} track {uri: +// "spotify:episode:5ZNj0vDZJOYTHRpaCJExCl"} track {uri: "spotify:episode:3SV9ap9wMxpDKHaeEv0kuc"} track {uri: +// "spotify:episode:7oe8eW41Vrh2xJAUOQRsvF"})xx"}; // auto onStderrMsg = [](const std::string& line)