[NUI] TCSACR-226 code change (#1032)
[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     internal static partial class Recorder
24     {
25         [DllImport(Libraries.Recorder, EntryPoint = "recorder_create_audiorecorder")]
26         internal static extern RecorderErrorCode Create(out RecorderHandle handle);
27
28         [DllImport(Libraries.Recorder, EntryPoint = "recorder_create_videorecorder")]
29         internal static extern RecorderErrorCode CreateVideo(IntPtr cameraHandle, out RecorderHandle handle);
30
31         [DllImport(Libraries.Recorder, EntryPoint = "recorder_prepare")]
32         internal static extern RecorderErrorCode Prepare(RecorderHandle handle);
33
34         [DllImport(Libraries.Recorder, EntryPoint = "recorder_unprepare")]
35         internal static extern RecorderErrorCode Unprepare(RecorderHandle handle);
36
37         [DllImport(Libraries.Recorder, EntryPoint = "recorder_start")]
38         internal static extern RecorderErrorCode Start(RecorderHandle handle);
39
40         [DllImport(Libraries.Recorder, EntryPoint = "recorder_pause")]
41         internal static extern RecorderErrorCode Pause(RecorderHandle handle);
42
43         [DllImport(Libraries.Recorder, EntryPoint = "recorder_commit")]
44         internal static extern RecorderErrorCode Commit(RecorderHandle handle);
45
46         [DllImport(Libraries.Recorder, EntryPoint = "recorder_cancel")]
47         internal static extern RecorderErrorCode Cancel(RecorderHandle handle);
48
49         [DllImport(Libraries.Recorder, EntryPoint = "recorder_get_state")]
50         internal static extern RecorderErrorCode GetState(RecorderHandle handle, out RecorderState state);
51
52         [DllImport(Libraries.Recorder, EntryPoint = "recorder_set_sound_stream_info")]
53         internal static extern RecorderErrorCode SetAudioStreamPolicy(RecorderHandle handle, AudioStreamPolicyHandle streamInfoHandle);
54
55         [DllImport(Libraries.Recorder, EntryPoint = "recorder_get_device_state")]
56         internal static extern RecorderErrorCode GetDeviceState(RecorderType type, out RecorderDeviceState state);
57     }
58
59     internal class RecorderHandle : SafeHandle
60     {
61         [DllImport(Libraries.Recorder, EntryPoint = "recorder_destroy")]
62         private static extern RecorderErrorCode Destroy(IntPtr handle);
63
64         [DllImport(Libraries.Recorder, EntryPoint = "recorder_cancel")]
65         private static extern RecorderErrorCode Cancel(IntPtr handle);
66
67         [DllImport(Libraries.Recorder, EntryPoint = "recorder_unprepare")]
68         private static extern RecorderErrorCode Unprepare(IntPtr handle);
69
70         protected RecorderHandle() : base(IntPtr.Zero, true)
71         {
72         }
73
74         public override bool IsInvalid => handle == IntPtr.Zero;
75
76         protected override bool ReleaseHandle()
77         {
78             Cancel(handle).Ignore(RecorderErrorCode.InvalidState).ThrowIfError("Failed to cancel");
79             Unprepare(handle).Ignore(RecorderErrorCode.InvalidState).ThrowIfError("Failed to unprepare");
80
81             var ret = Destroy(handle);
82             if (ret != RecorderErrorCode.None)
83             {
84                 Tizen.Log.Debug(GetType().FullName, $"Failed to release native RecorderHandle.");
85
86                 return false;
87             }
88             return true;
89         }
90     }
91 }