20 #include "WebvttSubtecDevParser.hpp"
22 std::string getTtmlHeader()
26 <metadata xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
27 <ttm:title>WebVTT to TTML template</ttm:title>
29 <styling xmlns:tts="http://www.w3.org/ns/ttml#styling">
30 <!-- s1 specifies default color, font, and text alignment -->
33 tts:fontFamily="XFINITY Sans Med"
38 <layout xmlns:tts="http://www.w3.org/ns/ttml#styling">
39 <region xml:id="subtitleArea"
43 tts:backgroundColor="#000000FF"
44 tts:displayAlign="after"
50 void stripHtmlTags(std::string& str)
52 size_t startpos = std::string::npos, endpos = std::string::npos;
56 startpos = str.find_first_of(
'<');
57 if (startpos != std::string::npos)
59 endpos = str.find_first_of(
'>');
60 if (endpos != std::string::npos)
62 str.erase(startpos, endpos - startpos + 1);
70 std::string convertCueToTtmlString(
int id,
VTTCue *cue,
double startTime)
72 std::ostringstream ss;
77 std::istringstream is(cue->mText);
79 ss <<
"<p xml:id=\"subtitle" <<
id <<
"\" begin=\"" << startTime / 1000.0 <<
"s\" end=\"" << (startTime + cue->mDuration) / 1000.0 <<
"s\" style=\"s1\">";
80 while (std::getline(is, text))
102 m_channel = SubtecChannel::SubtecChannelFactory(SubtecChannel::ChannelType::TTML);
103 if (!m_channel->InitComms())
105 AAMPLOG_INFO(
"Init failed - subtitle parsing disabled");
106 throw std::runtime_error(
"PacketSender init failed");
108 m_channel->SendResetAllPacket();
109 int width = 1920, height = 1080;
112 m_channel->SendSelectionPacket(width, height);
113 m_channel->SendMutePacket();
116 bool WebVTTSubtecDevParser::processData(
char *buffer,
size_t bufferLen,
double position,
double duration)
121 ret = WebVTTParser::processData(buffer, bufferLen, position, duration);
123 if (ret) sendCueData();
128 void WebVTTSubtecDevParser::sendCueData()
130 std::string ttml = getVttAsTtml();
131 std::vector<uint8_t> data(ttml.begin(), ttml.end());
133 m_channel->SendDataPacket(std::move(data), 0);
136 void WebVTTSubtecDevParser::reset()
140 m_channel->SendResetChannelPacket();
142 WebVTTParser::reset();
145 void WebVTTSubtecDevParser::updateTimestamp(
unsigned long long positionMs)
148 m_channel->SendTimestampPacket(positionMs);
151 bool WebVTTSubtecDevParser::init(
double startPosSeconds,
unsigned long long basePTS)
156 ret = WebVTTParser::init(startPosSeconds, 0);
159 m_channel->SendTimestampPacket(
static_cast<uint64_t
>(basePTS));
165 void WebVTTSubtecDevParser::mute(
bool mute)
168 m_channel->SendMutePacket();
170 m_channel->SendUnmutePacket();
173 void WebVTTSubtecDevParser::pause(
bool pause)
176 m_channel->SendPausePacket();
178 m_channel->SendResumePacket();
184 std::string WebVTTSubtecDevParser::getVttAsTtml()
190 ss +=
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
191 ss +=
"<tt xmlns=\"http://www.w3.org/ns/ttml\">\n";
192 ss += getTtmlHeader();
193 ss +=
"<body region=\"subtitleArea\">\n";
203 int start = cue->mStart;
206 ss += convertCueToTtmlString(counter++, cue, start);
218 AAMPLOG_INFO(
"Outgoing WebVTT file with start pos %.3fs is EMPTY",
mStartPos / 1000.0);