Release 4.0.0-preview1-00201
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.AudioIO / AudioIO / AudioIOUtil.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.Diagnostics;
19 using System.Linq;
20
21 namespace Tizen.Multimedia
22 {
23     internal static class AudioIOUtil
24     {
25         internal static void ThrowIfError(int errorCode, string msg = null)
26         {
27             AudioIOError code = (AudioIOError)errorCode;
28             // 현재는 에러코드 최상위 exception으로 전달, 추후 상황에 맞게 케이스 처리해야 함.
29
30             msg = (msg == null ? "" : msg + " ") + $": { code }";
31
32             if (code > 0)
33             {
34                 Log.Info("Audio", "Code > 0, no error!!!!");
35                 return;
36             }
37
38             switch (code)
39             {
40                 case AudioIOError.None:
41                     break;
42                 case AudioIOError.OutOfMemory:
43                     Log.Info("Audio", "OutOfMemoryException");
44                     throw new OutOfMemoryException();
45                 case AudioIOError.InvalidParameter:
46                     Log.Info("Audio", "ArgumentException");
47                     throw new ArgumentException(msg);
48                 case AudioIOError.InvalidOperation:
49                     throw new InvalidOperationException(msg);
50                 case AudioIOError.PermissionDenied:
51                     Log.Info("Audio", "Permission Denied Error");
52                     throw new InvalidOperationException(msg);
53                 case AudioIOError.NotSupported:
54                     throw new NotSupportedException(msg);
55                 case AudioIOError.DevicePolicyRestriction:
56                     Log.Info("Audio", "Device_policy_restriction");
57                     throw new Exception("Device_policy_restriction");
58                 case AudioIOError.DeviceNotOpened:
59                     Log.Info("Audio", "Device Not Opened Error");
60                     throw new Exception("Device Not Opened Error");
61                 case AudioIOError.DeviceNotClosed:
62                     Log.Info("Audio", "Device Not Closed Error");
63                     throw new Exception("Device Not Closed Error");
64                 case AudioIOError.InvalidBuffer:
65                     Log.Info("Audio", "Invalid Buffer Error");
66                     throw new InvalidOperationException(msg);
67                 case AudioIOError.SoundPolicy:
68                     Log.Info("Audio", "Sound Policy Error");
69                     throw new Exception(msg);
70                 case AudioIOError.InvalidState:
71                     Log.Info("Audio", "Invalid State Error");
72                     throw new InvalidOperationException(msg);
73                 case AudioIOError.NotSupportedType:
74                     Log.Info("Audio", "Not supported stream type Error");
75                     throw new NotSupportedException(msg);
76                 default:
77                     Log.Info("Audio", "Unknown Exception" + code);
78                     throw new Exception(msg);
79             }
80         }
81
82         internal static void ValidateState(AudioIOState curState, params AudioIOState[] desiredStates)
83         {
84             Debug.Assert(desiredStates.Length > 0);
85
86             if (desiredStates.Contains(curState))
87             {
88                 return;
89             }
90
91             throw new InvalidOperationException($"The audio is not in a valid state. " +
92                 $"Current State : { curState }, Valid State : { string.Join(", ", desiredStates) }.");
93         }
94     }
95 }