extratorSRT.cpp 2.18 KB
#include "extratorSRT.h"

using namespace std;

ExtratorSRT::ExtratorSRT(){
    listeners = new list<ListenerSRT*>();
    pcr_base = 0;
    finish = false;
    hasPCRBase = false;
    DPRINTF("Done!\n");
}

ExtratorSRT::~ExtratorSRT(){ 
    listeners->clear(); 
    delete listeners;
    //delete reader;
    DDDPRINTF("Extractor STR finalized!\n");
}

void ExtratorSRT::initialize(){    
    try{
        reader = new ReaderSRT(filepath, FileIO::MODE_READ);
    }catch(ReaderException ex){ 
        finish = true;
        Util::Logger::Instance()->writeLog((char*) ex.getMessage().c_str());
        throw ExtratorSrtException("Falha ao abrir o arquivo de legenda! Verifique se o mesmo existe.");
    }
    this->Start();
}


void ExtratorSRT::addListener(ListenerSRT* listener){
    listeners->push_back(listener);
}

void ExtratorSRT::notifyListeners(unsigned char* subtitle, int64_t pts) {
    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;
}

bool ExtratorSRT::isFinished(){
    return finish;
}

void ExtratorSRT::Run(){
    printf("\n");
    DDPRINTF("[AGUARDE] Extraindo SRT...\n")

    int sub_index = 0;
    string sub_text = "";
  
    while(reader->hasNextSubtitle()){
        subtitle = reader->next();
        sub_text = subtitle->getSubtitleText();
        notifyListeners((unsigned char*)sub_text.c_str(), calcula_pts((double) subtitle->getTimeIn()));
        cout << " . ";
        sub_index++;
        free(subtitle);
    }
    printf("\n");
    finish = true;
    notifyEndExtraction(sub_index);
}


void ExtratorSRT::notifyPCRBase(uint64_t pcrbase){
    //DDPRINTF("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));    
}