2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Runtime.InteropServices;
21 namespace Tizen.Multimedia
23 static internal class MetadataEditorLog
25 internal const string LogTag = "Tizen.Multimedia.MetadataEditor";
29 /// The Metadata editor class provides a set of functions to edit the metadata of the media file
32 /// If you want to access only internal storage,
33 /// you should add privilege http://tizen.org/privilege/mediastorage. \n
34 /// Or if you want to access only external storage,
35 /// you should add privilege http://tizen.org/privilege/externalstorage. \n
37 public class MetadataEditor : IDisposable
39 private bool _disposed = false;
40 private IntPtr _handle = IntPtr.Zero;
42 private IntPtr MetadataHandle
46 if (_handle == IntPtr.Zero)
48 throw new ObjectDisposedException(nameof(MetadataEditor));
56 /// Initializes a new instance of the <see cref="MetadataEditor"/> class with the specified path.
58 /// <since_tizen> 3 </since_tizen>
59 /// <param name="path"> The path of the media file to edit metadata </param>
60 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
61 /// <exception cref="NotSupportedException">The file is unsupported format.</exception>
62 /// <exception cref="System.IO.FileNotFoundException">The file does not exist.</exception>
63 public MetadataEditor(string path)
67 throw new ArgumentNullException(nameof(path));
70 MetadataEditorError ret = Interop.MetadataEditor.Create(out _handle);
71 MetadataEditorErrorFactory.ThrowIfError(ret, "Failed to create metadata");
75 MetadataEditorErrorFactory.ThrowIfError(
76 Interop.MetadataEditor.SetPath(MetadataHandle, path), "Failed to set path");
80 Interop.MetadataEditor.Destroy(_handle);
81 _handle = IntPtr.Zero;
86 private string GetParam(MetadataEditorAttr attr)
88 IntPtr val = IntPtr.Zero;
92 MetadataEditorError e = Interop.MetadataEditor.GetMetadata(MetadataHandle, attr, out val);
93 MetadataEditorErrorFactory.ThrowIfError(e, "Failed to get metadata");
95 return Marshal.PtrToStringAnsi(val);
99 Interop.Libc.Free(val);
103 private void SetParam(MetadataEditorAttr attr, string value)
105 MetadataEditorErrorFactory.ThrowIfError(
106 Interop.MetadataEditor.SetMetadata(MetadataHandle, attr, value), "Fail to set value");
112 /// <since_tizen> 3 </since_tizen>
117 return GetParam(MetadataEditorAttr.Artist);
122 SetParam(MetadataEditorAttr.Artist, value);
129 /// <since_tizen> 3 </since_tizen>
134 return GetParam(MetadataEditorAttr.Title);
139 SetParam(MetadataEditorAttr.Title, value);
144 /// Album name of media
146 /// <since_tizen> 3 </since_tizen>
151 return GetParam(MetadataEditorAttr.Album);
156 SetParam(MetadataEditorAttr.Album, value);
163 /// <since_tizen> 3 </since_tizen>
168 return GetParam(MetadataEditorAttr.Genre);
173 SetParam(MetadataEditorAttr.Genre, value);
180 /// <since_tizen> 3 </since_tizen>
185 return GetParam(MetadataEditorAttr.Author);
190 SetParam(MetadataEditorAttr.Author, value);
195 /// Copyright of media
197 /// <since_tizen> 3 </since_tizen>
198 public string Copyright
202 return GetParam(MetadataEditorAttr.Copyright);
207 SetParam(MetadataEditorAttr.Copyright, value);
214 /// <since_tizen> 3 </since_tizen>
216 /// If the added media contains ID3 tag, This parameter refers to the recording time.
217 /// If the added media is a mp4 format, This parameter refers to the year.
223 return GetParam(MetadataEditorAttr.Date);
228 SetParam(MetadataEditorAttr.Date, value);
233 /// Description of media
235 /// <since_tizen> 3 </since_tizen>
236 public string Description
240 return GetParam(MetadataEditorAttr.Description);
245 SetParam(MetadataEditorAttr.Description, value);
252 /// <since_tizen> 3 </since_tizen>
253 public string Comment
257 return GetParam(MetadataEditorAttr.Comment);
262 SetParam(MetadataEditorAttr.Comment, value);
267 /// Track number of media
269 /// <since_tizen> 3 </since_tizen>
270 public string TrackNumber
274 return GetParam(MetadataEditorAttr.TrackNumber);
279 SetParam(MetadataEditorAttr.TrackNumber, value);
284 /// Album art count of media
286 /// <since_tizen> 3 </since_tizen>
287 public string PictureCount
291 return GetParam(MetadataEditorAttr.PictureCount);
296 /// Conductor of media
298 /// <since_tizen> 3 </since_tizen>
299 public string Conductor
303 return GetParam(MetadataEditorAttr.Conductor);
308 SetParam(MetadataEditorAttr.Conductor, value);
313 /// Unsynchronized lyric of media
315 /// <since_tizen> 3 </since_tizen>
316 public string UnsyncLyrics
320 return GetParam(MetadataEditorAttr.UnsyncLyrics);
325 SetParam(MetadataEditorAttr.UnsyncLyrics, value);
330 /// Writes the modified metadata to a media file
332 /// <since_tizen> 3 </since_tizen>
333 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
336 MetadataEditorErrorFactory.ThrowIfError(
337 Interop.MetadataEditor.UpdateMetadata(MetadataHandle), "Failed to update file");
341 /// Gets the artwork image in a media file
343 /// <since_tizen> 3 </since_tizen>
344 /// <param name="index"> Index of picture to import </param>
345 /// <returns> Artwork included in the media file</returns>
346 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
347 /// <exception cref="ArgumentOutOfRangeException"> Wrong index number </exception>
348 public Artwork GetPicture(int index)
352 throw new ArgumentOutOfRangeException("Index should be larger than 0 [" + index + "]");
355 IntPtr data = IntPtr.Zero;
357 IntPtr mimeType = IntPtr.Zero;
361 MetadataEditorErrorFactory.ThrowIfError(
362 Interop.MetadataEditor.GetPicture(MetadataHandle, index, out data, out size, out mimeType), "Failed to get the value");
366 byte[] tmpBuf = new byte[size];
367 Marshal.Copy(data, tmpBuf, 0, size);
369 return new Artwork(tmpBuf, Marshal.PtrToStringAnsi(mimeType));
376 if (data != IntPtr.Zero)
378 Interop.Libc.Free(data);
381 if (mimeType != IntPtr.Zero)
383 Interop.Libc.Free(mimeType);
389 /// Appends the picture to the media file.
391 /// <since_tizen> 3 </since_tizen>
392 /// <param name="path">The path of picture for adding to the metadata.</param>
393 /// <exception cref="InvalidOperationException">Internal error occurs.</exception>
394 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
395 public void AddPicture(string path)
399 throw new ArgumentNullException(nameof(path));
402 MetadataEditorErrorFactory.ThrowIfError(
403 Interop.MetadataEditor.AddPicture(MetadataHandle, path), "Failed to append picture");
407 /// Removes the picture from the media file.
409 /// <since_tizen> 3 </since_tizen>
410 /// <param name="index"> Index of picture to remove </param>
411 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
412 /// <exception cref="ArgumentOutOfRangeException"> Wrong index number </exception>
413 public void RemovePicture(int index)
417 throw new ArgumentOutOfRangeException("Index should be larger than 0 [" + index + "]");
420 MetadataEditorErrorFactory.ThrowIfError(
421 Interop.MetadataEditor.RemovePicture(MetadataHandle, index), "Failed to remove picture");
425 /// Metadata Editor destructor
427 /// <since_tizen> 3 </since_tizen>
433 protected virtual void Dispose(bool disposing)
439 // To be used if there are any other disposable objects
442 if (_handle != IntPtr.Zero)
444 Interop.MetadataEditor.Destroy(_handle);
445 _handle = IntPtr.Zero;
452 public void Dispose()
455 GC.SuppressFinalize(this);