#include "extratorSRT.h" #include "listenerSRT.h" #include "listenerMonitorPCRBase.h" #include #include #include "dprintf.h" using namespace std; ExtratorSRT::ExtratorSRT(){ DPRINTF("Done!\n"); listeners = new list(); 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) { for(list::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::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 = 1; string sub_text = ""; char buffer [MAX_BUFFER]; double pts = 0; while (!feof(srt_file)) { cout << " . "; if(fgets(buffer, MAX_BUFFER, srt_file) != NULL) { // get index of subtitle if (((int) atoi((char*)buffer)) == sub_index) { // get time this subtitle fgets(buffer, MAX_BUFFER, srt_file); pts = (double) convert_pts((char*) buffer); string strtmp = ""; // get subtitle text do { fgets(buffer, MAX_BUFFER, srt_file); if (strcmp(buffer, "\r\n") == 0 || strcmp(buffer, "\n") == 0) break; strtmp = buffer; // file encoding with '\r\n' or only '\n' if (strtmp.find("\r") != std::string::npos) { sub_text.append(strtmp.substr(0, strtmp.length()-2)).append(" "); } else { sub_text.append(strtmp.substr(0, strtmp.length()-1)).append(" "); } } while (1); notifyListeners((unsigned char*)sub_text.c_str(), calcula_pts(pts)); sub_index++; sub_text = ""; } else { printf("[ERROR] Sequency of subtitles not found.\n"); } } } cout << "\n"; finish = true; notifyEndExtraction(sub_index-1); DDDPRINTF("Extractor STR finalized!\n"); /* printf("ExtratorSRT::Run...\n"); string subtitle; string strtmp; int cont = 1; int index_sub_file = 0; FILE *file = fopen(filepath, "r"); if(file != NULL){ char buffer [MAX_BUFFER]; double presentation_time = 0; while(!feof(file)){ if(fgets(buffer, MAX_BUFFER, file) != NULL){ index_sub_file = (int) atoi((char*)buffer); if(index_sub_file == cont) { if(cont > 1) { notifyListeners((unsigned char*)subtitle.c_str(), calcula_pts(presentation_time)); } subtitle = ""; fgets(buffer, MAX_BUFFER, file); presentation_time = (double) convert_pts((char*) buffer); cont++; } else { //FIXME: as linhas do arquivo devem ser lidas considerando apenas os caracteres válidos if (strcmp(buffer, "\r\n") != 0){ strtmp = (string) buffer; subtitle += strtmp.substr(0, strtmp.size()-2); // (-2): "\r\n" } } } else { // chegando aqui, é a última legenda!!! notifyListeners((unsigned char*)subtitle.c_str(), calcula_pts(presentation_time)); } } printf("#A extração das legendas do arquivo .srt foram concluídas.\n\n"); finish = true; fclose(file); } else { printf("#ERRO! Problemas ao tentar ler o arquivo de legendas .SRT\n"); } */ } void ExtratorSRT::notifyPCRBase(uint64_t pcrbase){ this->pcr_base = pcrbase; this->hasPCRBase = true; } double ExtratorSRT::convert_pts(char* strtimer){ vector parts; int tam = strlen(strtimer); char vinput [tam]; strcpy(vinput, strtimer); char * str; str = strtok(vinput, ":,"); while (str != NULL) { parts.push_back(string(str)); str = strtok(NULL, ":,"); } int h = atoi(parts[0].c_str()); int m = atoi(parts[1].c_str()); int s = atoi(parts[2].c_str()); int ms = atoi(parts[3].c_str()); double pts = ((double)ms/1000.0) + ((double)s) + ((double)m*60) + (((double)h*60)*60); return pts; } uint64_t ExtratorSRT::calcula_pts(double seconds){ return (uint64_t)(pcr_base + (seconds * 90000.0)); }