forked from MythTV/mythtv
/
remoteavformatcontext.h
162 lines (130 loc) · 4.63 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Wrapper for AVFormatContext (av_open_input_file/stream)
// supporting myth remote files
#ifndef REMOTEAVFORMATCONTEXT_H
#define REMOTEAVFORMATCONTEXT_H
// C++
#include <iostream>
// Qt
#include <QString>
// MythTV
#include <libmythbase/mythlogging.h>
#include <libmythbase/remotefile.h>
extern "C" {
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/opt.h>
}
class RemoteAVFormatContext
{
public:
explicit RemoteAVFormatContext(const QString &filename = "")
{ if (!filename.isEmpty()) Open(filename); }
~RemoteAVFormatContext()
{
Close();
if (m_buffer)
av_free(m_buffer);
}
AVFormatContext *getContext(void) { return m_inputFC; }
bool Open(const QString &filename)
{
LOG(VB_PLAYBACK, LOG_ERR, QString("RemoteAVFormatContext::Open: Opening %1").arg(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 = 0x20000;
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)
{
LOG(VB_GENERAL, LOG_ERR, QString("RemoteAVFormatContext::Open: Failed to probe file: %1").arg(filename));
return false;
}
LOG(VB_PLAYBACK, LOG_INFO, QString("RemoteAVFormatContext::Open: probed file as %1").arg(fmt->name));
m_rf->Seek(0, SEEK_SET);
m_inputFC->pb = m_byteIOContext;
int ret = avformat_open_input(&m_inputFC, "stream", fmt, nullptr);
if (ret)
{
LOG(VB_GENERAL, LOG_ERR, QString("RemoteAVFormatContext::Open: Failed to open input: %1").arg(filename));
return false;
}
}
else
{
// if this is a ice/shoutcast stream setup grabbing the inline metadata
AVDictionary *options = nullptr;
if (filename.startsWith("http://"))
av_dict_set(&options, "icy", "1", 0);
int ret = avformat_open_input(&m_inputFC, qPrintable(filename), nullptr, &options);
if (ret)
{
LOG(VB_GENERAL, LOG_ERR, QString("RemoteAVFormatContext::Open: Failed to open input: %1").arg(filename));
return false;
}
}
m_isOpen = true;
return m_isOpen;
}
void Close()
{
if (m_inputFC)
{
avformat_close_input(&m_inputFC);
m_inputFC = nullptr;
}
delete m_rf;
m_rf = nullptr;
m_isOpen = false;
}
bool isOpen() const { return m_isOpen; }
operator AVFormatContext * () const { return m_inputFC; }
//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 };
bool m_isOpen { false };
RemoteFile *m_rf { nullptr };
AVIOContext *m_byteIOContext { nullptr };
unsigned char *m_buffer { nullptr };
};
#endif // REMOTEAVFORMATCONTEXT_H