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 /// Metadata extractor constructor
58 /// <param name="path"> The path of the media file to edit metadata </param>
59 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
60 /// <exception cref="OutOfMemoryException">Memory allocation failed.</exception>
61 /// <exception cref="NotSupportedException">Unsupported file type</exception>
62 /// <exception cref="FileNotFoundException">File 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");
116 return GetParam(MetadataEditorAttr.Artist);
121 SetParam(MetadataEditorAttr.Artist, value);
132 return GetParam(MetadataEditorAttr.Title);
137 SetParam(MetadataEditorAttr.Title, value);
142 /// Album name of media
148 return GetParam(MetadataEditorAttr.Album);
153 SetParam(MetadataEditorAttr.Album, value);
164 return GetParam(MetadataEditorAttr.Genre);
169 SetParam(MetadataEditorAttr.Genre, value);
180 return GetParam(MetadataEditorAttr.Author);
185 SetParam(MetadataEditorAttr.Author, value);
190 /// Copyright of media
192 public string Copyright
196 return GetParam(MetadataEditorAttr.Copyright);
201 SetParam(MetadataEditorAttr.Copyright, value);
209 /// If the added media contains ID3 tag, This parameter refers to the recording time.
210 /// If the added media is a mp4 format, This parameter refers to the year.
216 return GetParam(MetadataEditorAttr.Date);
221 SetParam(MetadataEditorAttr.Date, value);
226 /// Description of media
228 public string Description
232 return GetParam(MetadataEditorAttr.Description);
237 SetParam(MetadataEditorAttr.Description, value);
244 public string Comment
248 return GetParam(MetadataEditorAttr.Comment);
253 SetParam(MetadataEditorAttr.Comment, value);
258 /// Track number of media
260 public string TrackNumber
264 return GetParam(MetadataEditorAttr.TrackNumber);
269 SetParam(MetadataEditorAttr.TrackNumber, value);
274 /// Album art count of media
276 public string PictureCount
280 return GetParam(MetadataEditorAttr.PictureCount);
285 /// Conductor of media
287 public string Conductor
291 return GetParam(MetadataEditorAttr.Conductor);
296 SetParam(MetadataEditorAttr.Conductor, value);
301 /// Unsynchronized lyric of media
303 public string UnsyncLyrics
307 return GetParam(MetadataEditorAttr.UnsyncLyrics);
312 SetParam(MetadataEditorAttr.UnsyncLyrics, value);
317 /// Writes the modified metadata to a media file
319 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
322 MetadataEditorErrorFactory.ThrowIfError(
323 Interop.MetadataEditor.UpdateMetadata(MetadataHandle), "Failed to update file");
327 /// Gets the artwork image in a media file
329 /// <param name="index"> Index of picture to import </param>
330 /// <returns> Artwork included in the media file</returns>
331 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
332 /// <exception cref="ArgumentOutOfRangeException"> Wrong index number </exception>
333 public Artwork GetPicture(int index)
337 throw new ArgumentOutOfRangeException("Index should be larger than 0 [" + index + "]");
340 IntPtr data = IntPtr.Zero;
342 IntPtr mimeType = IntPtr.Zero;
346 MetadataEditorErrorFactory.ThrowIfError(
347 Interop.MetadataEditor.GetPicture(MetadataHandle, index, out data, out size, out mimeType), "Failed to get the value");
351 byte[] tmpBuf = new byte[size];
352 Marshal.Copy(data, tmpBuf, 0, size);
354 return new Artwork(tmpBuf, Marshal.PtrToStringAnsi(mimeType));
361 if (data != IntPtr.Zero)
363 Interop.Libc.Free(data);
366 if (mimeType != IntPtr.Zero)
368 Interop.Libc.Free(mimeType);
374 /// Append the picture to the media file
376 /// <param name="path"> The path of picture for adding to the metadata </param>
377 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
378 /// <exception cref="ArgumentNullException"> Picture path is null</exception>
379 public void AddPicture(string path)
383 throw new ArgumentNullException(nameof(path));
386 MetadataEditorErrorFactory.ThrowIfError(
387 Interop.MetadataEditor.AddPicture(MetadataHandle, path), "Failed to append picture");
391 /// Remove the picture from the media file
393 /// <param name="index"> Index of picture to remove </param>
394 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
395 /// <exception cref="ArgumentOutOfRangeException"> Wrong index number </exception>
396 public void RemovePicture(int index)
400 throw new ArgumentOutOfRangeException("Index should be larger than 0 [" + index + "]");
403 MetadataEditorErrorFactory.ThrowIfError(
404 Interop.MetadataEditor.RemovePicture(MetadataHandle, index), "Failed to remove picture");
408 /// Metadata Editor destructor
415 protected virtual void Dispose(bool disposing)
421 // To be used if there are any other disposable objects
424 if (_handle != IntPtr.Zero)
426 Interop.MetadataEditor.Destroy(_handle);
427 _handle = IntPtr.Zero;
434 public void Dispose()
437 GC.SuppressFinalize(this);