AGI Record file too slow

Hi all,

I have the script below to record incoming calls but its taking like 4 to 5 seconds, which is too slow for me. Its on my laptop with 8GB RAM, Processor Core i5-5200U CPU @ 2.20GHZ X4, Ubuntu 18.04 64 bit.
I put php timestamp before and after the script to know how long it runs.
With all these specs i was expecting the recording to be faster, anything am doing wrong?
$agi->record_file("/var/www/asterisk/ai/speech/src/recordings/$unique_id-$caller_id",“raw”,’ ',-1,null,false,3); //Record call via Asterisk Server

If you turn up the logging, how long does it take from Asterisk receiving the RECORD FILE command to recording started? Any other delays are going to be in the OS, the PHP interpreter, and the class library (there are at least two different ones for PHP).

Whilst it surprises me that it takes so long, using AGI to do this is not going to be very efficient, compared with directly calling a dialplan application.

I’m not much of a PHP weenie, but if I read your argument list correctly, you specify no escape digits and a silence detection of 3 seconds to terminate the recording.

Is this correct and what you intend?

Do you want to record the call, or to record a message? RECORD FILE seems to be for the latter.

Hi David551 I actually want to record a call and not a message, what do you advise?
And this is the timestamp i get before and after this line of code runs: B4Record: 2020-05-15 01:19:10 AfRecord: 2020-05-15 01:19:20.
Actually i did couple of tests and that with this code it takes about 7 to 10 seconds for the recording and if i use escape digits and press after talking then it takes like 3 to 4 seconds

Also i tried this code below from the terminal and it took almost the same time

Yes sedwards thats what i want.
Actually i did couple of tests and that with this code it takes about 7 to 8 seconds for the recording and if i use escape digits and press after talking then it takes like 3 to 4 seconds

To record a call, you want MixMonitor (or Monitor), not Record.

Thanks @david551
the recording with Monitor is not working with the code below, may code is wrong. Any help how to use this in phpagi?

$agi->Monitor(“wav”,"/var/www/asterisk/ai/speech/src/recordings/$unique_id-$caller_id", true);

//Then do other things with the recorded file like convert it into text

Monitor (string $channel, [string $file = NULL], [string $format = NULL], [boolean $mix = NULL])

Thanks i have updated the code but its not working, not sure how to get the channel var.
$channel = $agi->get_variable($CHANNEL);
$file = “/var/www/asterisk/ai/speech/src/recordings/$unique_id-$caller_id”;
$agi->Monitor($channel,$file, “wav”, true);

//Then do other things with the rcorded file like convert into text

Try better the method agi_exec() to run the monitor app, is easier

Thanks for the support but its still not recording with the code below:
$mychannel = $agi->get_variable($CHANNEL);
$file = “/var/www/asterisk/ai/speech/src/recordings/$unique_id-$caller_id”;
$agi->exec(‘Monitor’,$mychannel,$file, “wav”, true);
$agi->exec(‘StopMonitor’, $mychannel);

The channel is passed automatically, as agi_channel. I don’t know how your, unnamed, class library handles that.

In any case, substituting an unset PHP variable and passing the result to GET VARIABLE is not going to do anything useful. Whilst I don’t know about your class library, there is no pass by name, so $CHANNEL will be substituted before the call.

I must admit I failed to find the Monitor action in the AGI documentation, but you still haven’t explained why you are using AGI, rather than dialplan.

You appear to be cancelling the recording as soon as you have started it, which doesn’t seem to make sense.

Hi and thanks again for your time.

The implementation is actually for a chatbot, so if a user calls, the question is recorded and then processed and then the answer/feedback played back to the caller.

The recording can be done in the Dialplan or AGI, it does not matter. I just need a way to record and save the wav file. Like if a user calls and ask a question, the call is recorded and if about 3 seconds of silence is detected then stop the recording because that the end of the question.

In that case, you are recording a message, not a call.

Wrong syntax, , exec method allows you to use an Asterisk app on your PHP code, the syntax for monitor app is Monitor(file_format:[urlbase],[fname_base,[options]]])

Using Mixmonitor

thanks for the rectification.

from david551’s recent comment, is like i have to go back to use record_file which i was using as indicated in my first post and it was very slow.

It’s not a matter of ‘have to’ it’s a matter of choosing the right tool for the job. Asterisk is a box of Lego’s – you get to choose the size and color of the blocks and arrange them as you see fit.

The ‘monitor’ application (and mixmonitor) start a recording. Your dialplan continues and your dialplan progresses. The recording continues until you explicitly stop it so ‘monitor’ is more useful to record a series of steps in your dialplan.

The ‘record’ application records until it ends by keystroke, timeout, or silence detection and then your dialplan continues. Since you said you want silence detection, that kind of makes the decision for you.

You also need to decide to write your application in ‘dialplan’ or as an AGI. They both have use cases. While I have a strong bias for writing applications as an AGI or a group of AGIs, I’d suggest you gain familiarity writing Asterisk applications in dialplan. One less technology to have to master at once.

Please try the following snippet:

        exten = s,1,                    verbose(1,[${EXTEN}@${CONTEXT}])
        same = n,                       record(/tmp/question.wav,3)
        same = n,                       verbose(The status returned by record() is ${RECORD_STATUS})
        same = n,                       playback(/tmp/question)
        same = n,                       hangup()

When I execute this, I immediately (not 3 or 4 seconds later) get a beep, I speak, I stop speaking for 3 seconds, and I hear the recording of me speaking. My console log (with 'core set verbose 3) looks like:

    -- Executing [s@record-test:1] Verbose("SIP/poly-77a1-00000006", "1,[s@record-test]") in new stack
    -- Executing [s@record-test:2] Record("SIP/poly-77a1-00000006", "/tmp/question.wav,3") in new stack
    -- <SIP/poly-77a1-00000006> Playing 'beep.gsm' (language 'en')
       > 0x74208070 -- Strict RTP learning complete - Locking on source address
    -- Executing [s@record-test:3] Verbose("SIP/poly-77a1-00000006", "The status returned by record() is SILENCE") in new stack
The status returned by record() is SILENCE
    -- Executing [s@record-test:4] Playback("SIP/poly-77a1-00000006", "/tmp/question") in new stack
    -- <SIP/poly-77a1-00000006> Playing '/tmp/question.slin' (language 'en')
    -- Executing [s@record-test:5] Hangup("SIP/poly-77a1-00000006", "") in new stack