MythTV  master
mythinteractivebuffer.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QScopedPointer>
3 #include <QWriteLocker>
4 
5 // Mythtv
6 #include "netstream.h"
7 #include "mythlogging.h"
9 
10 // Std
11 #include <cstdio>
12 
13 #define LOC QString("InteractiveBuf: ")
14 
17  m_parent(Parent)
18 {
19  m_startReadAhead = true;
21 }
22 
24 {
26  delete m_stream;
27  delete m_parent;
28 }
29 
31 {
32  return m_stream ? m_stream->IsOpen() : false;
33 }
34 
41 bool MythInteractiveBuffer::OpenFile(const QString &Url, std::chrono::milliseconds /*Retry*/)
42 {
43  if (!NetStream::IsSupported(Url))
44  {
45  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Unsupported URL '%1'").arg(Url));
46  return false;
47  }
48 
49  QScopedPointer<NetStream> stream(new NetStream(Url, NetStream::kNeverCache));
50  if (!stream || !stream->IsOpen())
51  {
52  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to open '%1'").arg(Url));
53  return false;
54  }
55 
56  if (!stream->WaitTillReady(30s))
57  {
58  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Stream not ready '%1'").arg(Url));
59  return false;
60  }
61 
62  if (m_parent)
63  m_parent->Pause();
64 
65  QWriteLocker locker(&m_rwLock);
66 
67  m_safeFilename = Url;
68  m_filename = Url;
69 
70  delete m_stream;
71  m_stream = stream.take();
72 
73  // The initial bitrate needs to be set with consideration for low bit rate
74  // streams (e.g. radio @ 64Kbps) such that fill_min bytes are received
75  // in a reasonable time period to enable decoders to peek the first few KB
76  // to determine type & settings.
77  m_rawBitrate = 128; // remotefile
79 
80  locker.unlock();
81  Reset(true, false, true);
82 
83  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Opened '%1'").arg(Url));
84  return true;
85 }
86 
88 {
89  return m_stream ? m_stream->GetReadPosition() : 0;
90 }
91 
92 long long MythInteractiveBuffer::SeekInternal(long long Position, int Whence)
93 {
94  long long result = -1;
95  if (!m_stream)
96  return result;
97 
98  QWriteLocker locker(&m_posLock);
99 
100  // Optimize no-op seeks
101  if (m_readAheadRunning && ((Whence == SEEK_SET && Position == m_readPos) ||
102  (Whence == SEEK_CUR && Position == 0)))
103  {
104  result = m_readPos;
105  return result;
106  }
107 
108  switch (Whence)
109  {
110  case SEEK_SET: break;
111  case SEEK_CUR:
112  Position += m_stream->GetReadPosition();
113  break;
114  case SEEK_END:
115  Position += m_stream->GetSize();
116  break;
117  default:
118  errno = EINVAL;
119  m_generalWait.wakeAll();
120  return result;
121  }
122 
123  result = m_stream->Seek(Position);
124  if (result >= 0)
125  {
126  m_readPos = result;
127  m_ignoreReadPos = -1;
128  if (m_readAheadRunning)
130  m_readAdjust = 0;
131  }
132 
133  m_generalWait.wakeAll();
134  return result;
135 }
136 
138 {
139  if (m_stream)
140  return m_stream->safe_read(Buffer, Size, 1000);
141  m_ateof = true;
142  return 0;
143 }
144 
146 {
147  return m_stream ? m_stream->GetSize() : -1;
148 }
149 
151 {
152  MythMediaBuffer *parent = m_parent;
153  if (parent && IsOpen())
154  parent->Unpause();
155  m_parent = nullptr;
156  return parent;
157 }
MythInteractiveBuffer::GetRealFileSizeInternal
long long GetRealFileSizeInternal(void) const override
Definition: mythinteractivebuffer.cpp:145
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:344
MythMediaBuffer
Definition: mythmediabuffer.h:50
MythMediaBuffer::Unpause
void Unpause(void)
Unpauses the read-ahead thread. Calls StartReads(void).
Definition: mythmediabuffer.cpp:697
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
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:92
MythMediaBuffer::m_rawBitrate
uint m_rawBitrate
Definition: mythmediabuffer.h:208
MythInteractiveBuffer::GetReadPosition
long long GetReadPosition(void) const override
Definition: mythinteractivebuffer.cpp:87
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:523
MythInteractiveBuffer::IsOpen
bool IsOpen(void) const override
Definition: mythinteractivebuffer.cpp:30
mythlogging.h
NetStream::GetSize
qlonglong GetSize() const
Definition: netstream.cpp:622
NetStream::GetReadPosition
qlonglong GetReadPosition() const
Definition: netstream.cpp:615
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:562
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:140
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:646
NetStream::IsOpen
bool IsOpen() const
Definition: netstream.cpp:531
Buffer
Definition: MythExternControl.h:36
MythMediaBuffer::ResetReadAhead
void ResetReadAhead(long long NewInternal)
Restart the read-ahead thread at the 'newinternal' position.
Definition: mythmediabuffer.cpp:568
MythInteractiveBuffer::~MythInteractiveBuffer
~MythInteractiveBuffer() override
Definition: mythinteractivebuffer.cpp:23
MythInteractiveBuffer::OpenFile
bool OpenFile(const QString &Url, std::chrono::milliseconds Retry=kDefaultOpenTimeout) override
Opens a BBC NetStream for reading.
Definition: mythinteractivebuffer.cpp:41
NetStream::Seek
qlonglong Seek(qlonglong pos)
Definition: netstream.cpp:594
LOC
#define LOC
Definition: mythinteractivebuffer.cpp:13
MythMediaBuffer::m_posLock
QReadWriteLock m_posLock
Definition: mythmediabuffer.h:164
MythInteractiveBuffer::SafeRead
int SafeRead(void *Buffer, uint Size) override
Definition: mythinteractivebuffer.cpp:137
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:683
MythInteractiveBuffer::TakeBuffer
MythMediaBuffer * TakeBuffer(void)
Definition: mythinteractivebuffer.cpp:150
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:236