Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
f6a705c7
Commit
f6a705c7
authored
Mar 13, 2019
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
input/ffmpeg: add AVIOContext wrapper class
parent
0c01840a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
31 deletions
+106
-31
FfmpegInputPlugin.cxx
src/input/plugins/FfmpegInputPlugin.cxx
+13
-31
IOContext.hxx
src/lib/ffmpeg/IOContext.hxx
+93
-0
No files found.
src/input/plugins/FfmpegInputPlugin.cxx
View file @
f6a705c7
...
@@ -21,28 +21,25 @@
...
@@ -21,28 +21,25 @@
#define __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#include "FfmpegInputPlugin.hxx"
#include "FfmpegInputPlugin.hxx"
#include "lib/ffmpeg/IOContext.hxx"
#include "lib/ffmpeg/Init.hxx"
#include "lib/ffmpeg/Init.hxx"
#include "lib/ffmpeg/Error.hxx"
#include "lib/ffmpeg/Error.hxx"
#include "../InputStream.hxx"
#include "../InputStream.hxx"
#include "../InputPlugin.hxx"
#include "../InputPlugin.hxx"
#include "PluginUnavailable.hxx"
#include "PluginUnavailable.hxx"
extern
"C"
{
#include <libavformat/avio.h>
}
class
FfmpegInputStream
final
:
public
InputStream
{
class
FfmpegInputStream
final
:
public
InputStream
{
AVIOContext
*
h
;
Ffmpeg
::
IOContext
io
;
bool
eof
=
false
;
bool
eof
=
false
;
public
:
public
:
FfmpegInputStream
(
const
char
*
_uri
,
Mutex
&
_mutex
,
FfmpegInputStream
(
const
char
*
_uri
,
Mutex
&
_mutex
)
AVIOContext
*
_h
)
:
InputStream
(
_uri
,
_mutex
),
:
InputStream
(
_uri
,
_mutex
),
h
(
_h
)
{
io
(
_uri
,
AVIO_FLAG_READ
)
seekable
=
(
h
->
seekable
&
AVIO_SEEKABLE_NORMAL
)
!=
0
;
{
size
=
avio_size
(
h
);
seekable
=
(
io
->
seekable
&
AVIO_SEEKABLE_NORMAL
)
!=
0
;
size
=
io
.
GetSize
();
/* hack to make MPD select the "ffmpeg" decoder plugin
/* hack to make MPD select the "ffmpeg" decoder plugin
- since avio.h doesn't tell us the MIME type of the
- since avio.h doesn't tell us the MIME type of the
...
@@ -52,10 +49,6 @@ public:
...
@@ -52,10 +49,6 @@ public:
SetReady
();
SetReady
();
}
}
~
FfmpegInputStream
()
noexcept
{
avio_close
(
h
);
}
/* virtual methods from InputStream */
/* virtual methods from InputStream */
bool
IsEOF
()
noexcept
override
;
bool
IsEOF
()
noexcept
override
;
size_t
Read
(
void
*
ptr
,
size_t
size
)
override
;
size_t
Read
(
void
*
ptr
,
size_t
size
)
override
;
...
@@ -84,28 +77,20 @@ static InputStreamPtr
...
@@ -84,28 +77,20 @@ static InputStreamPtr
input_ffmpeg_open
(
const
char
*
uri
,
input_ffmpeg_open
(
const
char
*
uri
,
Mutex
&
mutex
)
Mutex
&
mutex
)
{
{
AVIOContext
*
h
;
return
std
::
make_unique
<
FfmpegInputStream
>
(
uri
,
mutex
);
auto
result
=
avio_open
(
&
h
,
uri
,
AVIO_FLAG_READ
);
if
(
result
!=
0
)
throw
MakeFfmpegError
(
result
);
return
std
::
make_unique
<
FfmpegInputStream
>
(
uri
,
mutex
,
h
);
}
}
size_t
size_t
FfmpegInputStream
::
Read
(
void
*
ptr
,
size_t
read_size
)
FfmpegInputStream
::
Read
(
void
*
ptr
,
size_t
read_size
)
{
{
in
t
result
;
size_
t
result
;
{
{
const
ScopeUnlock
unlock
(
mutex
);
const
ScopeUnlock
unlock
(
mutex
);
result
=
avio_read
(
h
,
(
unsigned
char
*
)
ptr
,
read_size
);
result
=
io
.
Read
(
ptr
,
read_size
);
}
}
if
(
result
<=
0
)
{
if
(
result
==
0
)
{
if
(
result
<
0
)
throw
MakeFfmpegError
(
result
,
"avio_read() failed"
);
eof
=
true
;
eof
=
true
;
return
0
;
return
0
;
}
}
...
@@ -123,16 +108,13 @@ FfmpegInputStream::IsEOF() noexcept
...
@@ -123,16 +108,13 @@ FfmpegInputStream::IsEOF() noexcept
void
void
FfmpegInputStream
::
Seek
(
offset_type
new_offset
)
FfmpegInputStream
::
Seek
(
offset_type
new_offset
)
{
{
int64_t
result
;
u
int64_t
result
;
{
{
const
ScopeUnlock
unlock
(
mutex
);
const
ScopeUnlock
unlock
(
mutex
);
result
=
avio_seek
(
h
,
new_offset
,
SEEK_SET
);
result
=
io
.
Seek
(
new_offset
);
}
}
if
(
result
<
0
)
throw
MakeFfmpegError
(
result
,
"avio_seek() failed"
);
offset
=
result
;
offset
=
result
;
eof
=
false
;
eof
=
false
;
}
}
...
...
src/lib/ffmpeg/IOContext.hxx
0 → 100644
View file @
f6a705c7
/*
* Copyright 2003-2019 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_FFMPEG_IO_CONTEXT_HXX
#define MPD_FFMPEG_IO_CONTEXT_HXX
#include "util/Compiler.h"
#include "Error.hxx"
extern
"C"
{
#include <libavformat/avio.h>
}
#include <utility>
namespace
Ffmpeg
{
class
IOContext
{
AVIOContext
*
io_context
=
nullptr
;
public
:
IOContext
()
=
default
;
IOContext
(
const
char
*
url
,
int
flags
)
{
int
err
=
avio_open
(
&
io_context
,
url
,
flags
);
if
(
err
<
0
)
throw
MakeFfmpegError
(
err
);
}
~
IOContext
()
noexcept
{
if
(
io_context
!=
nullptr
)
avio_close
(
io_context
);
}
IOContext
(
IOContext
&&
src
)
noexcept
:
io_context
(
std
::
exchange
(
src
.
io_context
,
nullptr
))
{}
IOContext
&
operator
=
(
IOContext
&&
src
)
noexcept
{
using
std
::
swap
;
swap
(
io_context
,
src
.
io_context
);
return
*
this
;
}
AVIOContext
&
operator
*
()
noexcept
{
return
*
io_context
;
}
AVIOContext
*
operator
->
()
noexcept
{
return
io_context
;
}
gcc_pure
auto
GetSize
()
const
noexcept
{
return
avio_size
(
io_context
);
}
size_t
Read
(
void
*
buffer
,
size_t
size
)
{
int
result
=
avio_read
(
io_context
,
(
unsigned
char
*
)
buffer
,
size
);
if
(
result
<
0
)
throw
MakeFfmpegError
(
result
,
"avio_read() failed"
);
return
result
;
}
uint64_t
Seek
(
uint64_t
offset
)
{
int64_t
result
=
avio_seek
(
io_context
,
offset
,
SEEK_SET
);
if
(
result
<
0
)
throw
MakeFfmpegError
(
result
,
"avio_seek() failed"
);
return
result
;
}
};
}
// namespace Ffmpeg
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment