This repository has been archived by the owner on Apr 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
StreamActivity.cs
302 lines (253 loc) · 9.42 KB
/
StreamActivity.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using Android.Media;
using Android.Hardware.Input;
using SmartGlass.Common;
using SmartGlass.Nano.Consumer;
namespace SmartGlass.Nano.Droid
{
[Activity(Label = "StreamActivity",
ScreenOrientation = ScreenOrientation.Landscape)]
public class StreamActivity
: Activity, TextureView.ISurfaceTextureListener, InputManager.IInputDeviceListener
{
private const int INVALID_GAMEPAD_INDEX = -1;
private bool setupRan = false;
private TextureView _videoSurface;
private int _current_device_id;
private List<int> _connected_devices;
private InputManager _inputManager;
private string _hostName;
private SmartGlassClient _smartGlassClient;
private NanoClient _nanoClient;
private MediaCoreConsumer _mcConsumer;
private InputHandler _inputHandler;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//Remove title bar
RequestWindowFeature(WindowFeatures.NoTitle);
//Remove notification bar
Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
SetContentView(Resource.Layout.StreamLayout);
_hostName = Intent.Extras.GetString("hostName");
Toast.MakeText(this,
String.Format("Connecting to {0}...", _hostName),
ToastLength.Short)
.Show();
// Create your application here
_videoSurface = FindViewById<TextureView>(Resource.Id.tvVideoStream);
_videoSurface.SurfaceTextureListener = this;
_current_device_id = INVALID_GAMEPAD_INDEX;
_connected_devices = new List<int>();
_inputHandler = new InputHandler();
_inputManager = (InputManager)GetSystemService(Context.InputService);
_inputManager.RegisterInputDeviceListener(this, null);
EnumerateGamepads();
}
protected override void OnStop()
{
base.OnStop();
_videoSurface.Dispose();
_smartGlassClient.Dispose();
_nanoClient.Dispose();
_mcConsumer.Dispose();
}
/*
* Video / Texture surface
*/
public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
if (setupRan)
return;
Task.Run(() => StartStream(surface));
setupRan = true;
}
public bool OnSurfaceTextureDestroyed(SurfaceTexture surface)
{
return false;
}
public void OnSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
{
}
public void OnSurfaceTextureUpdated(SurfaceTexture surface)
{
}
/*
* Gamepad input
*/
public int EnumerateGamepads()
{
int[] deviceIds = _inputManager.GetInputDeviceIds();
foreach (int deviceId in deviceIds)
{
InputDevice dev = InputDevice.GetDevice(deviceId);
if (IsGamepad(dev))
{
if (!_connected_devices.Contains(deviceId))
{
_connected_devices.Add(deviceId);
if (_current_device_id == INVALID_GAMEPAD_INDEX)
{
_current_device_id = deviceId;
}
}
}
}
return _connected_devices.Count;
}
private bool IsGamepad(InputDevice device)
{
if ((device.Sources & InputSourceType.Gamepad) == InputSourceType.Gamepad ||
(device.Sources & InputSourceType.ClassJoystick) == InputSourceType.Joystick)
{
return true;
}
return false;
}
//Get the centered position for the joystick axis
private float GetCenteredAxis(MotionEvent e, InputDevice device, Axis axis)
{
InputDevice.MotionRange range = device.GetMotionRange(axis, e.Source);
if (range != null)
{
float flat = range.Flat;
float value = e.GetAxisValue(axis);
if (System.Math.Abs(value) > flat)
return value;
}
return 0;
}
public override bool OnGenericMotionEvent(MotionEvent e)
{
InputDevice device = e.Device;
if (device != null && device.Id == _current_device_id)
{
if (IsGamepad(device))
{
/*
for (int i = 0; i < AxesMapping.size; i++)
{
axes[i] = GetCenteredAxis(e, device, AxesMapping.OrdinalValueAxis(i));
}
return true;
*/
return true;
}
}
return base.OnGenericMotionEvent(e);
}
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
InputDevice device = e.Device;
if (device != null && device.Id == _current_device_id)
{
if (IsGamepad(device))
{
/*
int index = ButtonMapping.OrdinalValue(keyCode);
if (index >= 0)
{
buttons[index] = 1;
}
return true;
*/
return true;
}
}
return base.OnKeyDown(keyCode, e);
}
public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
{
InputDevice device = e.Device;
if (device != null && device.Id == _current_device_id)
{
if (IsGamepad(device))
{
/*
int index = ButtonMapping.OrdinalValue(keyCode);
if (index >= 0)
{
buttons[index] = 0;
}
return true;
*/
return true;
}
}
return base.OnKeyUp(keyCode, e);
}
public void OnInputDeviceAdded(int deviceId)
{
InputDevice dev = _inputManager.GetInputDevice(deviceId);
if (!IsGamepad(dev))
return;
else if (_current_device_id == INVALID_GAMEPAD_INDEX)
{
_current_device_id = deviceId;
if (!_connected_devices.Contains(deviceId))
_connected_devices.Add(deviceId);
}
}
public void OnInputDeviceChanged(int deviceId)
{
throw new NotImplementedException("OnInputDeviceChanged");
}
public void OnInputDeviceRemoved(int deviceId)
{
if (_connected_devices.Contains(deviceId))
{
_connected_devices.Remove(deviceId);
}
if (_current_device_id == deviceId)
{
_current_device_id = INVALID_GAMEPAD_INDEX;
}
}
/*
* Protocol
*/
public async Task StartStream(SurfaceTexture surface)
{
System.Diagnostics.Debug.WriteLine($"Connecting to console...");
_smartGlassClient = await SmartGlassClient.ConnectAsync(_hostName);
// Get general gamestream configuration
var config = GamestreamConfiguration.GetStandardConfig();
/* Modify standard config, if desired */
var broadcastChannel = _smartGlassClient.BroadcastChannel;
var session = await broadcastChannel.StartGamestreamAsync(config);
System.Diagnostics.Debug.WriteLine(
$"Connecting to Nano, TCP: {session.TcpPort}, UDP: {session.UdpPort}");
_nanoClient = new NanoClient(_hostName, session);
// General Handshaking & Opening channels
await _nanoClient.InitializeProtocolAsync();
// Audio & Video client handshaking
// Sets desired AV formats
Packets.AudioFormat audioFormat = _nanoClient.AudioFormats[0];
Packets.VideoFormat videoFormat = _nanoClient.VideoFormats[0];
await _nanoClient.InitializeStreamAsync(audioFormat, videoFormat);
// Start ChatAudio channel
Packets.AudioFormat chatAudioFormat = new Packets.AudioFormat(1, 24000, AudioCodec.Opus);
await _nanoClient.OpenChatAudioChannelAsync(chatAudioFormat);
_mcConsumer = new MediaCoreConsumer(surface, audioFormat, videoFormat);
_nanoClient.AddConsumer(_mcConsumer);
// Tell console to start sending AV frames
await _nanoClient.StartStreamAsync();
// Start Controller input channel
await _nanoClient.OpenInputChannelAsync(1280, 720);
System.Diagnostics.Debug.WriteLine($"Nano connected and running.");
}
}
}