MythTV  master
subtitlereader.cpp
Go to the documentation of this file.
3 
4 // If the count of subtitle buffers is greater than this, force a clear
5 static const int MAX_BUFFERS_BEFORE_CLEAR = 175; // 125 too low for karaoke
6 
8 {
11 }
12 
14 {
18 }
19 
21 {
22  m_avSubtitlesEnabled = enable;
23 }
24 
26 {
27  m_textSubtitlesEnabled = enable;
28 }
29 
31 {
33 }
34 
35 bool SubtitleReader::AddAVSubtitle(AVSubtitle &subtitle,
36  bool fix_position,
37  bool allow_forced)
38 {
39  bool enableforced = false;
40  bool forced = false;
41  for (unsigned i = 0; i < subtitle.num_rects; i++)
42  {
43  forced = forced || static_cast<bool>(subtitle.rects[i]->flags & AV_SUBTITLE_FLAG_FORCED);
44  }
45 
46  if (!m_avSubtitlesEnabled && !forced)
47  {
48  FreeAVSubtitle(subtitle);
49  return enableforced;
50  }
51 
52  if (!m_avSubtitlesEnabled && forced)
53  {
54  if (!allow_forced)
55  {
56  LOG(VB_PLAYBACK, LOG_INFO,
57  "SubtitleReader: Ignoring forced AV subtitle.");
58  FreeAVSubtitle(subtitle);
59  return enableforced;
60  }
61  LOG(VB_PLAYBACK, LOG_INFO,
62  "SubtitleReader: Allowing forced AV subtitle.");
63  enableforced = true;
64  }
65 
66  bool clearsubs = false;
67  m_avSubtitles.m_lock.lock();
68  m_avSubtitles.m_fixPosition = fix_position;
69  m_avSubtitles.m_buffers.push_back(subtitle);
70  // in case forced subtitles aren't displayed, avoid leaking by
71  // manually clearing the subtitles
73  {
74  LOG(VB_GENERAL, LOG_ERR,
75  QString("SubtitleReader: >%1 AVSubtitles queued - clearing.")
77  clearsubs = true;
78  }
79  m_avSubtitles.m_lock.unlock();
80 
81  if (clearsubs)
83 
84  return enableforced;
85 }
86 
88 {
89  m_avSubtitles.m_lock.lock();
90  while (!m_avSubtitles.m_buffers.empty())
91  {
93  m_avSubtitles.m_buffers.pop_front();
94  }
95  m_avSubtitles.m_lock.unlock();
96 }
97 
98 void SubtitleReader::FreeAVSubtitle(AVSubtitle &subtitle)
99 {
100  avsubtitle_free(&subtitle);
101 }
102 
103 void SubtitleReader::LoadExternalSubtitles(const QString &subtitleFileName,
104  bool isInProgress)
105 {
107  m_textSubtitles.SetInProgress(isInProgress);
108  if (!subtitleFileName.isEmpty())
109  TextSubtitleParser::LoadSubtitles(subtitleFileName, m_textSubtitles, false);
110 }
111 
113 {
114  return m_textSubtitles.GetSubtitleCount() >= 0;
115 }
116 
117 QStringList SubtitleReader::GetRawTextSubtitles(std::chrono::milliseconds &duration)
118 {
119  QMutexLocker lock(&m_rawTextSubtitles.m_lock);
120  if (m_rawTextSubtitles.m_buffers.empty())
121  return {};
122 
123  duration = m_rawTextSubtitles.m_duration;
124  QStringList result = m_rawTextSubtitles.m_buffers;
126  return result;
127 }
128 
129 void SubtitleReader::AddRawTextSubtitle(const QStringList& list, std::chrono::milliseconds duration)
130 {
131  if (!m_rawTextSubtitlesEnabled || list.empty())
132  return;
133 
134  QMutexLocker lock(&m_rawTextSubtitles.m_lock);
137  m_rawTextSubtitles.m_duration = duration;
138 }
139 
141 {
142  QMutexLocker lock(&m_rawTextSubtitles.m_lock);
144 }
MAX_BUFFERS_BEFORE_CLEAR
static const int MAX_BUFFERS_BEFORE_CLEAR
Definition: subtitlereader.cpp:5
SubtitleReader::TextSubtitlesUpdated
void TextSubtitlesUpdated()
TextSubtitles::SetInProgress
void SetInProgress(bool isInProgress)
Definition: textsubtitleparser.h:83
SubtitleReader::m_textSubtitles
TextSubtitles m_textSubtitles
Definition: subtitlereader.h:65
SubtitleReader::AddAVSubtitle
bool AddAVSubtitle(AVSubtitle &subtitle, bool fix_position, bool allow_forced)
Definition: subtitlereader.cpp:35
TextSubtitles::Clear
void Clear(void)
Definition: textsubtitleparser.cpp:257
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
subtitlereader.h
SubtitleReader::EnableAVSubtitles
void EnableAVSubtitles(bool enable)
Definition: subtitlereader.cpp:20
SubtitleReader::ClearAVSubtitles
void ClearAVSubtitles(void)
Definition: subtitlereader.cpp:87
RawTextSubs::m_duration
std::chrono::milliseconds m_duration
Definition: subtitlereader.h:29
mythlogging.h
SubtitleReader::LoadExternalSubtitles
void LoadExternalSubtitles(const QString &subtitleFileName, bool isInProgress)
Definition: subtitlereader.cpp:103
SubtitleReader::EnableTextSubtitles
void EnableTextSubtitles(bool enable)
Definition: subtitlereader.cpp:25
SubtitleReader::~SubtitleReader
~SubtitleReader() override
Definition: subtitlereader.cpp:13
SubtitleReader::ClearRawTextSubtitles
void ClearRawTextSubtitles(void)
Definition: subtitlereader.cpp:140
SubtitleReader::FreeAVSubtitle
static void FreeAVSubtitle(AVSubtitle &sub)
Definition: subtitlereader.cpp:98
RawTextSubs::m_lock
QMutex m_lock
Definition: subtitlereader.h:30
SubtitleReader::m_rawTextSubtitles
RawTextSubs m_rawTextSubtitles
Definition: subtitlereader.h:67
SubtitleReader::EnableRawTextSubtitles
void EnableRawTextSubtitles(bool enable)
Definition: subtitlereader.cpp:30
SubtitleReader::GetRawTextSubtitles
QStringList GetRawTextSubtitles(std::chrono::milliseconds &duration)
Definition: subtitlereader.cpp:117
TextSubtitles::GetSubtitleCount
int GetSubtitleCount(void) const
Definition: textsubtitleparser.h:90
SubtitleReader::SubtitleReader
SubtitleReader()
Definition: subtitlereader.cpp:7
SubtitleReader::m_rawTextSubtitlesEnabled
bool m_rawTextSubtitlesEnabled
Definition: subtitlereader.h:68
SubtitleReader::m_avSubtitlesEnabled
bool m_avSubtitlesEnabled
Definition: subtitlereader.h:64
AVSubtitles::m_buffers
MythDeque< AVSubtitle > m_buffers
Definition: subtitlereader.h:18
SubtitleReader::m_textSubtitlesEnabled
bool m_textSubtitlesEnabled
Definition: subtitlereader.h:66
RawTextSubs::m_buffers
QStringList m_buffers
Definition: subtitlereader.h:28
AVSubtitles::m_fixPosition
bool m_fixPosition
Definition: subtitlereader.h:20
AVSubtitles::m_lock
QMutex m_lock
Definition: subtitlereader.h:19
SubtitleReader::HasTextSubtitles
bool HasTextSubtitles(void)
Definition: subtitlereader.cpp:112
SubtitleReader::AddRawTextSubtitle
void AddRawTextSubtitle(const QStringList &list, std::chrono::milliseconds duration)
Definition: subtitlereader.cpp:129
TextSubtitles::TextSubtitlesUpdated
void TextSubtitlesUpdated()
SubtitleReader::m_avSubtitles
AVSubtitles m_avSubtitles
Definition: subtitlereader.h:63
TextSubtitleParser::LoadSubtitles
static void LoadSubtitles(const QString &fileName, TextSubtitles &target, bool inBackground)
Definition: textsubtitleparser.cpp:270