5fa70fba84cfa6c58099a03664b0060d3efb3aab
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Recorder / Interop / Interop.Recorder.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using Tizen.Multimedia;
20
21 internal static partial class Interop
22 {
23
24     internal static partial class Recorder
25     {
26         [DllImport(Libraries.Recorder, EntryPoint = "recorder_create_audiorecorder")]
27         internal static extern RecorderErrorCode Create(out RecorderHandle handle);
28
29         [DllImport(Libraries.Recorder, EntryPoint = "recorder_create_videorecorder")]
30         internal static extern RecorderErrorCode CreateVideo(IntPtr cameraHandle, out RecorderHandle handle);
31
32         [DllImport(Libraries.Recorder, EntryPoint = "recorder_prepare")]
33         internal static extern RecorderErrorCode Prepare(RecorderHandle handle);
34
35         [DllImport(Libraries.Recorder, EntryPoint = "recorder_unprepare")]
36         internal static extern RecorderErrorCode Unprepare(RecorderHandle handle);
37
38         [DllImport(Libraries.Recorder, EntryPoint = "recorder_start")]
39         internal static extern RecorderErrorCode Start(RecorderHandle handle);
40
41         [DllImport(Libraries.Recorder, EntryPoint = "recorder_pause")]
42         internal static extern RecorderErrorCode Pause(RecorderHandle handle);
43
44         [DllImport(Libraries.Recorder, EntryPoint = "recorder_commit")]
45         internal static extern RecorderErrorCode Commit(RecorderHandle handle);
46
47         [DllImport(Libraries.Recorder, EntryPoint = "recorder_cancel")]
48         internal static extern RecorderErrorCode Cancel(RecorderHandle handle);
49
50         [DllImport(Libraries.Recorder, EntryPoint = "recorder_get_state")]
51         internal static extern RecorderErrorCode GetState(RecorderHandle handle, out RecorderState state);
52
53         [DllImport(Libraries.Recorder, EntryPoint = "recorder_set_sound_stream_info")]
54         internal static extern RecorderErrorCode SetAudioStreamPolicy(RecorderHandle handle, AudioStreamPolicyHandle streamInfoHandle);
55
56         [DllImport(Libraries.Recorder, EntryPoint = "recorder_get_device_state")]
57         internal static extern RecorderErrorCode GetDeviceState(RecorderType type, out RecorderDeviceState state);
58     }
59
60     internal class RecorderHandle : SafeHandle
61     {
62         [DllImport(Libraries.Recorder, EntryPoint = "recorder_destroy")]
63         private static extern RecorderErrorCode Destroy(IntPtr handle);
64
65         [DllImport(Libraries.Recorder, EntryPoint = "recorder_cancel")]
66         private static extern RecorderErrorCode Cancel(IntPtr handle);
67
68         [DllImport(Libraries.Recorder, EntryPoint = "recorder_unprepare")]
69         private static extern RecorderErrorCode Unprepare(IntPtr handle);
70
71         protected RecorderHandle() : base(IntPtr.Zero, true)
72         {
73         }
74
75         public override bool IsInvalid => handle == IntPtr.Zero;
76
77         protected override bool ReleaseHandle()
78         {
79             Cancel(handle).Ignore(RecorderErrorCode.InvalidState).ThrowIfError("Failed to cancel");
80             Unprepare(handle).Ignore(RecorderErrorCode.InvalidState).ThrowIfError("Failed to unprepare");
81
82             var ret = Destroy(handle);
83             if (ret != RecorderErrorCode.None)
84             {
85                 Tizen.Log.Debug(GetType().FullName, $"Failed to release native RecorderHandle.");
86
87                 return false;
88             }
89             return true;
90         }
91     }
92 }