How to use timer of in esp32?

I want to read remote control key values using a timer of esp32. The timer should count the total value of high and low pulses so i can generate the same pulses on the other side. I tried using the given "repeat timer " in the example but it is not working the same as i expected. Is there any other way of using the timer to count pulses

Below code is not working

hw_timer_t * timer = NULL;
int ir_in = 15, value, arr[2000]={0}, arr1[2000]={0}, i;
unsigned int count, count1, flag = 0, flag1 = 0, no_pulse = 0, no_pulse1 = 0;
int dis_data();
void setup() {
  Serial.begin(115200);
  pinMode (ir_in, INPUT);
  timer = timerBegin(0, 80, true);  // timer 0, MWDT clock period = 12.5 ns * TIMGn_Tx_WDT_CLK_PRESCALE -> 12.5 ns * 80 -> 1000 ns = 1 us, countUp
  timerAttachInterrupt(timer, &onTimer, true); // edge (not level) triggered 
  timerAlarmWrite(timer, 1000, true); // 1000000 * 1 us = 1 s, autoreload true
  timerAlarmEnable(timer); // enable
}

  void  onTimer(){

    if(flag==1)
      count1++;
 }

void loop()
{
  // put your main code here, to run repeatedly:
  //    no_pulse=0;
  //   no_pulse1=0;
  value = digitalRead(ir_in);

  if (value == 0)
  {  
    flag = 1;
    while ((digitalRead(ir_in) == 0) && (flag == 1))
    {
      delayMicroseconds(1);
      count++;
    }
    arr[no_pulse++] = count;
    count = 0;
    while ((digitalRead(ir_in) == 1) && (flag == 1))
    {
      delayMicroseconds(1);
      count++;
    }
    arr1[no_pulse1++] = count;
    count = 0;
    flag = 0;
    flag1 = 1;
  }
  if((count1>1000)&& (flag1==1))
  {   
       for (i = 1; i <= no_pulse; i++)
        {
           Serial.print("arr-");
           Serial.println(arr[i]);
        }
        for (i = 1; i <= no_pulse1; i++)
        {
           Serial.print("arr1-");
           Serial.println(arr1[i]);
        }
    
         flag1=0;
         count1=0;
         no_pulse=0;
         no_pulse1=0;
  } 
}

Below snippet of pi code that i want to use in arduino to count remote pulses.

READ:- 

   LED1 = 0;
     TON_bit = 0;
     delay_ms(100);
     TON_bit = 1;
     while((no_pulse < total_pulse)&& (time_over == 0))
      {
        while((IR_IN == 1) && (time_over == 0) );
        TON_bit = 0;
        TON_bit = 1;
        TMR1 = 0;
        while((IR_IN == 0)&& (time_over ==0));
         irdata[no_pulse++] = (TMR1>>8)&0xff;
         irdata[no_pulse++] = TMR1&0xff;
        TON_bit = 0;
        TON_bit = 1;
        TMR1 = 0;
        while((IR_IN == 1)&& (time_over ==0));
         irdata[no_pulse++] = (TMR1>>8)&0xff;
         irdata[no_pulse++] = TMR1&0xff;
      }
       LED1 = 1;
       delay_ms(2000);

Also posted at:
https://stackoverflow.com/q/59354718

The ESP32 has a built in module called the PCNT Pulse Counter (PCNT) - ESP32 - — ESP-IDF Programming Guide latest documentation, Pulse Counter.