forked from h4tr3d/avcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffersrc.cpp
116 lines (99 loc) · 2.88 KB
/
buffersrc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <cassert>
#include "buffersrc.h"
using namespace std;
namespace av {
BufferSrcFilterContext::BufferSrcFilterContext(const FilterContext &ctx, OptionalErrorCode ec)
{
assign(ctx, ec);
}
void BufferSrcFilterContext::assign(const FilterContext &ctx, OptionalErrorCode ec)
{
clear_if(ec);
m_type = checkFilter(ctx.filter());
if (m_type == FilterMediaType::Unknown) {
throws_if(ec, Errors::IncorrectBufferSrcFilter);
return;
}
m_src = ctx;
}
BufferSrcFilterContext &BufferSrcFilterContext::operator=(const FilterContext &ctx)
{
assign(ctx);
return *this;
}
void BufferSrcFilterContext::addVideoFrame(VideoFrame &frame, int flags, OptionalErrorCode ec)
{
if (m_type == FilterMediaType::Video) {
addFrame(frame.raw(), flags, ec);
} else {
throws_if(ec, Errors::IncorrectBufferSrcMediaType);
}
}
void BufferSrcFilterContext::addVideoFrame(VideoFrame &frame, OptionalErrorCode ec)
{
addVideoFrame(frame, 0, ec);
}
void BufferSrcFilterContext::writeVideoFrame(const VideoFrame &frame, OptionalErrorCode ec)
{
if (m_type == FilterMediaType::Video) {
writeFrame(frame.raw(), ec);
} else {
throws_if(ec, Errors::IncorrectBufferSrcMediaType);
}
}
void BufferSrcFilterContext::addAudioSamples(AudioSamples &samples, int flags, OptionalErrorCode ec)
{
if (m_type == FilterMediaType::Audio) {
addFrame(samples.raw(), flags, ec);
} else {
throws_if(ec, Errors::IncorrectBufferSrcMediaType);
}
}
void BufferSrcFilterContext::addAudioSamples(AudioSamples &samples, OptionalErrorCode ec)
{
addAudioSamples(samples, 0, ec);
}
void BufferSrcFilterContext::writeAudioSamples(const AudioSamples &samples, OptionalErrorCode ec)
{
if (m_type == FilterMediaType::Audio) {
writeFrame(samples.raw(), ec);
} else {
throws_if(ec, Errors::IncorrectBufferSrcMediaType);
}
}
size_t BufferSrcFilterContext::failedRequestsCount()
{
if (m_src)
return av_buffersrc_get_nb_failed_requests(m_src.raw());
else
return 0;
}
FilterMediaType BufferSrcFilterContext::checkFilter(const Filter &filter)
{
if (filter) {
if (filter.name() == "buffer")
return FilterMediaType::Video;
else if (filter.name() == "abuffer")
return FilterMediaType::Audio;
}
return FilterMediaType::Unknown;
}
void BufferSrcFilterContext::addFrame(AVFrame *frame, int flags, OptionalErrorCode ec)
{
clear_if(ec);
int sts = av_buffersrc_add_frame_flags(m_src.raw(), frame, flags);
if (sts < 0) {
throws_if(ec, sts, ffmpeg_category());
return;
}
}
void BufferSrcFilterContext::writeFrame(const AVFrame *frame, OptionalErrorCode ec)
{
clear_if(ec);
int sts = av_buffersrc_write_frame(m_src.raw(), frame);
if (sts < 0) {
throws_if(ec, sts, ffmpeg_category());
return;
}
}
} // namespace av