MmsInputPlugin.cxx 2.76 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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
#include "InputStream.hxx"
23
#include "InputPlugin.hxx"
24 25
#include "util/Error.hxx"
#include "util/Domain.hxx"
26 27 28 29 30 31 32

#include <glib.h>
#include <libmms/mmsx.h>

#include <string.h>
#include <errno.h>

33
struct MmsInputStream {
34
	InputStream base;
35

36 37 38
	mmsx_t *mms;

	bool eof;
39 40

	MmsInputStream(const char *uri,
41
		       Mutex &mutex, Cond &cond,
42
		       mmsx_t *_mms)
43 44
		:base(input_plugin_mms, uri, mutex, cond),
		 mms(_mms), eof(false) {
45 46
		/* XX is this correct?  at least this selects the ffmpeg
		   decoder, which seems to work fine*/
47
		base.mime = "audio/x-ms-wma";
48 49 50 51 52 53 54

		base.ready = true;
	}

	~MmsInputStream() {
		mmsx_close(mms);
	}
55 56
};

57
static constexpr Domain mms_domain("mms");
58

59
static InputStream *
60
input_mms_open(const char *url,
61
	       Mutex &mutex, Cond &cond,
62
	       Error &error)
63 64 65 66 67
{
	if (!g_str_has_prefix(url, "mms://") &&
	    !g_str_has_prefix(url, "mmsh://") &&
	    !g_str_has_prefix(url, "mmst://") &&
	    !g_str_has_prefix(url, "mmsu://"))
68
		return nullptr;
69

70 71
	const auto mms = mmsx_connect(nullptr, nullptr, url, 128 * 1024);
	if (mms == nullptr) {
72
		error.Set(mms_domain, "mmsx_connect() failed");
73
		return nullptr;
74 75
	}

76
	auto m = new MmsInputStream(url, mutex, cond, mms);
77
	return &m->base;
78 79 80
}

static size_t
81
input_mms_read(InputStream *is, void *ptr, size_t size,
82
	       Error &error)
83
{
84
	MmsInputStream *m = (MmsInputStream *)is;
85 86
	int ret;

87
	ret = mmsx_read(nullptr, m->mms, (char *)ptr, size);
88
	if (ret <= 0) {
89 90
		if (ret < 0)
			error.SetErrno("mmsx_read() failed");
91 92 93 94 95 96 97 98 99 100 101

		m->eof = true;
		return false;
	}

	is->offset += ret;

	return (size_t)ret;
}

static void
102
input_mms_close(InputStream *is)
103
{
104
	MmsInputStream *m = (MmsInputStream *)is;
105

106
	delete m;
107 108 109
}

static bool
110
input_mms_eof(InputStream *is)
111
{
112
	MmsInputStream *m = (MmsInputStream *)is;
113 114 115 116

	return m->eof;
}

117
const InputPlugin input_plugin_mms = {
118 119 120 121 122 123 124 125 126 127 128
	"mms",
	nullptr,
	nullptr,
	input_mms_open,
	input_mms_close,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	input_mms_read,
	input_mms_eof,
129
	nullptr,
130
};