/
MetadataExtractor.cs
328 lines (282 loc) · 10.9 KB
/
MetadataExtractor.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
/*
* 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.IO;
using System.Runtime.InteropServices;
using Native = Interop.MetadataExtractor;
namespace Tizen.Multimedia
{
/// <summary>
/// Provides a means to get the metadata from a media file.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class MetadataExtractor : IDisposable
{
private bool _disposed = false;
private IntPtr _handle = IntPtr.Zero;
private IntPtr _buffer = IntPtr.Zero;
private void Create(Func<MetadataExtractorError> initFunc)
{
MetadataExtractorRetValidator.ThrowIfError(
Native.Create(out _handle), "Failed to create metadata");
try
{
MetadataExtractorRetValidator.ThrowIfError(initFunc(), "Failed to init");
_metadata = new Lazy<Metadata>(() => new Metadata(this));
}
catch
{
Release();
throw;
}
}
/// <summary>
/// Initializes a new instance of the MetadataExtractor class with the specified path.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="path">The path for the file to extract the metadata.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
public MetadataExtractor(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
Create(() => Native.SetPath(_handle, path));
}
/// <summary>
/// Initializes a new instance of the MetadataExtractor class with the specified buffer.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="buffer">The buffer to extract the metadata.</param>
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
/// <exception cref="ArgumentException">The length of <paramref name="buffer"/> is zero.</exception>
public MetadataExtractor(byte[] buffer)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (buffer.Length == 0)
{
throw new ArgumentException("buffer length is zero.", nameof(buffer));
}
_buffer = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, _buffer, buffer.Length);
try
{
Create(() => Native.SetBuffer(_handle, _buffer, buffer.Length));
}
catch (Exception)
{
Marshal.FreeHGlobal(_buffer);
throw;
}
}
internal IntPtr Handle
{
get
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(MetadataExtractor));
}
return _handle;
}
}
private Lazy<Metadata> _metadata;
/// <summary>
/// Retrieves the <see cref="Metadata"/>.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>The <see cref="Metadata"/> for the given source.</returns>
/// <exception cref="InvalidOperationException">An internal process error occurs.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MetadataExtractor"/> has been already disposed of.</exception>
public Metadata GetMetadata()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(MetadataExtractor));
}
return _metadata.Value;
}
/// <summary>
/// Gets the artwork image in the source.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>The <see cref="Artwork"/> if it exists, otherwise null.</returns>
/// <exception cref="InvalidOperationException">An internal process error occurs.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MetadataExtractor"/> has been already disposed of.</exception>
public Artwork GetArtwork()
{
IntPtr data = IntPtr.Zero;
IntPtr mimeType = IntPtr.Zero;
try
{
int size = 0;
var ret = Native.GetArtwork(Handle, out data, out size, out mimeType);
MetadataExtractorRetValidator.ThrowIfError(ret, "Failed to get value");
if (size > 0)
{
var buf = new byte[size];
Marshal.Copy(data, buf, 0, size);
return new Artwork(buf, Marshal.PtrToStringAnsi(mimeType));
}
return null;
}
finally
{
Marshal.FreeHGlobal(data);
Marshal.FreeHGlobal(mimeType);
}
}
/// <summary>
/// Gets the sync lyrics of the source.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="index">The index of lyrics to retrieve.</param>
/// <returns>The <see cref="SyncLyrics"/> object if <paramref name="index"/> is valid, otherwise null.</returns>
/// <exception cref="InvalidOperationException">An internal process error occurs.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MetadataExtractor"/> has been already disposed of.</exception>
public SyncLyrics GetSyncLyrics(int index)
{
IntPtr lyrics = IntPtr.Zero;
try
{
uint timestamp = 0;
var ret = Native.GetSynclyrics(Handle, index, out timestamp, out lyrics);
MetadataExtractorRetValidator.ThrowIfError(ret, "Failed to get sync lyrics");
if (lyrics == IntPtr.Zero)
{
return null;
}
return new SyncLyrics(Marshal.PtrToStringAnsi(lyrics), timestamp);
}
finally
{
Marshal.FreeHGlobal(lyrics);
}
}
/// <summary>
/// Gets the frame of a video media.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>The raw thumbnail data in RGB888 if it exists, otherwise null.</returns>
/// <exception cref="InvalidOperationException">An internal process error occurs.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MetadataExtractor"/> has been already disposed of.</exception>
public byte[] GetVideoThumbnail()
{
IntPtr data = IntPtr.Zero;
try
{
int size = 0;
var ret = Native.GetFrame(Handle, out data, out size);
MetadataExtractorRetValidator.ThrowIfError(ret, "Failed to get value");
if (size == 0)
{
return null;
}
var buf = new byte[size];
Marshal.Copy(data, buf, 0, size);
return buf;
}
finally
{
Marshal.FreeHGlobal(data);
}
}
/// <summary>
/// Gets the frame of a video media.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="timeStamp">The timestamp in milliseconds.</param>
/// <param name="accurate">true to get an accurate frame for the given timestamp,
/// otherwise false to get the nearest i-frame of the video rapidly.</param>
/// <returns>The raw frame data in RGB888 if a frame at specified time exists, otherwise null.</returns>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MetadataExtractor"/> has been already disposed of.</exception>
public byte[] GetFrameAt(uint timeStamp, bool accurate)
{
IntPtr data = IntPtr.Zero;
try
{
int size = 0;
var ret = Native.GetFrameAtTime(Handle, timeStamp, accurate, out data, out size);
MetadataExtractorRetValidator.ThrowIfError(ret, "Failed to get value");
if (size == 0)
{
return null;
}
var buf = new byte[size];
Marshal.Copy(data, buf, 0, size);
return buf;
}
finally
{
Marshal.FreeHGlobal(data);
}
}
/// <summary>
/// Finalizes an instance of the MetadataExtractor class.
/// </summary>
~MetadataExtractor()
{
Dispose(false);
}
/// <summary>
/// Releases the resources used by the <see cref="MetadataExtractor"/> object.
/// </summary>
/// <param name="disposing">
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
/// </param>
/// <since_tizen> 3 </since_tizen>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
Release();
_disposed = true;
}
}
private void Release()
{
if (_buffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(_buffer);
_buffer = IntPtr.Zero;
}
if (_handle != IntPtr.Zero)
{
var ret = Native.Destroy(_handle);
if (ret != MetadataExtractorError.None)
{
Log.Error(typeof(MetadataExtractor).FullName, $"DestroyHandle failed : {ret}.");
}
_handle = IntPtr.Zero;
}
}
/// <summary>
/// Releases all resources used by the <see cref="MetadataExtractor"/> object.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}