2010-05-18 20:48:52 +02:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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
|
|
|
|
|
2010-05-18 20:48:52 +02:00
|
|
|
#include "config.h"
|
2013-01-21 17:38:40 +01:00
|
|
|
#include "FfmpegInputPlugin.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "../InputStream.hxx"
|
|
|
|
#include "../InputPlugin.hxx"
|
2013-11-28 18:48:35 +01:00
|
|
|
#include "util/StringUtil.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2010-05-18 20:48:52 +02:00
|
|
|
|
2013-01-21 17:38:40 +01:00
|
|
|
extern "C" {
|
2010-05-18 20:48:52 +02:00
|
|
|
#include <libavformat/avio.h>
|
|
|
|
#include <libavformat/avformat.h>
|
2013-01-21 17:38:40 +01:00
|
|
|
}
|
2010-05-18 20:48:52 +02:00
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
struct FfmpegInputStream final : public InputStream {
|
2011-05-09 18:22:59 +02:00
|
|
|
AVIOContext *h;
|
2010-05-18 20:48:52 +02:00
|
|
|
|
|
|
|
bool eof;
|
2013-01-28 21:46:38 +01:00
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
FfmpegInputStream(const char *_uri, Mutex &_mutex, Cond &_cond,
|
2013-01-28 21:46:38 +01:00
|
|
|
AVIOContext *_h)
|
2014-05-11 18:25:55 +02:00
|
|
|
:InputStream(_uri, _mutex, _cond),
|
2013-01-28 20:32:23 +01:00
|
|
|
h(_h), eof(false) {
|
2014-05-11 16:02:57 +02:00
|
|
|
seekable = (h->seekable & AVIO_SEEKABLE_NORMAL) != 0;
|
|
|
|
size = avio_size(h);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
~FfmpegInputStream() {
|
|
|
|
avio_close(h);
|
|
|
|
}
|
2014-05-11 17:14:49 +02:00
|
|
|
|
|
|
|
/* virtual methods from InputStream */
|
|
|
|
bool IsEOF() override;
|
|
|
|
size_t Read(void *ptr, size_t size, Error &error) override;
|
2014-05-22 10:10:16 +02:00
|
|
|
bool Seek(offset_type offset, Error &error) override;
|
2010-05-18 20:48:52 +02:00
|
|
|
};
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain ffmpeg_domain("ffmpeg");
|
2010-05-18 20:48:52 +02:00
|
|
|
|
2011-05-09 18:22:59 +02:00
|
|
|
static inline bool
|
|
|
|
input_ffmpeg_supported(void)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2014-03-02 00:17:32 +01:00
|
|
|
static InputPlugin::InitResult
|
2013-08-04 13:47:48 +02:00
|
|
|
input_ffmpeg_init(gcc_unused const config_param ¶m,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
|
|
|
av_register_all();
|
|
|
|
|
|
|
|
/* disable this plugin if there's no registered protocol */
|
2011-05-09 18:22:59 +02:00
|
|
|
if (!input_ffmpeg_supported()) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(ffmpeg_domain, "No protocol");
|
2014-03-02 00:17:32 +01:00
|
|
|
return InputPlugin::InitResult::UNAVAILABLE;
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2014-03-02 00:17:32 +01:00
|
|
|
return InputPlugin::InitResult::SUCCESS;
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
static InputStream *
|
2011-09-14 21:46:41 +02:00
|
|
|
input_ffmpeg_open(const char *uri,
|
2013-01-27 17:20:50 +01:00
|
|
|
Mutex &mutex, Cond &cond,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2013-11-28 18:48:35 +01:00
|
|
|
if (!StringStartsWith(uri, "gopher://") &&
|
|
|
|
!StringStartsWith(uri, "rtp://") &&
|
|
|
|
!StringStartsWith(uri, "rtsp://") &&
|
|
|
|
!StringStartsWith(uri, "rtmp://") &&
|
|
|
|
!StringStartsWith(uri, "rtmpt://") &&
|
|
|
|
!StringStartsWith(uri, "rtmps://"))
|
2013-01-21 17:38:40 +01:00
|
|
|
return nullptr;
|
2010-05-18 20:48:52 +02:00
|
|
|
|
2013-01-28 21:46:38 +01:00
|
|
|
AVIOContext *h;
|
|
|
|
int ret = avio_open(&h, uri, AVIO_FLAG_READ);
|
2010-05-18 20:48:52 +02:00
|
|
|
if (ret != 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(ffmpeg_domain, ret,
|
|
|
|
"libavformat failed to open the URI");
|
2013-01-21 17:38:40 +01:00
|
|
|
return nullptr;
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
return new FfmpegInputStream(uri, mutex, cond, h);
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
size_t
|
|
|
|
FfmpegInputStream::Read(void *ptr, size_t read_size, Error &error)
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2014-05-11 17:14:49 +02:00
|
|
|
int ret = avio_read(h, (unsigned char *)ptr, read_size);
|
2010-05-18 20:48:52 +02:00
|
|
|
if (ret <= 0) {
|
|
|
|
if (ret < 0)
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(ffmpeg_domain, "avio_read() failed");
|
2010-05-18 20:48:52 +02:00
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
eof = true;
|
2010-05-18 20:48:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
offset += ret;
|
2010-05-18 20:48:52 +02:00
|
|
|
return (size_t)ret;
|
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
bool
|
|
|
|
FfmpegInputStream::IsEOF()
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2014-05-11 17:14:49 +02:00
|
|
|
return eof;
|
2010-05-18 20:48:52 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
bool
|
2014-05-22 10:10:16 +02:00
|
|
|
FfmpegInputStream::Seek(offset_type new_offset, Error &error)
|
2010-05-18 20:48:52 +02:00
|
|
|
{
|
2014-05-22 10:10:16 +02:00
|
|
|
int64_t ret = avio_seek(h, new_offset, SEEK_SET);
|
2010-05-18 20:48:52 +02:00
|
|
|
|
|
|
|
if (ret >= 0) {
|
2014-05-11 17:14:49 +02:00
|
|
|
eof = false;
|
2010-05-18 20:48:52 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(ffmpeg_domain, "avio_seek() failed");
|
2010-05-18 20:48:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
const InputPlugin input_plugin_ffmpeg = {
|
2013-01-21 17:38:40 +01:00
|
|
|
"ffmpeg",
|
|
|
|
input_ffmpeg_init,
|
|
|
|
nullptr,
|
|
|
|
input_ffmpeg_open,
|
2010-05-18 20:48:52 +02:00
|
|
|
};
|