Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
F
ffmpeg.WithCustomProto
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Linshizhi
ffmpeg.WithCustomProto
Commits
1ffe45e7
Commit
1ffe45e7
authored
Apr 13, 2022
by
Linshizhi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement IOCtx.
parent
1e0a94a7
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
231 additions
and
0 deletions
+231
-0
CMakeLists.txt
CMakeLists.txt
+1
-0
ioctx.cc
src/ioctx.cc
+37
-0
ioctx.h
src/ioctx.h
+66
-0
utils.cc
src/utils.cc
+61
-0
utils.h
src/utils.h
+25
-0
ioctxTestCases.cc
tests/ioctxTestCases.cc
+41
-0
No files found.
CMakeLists.txt
View file @
1ffe45e7
...
...
@@ -31,6 +31,7 @@ ExternalProject_Add(
--disable-ffmpeg --disable-ffprobe --disable-ffplay --prefix=
${
PROJECT_SOURCE_DIR
}
/lib/
BUILD_COMMAND make -j
INSTALL_COMMAND make install
UPDATE_COMMAND
""
)
set
(
SRC
${
ROOT
}
/src
)
...
...
src/ioctx.cc
View file @
1ffe45e7
#include "ioctx.h"
namespace
IOCtx
{
///////////////////////////////////////////////////////////////////////////////
// InCtx //
///////////////////////////////////////////////////////////////////////////////
InCtx
::
InCtx
(
std
::
string
path
,
CustomProto
proto
)
:
fmt
(
nullptr
)
{
// Build AVIOContex for custom proto
AVIOContext
*
customIO
=
nullptr
;
fmt
=
Utils
::
makeInAVFormat
(
path
,
customIO
);
}
void
InCtx
::
readFrame
(
AVPacket
*
packet
)
{
if
(
av_read_frame
(
fmt
.
get
(),
packet
)
<
0
)
throw
IO_ERROR
();
}
bool
InCtx
::
isReady
()
noexcept
{
return
fmt
.
get
()
!=
nullptr
;
}
///////////////////////////////////////////////////////////////////////////////
// OutCtx //
///////////////////////////////////////////////////////////////////////////////
OutCtx
::
OutCtx
(
std
::
string
path
,
CustomProto
proto
)
:
fmt
(
nullptr
)
{
}
}
src/ioctx.h
View file @
1ffe45e7
#include <string>
#include <stdexcept>
#include "utils.h"
extern
"C"
{
#include <libavformat/avformat.h>
}
#ifndef IOCTX_H
#define IOCTX_H
namespace
IOCtx
{
// Forward Declarations
class
InCtx
;
class
OutCtx
;
// Extra support custom protos
enum
CustomProto
{
SHARED_MEMORY
};
class
END_OF_FILE
:
public
std
::
runtime_error
{
public
:
END_OF_FILE
()
:
std
::
runtime_error
(
""
)
{}
};
class
IO_ERROR
:
public
std
::
runtime_error
{
public
:
IO_ERROR
()
:
std
::
runtime_error
(
""
)
{}
};
class
InCtx
{
// OutCtx require parameters of InCtx
// to initialize.
friend
OutCtx
;
public
:
InCtx
(
std
::
string
path
)
:
fmt
(
Utils
::
makeInAVFormat
(
path
,
nullptr
))
{}
InCtx
(
std
::
string
path
,
CustomProto
proto
);
void
readFrame
(
AVPacket
*
);
bool
isReady
()
noexcept
;
private
:
Utils
::
AVFormatContextShared
fmt
;
};
class
OutCtx
{
public
:
OutCtx
(
std
::
string
path
)
:
fmt
(
Utils
::
makeOutAVFormat
(
path
,
nullptr
))
{}
OutCtx
(
std
::
string
path
,
CustomProto
proto
);
void
writeFrame
(
AVPacket
*
);
bool
isReady
()
noexcept
;
private
:
Utils
::
AVFormatContextShared
fmt
;
};
}
#endif
/* IOCTX_H */
src/utils.cc
0 → 100644
View file @
1ffe45e7
#include "utils.h"
namespace
Utils
{
static
AVFormatContext
*
AVFormatInputContextConstructor
(
std
::
string
path
,
AVIOContext
*
customIO
)
{
AVFormatContext
*
ctx
=
customIO
==
nullptr
?
nullptr
:
avformat_alloc_context
();
ctx
->
pb
=
customIO
;
if
(
avformat_open_input
(
&
ctx
,
path
.
c_str
(),
nullptr
,
nullptr
)
<
0
)
{
return
nullptr
;
}
if
(
avformat_find_stream_info
(
ctx
,
nullptr
)
<
0
)
{
avformat_close_input
(
&
ctx
);
return
nullptr
;
}
return
ctx
;
}
static
void
AVFormatInputContextDestructor
(
AVFormatContext
*
ctx
)
{
avformat_close_input
(
&
ctx
);
}
AVFormatContextShared
makeInAVFormat
(
std
::
string
path
,
AVIOContext
*
customIO
)
{
AVFormatContextShared
ioCtx
{
AVFormatInputContextConstructor
(
path
,
customIO
),
AVFormatInputContextDestructor
};
return
ioCtx
;
}
static
AVFormatContext
*
AVFormatOutputContextConstructor
(
std
::
string
path
,
AVIOContext
*
customIO
)
{
AVFormatContext
*
ctx
=
customIO
==
nullptr
?
nullptr
:
avformat_alloc_context
();
ctx
->
pb
=
customIO
;
if
(
avformat_open_input
(
&
ctx
,
path
.
c_str
(),
nullptr
,
nullptr
)
<
0
)
{
return
nullptr
;
}
return
ctx
;
}
static
void
AVFormatOutputContextDestructor
(
AVFormatContext
*
ctx
)
{
avformat_close_input
(
&
ctx
);
}
AVFormatContextShared
makeOutAVFormat
(
std
::
string
path
,
AVIOContext
*
customIO
)
{
AVFormatContextShared
ioCtx
{
AVFormatOutputContextConstructor
(
path
,
customIO
),
AVFormatOutputContextDestructor
};
return
ioCtx
;
}
}
src/utils.h
0 → 100644
View file @
1ffe45e7
#include <string>
#include <memory>
extern
"C"
{
#include <libavformat/avformat.h>
}
#ifndef UTILS_H
#define UTILS_H
namespace
Utils
{
using
AVFormatContextShared
=
std
::
shared_ptr
<
AVFormatContext
>
;
AVFormatContextShared
makeInAVFormat
(
std
::
string
,
AVIOContext
*
);
AVFormatContextShared
makeOutAVFormat
(
std
::
string
,
AVIOContext
*
);
/* To check that is video specified by path valid
*
* This function require that ffmpeg is installed on
* running envrionment */
bool
isVideoValid
(
std
::
string
path
);
}
#endif
/* UTILS_H */
tests/ioctxTestCases.cc
View file @
1ffe45e7
#include <gtest/gtest.h>
#include <string>
#include "ioctx.h"
#include "utils.h"
extern
"C"
{
#include <libavformat/avformat.h>
}
class
IOCTX_With_Default_Proto_Fixture
:
public
::
testing
::
Test
{
protected
:
void
SetUp
()
override
{
oCtx
=
OutCtx
(
outFilePath
,
iCtx
);
}
std
::
string
inFilePath
=
"./resources/sample.mp4"
;
std
::
string
outFilePath
=
"./resources/sample_out.mp4"
;
InCtx
iCtx
{
inFilePath
};
OutCtx
oCtx
;
};
class
IOCTX_With_SharedMemory_Proto_Fixture
:
public
::
testing
::
Test
{
protected
:
void
SetUp
()
override
{
}
InCtx
iCtx
;
OutCtx
oCtx
;
};
TEST_F
(
IOCTX_With_Default_Proto_Fixture
,
Description
)
{
AVPacket
packet
;
iCtx
.
readFrame
(
&
packet
);
oCtx
.
writeFrame
(
&
packet
);
ASSERT_TRUE
(
Utils
::
isVideoValid
(
outFilePath
));
}
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