/
remoteavformatcontext.h
140 lines (113 loc) · 3.64 KB
/
remoteavformatcontext.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Wrapper for AVFormatContext (av_open_input_file/stream)
// supporting myth remote files
#ifndef REMOTEAVFORMATCONTEXT_H
#define REMOTEAVFORMATCONTEXT_H
#include <QString>
#include <libmythbase/mythlogging.h>
#include <libmythbase/remotefile.h>
extern "C" {
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
}
class RemoteAVFormatContext
{
public:
explicit RemoteAVFormatContext(const QString &filename = "")
{ if (!filename.isEmpty()) Open(filename); }
~RemoteAVFormatContext()
{
Close();
if (m_buffer)
av_free(m_buffer);
}
bool Open(const QString &filename)
{
if (isOpen())
return false;
if (m_inputFC)
avformat_free_context(m_inputFC);
m_inputFC = avformat_alloc_context();
delete m_rf;
m_inputIsRemote = filename.startsWith("myth://");
if (m_inputIsRemote)
{
m_rf = new RemoteFile(filename);
if (!m_rf->isOpen())
return false;
const int BUFFER_SIZE = 0x8000;
if (!m_buffer)
{
m_buffer = (unsigned char*)av_malloc(BUFFER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
if (!m_buffer)
return false;
}
m_byteIOContext = avio_alloc_context(m_buffer, BUFFER_SIZE, 0,
m_rf, &ReadFunc, &WriteFunc, &SeekFunc);
m_byteIOContext->seekable = 1;
// probe the stream
AVProbeData probe_data;
memset(&probe_data, 0, sizeof(AVProbeData));
probe_data.filename = "stream";
probe_data.buf_size = m_rf->Read(m_buffer, BUFFER_SIZE);
probe_data.buf = m_buffer;
const AVInputFormat *fmt = av_probe_input_format(&probe_data, 1);
if (!fmt)
return false;
m_rf->Seek(0, SEEK_SET);
m_inputFC->pb = m_byteIOContext;
int ret = avformat_open_input(&m_inputFC, "stream", fmt, nullptr);
if (ret)
return false;
}
else
{
int ret = avformat_open_input(&m_inputFC, qPrintable(filename), nullptr, nullptr);
if (ret)
return false;
}
return true;
}
void Close()
{
if (m_inputFC)
{
avformat_close_input(&m_inputFC);
m_inputFC = nullptr;
}
if (m_rf)
{
delete m_rf;
m_rf = nullptr;
}
}
bool isOpen() const
{
if (m_inputIsRemote)
return (m_inputFC != nullptr && m_rf != nullptr && m_rf->isOpen());
return (m_inputFC != nullptr);
}
operator AVFormatContext * () const { return m_inputFC; }
AVFormatContext * operator -> () const { return m_inputFC; }
private:
static int ReadFunc(void *opaque, uint8_t *buf, int buf_size)
{
auto *rf = reinterpret_cast< RemoteFile* >(opaque);
return rf->Read(buf, buf_size);
}
static int WriteFunc(void */*opaque*/, uint8_t */*buf*/, int /*buf_size*/)
{ return -1; }
static int64_t SeekFunc(void *opaque, int64_t offset, int whence)
{
auto *rf = reinterpret_cast< RemoteFile* >(opaque);
if (whence == AVSEEK_SIZE)
return rf->GetFileSize();
return rf->Seek(offset, whence);
}
private:
AVFormatContext *m_inputFC { nullptr };
bool m_inputIsRemote { false };
RemoteFile *m_rf { nullptr };
AVIOContext *m_byteIOContext { nullptr };
unsigned char *m_buffer { nullptr };
};
#endif // REMOTEAVFORMATCONTEXT_H