websocket-sharp/Example1/AudioStreamer.cs

224 lines
5.8 KiB
C#
Raw Normal View History

2012-07-31 09:36:52 +08:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using WebSocketSharp;
2012-08-06 13:34:39 +08:00
namespace Example1
2012-07-31 09:36:52 +08:00
{
2014-03-12 19:23:37 +08:00
internal class AudioStreamer : IDisposable
2012-07-31 09:36:52 +08:00
{
private Dictionary<uint, Queue> _audioBox;
2014-03-12 19:23:37 +08:00
private uint? _id;
private string _name;
private Notifier _notifier;
2016-07-26 15:52:42 +08:00
private Timer _timer;
2014-03-12 19:23:37 +08:00
private WebSocket _websocket;
2012-07-31 09:36:52 +08:00
2014-03-12 19:23:37 +08:00
public AudioStreamer (string url)
2012-07-31 09:36:52 +08:00
{
2014-03-12 19:23:37 +08:00
_websocket = new WebSocket (url);
_audioBox = new Dictionary<uint, Queue> ();
_id = null;
_notifier = new Notifier ();
2016-07-26 15:52:42 +08:00
_timer = new Timer (sendHeartbeat, null, -1, -1);
2012-07-31 09:36:52 +08:00
2014-03-12 19:23:37 +08:00
configure ();
2012-07-31 09:36:52 +08:00
}
2014-03-12 19:23:37 +08:00
private void configure ()
{
#if DEBUG
_websocket.Log.Level = LogLevel.Trace;
#endif
_websocket.OnOpen += (sender, e) =>
2016-07-26 15:52:42 +08:00
_websocket.Send (createTextMessage ("connection", String.Empty));
2014-03-12 19:23:37 +08:00
_websocket.OnMessage += (sender, e) => {
2016-07-26 15:52:42 +08:00
if (e.IsText) {
2016-07-28 14:31:04 +08:00
_notifier.Notify (processTextMessage (e.Data));
2014-03-12 19:23:37 +08:00
return;
2016-07-26 15:52:42 +08:00
}
if (e.IsBinary) {
2016-07-28 14:45:28 +08:00
processBinaryMessage (e.RawData);
2014-03-12 19:23:37 +08:00
return;
2012-07-31 09:36:52 +08:00
}
2016-07-26 15:52:42 +08:00
};
2012-07-31 09:36:52 +08:00
2014-03-12 19:23:37 +08:00
_websocket.OnError += (sender, e) =>
2016-07-26 15:52:42 +08:00
_notifier.Notify (
new NotificationMessage {
Summary = "AudioStreamer (error)",
Body = e.Message,
Icon = "notification-message-im"
}
);
2014-03-12 19:23:37 +08:00
_websocket.OnClose += (sender, e) =>
2016-07-26 15:52:42 +08:00
_notifier.Notify (
new NotificationMessage {
Summary = "AudioStreamer (disconnect)",
Body = String.Format ("code: {0} reason: {1}", e.Code, e.Reason),
Icon = "notification-message-im"
}
);
2012-07-31 09:36:52 +08:00
}
2016-07-29 15:01:49 +08:00
private static AudioMessage convertToAudioMessage (byte[] binaryMessage)
2014-10-02 15:16:15 +08:00
{
2016-07-26 15:52:42 +08:00
var id = binaryMessage.SubArray (0, 4).To<uint> (ByteOrder.Big);
var chNum = binaryMessage.SubArray (4, 1)[0];
var buffLen = binaryMessage.SubArray (5, 4).To<uint> (ByteOrder.Big);
2014-10-02 15:16:15 +08:00
var buffArr = new float[chNum, buffLen];
var offset = 9;
((int) chNum).Times (
2016-07-26 15:52:42 +08:00
i =>
buffLen.Times (
j => {
buffArr[i, j] = binaryMessage.SubArray (offset, 4).To<float> (ByteOrder.Big);
offset += 4;
}
)
);
2014-10-02 15:16:15 +08:00
return new AudioMessage {
2016-07-26 15:52:42 +08:00
user_id = id,
ch_num = chNum,
buffer_length = buffLen,
buffer_array = buffArr
};
2014-10-02 15:16:15 +08:00
}
2016-07-28 14:31:04 +08:00
private byte[] createBinaryMessage (float[,] bufferArray)
2014-10-02 15:16:15 +08:00
{
2016-07-28 14:31:04 +08:00
var msg = new List<byte> ();
var id = (uint) _id;
var chNum = bufferArray.GetLength (0);
var buffLen = bufferArray.GetLength (1);
msg.AddRange (id.ToByteArray (ByteOrder.Big));
msg.Add ((byte) chNum);
msg.AddRange (((uint) buffLen).ToByteArray (ByteOrder.Big));
chNum.Times (
i =>
buffLen.Times (
j => msg.AddRange (bufferArray[i, j].ToByteArray (ByteOrder.Big))
)
);
return msg.ToArray ();
}
private string createTextMessage (string type, string message)
{
return JsonConvert.SerializeObject (
new TextMessage {
user_id = _id,
name = _name,
type = type,
message = message
}
);
}
2016-07-28 14:45:28 +08:00
private void processBinaryMessage (byte[] data)
{
var msg = convertToAudioMessage (data);
if (msg.user_id == _id)
return;
Queue queue;
if (_audioBox.TryGetValue (msg.user_id, out queue)) {
queue.Enqueue (msg.buffer_array);
return;
}
queue = Queue.Synchronized (new Queue ());
queue.Enqueue (msg.buffer_array);
_audioBox.Add (msg.user_id, queue);
}
2016-07-28 14:31:04 +08:00
private NotificationMessage processTextMessage (string data)
{
var json = JObject.Parse (data);
2014-10-02 15:16:15 +08:00
var id = (uint) json["user_id"];
var name = (string) json["name"];
var type = (string) json["type"];
string body;
if (type == "message") {
body = String.Format ("{0}: {1}", name, (string) json["message"]);
}
else if (type == "start_music") {
body = String.Format ("{0}: Started playing music!", name);
}
else if (type == "connection") {
var users = (JArray) json["message"];
var buff = new StringBuilder ("Now keeping connections:");
2016-07-26 15:52:42 +08:00
foreach (JToken user in users) {
2014-10-02 15:16:15 +08:00
buff.AppendFormat (
2016-07-26 15:52:42 +08:00
"\n- user_id: {0} name: {1}", (uint) user["user_id"], (string) user["name"]
);
}
2014-10-02 15:16:15 +08:00
body = buff.ToString ();
}
else if (type == "connected") {
_id = id;
2016-07-26 15:52:42 +08:00
_timer.Change (30000, 30000);
2016-07-28 14:31:04 +08:00
2014-10-02 15:16:15 +08:00
body = String.Format ("user_id: {0} name: {1}", id, name);
}
else {
body = "Received unknown type message.";
}
return new NotificationMessage {
2016-07-26 15:52:42 +08:00
Summary = String.Format ("AudioStreamer ({0})", type),
Body = body,
Icon = "notification-message-im"
};
2014-10-02 15:16:15 +08:00
}
2014-03-12 19:23:37 +08:00
private void sendHeartbeat (object state)
2012-07-31 09:36:52 +08:00
{
2014-03-12 19:23:37 +08:00
_websocket.Send (createTextMessage ("heartbeat", String.Empty));
2012-07-31 09:36:52 +08:00
}
2014-03-13 15:08:28 +08:00
public void Connect (string username)
2012-07-31 09:36:52 +08:00
{
2014-03-13 15:08:28 +08:00
_name = username;
2014-03-12 19:23:37 +08:00
_websocket.Connect ();
2012-07-31 09:36:52 +08:00
}
2014-03-12 19:23:37 +08:00
public void Disconnect ()
2012-07-31 09:36:52 +08:00
{
2016-07-26 15:52:42 +08:00
_timer.Change (-1, -1);
2014-03-13 15:08:28 +08:00
_websocket.Close (CloseStatusCode.Away);
_audioBox.Clear ();
_id = null;
_name = null;
2012-07-31 09:36:52 +08:00
}
2014-03-12 19:23:37 +08:00
public void Write (string message)
2012-07-31 09:36:52 +08:00
{
2014-03-12 19:23:37 +08:00
_websocket.Send (createTextMessage ("message", message));
2012-07-31 09:36:52 +08:00
}
2014-03-12 19:23:37 +08:00
void IDisposable.Dispose ()
2012-07-31 09:36:52 +08:00
{
2014-03-12 19:23:37 +08:00
Disconnect ();
2016-07-26 15:52:42 +08:00
_timer.Dispose ();
2014-03-13 15:08:28 +08:00
_notifier.Close ();
2012-07-31 09:36:52 +08:00
}
}
}