2010-05-18 20:48:52 +02:00
|
|
|
/*
|
2019-03-13 09:57:50 +01:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2010-05-18 20:48:52 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-01-21 17:38:40 +01:00
|
|
|
/* necessary because libavutil/common.h uses UINT64_C */
|
|
|
|
#define __STDC_CONSTANT_MACROS
|
|
|
|
|
|
|
|
#include "FfmpegInputPlugin.hxx"
|
2019-03-13 10:01:17 +01:00
|
|
|
#include "lib/ffmpeg/IOContext.hxx"
|
2014-12-21 20:56:42 +01:00
|
|
|
#include "lib/ffmpeg/Init.hxx"
|
2014-08-18 20:20:51 +02:00
|
|
|
#include "lib/ffmpeg/Error.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "../InputStream.hxx"
|
|
|
|
#include "../InputPlugin.hxx"
|
2016-09-09 15:11:52 +02:00
|
|
|
#include "PluginUnavailable.hxx"
|
2010-05-18 20:48:52 +02:00
|
|
|
|
2019-03-13 09:59:04 +01:00
|
|
|
class FfmpegInputStream final : public InputStream {
|
2019-03-13 10:01:17 +01:00
|
|
|
Ffmpeg::IOContext io;
|
2010-05-18 20:48:52 +02:00
|
|
|
|
2019-03-13 09:59:04 +01:00
|
|
|
public:
|
2019-03-13 10:01:17 +01:00
|
|
|
FfmpegInputStream(const char *_uri, Mutex &_mutex)
|
2018-06-22 19:37:18 +02:00
|
|
|
:InputStream(_uri, _mutex),
|
2019-03-13 10:01:17 +01:00
|
|
|
io(_uri, AVIO_FLAG_READ)
|
|
|
|
{
|
|
|
|
seekable = (io->seekable & AVIO_SEEKABLE_NORMAL) != 0;
|
|
|
|
size = io.GetSize();
|
2013-01-28 21:46:38 +01:00
|
|
|
|
|
|
|
/* hack to make MPD select the "ffmpeg" decoder plugin
|
|
|
|
- since avio.h doesn't tell us the MIME type of the
|
|
|
|
resource, we can't select a decoder plugin, but the
|
|
|
|
"ffmpeg" plugin is quite good at auto-detection */
|
2014-05-11 16:02:57 +02:00
|
|
|
SetMimeType("audio/x-mpd-ffmpeg");
|
|
|
|
SetReady();
|
2013-01-28 21:46:38 +01:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
/* virtual methods from InputStream */
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsEOF() noexcept override;
|
2019-04-26 19:19:45 +02:00
|
|
|
size_t Read(std::unique_lock<Mutex> &lock,
|
|
|
|
void *ptr, size_t size) override;
|
|
|
|
void Seek(std::unique_lock<Mutex> &lock,
|
|
|
|
offset_type offset) override;
|
2010-05-18 20:48:52 +02:00
|
|
|
};
|
|
|
|
|
2019-03-13 09:58:33 +01:00
|
|
|
gcc_const
|
2011-05-09 18:22:59 +02:00
|
|
|
static inline bool
|
2019-03-13 09:58:33 +01:00
|
|
|
input_ffmpeg_supported() noexcept
|
2011-05-09 18:22:59 +02:00
|
|
|
{
|
2013-01-21 17:38:40 +01:00
|
|
|
void *opaque = nullptr;
|
|
|
|
return avio_enum_protocols(&opaque, 0) != nullptr;
|
2011-05-09 18:22:59 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 12:05:54 +02:00
|
|
|
static void
|
2017-01-25 23:06:12 +01:00
|
|
|
input_ffmpeg_init(EventLoop &, const ConfigBlock &)
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2014-12-21 20:56:42 +01:00
|
|
|
FfmpegInit();
|
2010-05-18 20:48:52 +02:00
|
|
|
|
|
|
|
/* disable this plugin if there's no registered protocol */
|
2016-09-09 15:11:52 +02:00
|
|
|
if (!input_ffmpeg_supported())
|
|
|
|
throw PluginUnavailable("No protocol");
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 20:05:22 +01:00
|
|
|
static InputStreamPtr
|
2011-09-14 21:46:41 +02:00
|
|
|
input_ffmpeg_open(const char *uri,
|
2018-06-22 19:37:18 +02:00
|
|
|
Mutex &mutex)
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2019-03-13 10:01:17 +01:00
|
|
|
return std::make_unique<FfmpegInputStream>(uri, mutex);
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
size_t
|
2019-04-26 19:19:45 +02:00
|
|
|
FfmpegInputStream::Read(std::unique_lock<Mutex> &,
|
|
|
|
void *ptr, size_t read_size)
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2019-03-13 10:01:17 +01:00
|
|
|
size_t result;
|
2017-11-13 17:08:00 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
2019-03-13 10:01:17 +01:00
|
|
|
result = io.Read(ptr, read_size);
|
2017-11-13 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2014-08-18 09:47:28 +02:00
|
|
|
offset += result;
|
|
|
|
return (size_t)result;
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
FfmpegInputStream::IsEOF() noexcept
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2019-03-13 10:41:12 +01:00
|
|
|
return io.IsEOF();
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
2019-04-26 19:19:45 +02:00
|
|
|
FfmpegInputStream::Seek(std::unique_lock<Mutex> &, offset_type new_offset)
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2019-03-13 10:01:17 +01:00
|
|
|
uint64_t result;
|
2017-11-13 17:08:00 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
2019-03-13 10:01:17 +01:00
|
|
|
result = io.Seek(new_offset);
|
2017-11-13 17:08:00 +01:00
|
|
|
}
|
2010-05-18 20:48:52 +02:00
|
|
|
|
2014-08-18 09:52:53 +02:00
|
|
|
offset = result;
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 20:25:32 +02:00
|
|
|
static constexpr const char *ffmpeg_prefixes[] = {
|
|
|
|
"gopher://",
|
|
|
|
"rtp://",
|
|
|
|
"rtsp://",
|
|
|
|
"rtmp://",
|
|
|
|
"rtmpt://",
|
|
|
|
"rtmps://",
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
const InputPlugin input_plugin_ffmpeg = {
|
2013-01-21 17:38:40 +01:00
|
|
|
"ffmpeg",
|
2018-10-24 20:25:32 +02:00
|
|
|
ffmpeg_prefixes,
|
2013-01-21 17:38:40 +01:00
|
|
|
input_ffmpeg_init,
|
|
|
|
nullptr,
|
|
|
|
input_ffmpeg_open,
|
2010-05-18 20:48:52 +02:00
|
|
|
};
|