/
MediaStreamSource.cs
349 lines (302 loc) · 14.7 KB
/
MediaStreamSource.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
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using static Interop;
namespace Tizen.Multimedia
{
/// <summary>
/// Provides the ability to push packets as the source of <see cref="Player"/>.
/// </summary>
/// <remarks>The source must be set as a source to a player before pushing.</remarks>
/// <seealso cref="Player.SetSource(MediaSource)"/>
/// <since_tizen> 3 </since_tizen>
public sealed class MediaStreamSource : MediaSource
{
private readonly MediaFormat _audioMediaFormat;
private readonly MediaFormat _videoMediaFormat;
private static List<MediaFormatAudioMimeType> _supportedAudioFormats;
private static List<MediaFormatVideoMimeType> _supportedVideoFormats;
/// <summary>
/// Gets all supported audio types.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public static IEnumerable<MediaFormatAudioMimeType> SupportedAudioTypes
{
get
{
GetSupportedTypes();
return _supportedAudioFormats.AsReadOnly();
}
}
/// <summary>
/// Gets all supported video types.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public static IEnumerable<MediaFormatVideoMimeType> SupportedVideoTypes
{
get
{
GetSupportedTypes();
return _supportedVideoFormats.AsReadOnly();
}
}
private static void GetSupportedTypes()
{
if (_supportedAudioFormats == null && _supportedVideoFormats == null)
{
PlayerHandle _playerHandle;
IntPtr _handle;
NativePlayer.Create(out _playerHandle).ThrowIfFailed(null, "Failed to create player");
_handle = _playerHandle.DangerousGetHandle();
Debug.Assert(_handle != IntPtr.Zero);
try
{
_supportedAudioFormats = new List<MediaFormatAudioMimeType>();
_supportedVideoFormats = new List<MediaFormatVideoMimeType>();
NativePlayer.SupportedMediaFormatCallback callback = (int format, IntPtr userData) =>
{
if (Enum.IsDefined(typeof(MediaFormatAudioMimeType), format))
{
Log.Debug(PlayerLog.Tag, "supported audio : " + ((MediaFormatAudioMimeType)format).ToString());
_supportedAudioFormats.Add((MediaFormatAudioMimeType)format);
}
else if (Enum.IsDefined(typeof(MediaFormatVideoMimeType), format))
{
Log.Debug(PlayerLog.Tag, "supported video : " + ((MediaFormatVideoMimeType)format).ToString());
_supportedVideoFormats.Add((MediaFormatVideoMimeType)format);
}
else
{
Log.Debug(PlayerLog.Tag, "skipped : " + format.ToString());
}
return true;
};
NativePlayer.SupportedMediaStreamFormat(_handle, callback, IntPtr.Zero).
ThrowIfFailed(null, "Failed to get the list");
}
finally
{
_playerHandle.Dispose();
}
}
}
private Player _player;
private MediaStreamConfiguration CreateAudioConfiguration(AudioMediaFormat format)
{
if (format == null)
{
return null;
}
if (!SupportedAudioTypes.Contains(format.MimeType))
{
Log.Error(PlayerLog.Tag, "The audio format is not supported : " + format.MimeType);
throw new ArgumentException($"The audio format is not supported, Type : {format.MimeType}.");
}
return new MediaStreamConfiguration(this, StreamType.Audio);
}
private MediaStreamConfiguration CreateVideoConfiguration(VideoMediaFormat format)
{
if (format == null)
{
return null;
}
if (!SupportedVideoTypes.Contains(format.MimeType))
{
Log.Error(PlayerLog.Tag, "The video format is not supported : " + format.MimeType);
throw new ArgumentException($"The video format is not supported, Type : {format.MimeType}.");
}
return new MediaStreamConfiguration(this, StreamType.Video);
}
/// <summary>
/// Initializes a new instance of the MediaStreamSource class
/// with the specified <see cref="AudioMediaFormat"/> and <see cref="VideoMediaFormat"/>.
/// </summary>
/// <param name="audioMediaFormat">The <see cref="AudioMediaFormat"/> for this source.</param>
/// <param name="videoMediaFormat">The <see cref="VideoMediaFormat"/> for this source.</param>
/// <remarks>AAC and H.264 are supported.</remarks>
/// <exception cref="ArgumentNullException">Both <paramref name="audioMediaFormat"/> and <paramref name="videoMediaFormat"/> are null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="audioMediaFormat"/> is not supported.<br/>
/// -or-<br/>
/// <paramref name="videoMediaFormat"/> is not supported.
/// </exception>
/// <seealso cref="SupportedAudioTypes"/>
/// <seealso cref="SupportedVideoTypes"/>
/// <since_tizen> 3 </since_tizen>
public MediaStreamSource(AudioMediaFormat audioMediaFormat, VideoMediaFormat videoMediaFormat)
{
if (audioMediaFormat == null && videoMediaFormat == null)
{
throw new ArgumentNullException(string.Concat(nameof(_audioMediaFormat), " and ", nameof(_videoMediaFormat)));
}
_audioMediaFormat = audioMediaFormat;
_videoMediaFormat = videoMediaFormat;
AudioConfiguration = CreateAudioConfiguration(audioMediaFormat);
VideoConfiguration = CreateVideoConfiguration(videoMediaFormat);
}
/// <summary>
/// Initializes a new instance of the MediaStreamSource class with the specified <see cref="AudioMediaFormat"/>.
/// </summary>
/// <param name="audioMediaFormat">The <see cref="AudioMediaFormat"/> for this source.</param>
/// <remarks>AAC is supported.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="audioMediaFormat"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="audioMediaFormat"/> is not supported.</exception>
/// <seealso cref="SupportedAudioTypes"/>
/// <since_tizen> 3 </since_tizen>
public MediaStreamSource(AudioMediaFormat audioMediaFormat)
{
_audioMediaFormat = audioMediaFormat ?? throw new ArgumentNullException(nameof(audioMediaFormat));
AudioConfiguration = CreateAudioConfiguration(audioMediaFormat);
}
/// <summary>
/// Initializes a new instance of the MediaStreamSource class with the specified <see cref="VideoMediaFormat"/>.
/// </summary>
/// <remarks>H.264 is supported.</remarks>
/// <param name="videoMediaFormat">The <see cref="VideoMediaFormat"/> for this source.</param>
/// <exception cref="ArgumentNullException"><paramref name="videoMediaFormat"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="videoMediaFormat"/> is not supported.</exception>
/// <seealso cref="SupportedVideoTypes"/>
/// <since_tizen> 3 </since_tizen>
public MediaStreamSource(VideoMediaFormat videoMediaFormat)
{
_videoMediaFormat = videoMediaFormat ?? throw new ArgumentNullException(nameof(videoMediaFormat));
VideoConfiguration = CreateVideoConfiguration(videoMediaFormat);
}
/// <summary>
/// Gets the audio configuration, or null if no AudioMediaFormat is specified in the constructor.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public MediaStreamConfiguration AudioConfiguration { get; }
/// <summary>
/// Gets the video configuration, or null if no VideoMediaFormat is specified in the constructor.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public MediaStreamConfiguration VideoConfiguration { get; }
/// <summary>
/// Pushes elementary stream to decode audio or video.
/// </summary>
/// <remarks>This source must be set as a source to a player and the player must be in the <see cref="PlayerState.Ready"/>,
/// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.</remarks>
/// <param name="packet">The <see cref="MediaPacket"/> to decode.</param>
/// <exception cref="InvalidOperationException">
/// This source is not set as a source to a player.<br/>
/// -or-<br/>
/// The player is not in the valid state.
/// </exception>
/// <exception cref="ArgumentNullException"><paramref name="packet"/> is null.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="packet"/> has been disposed of.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="packet"/> is neither video nor audio type.<br/>
/// -or-<br/>
/// The format of packet is not matched with the specified format in the constructor.
/// </exception>
/// <exception cref="NoBufferSpaceException">The internal buffer has reached its limits.</exception>
/// <seealso cref="Player.SetSource(MediaSource)"/>
/// <seealso cref="MediaStreamConfiguration.BufferMaxSize"/>
/// <seealso cref="MediaPacket"/>
/// <since_tizen> 3 </since_tizen>
public void Push(MediaPacket packet)
{
if (_player == null)
{
Log.Error(PlayerLog.Tag, "The source is not set as a source to a player yet.");
throw new InvalidOperationException("The source is not set as a source to a player yet.");
}
if (packet == null)
{
Log.Error(PlayerLog.Tag, "packet is null");
throw new ArgumentNullException(nameof(packet));
}
if (packet.IsDisposed)
{
Log.Error(PlayerLog.Tag, "packet is disposed");
throw new ObjectDisposedException(nameof(packet));
}
if (packet.Format.Type == MediaFormatType.Text || packet.Format.Type == MediaFormatType.Container)
{
Log.Error(PlayerLog.Tag, "The format of the packet is invalid : " + packet.Format.Type);
throw new ArgumentException($"The format of the packet is invalid : { packet.Format.Type }.");
}
if (!packet.Format.Equals(_audioMediaFormat) && !packet.Format.Equals(_videoMediaFormat))
{
Log.Error(PlayerLog.Tag, "The format of the packet is invalid : Unmatched format.");
throw new ArgumentException($"The format of the packet is invalid : Unmatched format.");
}
if (packet.Format.Type == MediaFormatType.Video && _videoMediaFormat == null)
{
Log.Error(PlayerLog.Tag, "Video is not configured with the current source.");
throw new ArgumentException("Video is not configured with the current source.");
}
if (packet.Format.Type == MediaFormatType.Audio && _audioMediaFormat == null)
{
Log.Error(PlayerLog.Tag, "Audio is not configured with the current source.");
throw new ArgumentException("Audio is not configured with the current source.");
}
_player.ValidatePlayerState(PlayerState.Paused, PlayerState.Playing, PlayerState.Ready);
NativePlayer.PushMediaStream(_player.Handle, packet.GetHandle()).
ThrowIfFailed(_player, "Failed to push the packet to the player");
}
private void SetMediaStreamInfo(StreamType streamType, MediaFormat mediaFormat)
{
if (mediaFormat == null)
{
Log.Warn(PlayerLog.Tag, "invalid media format");
return;
}
IntPtr ptr = IntPtr.Zero;
try
{
ptr = mediaFormat.AsNativeHandle();
NativePlayer.SetMediaStreamInfo(_player.Handle, streamType, ptr).
ThrowIfFailed(_player, "Failed to set the media stream info");
}
finally
{
MediaFormat.ReleaseNativeHandle(ptr);
}
}
internal override void OnAttached(Player player)
{
Debug.Assert(player != null);
if (_player != null)
{
Log.Error(PlayerLog.Tag, "The source is has already been assigned to another player.");
throw new InvalidOperationException("The source is has already been assigned to another player.");
}
AudioConfiguration?.OnPlayerSet(player);
VideoConfiguration?.OnPlayerSet(player);
_player = player;
SetMediaStreamInfo(StreamType.Audio, _audioMediaFormat);
SetMediaStreamInfo(StreamType.Video, _videoMediaFormat);
}
internal override void OnDetached(Player player)
{
base.OnDetached(player);
AudioConfiguration?.OnPlayerUnset(player);
VideoConfiguration?.OnPlayerUnset(player);
_player = null;
}
/// <summary>
/// Gets the <see cref="Player"/> that this source is assigned to as a source, or null if this source is not assigned.
/// </summary>
/// <seealso cref="Player.SetSource(MediaSource)"/>
/// <since_tizen> 3 </since_tizen>
public Player Player => _player;
}
}