#include <fstream>
#include <iostream>
#include <csignal>
#include "RtpSession.hxx"
#include "cpLog.h"

int conversacionActiva = 0;

extern "C" void enviarSonido (char *host) {
   RtpSession *stack;
   ifstream file("/dev/dsp");

   //cpLogSetPriority (LOG_DEBUG_HB);
   cpLogSetPriority (LOG_EMERG);
   
   
   if (!file) {
      cerr << "Could not open /dev/dsp\n";
   }
   
   // sending to localhost on port 5000/5001
   // use seperate RTCP ports for localhost usage
   stack = new RtpSession (host, 44444, 44440, 44445, 44441);
   //ApiFormat is application codec and timing rate
   // 160 = 20ms for PCMU
   // 240 = 30ms for PCMU
   stack->setApiFormat (rtpPayloadPCMU, 160);
   //NetworkFormat is network codec and timing rate
   // 160 = 20ms for PCMU
   stack->setNetworkFormat (rtpPayloadPCMU, 160);
   RtpPacket* packet = NULL;

   // set SDES information - optional
   //RtcpTransmitter* rtcpTran = stack.getRtcpTran();
   //rtcpTran->setSdesEmail("development@vovida.com");


   // send out data until end of file
   int result;
   int prevRtpTime = 4294960000;
   while (conversacionActiva == 1) {
   //for (i = 0; i<100; i++){
      packet = stack->createPacket();

      // load data from file into packet
      file.read (packet->getPayloadLoc(), packet->getPayloadSize());
      packet->setPayloadUsage (file.gcount());

      packet->setRtpTime( prevRtpTime );
      prevRtpTime += 160;

      // simulating blocking read call
      //usleep (10*1000);
      result = stack->transmit(packet);
      /*if (result < 0) {
         cerr << "x";
      } else { 
         cerr << ".";
      }*/
      delete packet;
      packet = NULL;

      // send and receive RTCP packet
      //stack.processRTCP();
      // send and receive RTCP packet
      // Control
      if (stack->checkIntervalRTCP()) {
        stack->transmitRTCP();
      }
      if (stack->receiveRTCP() == 1 ) {
         // Recibido bye!
         break;
      }
      
   }
   file.close();

   // leaving session
   //usleep (50*1000);

   stack->transmitRTCPBYE();
   delete stack;
   stack = NULL;
}

extern "C" void recibirSonido() {
   RtpSession *stack;
   ofstream file("/dev/dsp", ios::app);
   
   //cpLogSetPriority (LOG_DEBUG_HB);
   cpLogSetPriority (LOG_EMERG);
   
   if (!file) {
      cerr << "Could not open file /dev/dsp\n";
   }

   //RtpSession stack (host, 9002, serverPort, 9003, serverPort+1);
   stack = new RtpSession ("0.0.0.0", 44440, 44444, 44441, 44445);
   stack->setApiFormat (rtpPayloadPCMU, 160);
   stack->setNetworkFormat (rtpPayloadPCMU, 160);
   RtpPacket* p = NULL;

   // Bucle infinito a no ser que se reciba un BYE o SIGUSR1 (seņal)
   while (conversacionActiva == 1) {
      p = stack->receive();
      if (p) {
         // got a packet - write data to file
         file.write(p->getPayloadLoc(), p->getPayloadUsage());
         //cerr << "*";
         
         delete p;
         p = NULL;
         // simulate blocking write
         
         usleep (10*1000);
      } else {
         // no packet received
         //cerr << "x";
         usleep (5*1000);

      }
      
      // Control
      if (stack->checkIntervalRTCP()) {
         stack->transmitRTCP();
      }
      
      if (stack->receiveRTCP() == 1) {
         // Recibido bye!
         break;
      }

      // send and receive RTCP packet
      //stack.processRTCP();
   }
   
   stack->transmitRTCPBYE();
   file.close();
   
   delete stack;
   stack = NULL;
}
