1 /***
2     This file is part of snapcast
3     Copyright (C) 2014-2020  Johannes Pohl
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 ***/
18 
19 #ifndef CONTROLLER_HPP
20 #define CONTROLLER_HPP
21 
22 #include "client_connection.hpp"
23 #include "client_settings.hpp"
24 #include "decoder/decoder.hpp"
25 #include "message/message.hpp"
26 #include "message/server_settings.hpp"
27 #include "message/stream_tags.hpp"
28 #include "metadata.hpp"
29 #include "player/player.hpp"
30 #include "stream.hpp"
31 #include <atomic>
32 #include <thread>
33 
34 using namespace std::chrono_literals;
35 
36 /// Forwards PCM data to the audio player
37 /**
38  * Sets up a connection to the server (using ClientConnection)
39  * Sets up the audio decoder and player.
40  * Decodes audio (message_type::kWireChunk) and feeds PCM to the audio stream buffer
41  * Does timesync with the server
42  */
43 class Controller
44 {
45 public:
46     Controller(boost::asio::io_context& io_context, const ClientSettings& settings, std::unique_ptr<MetadataAdapter> meta);
47     void start();
48     // void stop();
49     static std::vector<std::string> getSupportedPlayerNames();
50 
51 private:
52     using MdnsHandler = std::function<void(const boost::system::error_code& ec, const std::string& host, uint16_t port)>;
53     void worker();
54     void reconnect();
55     void browseMdns(const MdnsHandler& handler);
56 
57     template <typename PlayerType>
58     std::unique_ptr<player::Player> createPlayer(ClientSettings::Player& settings, const std::string& player_name);
59 
60     void getNextMessage();
61     void sendTimeSyncMessage(int quick_syncs);
62 
63     boost::asio::io_context& io_context_;
64     boost::asio::steady_timer timer_;
65     ClientSettings settings_;
66     std::string meta_callback_;
67     SampleFormat sampleFormat_;
68     std::unique_ptr<ClientConnection> clientConnection_;
69     std::shared_ptr<Stream> stream_;
70     std::unique_ptr<decoder::Decoder> decoder_;
71     std::unique_ptr<player::Player> player_;
72     std::unique_ptr<MetadataAdapter> meta_;
73     std::unique_ptr<msg::ServerSettings> serverSettings_;
74     std::unique_ptr<msg::CodecHeader> headerChunk_;
75 };
76 
77 
78 #endif
79