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 /// <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="NotSupportedException">The file is unsupported format.</exception>
61 /// <exception cref="System.IO.FileNotFoundException">The file does not exist.</exception>
62 public MetadataEditor(string path)
66 throw new ArgumentNullException(nameof(path));
69 MetadataEditorError ret = Interop.MetadataEditor.Create(out _handle);
70 MetadataEditorErrorFactory.ThrowIfError(ret, "Failed to create metadata");
74 MetadataEditorErrorFactory.ThrowIfError(
75 Interop.MetadataEditor.SetPath(MetadataHandle, path), "Failed to set path");
79 Interop.MetadataEditor.Destroy(_handle);
80 _handle = IntPtr.Zero;
85 private string GetParam(MetadataEditorAttr attr)
87 IntPtr val = IntPtr.Zero;
91 MetadataEditorError e = Interop.MetadataEditor.GetMetadata(MetadataHandle, attr, out val);
92 MetadataEditorErrorFactory.ThrowIfError(e, "Failed to get metadata");
94 return Marshal.PtrToStringAnsi(val);
98 Interop.Libc.Free(val);
102 private void SetParam(MetadataEditorAttr attr, string value)
104 MetadataEditorErrorFactory.ThrowIfError(
105 Interop.MetadataEditor.SetMetadata(MetadataHandle, attr, value), "Fail to set value");
115 return GetParam(MetadataEditorAttr.Artist);
120 SetParam(MetadataEditorAttr.Artist, value);
131 return GetParam(MetadataEditorAttr.Title);
136 SetParam(MetadataEditorAttr.Title, value);
141 /// Album name of media
147 return GetParam(MetadataEditorAttr.Album);
152 SetParam(MetadataEditorAttr.Album, value);
163 return GetParam(MetadataEditorAttr.Genre);
168 SetParam(MetadataEditorAttr.Genre, value);
179 return GetParam(MetadataEditorAttr.Author);
184 SetParam(MetadataEditorAttr.Author, value);
189 /// Copyright of media
191 public string Copyright
195 return GetParam(MetadataEditorAttr.Copyright);
200 SetParam(MetadataEditorAttr.Copyright, value);
208 /// If the added media contains ID3 tag, This parameter refers to the recording time.
209 /// If the added media is a mp4 format, This parameter refers to the year.
215 return GetParam(MetadataEditorAttr.Date);
220 SetParam(MetadataEditorAttr.Date, value);
225 /// Description of media
227 public string Description
231 return GetParam(MetadataEditorAttr.Description);
236 SetParam(MetadataEditorAttr.Description, value);
243 public string Comment
247 return GetParam(MetadataEditorAttr.Comment);
252 SetParam(MetadataEditorAttr.Comment, value);
257 /// Track number of media
259 public string TrackNumber
263 return GetParam(MetadataEditorAttr.TrackNumber);
268 SetParam(MetadataEditorAttr.TrackNumber, value);
273 /// Album art count of media
275 public string PictureCount
279 return GetParam(MetadataEditorAttr.PictureCount);
284 /// Conductor of media
286 public string Conductor
290 return GetParam(MetadataEditorAttr.Conductor);
295 SetParam(MetadataEditorAttr.Conductor, value);
300 /// Unsynchronized lyric of media
302 public string UnsyncLyrics
306 return GetParam(MetadataEditorAttr.UnsyncLyrics);
311 SetParam(MetadataEditorAttr.UnsyncLyrics, value);
316 /// Writes the modified metadata to a media file
318 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
321 MetadataEditorErrorFactory.ThrowIfError(
322 Interop.MetadataEditor.UpdateMetadata(MetadataHandle), "Failed to update file");
326 /// Gets the artwork image in a media file
328 /// <param name="index"> Index of picture to import </param>
329 /// <returns> Artwork included in the media file</returns>
330 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
331 /// <exception cref="ArgumentOutOfRangeException"> Wrong index number </exception>
332 public Artwork GetPicture(int index)
336 throw new ArgumentOutOfRangeException("Index should be larger than 0 [" + index + "]");
339 IntPtr data = IntPtr.Zero;
341 IntPtr mimeType = IntPtr.Zero;
345 MetadataEditorErrorFactory.ThrowIfError(
346 Interop.MetadataEditor.GetPicture(MetadataHandle, index, out data, out size, out mimeType), "Failed to get the value");
350 byte[] tmpBuf = new byte[size];
351 Marshal.Copy(data, tmpBuf, 0, size);
353 return new Artwork(tmpBuf, Marshal.PtrToStringAnsi(mimeType));
360 if (data != IntPtr.Zero)
362 Interop.Libc.Free(data);
365 if (mimeType != IntPtr.Zero)
367 Interop.Libc.Free(mimeType);
373 /// Appends the picture to the media file.
375 /// <param name="path">The path of picture for adding to the metadata.</param>
376 /// <exception cref="InvalidOperationException">Internal error occurs.</exception>
377 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
378 public void AddPicture(string path)
382 throw new ArgumentNullException(nameof(path));
385 MetadataEditorErrorFactory.ThrowIfError(
386 Interop.MetadataEditor.AddPicture(MetadataHandle, path), "Failed to append picture");
390 /// Removes the picture from the media file.
392 /// <param name="index"> Index of picture to remove </param>
393 /// <exception cref="InvalidOperationException"> When internal process error is occured</exception>
394 /// <exception cref="ArgumentOutOfRangeException"> Wrong index number </exception>
395 public void RemovePicture(int index)
399 throw new ArgumentOutOfRangeException("Index should be larger than 0 [" + index + "]");
402 MetadataEditorErrorFactory.ThrowIfError(
403 Interop.MetadataEditor.RemovePicture(MetadataHandle, index), "Failed to remove picture");
407 /// Metadata Editor destructor
414 protected virtual void Dispose(bool disposing)
420 // To be used if there are any other disposable objects
423 if (_handle != IntPtr.Zero)
425 Interop.MetadataEditor.Destroy(_handle);
426 _handle = IntPtr.Zero;
433 public void Dispose()
436 GC.SuppressFinalize(this);