-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFFmpegIOCtx.cpp
More file actions
90 lines (72 loc) · 2.04 KB
/
Copy pathFFmpegIOCtx.cpp
File metadata and controls
90 lines (72 loc) · 2.04 KB
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
#include "FFmpegIOCtx.hpp"
FFmpegIOCtx::FFmpegIOCtx()
{
_read_fun_ptr = nullptr;
_write_fun_ptr = nullptr;
_seek_fun_ptr = nullptr;
_buffer = nullptr;
}
FFmpegIOCtx::~FFmpegIOCtx()
{
}
void FFmpegIOCtx::SetOnRead(std::function<int(uint8_t *buf, int buf_size)> cb)
{
if (cb)
{
_read_cb = std::move(cb);
_read_fun_ptr = &read_packet;
}
}
void FFmpegIOCtx::SetOnWrite(std::function<int(uint8_t *buf, int buf_size)> cb)
{
if (cb)
{
_write_cb = std::move(cb);
_write_fun_ptr = &read_packet;
}
}
void FFmpegIOCtx::SetOnSeek(std::function<int64_t(int64_t offset, int whence)> cb)
{
if (cb)
{
_seek_cb = std::move(cb);
_seek_fun_ptr = &seek_packet;
}
}
void FFmpegIOCtx::Init(int buffer_size, std::weak_ptr<FFmpegIOCtx> &weak_on_this_obj)
{
_buffer_size = buffer_size;
_buffer = new unsigned char[buffer_size];
int write_flag = _write_fun_ptr ? 1 : 0;
_avio.reset(avio_alloc_context(_buffer, _buffer_size, write_flag, &weak_on_this_obj, _read_fun_ptr, _write_fun_ptr, _seek_fun_ptr),
[](AVIOContext *ptr)
{
av_freep(&ptr->buffer);
avio_context_free(&ptr);
});
}
int FFmpegIOCtx::read_packet(void *opaque, uint8_t *buf, int buf_size)
{
auto strong_self = (*(std::weak_ptr<FFmpegIOCtx> *)opaque).lock();
if (strong_self.get())
return strong_self->_read_cb(buf, buf_size);
return AVERROR_EOF;
}
int FFmpegIOCtx::write_packet(void *opaque, uint8_t *buf, int buf_size)
{
auto strong_self = (*(std::weak_ptr<FFmpegIOCtx> *)opaque).lock();
if (strong_self.get())
return strong_self->_write_cb(buf, buf_size);
return AVERROR_EOF;
}
int64_t FFmpegIOCtx::seek_packet(void *opaque, int64_t offset, int whence)
{
auto strong_self = (*(std::weak_ptr<FFmpegIOCtx> *)opaque).lock();
if (strong_self.get())
return strong_self->_seek_cb(offset, whence);
return AVERROR_EOF;
}
AVIOContext *FFmpegIOCtx::get()
{
return _avio.get();
}