MythTV  master
textsubtitleparser.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
7 #ifndef TEXT_SUBTITLE_PARSER_H
8 #define TEXT_SUBTITLE_PARSER_H
9 
10 // C++ headers
11 #include <cstdint>
12 #include <vector>
13 
14 // Qt headers
15 #include <QObject>
16 #include <QMutexLocker>
17 #include <QStringList>
18 #include <QDateTime>
19 
21 {
22  public:
23  text_subtitle_t(long start_, long end_) : m_start(start_), m_end(end_) {}
24  text_subtitle_t() = default;
25  text_subtitle_t(const text_subtitle_t&) = default;
27 
28  public:
29  uint64_t m_start {0};
30  uint64_t m_end {0};
31  QStringList m_textLines;
32 };
33 
34 using TextSubtitleList = std::vector<text_subtitle_t>;
35 
36 class TextSubtitles : public QObject
37 {
38  Q_OBJECT
39 
40  signals:
42 
43  public:
45  {
48  }
49 
50  ~TextSubtitles() override;
51 
52  bool HasSubtitleChanged(uint64_t timecode) const;
53  QStringList GetSubtitles(uint64_t timecode);
54 
62  bool IsFrameBasedTiming(void) const
63  { return m_frameBasedTiming; }
64 
65  void SetFrameBasedTiming(bool frameBasedTiming) {
66  QMutexLocker locker(&m_lock);
67  m_frameBasedTiming = frameBasedTiming;
68  }
69 
70  void SetFilename(const QString &fileName) {
71  QMutexLocker locker(&m_lock);
72  m_fileName = fileName;
73  }
74 
75  void AddSubtitle(const text_subtitle_t& newSub);
76  void Clear(void);
77  void SetLastLoaded(void);
78  void SetByteCount(off_t count) {
79  QMutexLocker locker(&m_lock);
80  m_byteCount = count;
81  }
82  off_t GetByteCount(void) const { return m_byteCount; }
83  void SetInProgress(bool isInProgress) {
84  QMutexLocker locker(&m_lock);
85  m_isInProgress = isInProgress;
86  }
87  void SetHasSubtitles(bool hasSubs) { m_hasSubtitles = hasSubs; }
88 
89  // Returns -1 if there is no subtitle file.
90  int GetSubtitleCount(void) const
91  { return m_hasSubtitles ? m_subtitles.size() : -1; }
92 
93  void Lock(void) { m_lock.lock(); }
94  void Unlock(void) { m_lock.unlock(); }
95 
96  private:
99  bool m_frameBasedTiming {false};
100  QString m_fileName;
101  QDateTime m_lastLoaded;
103  // Note: m_isInProgress is overly conservative because it doesn't
104  // change from true to false after a recording completes.
105  bool m_isInProgress {false};
106  // It's possible to have zero subtitles at the start of playback
107  // because none have yet been written for an in-progress
108  // recording, so use m_hasSubtitles instead of m_subtitles.size().
109  bool m_hasSubtitles {false};
110  QMutex m_lock {QMutex::Recursive};
111 };
112 
114 {
115  public:
116  static void LoadSubtitles(const QString &fileName, TextSubtitles &target,
117  bool inBackground);
118 };
119 
120 #endif
text_subtitle_t::m_start
uint64_t m_start
Starting time in msec or starting frame.
Definition: textsubtitleparser.h:29
text_subtitle_t::text_subtitle_t
text_subtitle_t(const text_subtitle_t &)=default
TextSubtitleParser
Definition: textsubtitleparser.h:114
TextSubtitles::Unlock
void Unlock(void)
Definition: textsubtitleparser.h:94
TextSubtitles::AddSubtitle
void AddSubtitle(const text_subtitle_t &newSub)
Definition: textsubtitleparser.cpp:252
text_subtitle_t
TextSubtitles Copyright (c) 2006 by Pekka Jääskeläinen Distributed as part of MythTV under GPL v2 and lat...
Definition: textsubtitleparser.h:21
TextSubtitles::SetInProgress
void SetInProgress(bool isInProgress)
Definition: textsubtitleparser.h:83
TextSubtitles::m_lastLoaded
QDateTime m_lastLoaded
Definition: textsubtitleparser.h:101
text_subtitle_t::m_textLines
QStringList m_textLines
Definition: textsubtitleparser.h:31
text_subtitle_t::text_subtitle_t
text_subtitle_t(long start_, long end_)
Definition: textsubtitleparser.h:23
TextSubtitles::m_frameBasedTiming
bool m_frameBasedTiming
Definition: textsubtitleparser.h:99
TextSubtitles::HasSubtitleChanged
bool HasSubtitleChanged(uint64_t timecode) const
Returns true in case the subtitle to display has changed since the last GetSubtitles() call.
Definition: textsubtitleparser.cpp:167
TextSubtitles::SetByteCount
void SetByteCount(off_t count)
Definition: textsubtitleparser.h:78
TextSubtitles::Clear
void Clear(void)
Definition: textsubtitleparser.cpp:258
text_subtitle_t::operator=
text_subtitle_t & operator=(const text_subtitle_t &)=default
TextSubtitleList
std::vector< text_subtitle_t > TextSubtitleList
Definition: textsubtitleparser.h:34
TextSubtitles::~TextSubtitles
~TextSubtitles() override
Definition: textsubtitleparser.cpp:152
TextSubtitles::GetSubtitles
QStringList GetSubtitles(uint64_t timecode)
Returns the subtitles to display at the given timecode.
Definition: textsubtitleparser.cpp:180
TextSubtitles::m_byteCount
off_t m_byteCount
Definition: textsubtitleparser.h:102
text_subtitle_t::m_end
uint64_t m_end
Ending time in msec or ending frame.
Definition: textsubtitleparser.h:30
TextSubtitles::m_hasSubtitles
bool m_hasSubtitles
Definition: textsubtitleparser.h:109
text_subtitle_t::text_subtitle_t
text_subtitle_t()=default
TextSubtitles::TextSubtitles
TextSubtitles()
Definition: textsubtitleparser.h:44
TextSubtitles
Definition: textsubtitleparser.h:37
TextSubtitles::GetSubtitleCount
int GetSubtitleCount(void) const
Definition: textsubtitleparser.h:90
off_t
#define off_t
Definition: mythiowrapper.cpp:238
TextSubtitles::Lock
void Lock(void)
Definition: textsubtitleparser.h:93
TextSubtitles::m_lock
QMutex m_lock
Definition: textsubtitleparser.h:110
TextSubtitles::SetHasSubtitles
void SetHasSubtitles(bool hasSubs)
Definition: textsubtitleparser.h:87
TextSubtitles::GetByteCount
off_t GetByteCount(void) const
Definition: textsubtitleparser.h:82
TextSubtitles::m_lastReturnedSubtitle
text_subtitle_t m_lastReturnedSubtitle
Definition: textsubtitleparser.h:98
TextSubtitles::m_fileName
QString m_fileName
Definition: textsubtitleparser.h:100
TextSubtitles::SetLastLoaded
void SetLastLoaded(void)
Definition: textsubtitleparser.cpp:264
TextSubtitles::IsFrameBasedTiming
bool IsFrameBasedTiming(void) const
Returns true in case the subtitle timing data is frame-based.
Definition: textsubtitleparser.h:62
TextSubtitles::m_isInProgress
bool m_isInProgress
Definition: textsubtitleparser.h:105
TextSubtitles::m_subtitles
TextSubtitleList m_subtitles
Definition: textsubtitleparser.h:97
TextSubtitles::SetFrameBasedTiming
void SetFrameBasedTiming(bool frameBasedTiming)
Definition: textsubtitleparser.h:65
TextSubtitles::TextSubtitlesUpdated
void TextSubtitlesUpdated()
TextSubtitles::SetFilename
void SetFilename(const QString &fileName)
Definition: textsubtitleparser.h:70
TextSubtitleParser::LoadSubtitles
static void LoadSubtitles(const QString &fileName, TextSubtitles &target, bool inBackground)
Definition: textsubtitleparser.cpp:271