monitor_pcr.cpp 2.33 KB
#include <stdio.h>
#include <string.h>
#include <locale>
#include "monitor_pcr.h"
#define PCRBASE_PADRAO 1000000
using namespace std;

MonitorPCR::MonitorPCR(){
    pcr_base = 0;
    find = false;
    extvideo = (char*) "";
    listeners = new list<ListenerMonitorPCRBase*>();
    DPRINTF("Done!\n");    
}


MonitorPCR::~MonitorPCR(){
    DDDPRINTF("Monitor PCR finalized!\n");	
}


uint64_t MonitorPCR::getPCRBase(){
    return pcr_base;
}


void MonitorPCR::setFormatVideo(char* format) {
    extvideo = format;
}


void MonitorPCR::chegouInput(unsigned char* packet) {

    if (!find) { // verifica se o PCRBase já foi encontrado
        *extvideo = tolower(*extvideo);
        if (strcmp(extvideo, (char*) "ts") == 0) { // se for TS procura nos pacotes
            readPCRBase(packet);	
        } else { // outros formatos de vídeo, define o PCR padrão = 1000000
            notifyListenersWithPCRBase((uint64_t) PCRBASE_PADRAO);
            find = true;
        }
    }

}


void MonitorPCR::readPCRBase(unsigned char* packet){
  
    if (packet[0] == 0x47 && !find){
        if((((packet[1] & 0x1F) << 8) | packet[2]) != 0xFF) {            
            int adaptation_field_control = ((packet[3] & 0x30) >> 4);            
            if (adaptation_field_control == 2 || adaptation_field_control == 3) {                
                int adaptation_field_length = packet[4];                
                if (adaptation_field_length > 0) {                    
                    if (((packet[5] & 0x10) >> 4) == 1) {                        
                        uint64_t program_clock_reference_base = (uint64_t) packet[6] << 25 | packet[7] << 17 | packet[8] << 9
                                | packet[9] << 1 | (packet[10] & 0x80) >> 7;                        
                        notifyListenersWithPCRBase(program_clock_reference_base);
                        find = true;                        
                    }
                }                
            }            
        }        
    }
    
}

void MonitorPCR::addListenerPCRBase(ListenerMonitorPCRBase * listener){
    this->listeners->push_back(listener);
}

void MonitorPCR::notifyListenersWithPCRBase(uint64_t pcrbase){
    for(list<ListenerMonitorPCRBase*>::iterator it = this->listeners->begin(); it != this->listeners->end(); it++){
        (*it)->notifyPCRBase(pcrbase);
    }
}