基于Webrtc的视频通话录制功能-Android实现


webrtc本身并不支持视频通话的录制。

webrtc的sdk只是暴露了视频数据给开发者,而音频数据并没有向外暴露。

因此如果要进行视频通话的录制,需要修改webrtc的sdk,将音频数据暴露出来。

webrtc的下载和编译可参考之前的文章:WebRtc下载和编译

一、修改SDK,引出音频数据

1、音频采集数据的提取(mic输入,本地声音)

webrtc的音频数据的采集在audio_device_java.jar包中,具体类为WebRtcAudioRecord,其对应源码中的位置为:

src/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java

a) 在该类中添加如下代码:

// new add begin
private static WebRtcAudioRecordCallback mWebRtcAudioRecordCallback;
public static void setWebRtcAudioRecordCallback(WebRtcAudioRecordCallback callback) {
    Logging.d("WebRtcAudioRecord", "Set record callback");
    mWebRtcAudioRecordCallback = callback;
}
public interface WebRtcAudioRecordCallback {
    void onWebRtcAudioRecordInit(int audioSource, int audioFormat,
             int sampleRate, int channels, int bitPerSample, 
             int bufferPerSecond, int bufferSizeInBytes);
    void onWebRtcAudioRecordStart();
    void onWebRtcAudioRecording(ByteBuffer buffer, int bufferSize, 
             boolean microphoneMute);
    void onWebRtcAudioRecordStop();
}
// new add end

b) 在initRecording方法中添加如下代码:

private int initRecording(int sampleRate, int channels) {
    ......
    logMainParameters();
    logMainParametersExtended();
    // new add begin
    if (mWebRtcAudioRecordCallback != null) {
        mWebRtcAudioRecordCallback
             .onWebRtcAudioRecordInit(audioSource, 2, sampleRate, 
                         channels, 16, 100, bufferSizeInBytes);
    }
    // new add end
    return framesPerBuffer;
}

c) 在startRecording方法中添加如下代码:

private boolean startRecording() {
    ......
    try {
        this.audioRecord.startRecording();
        // new add begin
        if (mWebRtcAudioRecordCallback != null) {
            mWebRtcAudioRecordCallback.onWebRtcAudioRecordStart();
        }
        // new add end
    } 
    ......
}

d) 在stopRecording方法中添加如下代码:

private boolean stopRecording() {
    ......
    this.releaseAudioResources();
    // new add begin
    if(mWebRtcAudioRecordCallback != null) {
        mWebRtcAudioRecordCallback.onWebRtcAudioRecordStop();
    }
    // new add end
    return true;
}

e) 在内部类AudioRecordThread的run方法中添加如下代码:

@Override
public void run() {
    ......
    while (keepAlive) {
        int bytesRead = audioRecord.read(byteBuffer, byteBuffer.capacity());
        if (bytesRead == byteBuffer.capacity()) {
            ......
            if (keepAlive) {
                nativeDataIsRecorded(bytesRead, nativeAudioRecord);
                // new add begin
                if (mWebRtcAudioRecordCallback != null) {
                    mWebRtcAudioRecordCallback.onWebRtcAudioRecording(byteBuffer, 
                                                 bytesRead, microphoneMute);
                }
                // new add end
            }
        } else {
            ......
        }
        ......
    }
}

2、音频网络数据的提取(网络传入,对方声音)

webrtc的音频网络数据的提取在audio_device_java.jar包中,具体类为WebRtcAudioTrack,其对应源码中的位置为:

src/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java

a) 在该类中添加如下代码:

// new add begin
private static WebRtcAudioTrackCallback mWebRtcAudioTrackCallback;;
public static void setWebRtcAudioTrackCallback(WebRtcAudioTrackCallback callback) {
    Logging.d("WebRtcAudioTrack", "Set track callback");
    mWebRtcAudioTrackCallback = callback;
}
public interface WebRtcAudioTrackCallback {
    void onWebRtcAudioTrackInit(int audioFormat, int sampleRate,
                 int channels, int bitPerSample,
                 int bufferPerSecond, int minBufferSizeInBytes);
    void onWebRtcAudioTrackStart();
    void onWebRtcAudioTracking(ByteBuffer byteBuffer, int bytesWritten, 
                 boolean speakerMute);
    void onWebRtcAudioTrackStop();
}
// new add end

b) 在initPlayout方法中添加如下代码:

private boolean initPlayout(int sampleRate, int channels) {
    ......
    logMainParameters();
    logMainParametersExtended();
    // new add begin
    if (mWebRtcAudioTrackCallback != null) {
        mWebRtcAudioTrackCallback.onWebRtcAudioTrackInit(2, sampleRate, channels,
                                    16, 100, minBufferSizeInBytes);
    }
    // new add end
    return true;
}

c) 在startPlayout方法中添加如下代码:

private boolean startPlayout() {
    ......
    try {
        audioTrack.play();
        // new add begin
        if (mWebRtcAudioTrackCallback != null) {
            mWebRtcAudioTrackCallback.onWebRtcAudioTrackStart();
        }
        // new add end
    }
    ......
}

d) 在stopPlayout方法中添加如下代码:

private boolean stopPlayout() {
    ......
    // new add begin
    if (mWebRtcAudioTrackCallback != null) {
          mWebRtcAudioTrackCallback.onWebRtcAudioTrackStop();
    }
    // new add end
    return true;
}

e) 在内部类AudioTrackThread的run方法中添加如下代码:

@Override
public void run() {
    ......
    while (keepAlive) {
        ......
        // new add begin
        else if (mWebRtcAudioTrackCallback != null) {
            mWebRtcAudioTrackCallback
                  .onWebRtcAudioTracking(byteBuffer, bytesWritten, speakerMute);
        }
        // new add end
        byteBuffer.rewind();
    }
    ......
}

3、编译webrtc,复制audio_device_java.jar包到视频通话的项目的libs中

编译生成的audio_device_java.jar包位于:

src/out/Debug/lib.java/modules/audio_device/audio_device_java.jar

二、使用修改后SDK,实现视频通话录制功能

本demo基于WebRtc的android示例APP。该示例APP的源码路径:

src/examples/androidapp

本demo完整的代码在github中:

https://github.com/thfhongfeng/PineAppRtc

视频通话录制的主要实现类为MediaRecordController

1、实现方式

a) 视频数据采集使用MediaProjection+VirtualDisplay

b) 双路音频的混音使用平均算法

c) 使用MediaCodec编码,使用MediaMuxer封装成MP4文件

2、 录制流程

录制流程

三、搭建WebRtc服务器,完成端对端的WebRtc功能

1、 服务器搭建

参考之前的文章:搭建WebRtc服务器

2、修改App的房间服务器地址

(请读者使用自己搭建的服务器的地址进行设置,示例中的地址为作者本地搭建的服务器地址,外网是无法访问的):
两种方式:

a) 修改App源码中的房间服务器的默认地址为自己搭建的房间服务器的地址,修改文件pine_rtc\src\main\res\values\strings.xml:

<string translatable="false" name="pref_room_server_url_default">http://10.10.29.56:7000</string>

修改文件pine_rtc\src\main\AndroidManifest.xml:

<activity android:name=".org.component.ConnectActivity" 
          android:label="@string/app_name"
          android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="https" android:host="10.10.29.56"/>
        <data android:scheme="http" android:host="10.10.29.56"/>
    </intent-filter>
</activity>

重新编译Demo App

b) 不修改源码,启动App后直接在设置中修改房间服务器地址为自己搭建的房间服务器的地址。

原文转载自:https://www.jianshu.com/p/7746c7411826

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值