Commit 69a9d291 authored by Max Kellermann's avatar Max Kellermann

output/httpd: move code to methods Delay(), Play(), Cancel()

parent e2425592
......@@ -213,6 +213,9 @@ struct HttpdOutput final : private ServerSocket {
*/
void SendHeader(HttpdClient &client) const;
gcc_pure
unsigned Delay() const;
/**
* Reads data from the encoder (as much as available) and
* returns it as a new #page object.
......@@ -235,6 +238,10 @@ struct HttpdOutput final : private ServerSocket {
void SendTag(const Tag *tag);
size_t Play(const void *chunk, size_t size, Error &error);
void CancelAllClients();
private:
virtual void OnAccept(int fd, const sockaddr &address,
size_t address_length, int uid) override;
......
......@@ -361,17 +361,15 @@ HttpdOutput::SendHeader(HttpdClient &client) const
client.PushPage(header);
}
static unsigned
httpd_output_delay(struct audio_output *ao)
inline unsigned
HttpdOutput::Delay() const
{
HttpdOutput *httpd = HttpdOutput::Cast(ao);
if (!httpd->LockHasClients() && httpd->base.pause) {
if (!LockHasClients() && base.pause) {
/* if there's no client and this output is paused,
then httpd_output_pause() will not do anything, it
will not fill the buffer and it will not update the
timer; therefore, we reset the timer here */
httpd->timer->Reset();
timer->Reset();
/* some arbitrary delay that is long enough to avoid
consuming too much CPU, and short enough to notice
......@@ -379,11 +377,19 @@ httpd_output_delay(struct audio_output *ao)
return 1000;
}
return httpd->timer->IsStarted()
? httpd->timer->GetDelay()
return timer->IsStarted()
? timer->GetDelay()
: 0;
}
static unsigned
httpd_output_delay(struct audio_output *ao)
{
HttpdOutput *httpd = HttpdOutput::Cast(ao);
return httpd->Delay();
}
void
HttpdOutput::BroadcastPage(Page *page)
{
......@@ -426,24 +432,30 @@ HttpdOutput::EncodeAndPlay(const void *chunk, size_t size, Error &error)
return true;
}
static size_t
httpd_output_play(struct audio_output *ao, const void *chunk, size_t size,
Error &error)
inline size_t
HttpdOutput::Play(const void *chunk, size_t size, Error &error)
{
HttpdOutput *httpd = HttpdOutput::Cast(ao);
if (httpd->LockHasClients()) {
if (!httpd->EncodeAndPlay(chunk, size, error))
if (LockHasClients()) {
if (!EncodeAndPlay(chunk, size, error))
return 0;
}
if (!httpd->timer->IsStarted())
httpd->timer->Start();
httpd->timer->Add(size);
if (!timer->IsStarted())
timer->Start();
timer->Add(size);
return size;
}
static size_t
httpd_output_play(struct audio_output *ao, const void *chunk, size_t size,
Error &error)
{
HttpdOutput *httpd = HttpdOutput::Cast(ao);
return httpd->Play(chunk, size, error);
}
static bool
httpd_output_pause(struct audio_output *ao)
{
......@@ -515,14 +527,19 @@ httpd_output_tag(struct audio_output *ao, const Tag *tag)
httpd->SendTag(tag);
}
inline void
HttpdOutput::CancelAllClients()
{
const ScopeLock protect(mutex);
for (auto &client : clients)
client.CancelQueue();
}
static void
httpd_output_cancel(struct audio_output *ao)
{
HttpdOutput *httpd = HttpdOutput::Cast(ao);
const ScopeLock protect(httpd->mutex);
for (auto &client : httpd->clients)
client.CancelQueue();
httpd->CancelAllClients();
}
const struct audio_output_plugin httpd_output_plugin = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment