MythTV  master
recordingquality.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <utility>
3 
4 using namespace std;
5 
6 #include "recordingquality.h"
7 #include "mythcorecontext.h"
8 #include "recordinginfo.h"
9 #include "mythmiscutil.h"
10 #include "mythlogging.h"
11 
12 static void merge_overlapping(RecordingGaps &gaps);
13 static double score_gaps(const RecordingInfo& /*ri*/, const RecordingGaps& /*gaps*/);
14 static QDateTime get_start(const RecordingInfo& /*ri*/);
15 static QDateTime get_end(const RecordingInfo& /*ri*/);
16 
18  RecordingGaps rg)
19  : m_recordingGaps(std::move(rg))
20 {
21  if (!ri)
22  return;
23 
24  stable_sort(m_recordingGaps.begin(), m_recordingGaps.end());
26 
28 
29  LOG(VB_RECORD, LOG_INFO,
30  QString("RecordingQuality() score(%3)").arg(m_overallScore));
31 }
32 
34  const RecordingInfo *ri, RecordingGaps rg,
35  const QDateTime &first, const QDateTime &latest) :
36  m_recordingGaps(std::move(rg))
37 {
38  if (!ri)
39  return;
40 
42 
43  // trim start
44  QDateTime start = get_start(*ri);
45  while (!m_recordingGaps.empty() &&
46  m_recordingGaps.first().GetStart() < start)
47  {
48  RecordingGap &firstGap = m_recordingGaps.first();
49  if (start < firstGap.GetEnd())
50  firstGap = RecordingGap(start, firstGap.GetEnd());
51  else
52  m_recordingGaps.pop_front();
53  }
54 
55  // trim end
56  QDateTime end = get_end(*ri);
57  while (!m_recordingGaps.empty() &&
58  m_recordingGaps.back().GetEnd() > end)
59  {
61  if (back.GetStart() < end)
62  back = RecordingGap(back.GetStart(), end);
63  else
64  m_recordingGaps.pop_back();
65  }
66 
67  // account for late start
68  int start_gap = (first.isValid()) ? start.secsTo(first) : 0;
69  if (start_gap > 15)
70  m_recordingGaps.push_front(RecordingGap(start, first));
71 
72  // account for missing end
73  int end_gap = (latest.isValid()) ? latest.secsTo(end) : 0;
74  if (end_gap > 15)
75  m_recordingGaps.push_back(RecordingGap(latest, end));
76 
77  stable_sort(m_recordingGaps.begin(), m_recordingGaps.end());
79 
81 
82  LOG(VB_RECORD, LOG_INFO,
83  QString("RecordingQuality() start(%1) end(%2) score(%3)")
86  .arg(m_overallScore));
87 }
88 
90  int continuity_error_count, int packet_count)
91 {
92  m_continuityErrorCount = continuity_error_count;
93  m_packetCount = packet_count;
94  if (!m_packetCount)
95  return;
96 
97  double er = double(m_continuityErrorCount) / double(m_packetCount);
98  if (er >= 0.01)
99  m_overallScore = max(m_overallScore * 0.60, 0.0);
100  else if (er >= 0.001)
101  m_overallScore = max(m_overallScore * 0.80, 0.0);
102  else if (er >= 0.0001)
103  m_overallScore = max(m_overallScore * 0.90, 0.0);
104 
105  if (er >= 0.01)
106  m_overallScore = min(m_overallScore, 0.5);
107 }
108 
110 {
111  return (m_overallScore * 100) <
112  gCoreContext->GetNumSetting("MinimumRecordingQuality", 95);
113 }
114 
115 QString RecordingQuality::toStringXML(void) const
116 {
117  QString str =
118  QString(R"(<RecordingQuality overall_score="%1" key="%2")")
119  .arg(m_overallScore).arg(m_programKey);
120 
121  if (m_packetCount)
122  {
123  str += QString(R"( continuity_error_count="%1" packet_count="%2")")
125  }
126 
127  if (m_recordingGaps.empty())
128  return str + " />";
129 
130  str += ">\n";
131 
132  for (const auto & gap : qAsConst(m_recordingGaps))
133  {
134  str += xml_indent(1) +
135  QString("<Gap start=\"%1\" end=\"%2\" duration=\"%3\" />\n")
136  .arg(gap.GetStart().toString(Qt::ISODate))
137  .arg(gap.GetEnd().toString(Qt::ISODate))
138  .arg(gap.GetStart().secsTo(gap.GetEnd()));
139  }
140 
141  return str + "</RecordingQuality>";
142 }
143 
145 {
146  if (gaps.empty())
147  return;
148 
149  RecordingGaps::iterator it = gaps.begin();
150  RecordingGaps::iterator next = it; ++next;
151  while (next != gaps.end())
152  {
153  if ((*it).GetEnd() >= (*next).GetStart())
154  {
155  (*it) = RecordingGap((*it).GetStart(), (*next).GetEnd());
156  next = gaps.erase(next);
157  }
158  else
159  {
160  it = next;
161  ++next;
162  }
163  }
164 }
165 
166 static double score_gaps(const RecordingInfo &ri, const RecordingGaps &gaps)
167 {
168  RecordingGaps::const_iterator it = gaps.begin();
169  if (it == gaps.end())
170  return 1.0;
171 
172  QDateTime start = get_start(ri);
173 
174  double program_length = start.secsTo(get_end(ri));
175  if (program_length < 1.0)
176  return 0.0;
177 
178  double score = 1.0;
179  for (; it != gaps.end(); ++it)
180  {
181  double gap_start = start.secsTo((*it).GetStart());
182  double gap_end = start.secsTo((*it).GetEnd());
183  double gap_length = gap_end - gap_start;
184  double rel_start = gap_start / program_length;
185  double rel_end = gap_end / program_length;
186  double rel_center = (rel_start + rel_end) * 0.5;
187  double rel_length = rel_end - rel_start;
188 
189  /*
190  LOG(VB_GENERAL, LOG_INFO,
191  QString("%1 gap(%2,%3,%4) rel(%5,%6,%7)")
192  .arg((*it).toString())
193  .arg(gap_start).arg(gap_end).arg(gap_length)
194  .arg(rel_start).arg(rel_end).arg(rel_length));
195  */
196 
197  if (rel_center >= 0.9 || rel_end >= 0.95)
198  rel_length *= 4;
199 
200  if (rel_center < 0.1)
201  rel_length *= 2;
202 
203  if (gap_length > 5) // 5 secs
204  rel_length *= 1.5;
205 
206  if (gap_length > 120) // 2 minutes
207  rel_length *= 5;
208 
209  // NOTE: many more scoring adjustments could be made here
210  // and we may want to tune this differently depending on
211  // program length.
212 
213  score -= rel_length;
214  }
215 
216  return (score > 0.0) ? score : 0.0;
217 }
218 
219 static QDateTime get_start(const RecordingInfo &ri)
220 {
221  if (ri.GetDesiredStartTime().isValid())
222  {
223  return (ri.GetScheduledStartTime() > ri.GetDesiredStartTime()) ?
225  }
226  return ri.GetScheduledStartTime();
227 }
228 
229 static QDateTime get_end(const RecordingInfo &ri)
230 {
231  if (ri.GetDesiredEndTime().isValid())
232  {
233  return (ri.GetScheduledEndTime() < ri.GetDesiredEndTime()) ?
235  }
236  return ri.GetScheduledEndTime();
237 }
static guint32 * back
Definition: goom_core.cpp:29
QDateTime GetDesiredEndTime(void) const
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:34
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QList< RecordingGap > RecordingGaps
QDateTime GetScheduledStartTime(void) const
The scheduled start time of program.
Definition: programinfo.h:384
QString MakeUniqueKey(void) const
Creates a unique string that can be used to identify an existing recording.
Definition: programinfo.h:333
static QDateTime get_start(const RecordingInfo &)
RecordingGaps m_recordingGaps
QDateTime GetScheduledEndTime(void) const
The scheduled end time of the program.
Definition: programinfo.h:391
static double score_gaps(const RecordingInfo &, const RecordingGaps &)
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:101
RecordingQuality(const RecordingInfo *ri, RecordingGaps rg)
int GetNumSetting(const QString &key, int defaultval=0)
PictureAttribute next(PictureAttributeSupported Supported, PictureAttribute Attribute)
static void merge_overlapping(RecordingGaps &gaps)
QDateTime GetEnd(void) const
QString toStringXML(void) const
Default UTC.
Definition: mythdate.h:14
bool IsDamaged(void) const
static QDateTime get_end(const RecordingInfo &)
QString xml_indent(uint level)
QDateTime GetDesiredStartTime(void) const
void AddTSStatistics(int continuity_error_count, int packet_count)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23