MythTV  master
mythavformatbuffer.cpp
Go to the documentation of this file.
2 
3 #include <QtGlobal>
4 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
5 #include <QMutex>
6 #else
7 #include <QRecursiveMutex>
8 #endif
9 
11 
13  : m_buffer(Buffer)
14 {
15 }
16 
18 {
19  m_buffer = Buffer;
20 }
21 
23 {
24  return m_buffer;
25 }
26 
27 int MythAVFormatBuffer::Open(URLContext *Context, const char* /*Filename*/, int /*Flags*/)
28 {
29  Context->priv_data = nullptr;
30  return 0;
31 }
32 
33 int MythAVFormatBuffer::Read(URLContext *Context, uint8_t *Buffer, int Size)
34 {
35  auto *avfr = reinterpret_cast<MythAVFormatBuffer*>(Context->priv_data);
36  if (!avfr)
37  return 0;
38 
39  int ret = avfr->GetBuffer()->Read(Buffer, Size);
40 
41  if (ret == 0)
42  ret = AVERROR_EOF;
43  return ret;
44 }
45 
46 int MythAVFormatBuffer::Write(URLContext *Context, const uint8_t *Buffer, int Size)
47 {
48  auto *avfr = reinterpret_cast<MythAVFormatBuffer*>(Context->priv_data);
49  if (!avfr)
50  return 0;
51 
52  return avfr->GetBuffer()->Write(Buffer, static_cast<uint>(Size));
53 }
54 
55 int64_t MythAVFormatBuffer::Seek(URLContext *Context, int64_t Offset, int Whence)
56 {
57  auto *avfr = reinterpret_cast<MythAVFormatBuffer*>(Context->priv_data);
58  if (!avfr)
59  return 0;
60 
61  if (Whence == AVSEEK_SIZE)
62  return avfr->GetBuffer()->GetRealFileSize();
63 
64  if (Whence == SEEK_END)
65  return avfr->GetBuffer()->GetRealFileSize() + Offset;
66 
67  return avfr->GetBuffer()->Seek(Offset, Whence);
68 }
69 
70 int MythAVFormatBuffer::Close(URLContext* /*Context*/)
71 {
72  return 0;
73 }
74 
75 int MythAVFormatBuffer::WritePacket(void *Context, uint8_t *Buffer, int Size)
76 {
77  if (!Context)
78  return 0;
79  return ffurl_write(reinterpret_cast<URLContext*>(Context), Buffer, Size);
80 }
81 
82 int MythAVFormatBuffer::ReadPacket(void *Context, uint8_t *Buffer, int Size)
83 {
84  if (!Context)
85  return 0;
86  return ffurl_read(reinterpret_cast<URLContext*>(Context), Buffer, Size);
87 }
88 
89 int64_t MythAVFormatBuffer::SeekPacket(void *Context, int64_t Offset, int Whence)
90 {
91  if (!Context)
92  return 0;
93  return ffurl_seek(reinterpret_cast<URLContext*>(Context), Offset, Whence);
94 }
95 
97 {
98 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
99  static QMutex s_avringbufferLock(QMutex::Recursive);
100 #else
101  static QRecursiveMutex s_avringbufferLock;
102 #endif
103  static bool s_avringbufferInitialised = false;
104 
105  QMutexLocker lock(&s_avringbufferLock);
106  if (!s_avringbufferInitialised)
107  {
108  // just in case URLProtocol's members do not have default constructor
109  memset(static_cast<void*>(&s_avfrURL), 0, sizeof(s_avfrURL));
110  s_avfrURL.name = "rbuffer";
111  s_avfrURL.url_open = Open;
112  s_avfrURL.url_read = Read;
113  s_avfrURL.url_write = Write;
114  s_avfrURL.url_seek = Seek;
115  s_avfrURL.url_close = Close;
116  s_avfrURL.priv_data_size = 0;
117  s_avfrURL.flags = URL_PROTOCOL_FLAG_NETWORK;
118  s_avringbufferInitialised = true;
119  }
120  return &s_avfrURL;
121 }
122 
124 {
125  m_initState = State;
127 }
128 
130 {
131  return m_initState;
132 }
MythAVFormatBuffer::Seek
static int64_t Seek(URLContext *Context, int64_t Offset, int Whence)
Definition: mythavformatbuffer.cpp:55
MythAVFormatBuffer::s_avfrURL
static URLProtocol s_avfrURL
Definition: mythavformatbuffer.h:34
MythMediaBuffer::Write
int Write(const void *Buffer, uint Count)
Writes buffer to ThreadedFileWriter::Write(const void*,uint)
Definition: mythmediabuffer.cpp:1625
MythMediaBuffer
Definition: mythmediabuffer.h:50
mythavformatbuffer.h
MythAVFormatBuffer::Write
static int Write(URLContext *Context, const uint8_t *Buffer, int Size)
Definition: mythavformatbuffer.cpp:46
MythAVFormatBuffer::Read
static int Read(URLContext *Context, uint8_t *Buffer, int Size)
Definition: mythavformatbuffer.cpp:33
MythAVFormatBuffer::GetBuffer
MythMediaBuffer * GetBuffer(void)
Definition: mythavformatbuffer.cpp:22
MythMediaBuffer::GetRealFileSize
long long GetRealFileSize(void) const
Definition: mythmediabuffer.cpp:462
MythAVFormatBuffer::Open
static int Open(URLContext *Context, const char *Filename, int Flags)
Definition: mythavformatbuffer.cpp:27
MythAVFormatBuffer::Close
static int Close(URLContext *)
Definition: mythavformatbuffer.cpp:70
State
State
Definition: zmserver.h:68
MythAVFormatBuffer::IsInInit
bool IsInInit(void) const
Definition: mythavformatbuffer.cpp:129
MythAVFormatBuffer::WritePacket
static int WritePacket(void *Context, uint8_t *Buffer, int Size)
Definition: mythavformatbuffer.cpp:75
MythAVFormatBuffer::GetURLProtocol
static URLProtocol * GetURLProtocol(void)
Definition: mythavformatbuffer.cpp:96
MythAVFormatBuffer::m_initState
bool m_initState
Definition: mythavformatbuffer.h:33
MythAVFormatBuffer::m_buffer
MythMediaBuffer * m_buffer
Definition: mythavformatbuffer.h:32
MythAVFormatBuffer::SeekPacket
static int64_t SeekPacket(void *Context, int64_t Offset, int Whence)
Definition: mythavformatbuffer.cpp:89
uint
unsigned int uint
Definition: compat.h:81
MythAVFormatBuffer::SetBuffer
void SetBuffer(MythMediaBuffer *Buffer)
Definition: mythavformatbuffer.cpp:17
MythMediaBuffer::Read
int Read(void *Buffer, int Count)
This is the public method for reading from a file, it calls the appropriate read method if the file i...
Definition: mythmediabuffer.cpp:1487
Buffer
Definition: MythExternControl.h:36
MythAVFormatBuffer::SetInInit
void SetInInit(bool State)
Definition: mythavformatbuffer.cpp:123
MythMediaBuffer::SetReadInternalMode
bool SetReadInternalMode(bool Mode)
Definition: mythmediabuffer.cpp:517
MythAVFormatBuffer::ReadPacket
static int ReadPacket(void *Context, uint8_t *Buffer, int Size)
Definition: mythavformatbuffer.cpp:82
MythAVFormatBuffer::MythAVFormatBuffer
MythAVFormatBuffer(MythMediaBuffer *Buffer=nullptr)
Definition: mythavformatbuffer.cpp:12
MythAVFormatBuffer
Definition: mythavformatbuffer.h:13