MmsInputPlugin.cxx 2.71 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "MmsInputPlugin.hxx"
22 23
#include "input/ThreadInputStream.hxx"
#include "input/InputPlugin.hxx"
24
#include "system/Error.hxx"
25
#include "util/StringCompare.hxx"
26 27 28

#include <libmms/mmsx.h>

29 30
#include <stdexcept>

31
static constexpr size_t MMS_BUFFER_SIZE = 256 * 1024;
32

33
class MmsInputStream final : public ThreadInputStream {
34 35
	mmsx_t *mms;

36
public:
37
	MmsInputStream(const char *_uri, Mutex &_mutex, Cond &_cond)
38
		:ThreadInputStream(input_plugin_mms.name, _uri, _mutex, _cond,
39
				   MMS_BUFFER_SIZE) {
40 41
	}

42
protected:
43 44
	virtual void Open() override;
	virtual size_t ThreadRead(void *ptr, size_t size) override;
45

46
	void Close() override {
47 48
		mmsx_close(mms);
	}
49 50
};

51 52
void
MmsInputStream::Open()
53 54 55 56 57 58
{
	Unlock();

	mms = mmsx_connect(nullptr, nullptr, GetURI(), 128 * 1024);
	if (mms == nullptr) {
		Lock();
59
		throw std::runtime_error("mmsx_connect() failed");
60 61 62 63 64 65 66 67 68
	}

	Lock();

	/* TODO: is this correct?  at least this selects the ffmpeg
	   decoder, which seems to work fine */
	SetMimeType("audio/x-ms-wma");
}

69
static InputStream *
70
input_mms_open(const char *url,
71
	       Mutex &mutex, Cond &cond)
72
{
73 74 75 76
	if (!StringStartsWith(url, "mms://") &&
	    !StringStartsWith(url, "mmsh://") &&
	    !StringStartsWith(url, "mmst://") &&
	    !StringStartsWith(url, "mmsu://"))
77
		return nullptr;
78

79
	auto m = new MmsInputStream(url, mutex, cond);
80 81
	m->Start();
	return m;
82 83
}

84
size_t
85
MmsInputStream::ThreadRead(void *ptr, size_t read_size)
86
{
87 88 89 90 91 92 93
	/* unfortunately, mmsx_read() blocks until the whole buffer
	   has been filled; to avoid big latencies, limit the size of
	   each chunk we read to a reasonable size */
	constexpr size_t MAX_CHUNK = 16384;
	if (read_size > MAX_CHUNK)
		read_size = MAX_CHUNK;

94
	int nbytes = mmsx_read(nullptr, mms, (char *)ptr, read_size);
95 96
	if (nbytes <= 0) {
		if (nbytes < 0)
97
			throw MakeErrno("mmsx_read() failed");
98
		return 0;
99 100
	}

101
	return (size_t)nbytes;
102 103
}

104
const InputPlugin input_plugin_mms = {
105 106 107 108
	"mms",
	nullptr,
	nullptr,
	input_mms_open,
109
};