简体   繁体   English

PJSUA2-将通话音频录制到WAV文件

[英]PJSUA2 - Recording call audio to wav file

Using PJSUA2 I'm trying to record an incoming call directly to a wav file but can't hear any audio in the wav file. 我正在尝试使用PJSUA2将来电直接记录到wav文件中,但是听不到wav文件中的任何音频。

Code below: 代码如下:

void SipCall::onCallMediaState(pj::OnCallMediaStateParam& /*prm*/)
{
    pj::CallInfo call_info = getInfo();
    pj::AudioMedia* audio_media = 0;

    for (unsigned int i = 0; i < call_info.media.size(); ++i) {
        if (call_info.media[i].type == PJMEDIA_TYPE_AUDIO) {
            audio_media = static_cast<pj::AudioMedia*>(getMedia(i));
            break;
        }
    }

     if (audio_media != 0) {
            try {    
                pj::AudioMediaRecorder recorder;
                recorder.createRecorder("file.wav");
                audio_media->startTransmit(recorder);   
                pj_thread_sleep(5000);
                audio_media->stopTransmit(recorder);   
            }  
            catch (pj::Error& err) {
                qWarning("[SipAccount::onIncomingCall] : Failed to record call to %s. Error = %s", "file.wav", err.info().data());    
           }        
      }
 }

There's poor documentation on PJSUA2 so does anyone have any idea what I'm doing wrong? 关于PJSUA2的文档很差,所以有人知道我在做什么错吗?

There was a problem because the AudioMediaRecorder object went out of scope just after it was created. 存在一个问题,因为AudioMediaRecorder对象在创建后就超出了范围。 If you make it a member of the class the following will work. 如果您使其成为班级的成员,则将可以进行以下操作。

void SipCall::onCallMediaState(pj::OnCallMediaStateParam& /*prm*/)
{
   pj::CallInfo call_info = getInfo();
   pj::AudioMedia* audio_media = 0;
   for (unsigned int i = 0; i < call_info.media.size(); ++i) {
      if (call_info.media[i].type == PJMEDIA_TYPE_AUDIO) {
        audio_media = static_cast<pj::AudioMedia*>(getMedia(i));
        break;
      }
   }

   if (audio_media != 0) {
        try {    
            recorder.createRecorder("file.wav");
            audio_media->startTransmit(recorder);   
        }  
        catch (pj::Error& err) {
            qWarning("[SipAccount::onIncomingCall] : Failed to record call to %s. Error = %s", "file.wav", err.info().data());    
       }        
    }
 }

Do any necessary cleanup in the onCallState() method below: 在下面的onCallState()方法中进行任何必要的清理:

void SipCall::onCallState(pj::OnCallStateParam& prm)
{
    int i = prm.e.type;
    i = 0;

    pj::CallInfo call_info = getInfo();

    switch (call_info.state) {
       case PJSIP_INV_STATE_DISCONNECTED:
          // Add clean up code here
          delete this;        
          break;
       case PJSIP_INV_STATE_CONFIRMED:                        
          break;
       default:
          break;
   }
}

I had the same issue while calling PJ through JNI. 通过JNI调用PJ时,我遇到了同样的问题。 In my case I forgot to close the recorder. 就我而言,我忘了关闭录音机了。 If you look close in the documentation , it says that you can't playback the wav file unless you close the recorder. 如果您仔细阅读文档中的内容 ,它表示除非关闭记录器,否则无法播放wav文件。 So after you stop the transmission, do not forget to delete the recorder. 因此,停止传输后,请不要忘记删除录像机。

The thing you're doing wrong is waiting in onCallMediaState 's thread. 您做错了的事情是在onCallMediaState的线程中等待。 In order to process the call further this thread has to proceed running. 为了进一步处理该调用,该线程必须继续运行。 So, you create recorder, wait (while nothing happens, so you record nothing), close recorder, call continues. 因此,您创建了记录器,等待(什么都没发生,因此您什么也不记录),关闭记录器,继续通话。 So nothing has been recorded. 因此,没有任何记录。 It has nothing to do with scope, because in your case recorder had already done his job recording nothing when got out of scope. 它与范围无关,因为在您的情况下,记录仪在超出范围时已经不做任何记录。

Main thing you needed to do is indeed not to wait in onCallMediaState 's thread, let the call continue, and destroy the recorder when call is destroyed. 实际上,您需要做的主要事情是不要在onCallMediaState的线程中等待,让调用继续进行,并在销毁呼叫时销毁记录器。 For this to happen you need to get recorder declaration out of SipCall::onCallMediaState , of course or as you said recorder would get destroyed before it gets its job done. 为此,您当然需要从SipCall::onCallMediaState获取记录器声明,或者正如您所说的,记录器在完成其工作之前将被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM