VoipInput.cs
2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//This file is deprecated. Use the high level voip system instead:
// https://developer.oculus.com/documentation/unity/ps-voip/
#if false
namespace Oculus.Platform
{
using UnityEngine;
using System.Runtime.InteropServices;
using System.Collections;
public class VoipInput : MonoBehaviour
{
public delegate void OnCompressedData(byte[] compressedData);
public OnCompressedData onCompressedData;
protected IMicrophone micInput;
Encoder encoder;
public bool enableMicRecording;
protected void Start()
{
encoder = new Encoder();
if (UnityEngine.Application.platform == RuntimePlatform.WindowsEditor || UnityEngine.Application.platform == RuntimePlatform.WindowsPlayer)
{
micInput = new MicrophoneInputNative();
}
else
{
micInput = new MicrophoneInput();
}
enableMicRecording = true;
}
void OnApplicationQuit()
{
micInput.Stop();
}
void Update()
{
if (micInput == null || encoder == null)
{
throw new System.Exception("VoipInput failed to init");
}
if (micInput != null && enableMicRecording)
{
float[] rawMicSamples = micInput.Update();
if (rawMicSamples != null && rawMicSamples.Length > 5 * 1024)
{
Debug.Log(string.Format("Giant input mic data {0}", rawMicSamples.Length));
return;
}
if (rawMicSamples != null && rawMicSamples.Length > 0)
{
int startIdx = 0;
int remaining = rawMicSamples.Length;
int splitSize = 480;
do
{
int toCopy = System.Math.Min(splitSize, remaining);
float[] splitInput = new float[toCopy];
System.Array.Copy(rawMicSamples, startIdx, splitInput, 0, toCopy);
startIdx += toCopy;
remaining -= toCopy;
byte[] compressedMic = null;
compressedMic = encoder.Encode(splitInput);
if (compressedMic != null && compressedMic.Length > 0)
{
onCompressedData(compressedMic);
}
} while (remaining > 0);
}
}
}
}
}
#endif