BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Platform / Scripts / VoipInput.cs

VoipInput.cs

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