前面在树莓派上已经实现了视频硬件编码传输,但是声音却一直找不到好的方法,只能自己写代码实现。
程序说明:
1.alsa录音部分:
树莓派上使用的是usb的摄像头自带麦克,所以采集的声卡是“hw:1,0”, default未试。这里一定要注意的是采样频率,period_time,buffer_time等参数设置,因为rtp传输使用的是160的时间戳增量,必须采集后的size为320字节(g711编码后数据量减少一半,刚好为160字节)。
程序demo中有三种输出方式:1是保存pcm文件;2是保存g711文件;3是g711经rtp传输。
三种方式播放测试方法:网上都没有介绍的, 需安装sox sudo apt-get install sox
(1).播放pcm文件
play -t s16 -r 8000 -c 1 -e signed-integer dest.pcm
dest.pcm:
File Size: 4.81M Bit Rate: 128k
Encoding: Signed PCM
Channels: 1 @ 16-bit
Samplerate: 8000Hz
Replaygain: off
Duration: 00:05:00.42
(2).播放g711文件:
play -t raw -r 8000 -c 1 -e a-law test.g711
test.g711:
File Size: 2.40M Bit Rate: 64.0k
Encoding: A-law
Channels: 1 @ 13-bit
Samplerate: 8000Hz
Replaygain: off
Duration: 00:05:00.42
(3).播放rtp视频流
vlc播放sdp文件实现
m=audio 5100 RTP/AVP 0
a=rtpmap:0 pcma/8000/1
a=ptime:20
c=IN IP4 192.168.1.132
2.G711编码部分:
G711编码有a和u两种编码方式,在程序中已经实现了接口,直接调用就可以了。网上借鉴别人代码(github)。
3.RTP传输部分:
没有使用库文件,直接实现。使用160时间戳增量,rtp发送数据包大小为214字节。
程序demo代码如下:
1.alsa_record.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <netdb.h>
#include <time.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h> //close()
#include <alsa/asoundlib.h>
#include <math.h>
#include "g711codec.h"
#define DEST_PORT 5100
#define DEST_IP "192.168.1.132"
#define BUFFERSIZE 4096
#define PERIOD_SIZE 1024
#define PERIODS 2
#define SAMPLE_RATE 8000
#define CHANNELS 1
#define FSIZE 2*CHANNELS
/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_API
typedef struct
{
/** byte 0 */
unsigned char csrc_len:4; /** expect 0 */
unsigned char extension:1; /** expect 1, see RTP_OP below */
unsigned char padding:1; /** expect 0 */
unsigned char version:2; /** expect 2 */
/** byte 1 */
unsigned char payload:7; /** stream type */
unsigned char marker:1; /** when send the first framer,set it */
/** bytes 2, 3 */
unsigned short seq_no;
/** bytes 4-7 */
unsigned long timestamp;
/** bytes 8-11 */
unsigned long ssrc; /** stream number is used here. */
} RTP_FIXED_HEADER;
int main(int argc, char *argv[]) {
long loops; //define the record time.
int rc; //return code.
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
int err;
char *file;
int fd;
int fd2;
if(argc ==2)
{
file = argv[1];
}
else
{
file = "output.raw";
}
fd = open(file,O_WRONLY|O_CREAT,0777);
if( fd ==-1)
{
printf("open file:%s fail.\n",file);
exit(1);
}
fd2 = open("test.g711",O_WRONLY|O_CREAT,0777);
if( fd ==-1)
{
printf("open file:%s fail.\n",file);
exit(1);
}
/* Open PCM device for recording (capture). */
err = snd_pcm_open(&handle, "default",SND_PCM_STREAM_CAPTURE, 0);
if (err < 0) {
fprintf(stderr,"unable to open pcm device: %s\n",snd_strerror(err));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
err=snd_pcm_hw_params_any(handle, params);
if (err < 0) {
fprintf(stderr, "Can not configure this PCM device: %s\n",snd_strerror(err));
exit(1);
}
/* Set the desired hardware parameters. */
/* Interleaved mode */
err=snd_pcm_hw_params_set_access(handle, params,SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
fprintf(stderr, "Failed to set PCM device to interleaved: %s\n", snd_strerror(err));
exit(1);
}
/* Signed 16-bit little-endian format */
//err=snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_MU_LAW);
err=snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16_LE);
if (err < 0) {
fprintf(stderr,"Failed to set PCM device to 16-bit signed PCM: %s\n", snd_strerror(err));
exit(1);
}
/* One channels (mono) */
err=snd_pcm_hw_params_set_channels(handle, params, CHANNELS);
if (err < 0) {
fprintf(stderr, "Failed to set PCM device to mono: %s\n",snd_strerror(err));
exit(1);
}
/* 16000 bits/second sampling rate (CD quality) */
val = SAMPLE_RATE;//这里修改为8000
err=snd_pcm_hw_params_set_rate_near(handle, params,&val, &dir);
if (err < 0) {
fprintf(stderr, "Failed to set PCM device to sample rate =%d: %s\n",val,snd_strerror(err));
exit(1);
}
/* Set buffer time 500000. */
unsigned int buffer_time,period_time;
snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);
if ( buffer_time >500000)
buffer_time = 80000;//这里可修改
//period_time = buffer_time / 4;
period_time = buffer_time/4;//这里可修改 size = frames * FSIZE;
err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, 0);
if (err < 0) {
fprintf(stderr, "Failed to set PCM device to buffer time =%d: %s\n", buffer_time,snd_strerror(err));
exit(1);
}
err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, 0);
if (err < 0) {
fprintf(stderr, "Failed to set PCM device to period time =%d: %s\n",period_time,snd_strerror(err));
exit(1);
}
/* Write the parameters to the driver */
err = snd_pcm_hw_params(handle, params);
if (err < 0) {
fprintf(stderr,"unable to set hw parameters: %s\n",snd_strerror(err));
exit(1);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params,&frames, &dir);
size = frames * FSIZE; /* 2 bytes/sample, 1 channels *///这里应该=320 FSIZE=2 frames=160
buffer = (char *) malloc(size);
printf("read buffer size = %d\n",size);
printf("period size = %d frames\n", (int)frames);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,&val, &dir);
printf("period time is: %d\n",val);
/*print alsa config parameter*/
snd_pcm_hw_params_get_buffer_time(params, &val, &dir);
printf("buffer time = %d us\n", val);
snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val);
printf("buffer size = %d frames\n", val);
snd_pcm_hw_params_get_periods(params, &val, &dir);
printf("periods per buffer = %d frames\n", val);
//-----------------------------------rtp初始化部分--开始------------------------------------------------------
//int ret;
int M_bit;
M_bit = 1;
char sendbuf[1500];
memset(sendbuf,0,1500);
unsigned short seq_num = 0;
RTP_FIXED_HEADER *rtp_hdr;
int socket1;
struct sockaddr_in server;
int len = sizeof(server);
//float framerate = 25;
unsigned int timestamp_increse = 0,ts_current = 0;
timestamp_increse = 160;
server.sin_family = AF_INET;
server.sin_port = htons(DEST_PORT);
server.sin_addr.s_addr = inet_addr(DEST_IP);
socket1 = socket(AF_INET,SOCK_DGRAM,0);
connect(socket1, (struct sockaddr *)&server, len);
//----------------------------------------------rtp初始化部分 -----结束--------------------------------------------
loops = 1000;
while (1) {
loops--;
rc = snd_pcm_readi(handle, buffer, frames);
if (rc == -EPIPE) {
// EPIPE means overrun
fprintf(stderr, "overrun occurred\n");
err=snd_pcm_prepare(handle);
if( err <0){
fprintf(stderr, "Failed to recover form overrun : %s\n",
snd_strerror(err));
exit(1);
}
}
else if (rc < 0) {
fprintf(stderr,"error from read: %s\n",snd_strerror(rc));
exit(1);
}
else if (rc != (int)frames) {
fprintf(stderr, "short read, read %d frames\n", rc);
}
rc = write(fd, buffer, size);
if (rc <0){
perror("fail to write to audio file\n");
}
//#-------------------------------------------------------------------------------------------#
int Ret = -1;
unsigned char ucOutBuff[1024 + 1 ];
memset(ucOutBuff, 0, sizeof(ucOutBuff));
Ret = PCM2G711a( (char *)buffer, (char *)ucOutBuff, size, 0 );
//printf("***************g711 start*******************\n");
rc = PCM2G711a( (char *)buffer, (char *)&sendbuf[12], size, 0 );
printf("loops=%d,Ret=%d,size=%d\n",(int)loops,rc,size);
rtp_hdr =(RTP_FIXED_HEADER*)&sendbuf[0];
//设置RTP HEADER,
rtp_hdr->payload = 0; //负载类型号,
rtp_hdr->version = 2; //版本号,此版本固定为2
if(1 == M_bit)
{
rtp_hdr->marker = 1; //标志位,由具体协议规定其值。
M_bit = 0;
printf("M_bit = 1\n");
}
else
{
rtp_hdr->marker = 0; //标志位,由具体协议规定其值。
}
rtp_hdr->ssrc = htonl(10); //随机指定为10,并且在本RTP会话中全局唯一
rtp_hdr->seq_no = htons(seq_num ++);
//printf("sendbuf[1]=%x\n",sendbuf[1]);
ts_current = ts_current+timestamp_increse;
//printf("ts_current=%d\n",ts_current);
rtp_hdr->timestamp=htonl(ts_current);
//printf("calloc\n");
rc = send( socket1, sendbuf, rc+12, 0 );//发送rtp包
//printf("send:%d\n",rc);//=rc+12
if(rc<0)
{
perror("send");
break;
}
/**
printf("***************rtp*******************\n");
printf("ret = %d\n",rc);
printf("rtp_hdr->payload = %x\n",rtp_hdr->payload);
printf("rtp_hdr->version = %x\n",rtp_hdr->version);
printf("rtp_hdr->marker = %x\n",rtp_hdr->marker);
printf("rtp_hdr->ssrc = %x\n",(unsigned int)rtp_hdr->ssrc);
printf("rtp_hdr->seq_no = %x\n",rtp_hdr->seq_no);
printf("rtp_hdr->timestamp = %x\n",(unsigned int)rtp_hdr->timestamp);
printf("sendbuf[0]= %x\n",sendbuf[0]);
printf("sendbuf[12]= %x\n",sendbuf[12]);
printf("**********************************\n");
*/
//usleep(19000);
memset(sendbuf,0,1500);//清空sendbuf;此时会将上次的时间戳清空,因此需要ts_current来保存上次的时间戳值
//#-------------------------------------------------------------------------------------------#
Ret = write(fd2, &ucOutBuff, Ret);
if (Ret <0){
perror("fail to write to audio file\n");
}
}
close(fd);
//close(fd2);
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
return 0;
}
2.g711codec.h
/*
* G711 encode decode HEADER.
*/
#ifndef __G711CODEC_H__
#define __G711CODEC_H__
/*
* u-law, A-law and linear PCM conversions.
*/
#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
#define QUANT_MASK (0xf) /* Quantization field mask. */
#define NSEGS (8) /* Number of A-law segments. */
#define SEG_SHIFT (4) /* Left shift for segment number. */
#define SEG_MASK (0x70) /* Segment field mask. */
#define BIAS (0x84) /* Bias for linear code. */
int PCM2G711a( char *InAudioData, char *OutAudioData, int DataLen, int reserve );
int PCM2G711u( char *InAudioData, char *OutAudioData, int DataLen, int reserve );
int G711a2PCM( char *InAudioData, char *OutAudioData, int DataLen, int reserve );
int G711u2PCM( char *InAudioData, char *OutAudioData, int DataLen, int reserve );
int g711a_decode(short amp[], const unsigned char g711a_data[], int g711a_bytes);
int g711u_decode(short amp[], const unsigned char g711u_data[], int g711u_bytes);
int g711a_encode(unsigned char g711_data[], const short amp[], int len);
int g711u_encode(unsigned char g711_data[], const short amp[], int len);
#endif /* g711codec.h */
3.g711.c
#include <stdio.h>
#include "g711codec.h"
/*
* function: convert PCM audio format to g711 alaw/ulaw.(zqj)
* InAudioData: PCM data prepared for encoding to g711 alaw/ulaw.
* OutAudioData: encoded g711 alaw/ulaw.
* DataLen: PCM data size.
* reserve: reserved param, no use.
*/
/*alaw*/
int PCM2G711a( char *InAudioData, char *OutAudioData, int DataLen, int reserve )
{
//check params.
if( (NULL == InAudioData) && (NULL == OutAudioData) && (0 == DataLen) )
{
printf("Error, empty data or transmit failed, exit !\n");
return -1;
}
//printf("DataLen = %d, %s, %d\n", DataLen, __func__, __LINE__);
int Retaen = 0;
//printf("G711a encode start......\n");
Retaen = g711a_encode( (unsigned char *)OutAudioData, (short*)InAudioData, DataLen/2 );
//printf("Retaen = %d, %s, %d\n", Retaen, __func__, __LINE__);
return Retaen; //index successfully encoded data len.
}
/*ulaw*/
int PCM2G711u( char *InAudioData, char *OutAudioData, int DataLen, int reserve )
{
//check params.
if( (NULL == InAudioData) && (NULL == OutAudioData) && (0 == DataLen) )
{
printf("Error, empty data or transmit failed, exit !\n");
return -1;
}
printf("DataLen = %d, %s, %d\n", DataLen, __func__, __LINE__);
int Retuen = 0;
printf("G711u encode start......\n");
Retuen = g711u_encode( (unsigned char *)OutAudioData, (short*)InAudioData, DataLen/2 );
printf("Retuen = %d, %s, %d\n", Retuen, __func__, __LINE__);
return Retuen;
}
/*
* function: convert g711 alaw audio format to PCM.(zqj)
* InAudioData: g711 alaw data prepared for encoding to PCM.
* OutAudioData: encoded PCM audio data.
* DataLen: g711a data size.
* reserve: reserved param, no use.
*/
/*alaw*/
int G711a2PCM( char *InAudioData, char *OutAudioData, int DataLen, int reserve )
{
//check param.
if( (NULL == InAudioData) && (NULL == OutAudioData) && (0 == DataLen) )
{
printf("Error, empty data or transmit failed, exit !\n");
return -1;
}
printf("DataLen = %d, %s, %d\n", DataLen, __func__, __LINE__);
int Retade = 0;
printf("G711a decode start......\n");
Retade = g711a_decode( (short*)OutAudioData, (unsigned char *)InAudioData, DataLen );
printf("Retade = %d, %s, %d\n", Retade, __func__, __LINE__);
return Retade; //index successfully decoded data len.
}
/*ulaw*/
int G711u2PCM( char *InAudioData, char *OutAudioData, int DataLen, int reserve )
{
//check param.
if( (NULL == InAudioData) && (NULL == OutAudioData) && (0 == DataLen) )
{
printf("Error, empty data or transmit failed, exit !\n");
return -1;
}
printf("DataLen = %d, %s, %d\n", DataLen, __func__, __LINE__);
int Retude = 0;
printf("G711u decode start......\n");
Retude = g711u_decode( (short*)OutAudioData, (unsigned char *)InAudioData, DataLen );
printf("Retude = %d, %s, %d\n", Retude, __func__, __LINE__);
return Retude;
}
4.g711codec.c
#include "g711codec.h"
static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,
0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
static int search(int val, short *table, int size)
{
int i;
for (i = 0; i < size; i++) {
if (val <= *table++)
return (i);
}
return (size);
}
/*
* alaw2linear() - Convert an A-law value to 16-bit linear PCM
*
*/
static int alaw2linear( unsigned char a_val )
{
int t;
int seg;
a_val ^= 0x55;
t = (a_val & QUANT_MASK) << 4;
seg = ( (unsigned)a_val & SEG_MASK ) >> SEG_SHIFT;
switch (seg)
{
case 0:
t += 8;
break;
case 1:
t += 0x108;
break;
default:
t += 0x108;
t <<= seg - 1;
}
return ((a_val & SIGN_BIT) ? t : -t);
}
/*
* ulaw2linear() - Convert a u-law value to 16-bit linear PCM
*
* First, a biased linear code is derived from the code word. An unbiased
* output can then be obtained by subtracting 33 from the biased code.
*
* Note that this function expects to be passed the complement of the
* original code word. This is in keeping with ISDN conventions.
*/
static int ulaw2linear(unsigned char u_val)
{
int t;
/* Complement to obtain normal u-law value. */
u_val = ~u_val;
/*
* Extract and bias the quantization bits. Then
* shift up by the segment number and subtract out the bias.
*/
t = ((u_val & QUANT_MASK) << 3) + BIAS;
t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
}
/*
* linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
*
*/
unsigned char linear2alaw(int pcm_val) /* 2's complement (16-bit range) */
{
int mask;
int seg;
unsigned char aval;
if (pcm_val >= 0) {
mask = 0xD5; /* sign (7th) bit = 1 */
} else {
mask = 0x55; /* sign bit = 0 */
pcm_val = -pcm_val - 8;
}
/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_end, 8);
/* Combine the sign, segment, and quantization bits. */
if (seg >= 8) /* out of range, return maximum value. */
return (0x7F ^ mask);
else {
aval = seg << SEG_SHIFT;
if (seg < 2)
aval |= (pcm_val >> 4) & QUANT_MASK;
else
aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
return (aval ^ mask);
}
}
/*
* linear2ulaw() - Convert a linear PCM value to u-law
*
*/
unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */
{
int mask;
int seg;
unsigned char uval;
/* Get the sign and the magnitude of the value. */
if (pcm_val < 0) {
pcm_val = BIAS - pcm_val;
mask = 0x7F;
} else {
pcm_val += BIAS;
mask = 0xFF;
}
/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_end, 8);
/*
* Combine the sign, segment, quantization bits;
* and complement the code word.
*/
if (seg >= 8) /* out of range, return maximum value. */
return (0x7F ^ mask);
else {
uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
return (uval ^ mask);
}
}
int g711a_decode( short amp[], const unsigned char g711a_data[], int g711a_bytes )
{
int i;
int samples;
unsigned char code;
int sl;
for ( samples = i = 0; ; )
{
if (i >= g711a_bytes)
break;
code = g711a_data[i++];
sl = alaw2linear( code );
amp[samples++] = (short) sl;
}
return samples*2;
}
int g711u_decode(short amp[], const unsigned char g711u_data[], int g711u_bytes)
{
int i;
int samples;
unsigned char code;
int sl;
for (samples = i = 0;;)
{
if (i >= g711u_bytes)
break;
code = g711u_data[i++];
sl = ulaw2linear(code);
amp[samples++] = (short) sl;
}
return samples*2;
}
int g711a_encode(unsigned char g711_data[], const short amp[], int len)
{
int i;
for (i = 0; i < len; i++)
{
g711_data[i] = linear2alaw(amp[i]);
}
return len;
}
int g711u_encode(unsigned char g711_data[], const short amp[], int len)
{
int i;
for (i = 0; i < len; i++)
{
g711_data[i] = linear2ulaw(amp[i]);
}
return len;
}
5.makefile
CC=gcc
CCFLAGS=-g -Wall
LDFLAGS=-lasound
all:recordc play
recordc:alsa_record.c
$(CC) alsa_record.c g711.c g711codec.c $(CCFLAGS) $(LDFLAGS) -o recordc
play:alsa_play.c
$(CC) alsa_play.c $(CCFLAGS) $(LDFLAGS) -o play
clean:
rm recordc play output.raw test.g711
总结:1.程序还只是实现了声音采集+编码+rtp传输,能够配合python+exosip和pc端ekiga实现声音通话。
2.编码只实现了g711a和g711u两种编码方式,带宽需64kpbs。
3.程序中端口和ip地址是固定的,需修改源代码。
下一步:
1.将音频传输部分,整合到Camkit中,实现音频和视频同时传输。
2.有时间的话,将exosip改为c语音,python需调用视频和音频c语音部分。