-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamPlayerControl.cs
239 lines (200 loc) · 7.6 KB
/
StreamPlayerControl.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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace RAC.Controls.StreamPlayerControl
{
using System.Runtime.InteropServices;
/// <summary>
/// The stream player control.
/// </summary>
public partial class StreamPlayerControl: UserControl
{
/// <summary>
/// Initializes a new instance of the <see cref="StreamPlayerControl"/> class.
/// </summary>
public StreamPlayerControl()
{
InitializeComponent();
}
private StreamPlayerProxy _player;
private StreamPlayerProxy Player
{
get
{
if (_player == null)
{
_player = CreateAndInitializePlayer();
}
return _player;
}
}
/// <summary>
/// Asynchronously plays a stream.
/// </summary>
/// <param name="uri">The uri of a stream to play.</param>
/// <param name="connectionTimeout"></param>
/// <exception cref="Win32Exception">Failed to load the FFmpeg facade dll.</exception>
/// <exception cref="StreamPlayerException">Failed to play the stream.</exception>
/// <param name="transport">RTSP transport protocol.</param>
/// <param name="flags">RTSP flags.</param>
public void StartPlay(Uri uri, TimeSpan connectionTimeout,
RtspTransport transport, RtspFlags flags, FrameDelay framedelay)
{
if (IsPlaying)
{
Stop();
}
Player.StartPlay(uri.IsFile ? uri.LocalPath : uri.ToString(),
connectionTimeout, transport, flags, framedelay);
}
/// <summary>
/// Asynchronously plays a stream.
/// </summary>
/// <param name="uri">The uri of a stream to play.</param>
/// <exception cref="Win32Exception">Failed to load the FFmpeg facade dll.</exception>
/// <exception cref="StreamPlayerException">Failed to play the stream.</exception>
public void StartPlay(Uri uri)
{
StartPlay(uri, TimeSpan.FromSeconds(5.0), RtspTransport.Undefined, RtspFlags.None, FrameDelay.Calculated);
}
/// <summary>
/// Retrieves the image being played.
/// </summary>
/// <returns>The current image.</returns>
/// <exception cref="InvalidOperationException">The control is not playing a video stream.</exception>
/// <exception cref="StreamPlayerException">Failed to get the current image.</exception>
public Bitmap GetCurrentFrame()
{
if (!IsPlaying)
{
throw new InvalidOperationException();
}
return Player.GetCurrentFrame();
}
/// <summary>
/// Stops a stream.
/// </summary>
/// <exception cref="InvalidOperationException">The control is not playing a stream.</exception>
/// <exception cref="StreamPlayerException">Failed to stop a stream.</exception>
public void Stop()
{
if (!IsPlaying)
{
throw new InvalidOperationException();
}
Player.Stop();
IsPlaying = false;
}
/// <summary>
/// Gets a value indicating whether the control is playing a video stream.
/// </summary>
[Browsable(false)]
public Boolean IsPlaying { get; private set; }
/// <summary>
/// Gets the unstretched frame size.
/// </summary>
[Browsable(false)]
public Size VideoSize
{
get { return IsPlaying ? Player.GetFrameSize() : new Size(0, 0); }
}
private bool _disposed;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing && (_player != null))
{
if (IsPlaying)
{
Stop();
}
_player.Uninitialize();
_player.Dispose();
}
if (disposing && (components != null))
{
components.Dispose();
}
_disposed = true;
base.Dispose(disposing);
}
/// <summary>
/// Specifies a set of values that are used when you start the player.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct StreamPlayerParams
{
internal IntPtr window;
internal IntPtr streamStartedCallback;
internal IntPtr streamStoppedCallback;
internal IntPtr streamFailedCallback;
}
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
private delegate void CallbackDelegate();
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
delegate void ErrorCallbackDelegate(string error);
private Delegate _streamStartedCallback;
private Delegate _streamStoppedCallback;
private Delegate _streamFailedCallback;
private StreamPlayerProxy CreateAndInitializePlayer()
{
var player = new StreamPlayerProxy();
_streamStartedCallback = new CallbackDelegate(RaiseStreamStartedEvent);
_streamStoppedCallback = new CallbackDelegate(RaiseStreamStoppedEvent);
_streamFailedCallback = new ErrorCallbackDelegate(RaiseStreamFailedEvent);
var playerParams = new StreamPlayerParams
{
window = Handle,
streamStartedCallback = Marshal.GetFunctionPointerForDelegate(_streamStartedCallback),
streamStoppedCallback = Marshal.GetFunctionPointerForDelegate(_streamStoppedCallback),
streamFailedCallback = Marshal.GetFunctionPointerForDelegate(_streamFailedCallback)
};
player.Initialize(playerParams);
return player;
}
/// <summary>
/// Occurs when the first frame is read from a stream.
/// </summary>
public event EventHandler StreamStarted;
private void RaiseStreamStartedEvent()
{
IsPlaying = true;
if (StreamStarted != null)
{
StreamStarted(this, EventArgs.Empty);
}
}
/// <summary>
/// Occurs when there are no more frames to read from a stream.
/// </summary>
public event EventHandler StreamStopped;
private void RaiseStreamStoppedEvent()
{
IsPlaying = false;
if (StreamStopped != null)
{
StreamStopped(this, EventArgs.Empty);
}
}
/// <summary>
/// Occurs when the player fails to play a stream.
/// </summary>
public event EventHandler<StreamFailedEventArgs> StreamFailed;
private void RaiseStreamFailedEvent(string error)
{
IsPlaying = false;
if (StreamFailed != null)
{
StreamFailed(this, new StreamFailedEventArgs(error));
}
}
}
}