FfmpegIo.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 22 23 24 25 26
 * 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.
 */

/* necessary because libavutil/common.h uses UINT64_C */
#define __STDC_CONSTANT_MACROS

#include "config.h"
#include "FfmpegIo.hxx"
#include "../DecoderAPI.hxx"
#include "input/InputStream.hxx"
27 28

#include <stdexcept>
29 30 31

AvioStream::~AvioStream()
{
Max Kellermann's avatar
Max Kellermann committed
32 33 34 35
	if (io != nullptr) {
		av_free(io->buffer);
		av_free(io);
	}
36 37
}

38 39
inline int
AvioStream::Read(void *dest, int size)
40
{
41
	return decoder_read(client, input, dest, size);
42 43
}

44 45
inline int64_t
AvioStream::Seek(int64_t pos, int whence)
46 47 48 49 50 51
{
	switch (whence) {
	case SEEK_SET:
		break;

	case SEEK_CUR:
52
		pos += input.GetOffset();
53 54 55
		break;

	case SEEK_END:
56
		if (!input.KnownSize())
57 58
			return -1;

59
		pos += input.GetSize();
60 61 62
		break;

	case AVSEEK_SIZE:
63
		if (!input.KnownSize())
64 65
			return -1;

66
		return input.GetSize();
67 68 69 70 71

	default:
		return -1;
	}

72 73 74 75
	try {
		input.LockSeek(pos);
		return input.GetOffset();
	} catch (const std::runtime_error &) {
76
		return -1;
77
	}
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

int
AvioStream::_Read(void *opaque, uint8_t *buf, int size)
{
	AvioStream &stream = *(AvioStream *)opaque;

	return stream.Read(buf, size);
}

int64_t
AvioStream::_Seek(void *opaque, int64_t pos, int whence)
{
	AvioStream &stream = *(AvioStream *)opaque;

	return stream.Seek(pos, whence);
94 95 96 97 98
}

bool
AvioStream::Open()
{
Max Kellermann's avatar
Max Kellermann committed
99 100 101 102 103 104
	constexpr size_t BUFFER_SIZE = 8192;
	auto buffer = (unsigned char *)av_malloc(BUFFER_SIZE);
	if (buffer == nullptr)
		return false;

	io = avio_alloc_context(buffer, BUFFER_SIZE,
105
				false, this,
106 107
				_Read, nullptr,
				input.IsSeekable() ? _Seek : nullptr);
Max Kellermann's avatar
Max Kellermann committed
108 109 110 111
	/* If avio_alloc_context() fails, who frees the buffer?  The
	   libavformat API documentation does not specify this, it
	   only says that AVIOContext.buffer must be freed in the end,
	   however no AVIOContext exists in that failure code path. */
112 113
	return io != nullptr;
}