MPD  0.20.18
HttpdInternal.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
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 2 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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
25 #ifndef MPD_OUTPUT_HTTPD_INTERNAL_H
26 #define MPD_OUTPUT_HTTPD_INTERNAL_H
27 
28 #include "HttpdClient.hxx"
29 #include "output/Internal.hxx"
30 #include "output/Timer.hxx"
31 #include "thread/Mutex.hxx"
32 #include "event/ServerSocket.hxx"
34 #include "util/Cast.hxx"
35 #include "Compiler.h"
36 
37 #include <boost/intrusive/list.hpp>
38 
39 #include <queue>
40 #include <list>
41 
42 struct ConfigBlock;
43 class EventLoop;
44 class ServerSocket;
45 class HttpdClient;
46 class Page;
47 class PreparedEncoder;
48 class Encoder;
49 struct Tag;
50 
52  AudioOutput base;
53 
58  bool open;
59 
63  PreparedEncoder *prepared_encoder = nullptr;
64  Encoder *encoder;
65 
72  size_t unflushed_input;
73 
74 public:
78  const char *content_type;
79 
84  mutable Mutex mutex;
85 
91 
92 private:
97  Timer *timer;
98 
102  Page *header;
103 
107  Page *metadata;
108 
115  std::queue<Page *, std::list<Page *>> pages;
116 
117  public:
121  char const *name;
125  char const *genre;
129  char const *website;
130 
131 private:
136  boost::intrusive::list<HttpdClient,
137  boost::intrusive::constant_time_size<true>> clients;
138 
143  char buffer[32768];
144 
149  unsigned clients_max;
150 
151 public:
152  HttpdOutput(EventLoop &_loop, const ConfigBlock &block);
153  ~HttpdOutput();
154 
155  operator AudioOutput *() {
156  return &base;
157  }
158 
159 #if CLANG_OR_GCC_VERSION(4,7)
160  constexpr
161 #endif
162  static HttpdOutput *Cast(AudioOutput *ao) {
163  return &ContainerCast(*ao, &HttpdOutput::base);
164  }
165 
167 
168  void Bind();
169  void Unbind();
170 
176  void OpenEncoder(AudioFormat &audio_format);
177 
181  void Open(AudioFormat &audio_format);
182 
186  void Close();
187 
193  gcc_pure
194  bool HasClients() const noexcept {
195  return !clients.empty();
196  }
197 
201  gcc_pure
202  bool LockHasClients() const noexcept {
203  const std::lock_guard<Mutex> protect(mutex);
204  return HasClients();
205  }
206 
207  void AddClient(int fd);
208 
212  void RemoveClient(HttpdClient &client);
213 
218  void SendHeader(HttpdClient &client) const;
219 
220  gcc_pure
221  std::chrono::steady_clock::duration Delay() const noexcept;
222 
227  Page *ReadPage();
228 
234  void BroadcastPage(Page *page);
235 
239  void BroadcastFromEncoder();
240 
244  void EncodeAndPlay(const void *chunk, size_t size);
245 
246  void SendTag(const Tag &tag);
247 
248  size_t Play(const void *chunk, size_t size);
249 
250  void CancelAllClients();
251 
252 private:
253  virtual void RunDeferred() override;
254 
255  void OnAccept(int fd, SocketAddress address, int uid) override;
256 };
257 
258 extern const class Domain httpd_output_domain;
259 
260 #endif
char const * name
The configured name.
void Close()
Caller must lock the mutex.
void SendTag(const Tag &tag)
This structure describes the format of a raw PCM stream.
Definition: AudioFormat.hxx:37
gcc_pure bool HasClients() const noexcept
Check whether there is at least one client.
void Unbind()
EventLoop & GetEventLoop()
const char * content_type
The MIME type produced by the #encoder.
The meta information about a song file.
Definition: Tag.hxx:34
void Open()
Throws #std::runtime_error on error.
An OO wrapper for struct sockaddr.
char const * website
The configured website address.
void SendHeader(HttpdClient &client) const
Sends the encoder header to the client.
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:52
gcc_pure std::chrono::steady_clock::duration Delay() const noexcept
void BroadcastPage(Page *page)
Broadcasts a page struct to all clients.
A socket that accepts incoming stream connections (e.g.
Definition: Cond.hxx:41
const class Domain httpd_output_domain
Definition: Mutex.hxx:43
Defer execution of an event into an EventLoop.
size_t Play(const void *chunk, size_t size)
void CancelAllClients()
void EncodeAndPlay(const void *chunk, size_t size)
Throws #std::runtime_error on error.
static C & ContainerCast(A &a, A C::*member)
Cast the given pointer to a struct member to its parent structure.
Definition: Cast.hxx:91
HttpdOutput(EventLoop &_loop, const ConfigBlock &block)
char const * genre
The configured genre.
void BroadcastFromEncoder()
Broadcasts data from the encoder to all clients.
gcc_pure bool LockHasClients() const noexcept
Check whether there is at least one client.
Mutex mutex
This mutex protects the listener socket and the client list.
Page * ReadPage()
Reads data from the encoder (as much as available) and returns it as a new #page object.
static HttpdOutput * Cast(AudioOutput *ao)
void AddClient(int fd)
Cond cond
This condition gets signalled when an item is removed from #pages.
void RemoveClient(HttpdClient &client)
Removes a client from the httpd_output.clients linked list.
#define gcc_pure
Definition: Compiler.h:116
Definition: Timer.hxx:27
void OpenEncoder(AudioFormat &audio_format)
Caller must lock the mutex.
A dynamically allocated buffer which keeps track of its reference count.
Definition: Page.hxx:37