客户端用HTML5的WebRTC, 服务器端用C# Socket实现的网页录音平台

franky9901 2013-10-04 09:26:27
最近研究WebSocket已经实现了用C#的Socket作为服务器端的双工互动。不过只是文字信息的聊天室功能,不是很过瘾。于是开始琢磨语音互动。目前已经实现了语音发送和接收,只是服务器端拿到语音数据后,生成的wav文件听起来断断续续的,不知道怎么回事。两边的缓冲区大小和取样时间间隔都调整过,始终不理想。请熟悉WebRTC、C#、Java的朋友帮忙看看:
【客户端】
function AudioRecordClicked() {
rec.record();
ws.send("start");
intervalKey = setInterval(
function () {
rec.exportWAV(function (blob) {
rec.clear();
ws.send(blob);
});
}, 2500);
};

function AudioExportClicked() {
rec.stop();
ws.send("stop");
clearInterval(intervalKey);
$("#message").text("");
}
【recorder.js 这个大家搜索一下就能找到】
(function (window) {

var WORKER_PATH = 'js/recorderWorker.js';

var Recorder = function (source, cfg) {
var config = cfg || {};
var bufferLen = config.bufferLen || 4096;
this.context = source.context;
this.node = this.context.createJavaScriptNode(bufferLen, 2, 2);
var worker = new Worker(config.workerPath || WORKER_PATH);
worker.postMessage({
command: 'init',
config: {
sampleRate: this.context.sampleRate
}
});
【服务器端一】
int count = 0;
private static System.IO.FileStream ss = new System.IO.FileStream(@"D:\ttt.wav", System.IO.FileMode.Create);
System.IO.BinaryWriter mWriter = new System.IO.BinaryWriter(ss);

private void OnReceiveMessage(IAsyncResult result)
{
if (this.clientSocket.Connected)
{
string msgReceived = string.Empty;
byte[] dataBytes = null;
FramePackage msgPackage = new FramePackage(this.bufClientReceive);

try
{
msgReceived = msgPackage.Content;
dataBytes = msgPackage.ContentBytes;

if (count > 0 && dataBytes != null)
{
mWriter.Write(dataBytes);
if (this.OnReceived != null)
{
this.InvokeAddLogDelegate("BBB");
this.OnReceived(this, "BBB", EventArgs.Empty);
}
Array.Clear(this.bufClientReceive, 0, this.bufClientReceive.Length);
this.clientSocket.BeginReceive(
this.bufClientReceive, 0, this.bufClientReceive.Length, 0,
new AsyncCallback(this.OnReceiveMessage), this.clientSocket.Available);

if (msgReceived.Contains("stop"))
{
count = 0;
mWriter.Close();
ss.Close();
mWriter = null;
ss = null;
}
}
else
{
if (msgReceived.Contains("start"))
{
count = 1;
}
if (this.OnReceived != null)
{
this.InvokeAddLogDelegate("Received Message Other");
this.OnReceived(this, "Received Message Other", EventArgs.Empty);
}
Array.Clear(this.bufClientReceive, 0, this.bufClientReceive.Length);
this.clientSocket.BeginReceive(
this.bufClientReceive, 0, this.bufClientReceive.Length, 0,
new AsyncCallback(this.OnReceiveMessage), this.clientSocket.Available);
}
}
catch (Exception ex)
{
this.InvokeAddLogDelegate("---- " + ex.Message + " -----");
if (this.OnLost != null)
{
this.OnLost(this, EventArgs.Empty);
}
}
}
}
【服务器端二】
public FramePackage(byte[] bufMsg)
{
// Frame Header
this.msgHeader = new FramePackageHeader(bufMsg);

// Extend Length
if (this.msgHeader.Length == 126)
{
this.arrExtend = new byte[2];
Buffer.BlockCopy(bufMsg, 2, this.arrExtend, 0, 2);
}
else if (this.msgHeader.Length == 127)
{
this.arrExtend = new byte[8];
Buffer.BlockCopy(bufMsg, 2, this.arrExtend, 0, 8);
}

if (this.msgHeader.HasMask)
{
this.arrMask = new byte[4];
Buffer.BlockCopy(bufMsg, this.arrExtend.Length + 2, this.arrMask, 0, 4);
}

// Message Content
if (this.arrExtend.Length == 0)
{
this.arrContent = new byte[this.msgHeader.Length];
Buffer.BlockCopy(bufMsg, this.arrExtend.Length + this.arrMask.Length + 2, this.arrContent, 0, this.arrContent.Length);
}
else if (arrExtend.Length == 2)
{
int lenContent = int.Parse(this.arrExtend[0].ToString()) * 256 + int.Parse(this.arrExtend[1].ToString());
this.arrContent = new byte[lenContent];
Buffer.BlockCopy(
bufMsg, this.arrExtend.Length + this.arrMask.Length + 2, this.arrContent, 0, lenContent > (1024 * 100 - 14) ? (1024 * 100 - 14) : lenContent);
}
else
{
ulong lenContent = 0;
ulong times = 1;
for (int indx = 7; indx >= 0; indx--)
{
lenContent += ulong.Parse(this.arrExtend[indx].ToString()) * times;
times *= 256;
}

int lenTrim = 0;
if (lenContent >= ulong.Parse((1024 * 100).ToString()))
{
lenTrim = 1024 * 100;
}
else
{
lenTrim = int.Parse(lenContent.ToString());
}
this.arrContent = new byte[lenTrim];
int lenHeader = this.arrExtend.Length + this.arrMask.Length + 2;
Buffer.BlockCopy(bufMsg, lenHeader, this.arrContent, 0, lenTrim > (1024 * 100 - lenHeader) ? (1024 * 100 - lenHeader) : lenTrim);
}

if (this.msgHeader.HasMask)
{
this.arrContent = this.GetMaskedBytes(this.arrContent, this.arrMask);
}
}
...全文
903 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
你那速度放缓 2015-06-14
  • 打赏
  • 举报
回复
有代码吗,可以发分看看吗?
溪凤逸晨 2014-04-18
  • 打赏
  • 举报
回复
问题解决了没有 啊
main_xtgjfge 2013-12-12
  • 打赏
  • 举报
回复
楼主,我录制的一点音也没有,求助,,QQ:249982535 谢谢
HMHML 2013-11-26
  • 打赏
  • 举报
回复
研究研究看看!
FantasyTC 2013-10-28
  • 打赏
  • 举报
回复
我倒没出现你这种情况,不是WAV太大,上传太慢,在研究如何解决!

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧