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