MPD: output/plugins/httpd/HttpdInternal.hxx Source File

MPD 0.20.15

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"
33 #include "event/DeferredMonitor.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
51 class HttpdOutput final : ServerSocket, DeferredMonitor {
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
90 Cond cond;
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
166 using DeferredMonitor::GetEventLoop;
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
HttpdOutput::name
char const * name
The configured name.
Definition: HttpdInternal.hxx:121
HttpdOutput::Close
void Close()
Caller must lock the mutex.
HttpdOutput::SendTag
void SendTag(const Tag &tag)
HttpdOutput
Definition: HttpdInternal.hxx:51
AudioFormat
This structure describes the format of a raw PCM stream.
Definition: AudioFormat.hxx:37
HttpdOutput::HasClients
gcc_pure bool HasClients() const noexcept
Check whether there is at least one client.
Definition: HttpdInternal.hxx:194
HttpdOutput::Unbind
void Unbind()
DeferredMonitor::GetEventLoop
EventLoop & GetEventLoop()
Definition: DeferredMonitor.hxx:46
HttpdOutput::content_type
const char * content_type
The MIME type produced by the #encoder.
Definition: HttpdInternal.hxx:78
AudioOutput
Definition: Internal.hxx:45
Tag
The meta information about a song file.
Definition: Tag.hxx:34
ServerSocket::Open
void Open()
Throws #std::runtime_error on error.
SocketAddress
An OO wrapper for struct sockaddr.
Definition: SocketAddress.hxx:46
HttpdOutput::website
char const * website
The configured website address.
Definition: HttpdInternal.hxx:129
HttpdOutput::SendHeader
void SendHeader(HttpdClient &client) const
Sends the encoder header to the client.
EventLoop
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:51
HttpdOutput::Delay
gcc_pure std::chrono::steady_clock::duration Delay() const noexcept
HttpdOutput::BroadcastPage
void BroadcastPage(Page *page)
Broadcasts a page struct to all clients.
ServerSocket
A socket that accepts incoming stream connections (e.g.
Definition: ServerSocket.hxx:34
Cond
Definition: Cond.hxx:41
httpd_output_domain
const class Domain httpd_output_domain
Mutex
Definition: Mutex.hxx:43
DeferredMonitor
Defer execution of an event into an EventLoop.
Definition: DeferredMonitor.hxx:32
HttpdOutput::Play
size_t Play(const void *chunk, size_t size)
ServerSocket.hxx
Encoder
Definition: EncoderInterface.hxx:32
HttpdOutput::CancelAllClients
void CancelAllClients()
HttpdOutput::EncodeAndPlay
void EncodeAndPlay(const void *chunk, size_t size)
Throws #std::runtime_error on error.
ContainerCast
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::HttpdOutput
HttpdOutput(EventLoop &_loop, const ConfigBlock &block)
ConfigBlock
Definition: Block.hxx:54
Compiler.h
Internal.hxx
HttpdOutput::genre
char const * genre
The configured genre.
Definition: HttpdInternal.hxx:125
HttpdOutput::Bind
void Bind()
HttpdOutput::BroadcastFromEncoder
void BroadcastFromEncoder()
Broadcasts data from the encoder to all clients.
Mutex.hxx
HttpdOutput::LockHasClients
gcc_pure bool LockHasClients() const noexcept
Check whether there is at least one client.
Definition: HttpdInternal.hxx:202
DeferredMonitor.hxx
HttpdClient
Definition: HttpdClient.hxx:37
Cast.hxx
HttpdOutput::mutex
Mutex mutex
This mutex protects the listener socket and the client list.
Definition: HttpdInternal.hxx:84
Domain
Definition: Domain.hxx:33
HttpdOutput::ReadPage
Page * ReadPage()
Reads data from the encoder (as much as available) and returns it as a new #page object.
PreparedEncoder
Definition: EncoderInterface.hxx:112
HttpdOutput::Cast
static HttpdOutput * Cast(AudioOutput *ao)
Definition: HttpdInternal.hxx:162
HttpdClient.hxx
HttpdOutput::AddClient
void AddClient(int fd)
HttpdOutput::cond
Cond cond
This condition gets signalled when an item is removed from #pages.
Definition: HttpdInternal.hxx:90
HttpdOutput::RemoveClient
void RemoveClient(HttpdClient &client)
Removes a client from the httpd_output.clients linked list.
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
Timer
Definition: Timer.hxx:27
HttpdOutput::~HttpdOutput
~HttpdOutput()
HttpdOutput::OpenEncoder
void OpenEncoder(AudioFormat &audio_format)
Caller must lock the mutex.
Page
A dynamically allocated buffer which keeps track of its reference count.
Definition: Page.hxx:37
Timer.hxx