Android手机通过rtp发送aac数据给vlc播放
截屏
AudioRecord音频采集
private val sampleRate = mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE) private val channelCount = mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT) private val minBufferSize = AudioRecord.getMinBufferSize(sampleRate, if (channelCount == 1) CHANNEL_IN_MONO else CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT); runInBackground { audioRecord = AudioRecord( MediaRecorder.AudioSource.MIC, sampleRate, if (channelCount == 1) CHANNEL_IN_MONO else CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, 2 * minBufferSize ) audioRecord.startRecording() }
音频采集时需要设置采集参数,设置的这些参数需要与创建MediaCodec时的参数一致。
- sampleRate是采样率:44100
- channelCount是通道数:1
- 单个采样数据大小格式:AudioFormat.ENCODING_PCM_16BIT
- 最小数据buffer:AudioRecord.getMinBufferSize()计算获取
override fun onInputBufferAvailable(codec: MediaCodec, index: Int) { try { codec.getInputBuffer(index)?.let { bb -> var startTime = System.currentTimeMillis(); var readSize = audioRecord.read(bb, bb.capacity()) log { "read time ${System.currentTimeMillis() - startTime} read size $readSize" } if (readSize < 0) { readSize = 0 } codec.queueInputBuffer(index, 0, readSize, System.nanoTime() / 1000, 0) } }catch (e:Exception){ e.printStackTrace() } }
这里采用的阻塞的方式采集数据,所以AudioRecord依据设置的采样频率生成数据的,我们可以直接把当前的时间设置为录制的时间戳。
MediaCodec编码音频数据
val mediaFormat = MediaFormat.createAudioFormat( MediaFormat.MIMETYPE_AUDIO_AAC, audioSampleRate, audioChannelCount ) mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, audioBitRate) mediaFormat.setInteger( MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC ) mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, audioMaxBufferSize)
为MediaCodec创建MediaFormat并设置参数,这里设置的音频参数必须与AudioRecord一致。
- MIME_TYPE:"audio/mp4a-latm"
- 采样频率与AudioRecord一致:44100
- 通道数与AudioRecord一致:1
- KEY_AAC_PROFILE配置为低带宽要求类型:AACObjectLC
- KEY_BIT_RATE设置的大小影响编码压缩率:128 * 1024
override fun onInputBufferAvailable(codec: MediaCodec, index: Int) { try { codec.getInputBuffer(index)?.let { bb -> var startTime = System.currentTimeMillis(); var readSize = audioRecord.read(bb, bb.capacity()) log { "read time ${System.currentTimeMillis() - startTime} read size $readSize" } if (readSize < 0) { readSize = 0 } codec.queueInputBuffer(index, 0, readSize, System.nanoTime() / 1000, 0) } }catch (e:Exception){ e.printStackTrace() } }
给MediaCodec传数据的时候设置的时间戳是当前的系统时间,由于我们使用rtp发送实时数据,所以flag不需要设置结束标志。
audioCodec = object : AudioEncodeCodec(mediaFormat) { override fun onOutputBufferAvailable( codec: MediaCodec, index: Int, info: MediaCodec.BufferInfo ) { try { val buffer = codec.getOutputBuffer(index) ?: return if (lastSendAudioTime == 0L) { lastSendAudioTime = info.presentationTimeUs; } val increase = (info.presentationTimeUs - lastSendAudioTime) * audioSampleRate / 1000 / 1000 if (hasAuHeader) { buffer.position(info.offset) buffer.get(bufferArray, 4, info.size) auHeaderLength.apply { bufferArray[0] = this[0] bufferArray[1] = this[1] } auHeader(info.size).apply { bufferArray[2] = this[0] bufferArray[3] = this[1] } audioRtpWrapper?.sendData(bufferArray, info.size + 4, 97, true, increase.toInt()) } else { buffer.position(info.offset) buffer.get(bufferArray, 0, info.size) audioRtpWrapper?.sendData(bufferArray, info.size, 97, true, increase.toInt()) } lastSendAudioTime = info.presentationTimeUs codec.releaseOutputBuffer(index, false) } catch (e: Exception) { e.printStackTrace() } } }
从MediaCodec读出的是aac原始的数据,我们可以根据具体的需求来决定是否添加au header发送。这里实现了有au header和没有 au header两种方案。没有au header的情况我们直接把MediaCode读出的数据通过rtp发送出去。有au header的情况我们需要在原始的aac数据前面追加4个字节的au header。是否有au header与vlc播放的sdp内容有关。后面会详解介绍sdp内容的设置。
private val auHeaderLength = ByteArray(2).apply { this[0] = 0 this[1] = 0x10 } private fun auHeader(len: Int): ByteArray { return ByteArray(2).apply { this[0] = (len and 0x1fe0 shr 5).toByte() this[1] = (len and 0x1f shl 3).toByte() } }
- au header length占用两个字节,它会描述au header的大小,这里设置为2.
- au header 占用两个字节,它描述了aac原始数据的大小,这里需要根据MediaCodec返回的aac原始数据大小进行设置。
Rtp发送数据
我们使用jrtplib库来发送数据,这里对库进行简单的封装并提供了java封装类RtpWrapper。
public class RtpWrapper { private long nativeObject = 0; private IDataCallback callback; public RtpWrapper() { init(); } @Override protected void finalize() throws Throwable { release(); super.finalize(); } public void setCallback(IDataCallback callback) { this.callback = callback; } void receivedData(byte[] buffer, int len) { if(this.callback != null) this.callback.onReceivedData(buffer, len); } public interface IDataCallback { void onReceivedData(byte[] buffer, int len); } static { try { System.loadLibrary("rtp-lib"); initLib(); } catch (Throwable e) { e.printStackTrace(); } } private native static void initLib(); private native boolean init(); private native boolean release(); public native boolean open(int port, int payloadType, int sampleRate); public native boolean close(); /** * @param ip "192.168.1.1" * @return */ public native boolean addDestinationIp(String ip); public native int sendData(byte[] buffer, int len, int payloadType, boolean mark, int increase); }
open方法要指定发送数据使用的端口,payloadType设置载体类型,sampleRate是采样率。
addDestinationIp用于添加接收端ip地址,地址格式: "192.168.1.1"。
sendData方法用于发送数据,increase是时间间隔,时间单位是 sampleRate/秒
override fun onOutputFormatChanged(codec: MediaCodec, format: MediaFormat) { audioRtpWrapper = RtpWrapper() audioRtpWrapper?.open(audioRtpPort, audioPayloadType, audioSampleRate) audioRtpWrapper?.addDestinationIp(ip) }
MediaCodec返回format的时候创建rtp连接并指定目的地址。
try { val buffer = codec.getOutputBuffer(index) ?: return if (lastSendAudioTime == 0L) { lastSendAudioTime = info.presentationTimeUs; } val increase = (info.presentationTimeUs - lastSendAudioTime) * audioSampleRate / 1000 / 1000 if (hasAuHeader) { buffer.position(info.offset) buffer.get(bufferArray, 4, info.size) auHeaderLength.apply { bufferArray[0] = this[0] bufferArray[1] = this[1] } auHeader(info.size).apply { bufferArray[2] = this[0] bufferArray[3] = this[1] } audioRtpWrapper?.sendData(bufferArray, info.size + 4, 97, true, increase.toInt()) } else { buffer.position(info.offset) buffer.get(bufferArray, 0, info.size) audioRtpWrapper?.sendData(bufferArray, info.size, 97, true, increase.toInt()) } lastSendAudioTime = info.presentationTimeUs codec.releaseOutputBuffer(index, false) } catch (e: Exception) { e.printStackTrace() }
发送数据的时候需要指定payloadType,距离上次发送数据的时间间隔等信息。
(info.presentationTimeUs - lastSendAudioTime)计算的是以微妙为单位的时间间隔。
(info.presentationTimeUs - lastSendAudioTime) * audioSampleRate / 1000 / 1000转换成sampleRate/秒为单位的时间间隔。
rtp发送aac数据使用的payloadType为97。
SDP文件配置
vlc播放器播放rtp音频数据时需要指定sdp文件,它通过读取sdp文件中的信息可以了解rpt接收端口、payloadType类型、音频的格式等信息用于接收数据流并解码播放。这里有两种配置方式用于支持有au header和没有au header的情况。
- 有au header
m=audio 40020 RTP/AVP 97 a=rtpmap:97 mpeg4-generic/44100/1 a=fmtp: 97 streamtype=5;config=1208;sizeLength=13; indexLength=3
- 没有au header
m=audio 40020 RTP/AVP 97 a=rtpmap:97 mpeg4-generic/44100/1 a=fmtp: 97 streamtype=5;config=1208
sdp文件配置了端口号为40020, Rtp payload type为97,音频的采样率为44100、通道数为1。
config是用于描述音频信息的,它的生成方式如下:
private val audioChannelCount = 1; private val audioProfile = 2 /** * 97000, 88200, 64000, 48000,44100, 32000, 24000, 22050,16000, 12000, 11025, 8000,7350, 0, 0, 0 */ private val audioIndex = 4 private val audioSpecificConfig = ByteArray(2).apply { this[0] = ((audioProfile).shl(3).and(0xff)).or(audioIndex.ushr(1).and(0xff)).toByte() this[1] = ((audioIndex.shl(7).and(0xff)).or(audioChannelCount.shl(3).and(0xff))).toByte() }
audioProfile 与设置给MediaCodec的Profile一致,这里设置的是AACObjectLC(2),所以audioProfile是2。
audioChannelCount通道数是1 。
audioIndex通过注释的采样率表格找到对应44100采样率的index为4 。
比较有au header和没有au header的两个版本,发现它们的区别在于是否配置了sizeLength和indexLength。
我这里的au header是两个字节的,sizeLength为13代表占用了13bit,indexLength为3代表占用3bit。配合发送数据时添加au header的代码就容易理解了。
private fun auHeader(len: Int): ByteArray { return ByteArray(2).apply { this[0] = (len and 0x1fe0 shr 5).toByte() this[1] = (len and 0x1f shl 3).toByte() } }
vlc测试播放
- vlc打开工程目录下的play_audio.sdp/play_audio_auheader.sdp 。
- 启动Android应用指定运行vlc的电脑的ip地址。
- 开始录制,如何vlc打开的是play_audio_auheader.sdp,那么在开始录制前需要选中auHeader check box
总结
- AudioRecord的设置信息与MediaCodec的配置信息必须一致。
- AudioRecord采用block的方式读取数据,这样我们可以直接使用系统时间来配置encode时间戳。
- 是否需要添加au header与sdp配置有关,vlc播放器会按照sdp配置解析au header。
- sdp中的config需要按照实际的音频配置信息计算得出,否则不能正常播放。
工程git地址
- 天翼云全场景业务无缝替换至国产原生操作系统CTyunOS!
- 以羊了个羊为例,浅谈小程序抓包与响应报文修改
- 这几种常见的 JVM 调优场景,你知道吗?
- 如此狂妄,自称高性能队列的Disruptor有啥来头?
- 为什么要学习GoF设计模式?
- 827. 最大人工岛 : 简单「并查集 枚举」运用题
- 手把手教你如何使用 Timestream 实现物联网时序数据存储和分析
- 850. 矩形面积 II : 扫描线模板题
- Java 并发编程解析 | 基于JDK源码解析Java领域中的并发锁,我们可以从中学习到什么内容?
- 【手把手】光说不练假把式,这篇全链路压测实践探索
- 大厂钟爱的全链路压测有什么意义?四种压测方案详细对比分析
- 写个续集,填坑来了!关于“Thread.sleep(0)这一行‘看似无用’的代码”里面留下的坑。
- 857. 雇佣 K 名工人的最低成本 : 枚举 优先队列(堆)运用题
- Vue3 实现一个自定义toast(小弹窗)
- 669. 修剪二叉搜索树 : 常规树的遍历与二叉树性质
- 读完 RocketMQ 源码,我学会了如何优雅的创建线程
- 性能调优——小小的log大大的坑
- 1582. 二进制矩阵中的特殊位置 : 简单模拟题
- elementui源码学习之仿写一个el-switch
- 646. 最长数对链 : 常规贪心 DP 运用题