[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.MediaCodec / MediaCodec / MediaCodecError.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.IO;
19 using Tizen.Internals.Errors;
20
21 namespace Tizen.Multimedia.MediaCodec
22 {
23     internal enum MediaCodecErrorCode
24     {
25         CodecDefinedBase = -0x019B0000,
26
27         None = ErrorCode.None,
28         OutOfMemory = ErrorCode.OutOfMemory,
29         InvalidParameter = ErrorCode.InvalidParameter,
30         InvalidOperation = ErrorCode.InvalidOperation,
31         NotSupportedOnDevice = ErrorCode.NotSupported,
32         PermissionDenied = ErrorCode.PermissionDenied,
33
34         InvalidState = CodecDefinedBase | 0x01,
35         InvalidInBuffer = CodecDefinedBase | 0x02,
36         InvalidOutBuffer = CodecDefinedBase | 0x03,
37         Internal = CodecDefinedBase | 0x04,
38         NotInitialized = CodecDefinedBase | 0x05,
39         InvalidStream = CodecDefinedBase | 0x06,
40         CodecNotFound = CodecDefinedBase | 0x07,
41         DecodingError = CodecDefinedBase | 0x08,
42         OutOfStorage = CodecDefinedBase | 0x09,
43         StreamNotFound = CodecDefinedBase | 0x0a,
44         NotSupportedFormat = CodecDefinedBase | 0x0b,
45         NoAvailableBuffer = CodecDefinedBase | 0x0c,
46         OverflowInBuffer = CodecDefinedBase | 0x0d,
47         ResourceOverloaded = CodecDefinedBase | 0x0e,
48         ResourceConflict = CodecDefinedBase | 0x0f
49     }
50
51     /// <summary>
52     /// Specifies the errors of <see cref="MediaCodec"/>.
53     /// </summary>
54     /// <seealso cref="MediaCodec.ErrorOccurred"/>
55     /// <since_tizen> 3 </since_tizen>
56     public enum MediaCodecError
57     {
58         /// <summary>
59         /// The format is not supported.
60         /// </summary>
61         NotSupportedFormat = MediaCodecErrorCode.NotSupportedFormat,
62
63         /// <summary>
64         /// An internal error.
65         /// </summary>
66         InternalError = MediaCodecErrorCode.Internal,
67
68         /// <summary>
69         /// Not enough storage.
70         /// </summary>
71         OutOfStorage = MediaCodecErrorCode.OutOfStorage,
72
73         /// <summary>
74         /// The stream is invalid.
75         /// </summary>
76         InvalidStream = MediaCodecErrorCode.InvalidStream,
77     }
78
79     internal static class MediaCodecErrorExtensions
80     {
81         internal static void ThrowIfFailed(this MediaCodecErrorCode errorCode, string message)
82         {
83             if (errorCode == MediaCodecErrorCode.None)
84             {
85                 return;
86             }
87
88             string msg = $"{ (message ?? "Operation failed") } : { errorCode.ToString() }.";
89
90             switch (errorCode)
91             {
92                 case MediaCodecErrorCode.OutOfMemory:
93                     throw new OutOfMemoryException(msg);
94
95                 case MediaCodecErrorCode.InvalidParameter:
96                     throw new ArgumentException(msg);
97
98                 case MediaCodecErrorCode.CodecNotFound:
99                 case MediaCodecErrorCode.DecodingError:
100                 case MediaCodecErrorCode.InvalidOperation:
101                 case MediaCodecErrorCode.InvalidState:
102                 case MediaCodecErrorCode.InvalidInBuffer:
103                 case MediaCodecErrorCode.InvalidOutBuffer:
104                 case MediaCodecErrorCode.Internal:
105                 case MediaCodecErrorCode.InvalidStream:
106                 case MediaCodecErrorCode.NotInitialized:
107                 case MediaCodecErrorCode.ResourceConflict:
108                 case MediaCodecErrorCode.ResourceOverloaded:
109                 case MediaCodecErrorCode.StreamNotFound:
110                     throw new InvalidOperationException(msg);
111
112                 case MediaCodecErrorCode.NotSupportedOnDevice:
113                 case MediaCodecErrorCode.NotSupportedFormat:
114                     throw new NotSupportedException(msg);
115
116                 case MediaCodecErrorCode.PermissionDenied:
117                     throw new UnauthorizedAccessException(msg);
118
119                 case MediaCodecErrorCode.OutOfStorage:
120                     throw new IOException($"{msg}; Not enough disk space or specified path is not available to .");
121
122                 case MediaCodecErrorCode.NoAvailableBuffer:
123                 case MediaCodecErrorCode.OverflowInBuffer:
124                     throw new InternalBufferOverflowException(msg);
125
126                 default:
127                     throw new Exception($"Unknown error : {errorCode.ToString()}.");
128             }
129         }
130     }
131 }