ProxyInputStream.cxx 2.5 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * http://www.musicpd.org
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "ProxyInputStream.hxx"
22
#include "tag/Tag.hxx"
23 24

#include <stdexcept>
25

26
ProxyInputStream::ProxyInputStream(InputStreamPtr _input) noexcept
27
	:InputStream(_input->GetURI(), _input->mutex),
28
	 input(std::move(_input))
29
{
30
	assert(input);
31 32

	input->SetHandler(this);
33 34
}

35 36
ProxyInputStream::~ProxyInputStream() noexcept = default;

37 38 39 40 41 42 43
void
ProxyInputStream::SetInput(InputStreamPtr _input) noexcept
{
	assert(!input);
	assert(_input);

	input = std::move(_input);
44
	input->SetHandler(this);
45 46 47 48

	/* this call wakes up client threads if the new input is
	   ready */
	CopyAttributes();
49 50

	set_input_cond.signal();
51 52
}

53 54 55
void
ProxyInputStream::CopyAttributes()
{
56 57
	assert(input);

58
	if (input->IsReady()) {
59
		if (!IsReady()) {
60 61
			if (input->HasMimeType())
				SetMimeType(input->GetMimeType());
62

63 64
			size = input->KnownSize()
				? input->GetSize()
65
				: UNKNOWN_SIZE;
66

67
			seekable = input->IsSeekable();
68 69 70
			SetReady();
		}

71
		offset = input->GetOffset();
72 73 74
	}
}

75 76
void
ProxyInputStream::Check()
77
{
78 79
	if (input)
		input->Check();
80 81 82
}

void
83
ProxyInputStream::Update() noexcept
84
{
85 86 87
	if (!input)
		return;

88
	input->Update();
89 90 91
	CopyAttributes();
}

92 93
void
ProxyInputStream::Seek(offset_type new_offset)
94
{
95
	while (!input)
96
		set_input_cond.wait(mutex);
97

98
	input->Seek(new_offset);
99 100 101 102
	CopyAttributes();
}

bool
103
ProxyInputStream::IsEOF() noexcept
104
{
105
	return input && input->IsEOF();
106 107
}

108
std::unique_ptr<Tag>
109 110
ProxyInputStream::ReadTag()
{
111 112 113
	if (!input)
		return nullptr;

114
	return input->ReadTag();
115 116 117
}

bool
118
ProxyInputStream::IsAvailable() noexcept
119
{
120
	return input && input->IsAvailable();
121 122 123
}

size_t
124
ProxyInputStream::Read(void *ptr, size_t read_size)
125
{
126
	while (!input)
127
		set_input_cond.wait(mutex);
128

129
	size_t nbytes = input->Read(ptr, read_size);
130 131 132
	CopyAttributes();
	return nbytes;
}