MythTV  master
subtitlereader.cpp
Go to the documentation of this file.
1 #include "mythlogging.h"
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  if (!m_avSubtitlesEnabled && !subtitle.forced)
41  {
42  FreeAVSubtitle(subtitle);
43  return enableforced;
44  }
45 
46  if (!m_avSubtitlesEnabled && subtitle.forced)
47  {
48  if (!allow_forced)
49  {
50  LOG(VB_PLAYBACK, LOG_INFO,
51  "SubtitleReader: Ignoring forced AV subtitle.");
52  FreeAVSubtitle(subtitle);
53  return enableforced;
54  }
55  LOG(VB_PLAYBACK, LOG_INFO,
56  "SubtitleReader: Allowing forced AV subtitle.");
57  enableforced = true;
58  }
59 
60  bool clearsubs = false;
61  m_avSubtitles.m_lock.lock();
62  m_avSubtitles.m_fixPosition = fix_position;
63  m_avSubtitles.m_buffers.push_back(subtitle);
64  // in case forced subtitles aren't displayed, avoid leaking by
65  // manually clearing the subtitles
67  {
68  LOG(VB_GENERAL, LOG_ERR,
69  QString("SubtitleReader: >%1 AVSubtitles queued - clearing.")
71  clearsubs = true;
72  }
73  m_avSubtitles.m_lock.unlock();
74 
75  if (clearsubs)
77 
78  return enableforced;
79 }
80 
82 {
83  m_avSubtitles.m_lock.lock();
84  while (!m_avSubtitles.m_buffers.empty())
85  {
87  m_avSubtitles.m_buffers.pop_front();
88  }
89  m_avSubtitles.m_lock.unlock();
90 }
91 
92 void SubtitleReader::FreeAVSubtitle(AVSubtitle &subtitle)
93 {
94  avsubtitle_free(&subtitle);
95 }
96 
97 void SubtitleReader::LoadExternalSubtitles(const QString &subtitleFileName,
98  bool isInProgress)
99 {
101  m_textSubtitles.SetInProgress(isInProgress);
102  if (!subtitleFileName.isEmpty())
103  TextSubtitleParser::LoadSubtitles(subtitleFileName, m_textSubtitles, false);
104 }
105 
107 {
108  return m_textSubtitles.GetSubtitleCount() >= 0;
109 }
110 
111 QStringList SubtitleReader::GetRawTextSubtitles(std::chrono::milliseconds &duration)
112 {
113  QMutexLocker lock(&m_rawTextSubtitles.m_lock);
114  if (m_rawTextSubtitles.m_buffers.empty())
115  return QStringList();
116 
117  duration = m_rawTextSubtitles.m_duration;
118  QStringList result = m_rawTextSubtitles.m_buffers;
120  return result;
121 }
122 
123 void SubtitleReader::AddRawTextSubtitle(const QStringList& list, std::chrono::milliseconds duration)
124 {
125  if (!m_rawTextSubtitlesEnabled || list.empty())
126  return;
127 
128  QMutexLocker lock(&m_rawTextSubtitles.m_lock);
131  m_rawTextSubtitles.m_duration = duration;
132 }
133 
135 {
136  QMutexLocker lock(&m_rawTextSubtitles.m_lock);
138 }
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:258
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
subtitlereader.h
SubtitleReader::EnableAVSubtitles
void EnableAVSubtitles(bool enable)
Definition: subtitlereader.cpp:20
SubtitleReader::ClearAVSubtitles
void ClearAVSubtitles(void)
Definition: subtitlereader.cpp:81
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:97
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:134
SubtitleReader::FreeAVSubtitle
static void FreeAVSubtitle(AVSubtitle &sub)
Definition: subtitlereader.cpp:92
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:111
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:106
SubtitleReader::AddRawTextSubtitle
void AddRawTextSubtitle(const QStringList &list, std::chrono::milliseconds duration)
Definition: subtitlereader.cpp:123
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:271