-
Notifications
You must be signed in to change notification settings - Fork 144
/
PlayerService.cs
500 lines (422 loc) · 16.8 KB
/
PlayerService.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using System;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using AudioVisualizer;
using BuildCast.Controls;
using BuildCast.DataModel;
using BuildCast.Helpers;
using Microsoft.Toolkit.Uwp.Helpers;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Core;
using Windows.Media.Editing;
using Windows.Media.Playback;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System.Display;
using Windows.UI.Composition;
using Windows.UI.Core;
using Windows.UI.Xaml;
namespace BuildCast.Services
{
public class PlayerService : IPlayerService
{
private static PlayerService _current;
private NowPlayingState _nowPlayingState;
private MediaPlayer _mediaPlayer;
private AudioVisualizer.PlaybackSource _source;
private string _localFilename;
private Uri _sourceUri;
private TimeSpan _startTime;
private DispatcherTimer _playerEventDebounceTimer;
private CoreDispatcher _dispatcher;
private object _syncLock = new object();
private Timeline _timelinectrl;
private MediaPlaybackState _nextEventtoFire;
private MediaPlaybackState _lastEventFired;
private StorageFile _currentFile;
public event EventHandler<MediaPlaybackState> PlayPauseChanged;
public event EventHandler MediaLoaded;
public event EventHandler MediaLoading;
public event EventHandler<IVisualizationSource> VisualizationSourceChanged;
public static PlayerService Current
{
get
{
if (_current == null)
{
_current = new PlayerService();
}
return _current;
}
}
public CompositionBrush GetBrush(Compositor compositor)
{
var surface = _mediaPlayer.GetSurface(compositor);
var surfaceBrush = compositor.CreateSurfaceBrush(surface.CompositionSurface);
surfaceBrush.Stretch = CompositionStretch.Uniform;
return surfaceBrush;
}
public async Task HandlePlayRequest(BuildCast.DataModel.InkNote i)
{
var shouldPlay = _nowPlayingState.HandlePlayRequest(i);
await SetNewItem(new Uri(_nowPlayingState.CurrentEpisode.Key), _nowPlayingState.CurrentTime, shouldPlay, _nowPlayingState.CurrentEpisode.Id.ToString());
}
public async Task HandlePlayRequest(Episode e)
{
var shouldPlay = await _nowPlayingState.HandlePlayRequest(e);
if (_nowPlayingState.CurrentEpisode != null && shouldPlay.Item1)
{
// if we need to switch to a new item, set new item and open it for playback
await SetNewItem(new Uri(_nowPlayingState.CurrentEpisode.Key), _nowPlayingState.CurrentTime, shouldPlay.Item2, _nowPlayingState.CurrentEpisode.Id.ToString());
}
else if (_nowPlayingState.CurrentEpisode != null && !shouldPlay.Item1)
{
// if we don't need to switch (being asked to play what is already playing), just set the time - unless the time is already set.
if (_startTime <= TimeSpan.Zero)
{
SetTime(_nowPlayingState.CurrentTime);
}
if (shouldPlay.Item2)
{
_mediaPlayer.Play();
}
}
}
#region Playback Commands
public async Task Play(Episode ep, TimeSpan playTime)
{
await Microsoft.Toolkit.Uwp.Helpers.DispatcherHelper.AwaitableRunAsync(_dispatcher, () =>
{
if (_mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.None)
{
return SetNewItem(new Uri(ep.Key), playTime, true, string.Empty);
}
else
{
SetTime(playTime);
return HandlePlayRequest(ep);
}
});
}
public void Pause()
{
var ignored = DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
_mediaPlayer.Pause();
});
}
public async Task TogglePlayPaused()
{
if (NowPlaying.HasItem)
{
switch (_mediaPlayer.PlaybackSession.PlaybackState)
{
case MediaPlaybackState.None:
await OpenAndPlayNewItem();
break;
case MediaPlaybackState.Playing:
_mediaPlayer.Pause();
break;
case MediaPlaybackState.Paused:
_mediaPlayer.Play();
break;
}
}
}
public void Rewind()
{
var ignored = DispatcherHelper.ExecuteOnUIThreadAsync(() => _mediaPlayer.PlaybackSession.Position = _mediaPlayer.PlaybackSession.Position - TimeSpan.FromSeconds(10));
}
public void FastForward()
{
var ignored = DispatcherHelper.ExecuteOnUIThreadAsync(() => _mediaPlayer.PlaybackSession.Position = _mediaPlayer.PlaybackSession.Position + TimeSpan.FromSeconds(30));
}
#endregion
public async Task SetNewItem(Uri currentItem, TimeSpan currentTime, bool startPlayback, string localFilename)
{
var wasPlaying = _mediaPlayer?.PlaybackSession.PlaybackState == MediaPlaybackState.Playing;
ClearPlayback();
_startTime = currentTime;
_sourceUri = currentItem;
_localFilename = localFilename;
if (startPlayback)
{
await OpenAndPlayNewItem();
}
}
public void SetTime(TimeSpan currentTime)
{
if (_mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Playing || _mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Paused)
{
_mediaPlayer.PlaybackSession.Position = currentTime;
_timelinectrl?.SetTime(currentTime, _mediaPlayer.PlaybackSession.NaturalDuration);
}
else
{
_startTime = currentTime;
}
}
public void SetTimeControls(Timeline timeCtrl)
{
this._timelinectrl = timeCtrl;
}
public async Task<byte[]> GetBitmapForCurrentFrameFromLocalFile()
{
if (_currentFile != null)
{
return await GetBitmapBytesFromLocalFile(_mediaPlayer.PlaybackSession.Position);
}
return null;
}
#region Properties
public bool IsPaused
{
get
{
return _mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Paused;
}
}
public bool IsPlaying
{
get
{
return this.IsPlayingInternal(_mediaPlayer.PlaybackSession.PlaybackState);
}
}
public MediaPlaybackState State
{
get
{
return _mediaPlayer.PlaybackSession.PlaybackState;
}
}
public bool IsOpening
{
get
{
return this.IsOpeningInternal(_mediaPlayer.PlaybackSession.PlaybackState);
}
}
public bool IsClosed
{
get
{
return _mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.None;
}
}
public double CurrentTime
{
get
{
return _mediaPlayer.PlaybackSession.Position.TotalMilliseconds;
}
}
public NowPlayingState NowPlaying { get => _nowPlayingState; }
public PlaybackSource VisualizationSource { get => _source; set => _source = value; }
#endregion
private PlayerService()
{
_dispatcher = Window.Current.Dispatcher;
InitializeMediaPlayer();
_playerEventDebounceTimer = new DispatcherTimer()
{
Interval = TimeSpan.FromMilliseconds(250)
};
_playerEventDebounceTimer.Tick += PlayerEventDebounceTimer_Tick;
_nowPlayingState = new NowPlayingState();
_nowPlayingState.LoadStateAsync().ContinueWith((a) =>
{
// TODO: handle error
if (_nowPlayingState.CurrentEpisode != null)
{
var setItemTask = SetNewItem(new Uri(_nowPlayingState.CurrentEpisode.Key), _nowPlayingState.CurrentTime, false, _nowPlayingState.CurrentEpisode.Id.ToString());
}
});
}
private void InitializeMediaPlayer()
{
_mediaPlayer = new MediaPlayer();
VisualizationSource = new PlaybackSource(_mediaPlayer);
VisualizationSource.SourceChanged += _source_SourceChanged;
_mediaPlayer.PlaybackSession.PositionChanged += PlaybackSession_PositionChanged;
_mediaPlayer.PlaybackSession.PlaybackStateChanged += PlaybackSession_PlaybackStateChanged;
_mediaPlayer.MediaFailed += MediaPlayer_MediaFailed;
_mediaPlayer.PlaybackSession.NaturalVideoSizeChanged += (sender, args) =>
{
sender.MediaPlayer.SetSurfaceSize(new Size(sender.NaturalVideoWidth, sender.NaturalVideoHeight));
};
}
private void _source_SourceChanged(object sender, AudioVisualizer.IVisualizationSource args)
{
VisualizationSourceChanged?.Invoke(sender, args);
}
private async Task OpenAndPlayNewItem()
{
TypedEventHandler<MediaPlayer, object> openedHandler = null;
OnMediaLoading();
openedHandler = (s, e) =>
{
OnMediaLoaded();
_mediaPlayer.MediaOpened -= openedHandler;
if (_startTime > TimeSpan.Zero)
{
_mediaPlayer.PlaybackSession.Position = _startTime;
}
};
_mediaPlayer.MediaOpened += openedHandler;
var result = await GetSourceForUri();
_mediaPlayer.Source = result.source;
_currentFile = result.storageFile;
_mediaPlayer.Play();
var disp = new DisplayRequest();
disp.RequestActive();
}
private async Task<(MediaSource source, StorageFile storageFile)> GetSourceForUri()
{
MediaSource source = null;
var currentFile = await BackgroundDownloadHelper.CheckLocalFileExistsFromUriHash(_sourceUri);
if (currentFile != null)
{
source = MediaSource.CreateFromStorageFile(currentFile);
}
else
{
source = MediaSource.CreateFromUri(_sourceUri);
}
return (source, currentFile);
}
private async Task<byte[]> GetBitmapBytesFromLocalFile(TimeSpan span)
{
var thumbnail = await GetThumbnailAsync(_currentFile, span);
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
await RandomAccessStream.CopyAsync(thumbnail, randomAccessStream);
randomAccessStream.Seek(0);
var bytes = new byte[randomAccessStream.Size];
await randomAccessStream.ReadAsync(bytes.AsBuffer(), (uint)randomAccessStream.Size, InputStreamOptions.None);
return bytes;
}
private async Task<IInputStream> GetThumbnailAsync(StorageFile file, TimeSpan span)
{
var mediaClip = await MediaClip.CreateFromFileAsync(file);
var mediaComposition = new MediaComposition();
mediaComposition.Clips.Add(mediaClip);
return await mediaComposition.GetThumbnailAsync(
span, 0, 0, VideoFramePrecision.NearestFrame);
}
private void ClearPlayback()
{
if (_mediaPlayer.PlaybackSession.PlaybackState != MediaPlaybackState.None)
{
_mediaPlayer.Source = null;
}
}
private void PlaybackSession_PositionChanged(MediaPlaybackSession sender, object args)
{
_timelinectrl?.SetTime(_mediaPlayer.PlaybackSession.Position, _mediaPlayer.PlaybackSession.NaturalDuration);
_nowPlayingState.CurrentTime = _mediaPlayer.PlaybackSession.Position;
}
private async void PlaybackSession_PlaybackStateChanged(MediaPlaybackSession sender, object args)
{
await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
lock (_syncLock)
{
_nextEventtoFire = sender.PlaybackState;
if (_playerEventDebounceTimer.IsEnabled)
{
Debug.WriteLine($"Debounce");
_playerEventDebounceTimer.Stop();
_playerEventDebounceTimer.Start();
}
_playerEventDebounceTimer.Start();
}
});
}
private void MediaPlayer_MediaFailed(MediaPlayer sender, MediaPlayerFailedEventArgs args)
{
Debug.WriteLine($"failed:{args.ErrorMessage}");
}
private async Task HandlePlayPauseChanged(MediaPlaybackState playOrPause)
{
if (playOrPause == MediaPlaybackState.Paused)
{
await _nowPlayingState.PeristEpisodePlaybackState();
}
}
private void PlayerEventDebounceTimer_Tick(object sender, object e)
{
lock (_syncLock)
{
_playerEventDebounceTimer.Stop();
CheckFireStateChanged();
}
}
private void CheckFireStateChanged()
{
// Only fire the event if state has actually changed based on what was last fired
var wasPlaying = IsPlayingInternal(_lastEventFired);
var isPlaying = IsPlayingInternal(_nextEventtoFire);
if (wasPlaying != isPlaying)
{
if (PlayPauseChanged != null)
{
// Don't fire the event while the lock is held
var dispatcherTask = _dispatcher.RunIdleAsync(async (s) =>
{
var playOrPause = IsPlayingInternal(_nextEventtoFire) ? MediaPlaybackState.Playing : MediaPlaybackState.Paused;
Debug.WriteLine($"Fire either play or pause : {playOrPause}");
await OnPlayPauseChanged(playOrPause);
});
}
}
_lastEventFired = _nextEventtoFire;
}
private async Task OnPlayPauseChanged(MediaPlaybackState playOrPause)
{
await HandlePlayPauseChanged(playOrPause);
PlayPauseChanged(this, playOrPause);
}
/// <summary>
/// Use by callers who care about playing vs paused like play pause button
/// </summary>
/// <param name="eventToTest"></param>
/// <returns></returns>
private bool IsPlayingInternal(MediaPlaybackState eventToTest)
{
return eventToTest == MediaPlaybackState.Buffering
|| eventToTest == MediaPlaybackState.Opening
|| eventToTest == MediaPlaybackState.Playing;
}
/// <summary>
/// Used by callers who are showing loading UX
/// </summary>
/// <param name="eventToTest"></param>
/// <returns></returns>
private bool IsOpeningInternal(MediaPlaybackState eventToTest)
{
return eventToTest == MediaPlaybackState.Buffering
|| eventToTest == MediaPlaybackState.Opening;
}
private void OnMediaLoaded()
{
this.MediaLoaded?.Invoke(this, EventArgs.Empty);
}
private void OnMediaLoading()
{
this.MediaLoading?.Invoke(this, EventArgs.Empty);
}
}
}