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