1 /*
2     SPDX-FileCopyrightText: 2010-2019 Mladen Milinkovic <max@smoothware.net>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "textdemux.h"
8 
9 #include "core/richdocument.h"
10 #include "core/subtitle.h"
11 #include "streamprocessor/streamprocessor.h"
12 
13 #include <KLocalizedString>
14 
15 #include <QLabel>
16 #include <QBoxLayout>
17 #include <QProgressBar>
18 
19 using namespace SubtitleComposer;
20 
TextDemux(QWidget * parent)21 TextDemux::TextDemux(QWidget *parent)
22 	: QObject(parent),
23 	  m_subtitle(nullptr),
24 	  m_subtitleTemp(nullptr),
25 	  m_streamProcessor(new StreamProcessor(this)),
26 	  m_progressWidget(new QWidget(parent))
27 {
28 	// progress Bar
29 	m_progressWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
30 	m_progressWidget->hide();
31 
32 	QLabel *label = new QLabel(i18n("Importing Subtitle Stream"), m_progressWidget);
33 
34 	m_progressBar = new QProgressBar(m_progressWidget);
35 	m_progressBar->setMinimumWidth(300);
36 	m_progressBar->setTextVisible(true);
37 
38 	QBoxLayout *layout = new QBoxLayout(QBoxLayout::LeftToRight, m_progressWidget);
39 	layout->setContentsMargins(1, 0, 1, 0);
40 	layout->setSpacing(1);
41 	layout->addWidget(label);
42 	layout->addWidget(m_progressBar);
43 
44 	// stream processor
45 	connect(m_streamProcessor, &StreamProcessor::streamProgress, this, &TextDemux::onStreamProgress);
46 	connect(m_streamProcessor, &StreamProcessor::streamError, this, &TextDemux::onStreamError);
47 	connect(m_streamProcessor, &StreamProcessor::streamFinished, this, &TextDemux::onStreamFinished);
48 	connect(m_streamProcessor, &StreamProcessor::textDataAvailable, this, &TextDemux::onStreamData);
49 }
50 
51 void
demuxFile(Subtitle * subtitle,const QString filename,int textStreamIndex)52 TextDemux::demuxFile(Subtitle *subtitle, const QString filename, int textStreamIndex)
53 {
54 	if(!subtitle)
55 		return;
56 
57 	m_streamProcessor->close();
58 
59 	m_subtitle = subtitle;
60 	m_subtitleTemp = new Subtitle();
61 
62 	if(m_streamProcessor->open(filename) && m_streamProcessor->initText(textStreamIndex))
63 		m_streamProcessor->start();
64 }
65 
66 void
onStreamData(const QString & text,quint64 msecStart,quint64 msecDuration)67 TextDemux::onStreamData(const QString &text, quint64 msecStart, quint64 msecDuration)
68 {
69 	SString stxt;
70 	stxt.setRichString(text);
71 
72 	SubtitleLine *line = new SubtitleLine(Time(double(msecStart)), Time(double(msecStart) + double(msecDuration)));
73 	line->primaryDoc()->setPlainText(stxt);
74 	m_subtitleTemp->insertLine(line);
75 }
76 
77 void
onStreamProgress(quint64 msecPos,quint64 msecLength)78 TextDemux::onStreamProgress(quint64 msecPos, quint64 msecLength)
79 {
80 	m_progressBar->setRange(0, msecLength / 1000);
81 	m_progressBar->setValue(msecPos / 1000);
82 	m_progressWidget->show();
83 }
84 
85 void
onStreamError(int code,const QString & message,const QString & debug)86 TextDemux::onStreamError(int code, const QString &message, const QString &debug)
87 {
88 	m_subtitleTemp.reset();
89 
90 	emit onError(i18n("Subtitle demux failed %1: %2\n%3", code, message, debug));
91 	m_streamProcessor->close();
92 	m_progressWidget->hide();
93 }
94 
95 void
onStreamFinished()96 TextDemux::onStreamFinished()
97 {
98 	m_subtitle->setPrimaryData(*m_subtitleTemp, true);
99 	m_subtitleTemp.reset();
100 
101 	m_progressWidget->hide();
102 }
103 
104 QWidget *
progressWidget()105 TextDemux::progressWidget()
106 {
107 	return m_progressWidget;
108 }
109