仅在PJSIP中传入录音

时间:2013-07-03 14:15:18

标签: iphone voip pjsip

现在我能够在pjsip中录制我自己的声音或说出来(通过麦克风),但我只需要录制通过扬声器传输的传入音频。这是我以.wav格式记录传出语音的代码。 请帮我完成这个任务。感谢Adv。

-(int)startRecordAudio 
{
    pj_caching_pool cp;
    pjmedia_endpt *med_endpt;
    pj_pool_t *pool;
    pjmedia_port *file_port;
    pjmedia_snd_port *snd_port;
    char tmp[10];

    pj_status_t status;

    /* Must init PJLIB first: */
    status = pj_init();
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);

    /* Must create a pool factory before we can allocate any memory. */
    pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
    status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);

    /* Create memory pool for our file player */
    pool = pj_pool_create(&cp.factory,    /* pool factory       */
                          "app",          /* pool name.     */
                          4000,           /* init size      */
                          4000,           /* increment size     */
                          NULL            /* callback on error    */
                          );

    /* Create WAVE file writer port. */
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES) objectAtIndex:0];
    NSString* foofile = [documentsPath stringByAppendingPathComponent:@"output.wav"];

    status = pjmedia_wav_writer_port_create(pool,
                                            [foofile cStringUsingEncoding:NSUTF8StringEncoding],
                                            CLOCK_RATE,
                                            NCHANNELS,
                                            SAMPLES_PER_FRAME,
                                            BITS_PER_SAMPLE,
                                            0, 0,
                                            &file_port);

    if (status != PJ_SUCCESS) {
        NSLog(@"Unable to write file");
        return 1;
    }

    /* Create sound player port. */
    status = pjmedia_snd_port_create_rec(pool,       /* pool         */
                                         -1,         /* use default dev.        */
                                         PJMEDIA_PIA_SRATE(&file_port->info),/* clock rate.     */
                                         PJMEDIA_PIA_CCNT(&file_port->info),/* # of channels.       */
                                         PJMEDIA_PIA_SPF(&file_port->info), /* samples per frame.   */
                                         PJMEDIA_PIA_BITS(&file_port->info),/* bits per sample.     */
                                         0,      /* options      */
                                         &snd_port       /* returned port       */
                                         );

    if (status != PJ_SUCCESS) {
      //  app_perror(THIS_FILE, "Unable to open sound device", status);
        return 1;
    }

    /* Connect file port to the sound player.
     * Stream playing will commence immediately.
     */

    status = pjmedia_snd_port_connect( snd_port, file_port);
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);

    /* Sleep to allow log messages to flush */
    pj_thread_sleep(10);

    if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
        puts("EOF while reading stdin, will quit now..");
    }

    return 0;
} 

1 个答案:

答案 0 :(得分:0)

你不需要做太多。 在呼叫媒体状态检查呼叫激活和连接以下是在Java但函数调用完全相同。

[ a => a, b => b, ... ]