2013-08-06

WebRTC C++ audio recording example

Views: 13160 | 1 Comment

The WebRTC project included a voice engine(GIPS, Global IP Solutions), here is an example to show how you can use the voice engine to record audio from microphone.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "webrtc/modules/audio_device/include/audio_device.h"
#include "webrtc/common_audio/resampler/include/resampler.h"
#include "webrtc/modules/audio_processing/aec/include/echo_cancellation.h"

class AudioTransportImpl: public webrtc::AudioTransport
{
private:
    
public:
    AudioTransportImpl(webrtc::AudioDeviceModule* audio){
    }
    
    ~AudioTransportImpl(){
    }

    virtual int32_t RecordedDataIsAvailable(
        const void* audioSamples,
        const uint32_t nSamples,
        const uint8_t nBytesPerSample,
        const uint8_t nChannels,
        const uint32_t samplesPerSec,
        const uint32_t totalDelayMS,
        const int32_t clockDrift,
        const uint32_t currentMicLevel,
        const bool keyPressed,
        uint32_t& newMicLevel)
    {
        printf("record %d %d %d %d %d %d %d %d\n", nSamples,
            nBytesPerSample, nChannels,
            samplesPerSec, totalDelayMS,
            clockDrift, currentMicLevel, keyPressed);
        // now you have the audioSamples...
        return 0;
    }
};


int main(int argc, char **argv){
    webrtc::AudioDeviceModule *audio;
    audio = webrtc::CreateAudioDeviceModule(0, webrtc::AudioDeviceModule::kPlatformDefaultAudio);
    assert(audio);
    audio->Init();
    
    int num;
    int ret;
    
    num = audio->RecordingDevices();
    printf("Input devices: %d\n", num);
    for(int i=0; i<num; i++){
        char name[webrtc::kAdmMaxDeviceNameSize];
        char guid[webrtc::kAdmMaxGuidSize];
        int ret = audio->RecordingDeviceName(i, name, guid);
        if(ret != -1){
            printf("    %d %s %s\n", i, name, guid);
        }
    }
    
    audio->SetRecordingDevice(0);
    audio->InitRecording();
    
    AudioTransportImpl callback(audio);
    audio->StartRecording();

    getchar();
}

For more webrtc demos, visit: https://github.com/ideawu/webrtc-demo

Posted by ideawu at 2013-08-06 23:08:32 Tags: ,

One Response to "WebRTC C++ audio recording example"

  • Thanks for this useful post! I am playing around with WebRTC using this sample: \trunk\talk\examples\peerconnection

    I have modified the server_test.html file so it streams audio data from the browser to the peerconnection_client application. Do you know how I can save the stream into a file at the receiving end for later processing? I know there are WebRTC C++ functions like StartRTPDump() or ConvertPCMToWAV() but I don’t know how to directly access the incoming RTP packet stream so I can’t use them. Many thanks. Reply

Leave a Comment