/
StreamInfo.cs
407 lines (366 loc) · 15.5 KB
/
StreamInfo.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
/*
* 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.Runtime.InteropServices;
using static Interop;
namespace Tizen.Multimedia
{
/// <summary>
/// Represents properties for the audio stream.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public struct AudioStreamProperties
{
/// <summary>
/// Initializes a new instance of the AudioStreamProperties struct with the specified sample rate, channels, and bit rate.
/// </summary>
/// <param name="sampleRate">The sample rate of the stream.</param>
/// <param name="channels">The number of channels of the stream.</param>
/// <param name="bitRate">The bit rate of the stream.</param>
/// <since_tizen> 3 </since_tizen>
public AudioStreamProperties(int sampleRate, int channels, int bitRate)
{
SampleRate = sampleRate;
Channels = channels;
BitRate = bitRate;
Log.Debug(PlayerLog.Tag, $"sampleRate={sampleRate}, channels={channels}, bitRate={bitRate}");
}
/// <summary>
/// Gets or sets the sample rate.
/// </summary>
/// <value>The audio sample rate(Hz).</value>
/// <since_tizen> 3 </since_tizen>
public int SampleRate
{
get;
set;
}
/// <summary>
/// Gets or sets the channels.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public int Channels
{
get;
set;
}
/// <summary>
/// Gets or sets the bit rate.
/// </summary>
/// <value>The audio bit rate(Hz).</value>
/// <since_tizen> 3 </since_tizen>
public int BitRate
{
get;
set;
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>A string that represents the current object.</returns>
/// <since_tizen> 3 </since_tizen>
public override string ToString() =>
$"SampleRate={ SampleRate.ToString() }, Channels={ Channels.ToString() }, BitRate={ BitRate.ToString() }";
}
/// <summary>
/// Represents properties for the video stream.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public struct VideoStreamProperties
{
/// <summary>
/// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, and size.
/// </summary>
/// <param name="fps">The fps of the stream.</param>
/// <param name="bitRate">The bit rate of the stream.</param>
/// <param name="size">The size of the stream.</param>
/// <since_tizen> 3 </since_tizen>
public VideoStreamProperties(int fps, int bitRate, Size size)
{
Fps = fps;
BitRate = bitRate;
Size = size;
Log.Debug(PlayerLog.Tag, $"fps={fps}, bitrate={bitRate}, size=({size})");
}
/// <summary>
/// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, width, and height.
/// </summary>
/// <param name="fps">The fps of the stream.</param>
/// <param name="bitRate">The bit rate of the stream.</param>
/// <param name="width">The width of the stream.</param>
/// <param name="height">The height of the stream.</param>
/// <since_tizen> 3 </since_tizen>
public VideoStreamProperties(int fps, int bitRate, int width, int height)
: this(fps, bitRate, new Size(width, height))
{
}
/// <summary>
/// Gets or sets the fps.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public int Fps
{
get;
set;
}
/// <summary>
/// Gets or sets the bit rate.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public int BitRate
{
get;
set;
}
/// <summary>
/// Gets or sets the size.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public Size Size
{
get;
set;
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>A string that represents the current object.</returns>
/// <since_tizen> 3 </since_tizen>
public override string ToString()
{
return $"Fps={ Fps.ToString() }, BitRate={ BitRate.ToString() }, Size=[{ Size.ToString() }]";
}
}
/// <summary>
/// Provides a means to retrieve stream information.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class StreamInfo
{
internal StreamInfo(Player owner)
{
Player = owner;
}
/// <summary>
/// Retrieves the album art of the stream, or null if there is no album art data.
/// </summary>
/// <returns>Raw byte array if album art exists; otherwise null.</returns>
/// <remarks>
/// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
/// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
/// </remarks>
/// <exception cref="ObjectDisposedException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
/// </exception>
/// <since_tizen> 3 </since_tizen>
public byte[] GetAlbumArt()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
ThrowIfFailed(Player, "Failed to get the album art");
if (art == IntPtr.Zero || size == 0)
{
return null;
}
byte[] albumArt = new byte[size];
Marshal.Copy(art, albumArt, 0, size);
return albumArt;
}
private string GetCodecInfo(bool audioInfo)
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
IntPtr audioPtr = IntPtr.Zero;
IntPtr videoPtr = IntPtr.Zero;
try
{
NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
ThrowIfFailed(Player, "Failed to get codec info");
if (audioInfo)
{
Log.Debug(PlayerLog.Tag, "it is audio case");
return Marshal.PtrToStringAnsi(audioPtr);
}
else
{
Log.Debug(PlayerLog.Tag, "it is video case");
return Marshal.PtrToStringAnsi(videoPtr);
}
}
finally
{
LibcSupport.Free(audioPtr);
LibcSupport.Free(videoPtr);
}
}
/// <summary>
/// Retrieves the codec name of the audio or null if there is no audio.
/// </summary>
/// <returns>A string that represents the codec name.</returns>
/// <since_tizen> 3 </since_tizen>
public string GetAudioCodec()
{
return GetCodecInfo(true);
}
/// <summary>
/// Retrieves the codec name of the video or null if there is no video.
/// </summary>
/// <returns>A string that represents the codec name.</returns>
/// <since_tizen> 3 </since_tizen>
public string GetVideoCodec()
{
return GetCodecInfo(false);
}
/// <summary>
/// Gets the duration.
/// </summary>
/// <returns>The duration of the stream.</returns>
/// <remarks>
/// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
/// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
/// </remarks>
/// <exception cref="ObjectDisposedException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
/// </exception>\
/// <seealso cref="GetDurationNanoseconds"/>
/// <since_tizen> 3 </since_tizen>
public int GetDuration()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
NativePlayer.GetDuration(Player.Handle, out var duration).
ThrowIfFailed(Player, "Failed to get the duration");
Log.Info(PlayerLog.Tag, "get duration : " + duration);
return duration;
}
/// <summary>
/// Gets the duration in nanoseconds.
/// </summary>
/// <returns>The duration of the stream.</returns>
/// <remarks>
/// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
/// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
/// </remarks>
/// <exception cref="ObjectDisposedException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
/// </exception>
/// <seealso cref="GetDuration"/>
/// <since_tizen> 5 </since_tizen>
public long GetDurationNanoseconds()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
NativePlayer.GetDurationNanoseconds(Player.Handle, out var duration).
ThrowIfFailed(Player, "Failed to get the duration in nanoseconds");
Log.Info(PlayerLog.Tag, "get duration(nsec) : " + duration);
return duration;
}
/// <summary>
/// Gets the properties of the audio.
/// </summary>
/// <returns>A <see cref="AudioStreamProperties"/> that contains the audio stream information.</returns>
/// <remarks>
/// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
/// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
/// </remarks>
/// <exception cref="ObjectDisposedException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
/// </exception>
/// <since_tizen> 3 </since_tizen>
public AudioStreamProperties GetAudioProperties()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
NativePlayer.GetAudioStreamInfo(Player.Handle, out var sampleRate,
out var channels, out var bitRate).
ThrowIfFailed(Player, "Failed to get audio stream info");
return new AudioStreamProperties(sampleRate, channels, bitRate);
}
/// <summary>
/// Gets the properties of the video.
/// </summary>
/// <returns>A <see cref="VideoStreamProperties"/> that contains the video stream information.</returns>
/// <remarks>
/// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
/// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
/// </remarks>
/// <exception cref="ObjectDisposedException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
/// </exception>
/// <since_tizen> 3 </since_tizen>
public VideoStreamProperties GetVideoProperties()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
NativePlayer.GetVideoStreamInfo(Player.Handle, out var fps, out var bitRate).
ThrowIfFailed(Player, "Failed to get the video stream info");
return new VideoStreamProperties(fps, bitRate, GetVideoSize());
}
private Size GetVideoSize()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
NativePlayer.GetVideoSize(Player.Handle, out var width, out var height).
ThrowIfFailed(Player, "Failed to get the video size");
return new Size(width, height);
}
/// <summary>
/// Gets the metadata with the specified key.
/// </summary>
/// <returns>A string that represents the value of the specified key.</returns>
/// <param name="key">The key to query.</param>
/// <remarks>
/// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
/// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.</remarks>
/// <exception cref="ObjectDisposedException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
/// </exception>
/// <since_tizen> 3 </since_tizen>
public string GetMetadata(StreamMetadataKey key)
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key, nameof(key));
IntPtr ptr = IntPtr.Zero;
try
{
NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
ThrowIfFailed(Player, $"Failed to get the meta data with the key '{ key }'");
return Marshal.PtrToStringAnsi(ptr);
}
finally
{
LibcSupport.Free(ptr);
}
}
/// <summary>
/// Gets the <see cref="Multimedia.Player"/> that owns this instance.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public Player Player { get; }
}
}