MythTV  master
mythstreamingbuffer.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QUrl>
3 
4 // MythTV
5 #include "mythcorecontext.h"
6 #include "mythlogging.h"
8 
9 #define LOC QString("StreamRingBuf(%1): ").arg(m_filename)
10 
13 {
14  m_startReadAhead = false;
16 }
17 
19 {
21 
22  m_rwLock.lockForWrite();
23  if (m_context)
24  ffurl_close(m_context);
25  m_rwLock.unlock();
26 }
27 
29 {
30  m_rwLock.lockForRead();
31  bool result = m_context;
32  m_rwLock.unlock();
33  return result;
34 }
35 
37 {
38  return 0;
39 }
40 
49 bool MythStreamingBuffer::OpenFile(const QString &Filename, std::chrono::milliseconds /*Retry*/)
50 {
52 
53  m_rwLock.lockForWrite();
54 
55  m_safeFilename = Filename;
56  m_filename = Filename;
57 
58  // TODO check whether local area file
59 
60  QUrl url = m_filename;
61  if (url.path().endsWith(QLatin1String("m3u8"), Qt::CaseInsensitive))
62  url.setScheme("hls+http");
63 
64  int res = ffurl_open(&m_context, url.toString().toLatin1(), AVIO_FLAG_READ, nullptr, nullptr);
65  if (res >= 0 && m_context && !m_context->is_streamed && ffurl_seek(m_context, 0, SEEK_SET) >= 0)
66  {
67  m_streamed = false;
68  m_allowSeeks = true;
69  }
70 
71  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Trying %1 (allow seeks: %2")
73 
74  if (res < 0 || !m_context)
75  {
76  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to open stream (error %1)") .arg(res));
77  m_lastError = QObject::tr("Failed to open stream (%1)").arg(res);
78  m_rwLock.unlock();
79  return false;
80  }
81 
82  m_rwLock.unlock();
83  return true;
84 }
85 
86 long long MythStreamingBuffer::SeekInternal(long long Position, int Whence)
87 {
88  if (!m_context)
89  return 0;
90 
91  m_posLock.lockForWrite();
92  int seek = static_cast<int>(ffurl_seek(m_context, Position, Whence));
93  m_posLock.unlock();
94 
95  if (seek < 0)
96  {
97  m_ateof = true;
98  return 0;
99  }
100  return Position;
101 }
102 
104 {
105  int len = 0;
106 
107  if (m_context)
108  {
109  while (len < static_cast<int>(Size))
110  {
111  int ret = ffurl_read(m_context, static_cast<unsigned char*>(Buffer) + len,
112  static_cast<int>(Size) - len);
113  if (ret < 0)
114  {
115  if (ret == AVERROR_EOF)
116  m_ateof = true;
117  errno = ret;
118  break;
119  }
120  if (ret == 0) // nothing read, exit early
121  break; // should EOF be set ??
122  len += ret;
123  }
124  }
125  return len;
126 }
127 
129 {
130  long long result = -1;
131  m_rwLock.lockForRead();
132  if (m_context)
133  result = ffurl_size(m_context);
134  m_rwLock.unlock();
135  return result;
136 }
MythStreamingBuffer::GetRealFileSizeInternal
long long GetRealFileSizeInternal(void) const override
Definition: mythstreamingbuffer.cpp:128
MythMediaBuffer::m_startReadAhead
bool m_startReadAhead
Definition: mythmediabuffer.h:195
MythStreamingBuffer::IsOpen
bool IsOpen(void) const override
Definition: mythstreamingbuffer.cpp:28
MythMediaBuffer::m_safeFilename
QString m_safeFilename
Definition: mythmediabuffer.h:181
arg
arg(title).arg(filename).arg(doDelete))
MythStreamingBuffer::m_allowSeeks
bool m_allowSeeks
Definition: mythstreamingbuffer.h:34
MythMediaBuffer
Definition: mythmediabuffer.h:50
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythMediaBuffer::m_lastError
QString m_lastError
Definition: mythmediabuffer.h:186
MythStreamingBuffer::m_streamed
bool m_streamed
Definition: mythstreamingbuffer.h:33
MythStreamingBuffer::m_context
URLContext * m_context
Definition: mythstreamingbuffer.h:32
MythStreamingBuffer::SafeRead
int SafeRead(void *Buffer, uint Size) override
Definition: mythstreamingbuffer.cpp:103
mythlogging.h
MythStreamingBuffer::GetReadPosition
long long GetReadPosition(void) const override
Definition: mythstreamingbuffer.cpp:36
kMythBufferHTTP
@ kMythBufferHTTP
Definition: mythmediabuffer.h:45
MythStreamingBuffer::~MythStreamingBuffer
~MythStreamingBuffer() override
Definition: mythstreamingbuffer.cpp:18
uint
unsigned int uint
Definition: compat.h:140
MythMediaBuffer::m_filename
QString m_filename
Definition: mythmediabuffer.h:184
MythStreamingBuffer::OpenFile
bool OpenFile(const QString &Filename, std::chrono::milliseconds Retry=kDefaultOpenTimeout) override
Definition: mythstreamingbuffer.cpp:49
MythMediaBuffer::KillReadAheadThread
void KillReadAheadThread(void)
Stops the read-ahead thread, and waits for it to stop.
Definition: mythmediabuffer.cpp:646
MythMediaBuffer::AVFormatInitNetwork
static void AVFormatInitNetwork(void)
Definition: mythmediabuffer.cpp:1849
Buffer
Definition: MythExternControl.h:36
mythcorecontext.h
MythStreamingBuffer::SeekInternal
long long SeekInternal(long long Position, int Whence) override
Definition: mythstreamingbuffer.cpp:86
MythStreamingBuffer::MythStreamingBuffer
MythStreamingBuffer(const QString &Filename)
Definition: mythstreamingbuffer.cpp:11
LOC
#define LOC
Definition: mythstreamingbuffer.cpp:9
MythMediaBuffer::m_posLock
QReadWriteLock m_posLock
Definition: mythmediabuffer.h:164
mythstreamingbuffer.h
MythMediaBuffer::m_rwLock
QReadWriteLock m_rwLock
Definition: mythmediabuffer.h:183
MythMediaBuffer::m_ateof
bool m_ateof
Definition: mythmediabuffer.h:201