extratorSRT.cpp
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "extratorSRT.h"
#include "listenerSRT.h"
#include "subtitle.h"
#include "reader_srt.h"
#include "listenerMonitorPCRBase.h"
#include <stdio.h>
#include <iostream>
#include <lavidlib/io/FileIO.h>
#include "subtitle.h"
#include "dprintf.h"
using namespace std;
ExtratorSRT::ExtratorSRT(){
DPRINTF("Done!\n");
listeners = new list<ListenerSRT*>();
count_legendas = 0;
pcr_base = 0;
finish = false;
hasPCRBase = false;
}
ExtratorSRT::~ExtratorSRT(){
if (listeners != NULL)
delete listeners;
}
//@Deprecated
void ExtratorSRT::initialize(){
/* printf("ExtratorSRT::initialize()\n");
if(filepath)
Start();
else
printf("[ERRO] File SRT not found!\n");*/
}
void ExtratorSRT::addListener(ListenerSRT* listener){
listeners->push_back(listener);
}
void ExtratorSRT::notifyListeners(unsigned char* subtitle, int64_t pts) {
//cout << "\nNotificou: " << subtitle << endl;
for(list<ListenerSRT*>::iterator it = listeners->begin(); it != listeners->end(); it++){
(*it)->notifySubtitle(subtitle, pts);
}
}
void ExtratorSRT::notifyEndExtraction(int sub_size) {
DDPRINTF("Extrator SRT concluiu a extração: %d legendas.\n", sub_size);
for(list<ListenerSRT*>::iterator it = listeners->begin(); it != listeners->end(); it++){
(*it)->notifyEnd(sub_size);
}
}
void ExtratorSRT::setFilePath(char* path){
filepath = (char*) path;
}
char* ExtratorSRT::getFilePath(){
return filepath;
}
bool ExtratorSRT::isFinished(){
return finish;
}
void ExtratorSRT::Run(){
DPRINTF("[AGUARDE] Extraindo SRT...\n")
FILE *srt_file = fopen(filepath, "r");
if (srt_file == NULL) {
printf("[ERROR] Can't open SRT file!\n");
finish = true;
return;
}
int sub_index = 0;
string sub_text = "";
reader = new ReaderSRT(filepath, FileIO::MODE_READ);
while(reader->hasNextSubtitle()){
cout << " . ";
subtitle = reader->next();
sub_text = subtitle->getSubtitleText();
//notifyListeners((unsigned char*)sub_text.c_str(), subtitle->getTimeIn());
notifyListeners((unsigned char*)sub_text.c_str(),
calcula_pts(subtitle->getTimeIn()));
sub_index++;
//free(subtitle);
}
cout << endl;
finish = true;
notifyEndExtraction(sub_index);
DDDPRINTF("Extractor STR finalized!\n");
}
void ExtratorSRT::notifyPCRBase(uint64_t pcrbase){
printf("ExtratorSRT recebeu o pcrbase = %ld\n", pcrbase);
this->pcr_base = pcrbase;
this->hasPCRBase = true;
}
uint64_t ExtratorSRT::calcula_pts(double msec) {
return (uint64_t)(pcr_base + ((msec/1000) * 100000.0));
}