MythTV  master
mythinteractivebuffer.cpp
Go to the documentation of this file.
1 // Std
2 #include <cstdio>
3 
4 // Qt
5 #include <QScopedPointer>
6 #include <QWriteLocker>
7 
8 // Mythtv
10 
12 #include "mheg/netstream.h"
13 
14 #define LOC QString("InteractiveBuf: ")
15 
18  m_parent(Parent)
19 {
20  m_startReadAhead = true;
22 }
23 
25 {
27  delete m_stream;
28  delete m_parent;
29 }
30 
32 {
33  return m_stream ? m_stream->IsOpen() : false;
34 }
35 
42 bool MythInteractiveBuffer::OpenFile(const QString &Url, std::chrono::milliseconds /*Retry*/)
43 {
44  if (!NetStream::IsSupported(Url))
45  {
46  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Unsupported URL '%1'").arg(Url));
47  return false;
48  }
49 
50  QScopedPointer<NetStream> stream(new NetStream(Url, NetStream::kNeverCache));
51  if (!stream || !stream->IsOpen())
52  {
53  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to open '%1'").arg(Url));
54  return false;
55  }
56 
57  if (!stream->WaitTillReady(30s))
58  {
59  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Stream not ready '%1'").arg(Url));
60  return false;
61  }
62 
63  if (m_parent)
64  m_parent->Pause();
65 
66  QWriteLocker locker(&m_rwLock);
67 
68  m_safeFilename = Url;
69  m_filename = Url;
70 
71  delete m_stream;
72  m_stream = stream.take();
73 
74  // The initial bitrate needs to be set with consideration for low bit rate
75  // streams (e.g. radio @ 64Kbps) such that fill_min bytes are received
76  // in a reasonable time period to enable decoders to peek the first few KB
77  // to determine type & settings.
78  m_rawBitrate = 128; // remotefile
80 
81  locker.unlock();
82  Reset(true, false, true);
83 
84  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Opened '%1'").arg(Url));
85  return true;
86 }
87 
89 {
90  return m_stream ? m_stream->GetReadPosition() : 0;
91 }
92 
93 long long MythInteractiveBuffer::SeekInternal(long long Position, int Whence)
94 {
95  long long result = -1;
96  if (!m_stream)
97  return result;
98 
99  QWriteLocker locker(&m_posLock);
100 
101  // Optimize no-op seeks
102  if (m_readAheadRunning && ((Whence == SEEK_SET && Position == m_readPos) ||
103  (Whence == SEEK_CUR && Position == 0)))
104  {
105  result = m_readPos;
106  return result;
107  }
108 
109  switch (Whence)
110  {
111  case SEEK_SET: break;
112  case SEEK_CUR:
113  Position += m_stream->GetReadPosition();
114  break;
115  case SEEK_END:
116  Position += m_stream->GetSize();
117  break;
118  default:
119  errno = EINVAL;
120  m_generalWait.wakeAll();
121  return result;
122  }
123 
124  result = m_stream->Seek(Position);
125  if (result >= 0)
126  {
127  m_readPos = result;
128  m_ignoreReadPos = -1;
129  if (m_readAheadRunning)
131  m_readAdjust = 0;
132  }
133 
134  m_generalWait.wakeAll();
135  return result;
136 }
137 
139 {
140  if (m_stream)
141  return m_stream->safe_read(Buffer, Size, 1000);
142  m_ateof = true;
143  return 0;
144 }
145 
147 {
148  return m_stream ? m_stream->GetSize() : -1;
149 }
150 
152 {
153  MythMediaBuffer *parent = m_parent;
154  if (parent && IsOpen())
155  parent->Unpause();
156  m_parent = nullptr;
157  return parent;
158 }
MythInteractiveBuffer::GetRealFileSizeInternal
long long GetRealFileSizeInternal(void) const override
Definition: mythinteractivebuffer.cpp:146
MythMediaBuffer::m_readAdjust
long long m_readAdjust
Definition: mythmediabuffer.h:219
MythMediaBuffer::m_startReadAhead
bool m_startReadAhead
Definition: mythmediabuffer.h:195
MythMediaBuffer::m_safeFilename
QString m_safeFilename
Definition: mythmediabuffer.h:181
MythMediaBuffer::CalcReadAheadThresh
void CalcReadAheadThresh(void)
Calculates m_fillMin, m_fillThreshold, and m_readBlockSize from the estimated effective bitrate of th...
Definition: mythmediabuffer.cpp:345
MythMediaBuffer
Definition: mythmediabuffer.h:50
MythMediaBuffer::Unpause
void Unpause(void)
Unpauses the read-ahead thread. Calls StartReads(void).
Definition: mythmediabuffer.cpp:698
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythMediaBuffer::m_generalWait
QWaitCondition m_generalWait
Condition to signal that the read ahead thread is running.
Definition: mythmediabuffer.h:237
mythinteractivebuffer.h
MythInteractiveBuffer::SeekInternal
long long SeekInternal(long long Position, int Whence) override
Definition: mythinteractivebuffer.cpp:93
MythMediaBuffer::m_rawBitrate
uint m_rawBitrate
Definition: mythmediabuffer.h:208
MythInteractiveBuffer::GetReadPosition
long long GetReadPosition(void) const override
Definition: mythinteractivebuffer.cpp:88
netstream.h
MythMediaBuffer::m_readPos
long long m_readPos
Definition: mythmediabuffer.h:165
NetStream::IsSupported
static bool IsSupported(const QUrl &url)
RingBuffer interface.
Definition: netstream.cpp:514
MythInteractiveBuffer::IsOpen
bool IsOpen(void) const override
Definition: mythinteractivebuffer.cpp:31
mythlogging.h
NetStream::GetSize
qlonglong GetSize() const
Definition: netstream.cpp:613
NetStream::GetReadPosition
qlonglong GetReadPosition() const
Definition: netstream.cpp:606
MythInteractiveBuffer::m_parent
MythMediaBuffer * m_parent
Definition: mythinteractivebuffer.h:31
NetStream::safe_read
int safe_read(void *data, unsigned sz, unsigned millisecs=0)
Definition: netstream.cpp:553
kMythBufferMHEG
@ kMythBufferMHEG
Definition: mythmediabuffer.h:47
MythInteractiveBuffer::m_stream
NetStream * m_stream
Definition: mythinteractivebuffer.h:30
NetStream
Stream content from a URI.
Definition: netstream.h:33
uint
unsigned int uint
Definition: compat.h:81
MythMediaBuffer::m_filename
QString m_filename
Definition: mythmediabuffer.h:184
MythMediaBuffer::KillReadAheadThread
void KillReadAheadThread(void)
Stops the read-ahead thread, and waits for it to stop.
Definition: mythmediabuffer.cpp:647
NetStream::IsOpen
bool IsOpen() const
Definition: netstream.cpp:522
Buffer
Definition: MythExternControl.h:36
MythMediaBuffer::ResetReadAhead
void ResetReadAhead(long long NewInternal)
Restart the read-ahead thread at the 'newinternal' position.
Definition: mythmediabuffer.cpp:569
MythInteractiveBuffer::~MythInteractiveBuffer
~MythInteractiveBuffer() override
Definition: mythinteractivebuffer.cpp:24
MythInteractiveBuffer::OpenFile
bool OpenFile(const QString &Url, std::chrono::milliseconds Retry=kDefaultOpenTimeout) override
Opens a BBC NetStream for reading.
Definition: mythinteractivebuffer.cpp:42
NetStream::Seek
qlonglong Seek(qlonglong pos)
Definition: netstream.cpp:585
LOC
#define LOC
Definition: mythinteractivebuffer.cpp:14
MythMediaBuffer::m_posLock
QReadWriteLock m_posLock
Definition: mythmediabuffer.h:164
MythInteractiveBuffer::SafeRead
int SafeRead(void *Buffer, uint Size) override
Definition: mythinteractivebuffer.cpp:138
MythMediaBuffer::m_rwLock
QReadWriteLock m_rwLock
Definition: mythmediabuffer.h:183
NetStream::kNeverCache
@ kNeverCache
Definition: netstream.h:38
MythMediaBuffer::m_ateof
bool m_ateof
Definition: mythmediabuffer.h:201
MythMediaBuffer::Pause
void Pause(void)
Pauses the read-ahead thread. Calls StopReads(void).
Definition: mythmediabuffer.cpp:684
MythInteractiveBuffer::TakeBuffer
MythMediaBuffer * TakeBuffer(void)
Definition: mythinteractivebuffer.cpp:151
MythMediaBuffer::MythInteractiveBuffer
friend class MythInteractiveBuffer
Definition: mythmediabuffer.h:52
MythMediaBuffer::m_ignoreReadPos
long long m_ignoreReadPos
Definition: mythmediabuffer.h:168
MythMediaBuffer::m_readAheadRunning
bool m_readAheadRunning
Definition: mythmediabuffer.h:197
MythMediaBuffer::Reset
void Reset(bool Full=false, bool ToAdjust=false, bool ResetInternal=false)
Resets the read-ahead thread and our position in the file.
Definition: mythmediabuffer.cpp:237