extratorSRT.cpp 2.65 KB
#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));
    
}