Release 4.0.0-preview1-00183
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.StreamRecorder / Interop / Interop.StreamRecorder.cs
1 using System;
2 using System.Runtime.InteropServices;
3 using Tizen.Multimedia;
4
5 internal static partial class Interop
6 {
7     internal static partial class StreamRecorder
8     {
9         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_create")]
10         internal static extern StreamRecorderErrorCode Create(out StreamRecorderHandle handle);
11
12         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_prepare")]
13         internal static extern StreamRecorderErrorCode Prepare(StreamRecorderHandle handle);
14
15         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_unprepare")]
16         internal static extern StreamRecorderErrorCode Unprepare(StreamRecorderHandle handle);
17
18         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_start")]
19         internal static extern StreamRecorderErrorCode Start(StreamRecorderHandle handle);
20
21         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_pause")]
22         internal static extern StreamRecorderErrorCode Pause(StreamRecorderHandle handle);
23
24         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_commit")]
25         internal static extern StreamRecorderErrorCode Commit(StreamRecorderHandle handle);
26
27         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_cancel")]
28         internal static extern StreamRecorderErrorCode Cancel(StreamRecorderHandle handle);
29
30         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_push_stream_buffer")]
31         internal static extern StreamRecorderErrorCode PushStreamBuffer(StreamRecorderHandle handle,
32             IntPtr mediaPacketHandle);
33
34         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_enable_source_buffer")]
35         internal static extern StreamRecorderErrorCode EnableSourceBuffer(StreamRecorderHandle handle,
36             StreamRecorderSourceType type);
37
38         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_get_state")]
39         internal static extern StreamRecorderErrorCode GetState(StreamRecorderHandle handle, out RecorderState state);
40     }
41
42     internal class StreamRecorderHandle : SafeHandle
43     {
44         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_destroy")]
45         private static extern StreamRecorderErrorCode Destroy(IntPtr handle);
46
47         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_cancel")]
48         private static extern StreamRecorderErrorCode Cancel(IntPtr handle);
49
50         [DllImport(Libraries.StreamRecorder, EntryPoint = "streamrecorder_unprepare")]
51         private static extern StreamRecorderErrorCode Unprepare(IntPtr handle);
52
53         protected StreamRecorderHandle() : base(IntPtr.Zero, true)
54         {
55         }
56
57         public override bool IsInvalid => handle == IntPtr.Zero;
58
59         protected override bool ReleaseHandle()
60         {
61             try
62             {
63                 Cancel(handle).Ignore(StreamRecorderErrorCode.InvalidState).ThrowIfError("Failed to cancel.");
64                 Unprepare(handle).Ignore(StreamRecorderErrorCode.InvalidState).ThrowIfError("Failed to unprepare.");
65                 Destroy(handle).ThrowIfError("Failed to destory.");
66
67                 return true;
68             }
69             catch (Exception e)
70             {
71                 Tizen.Log.Debug(GetType().FullName, $"Failed to release native RecorderHandle; {e.Message}");
72
73                 return false;
74             }
75         }
76     }
77 }