[MediaPlayer] add missing error code (#1781)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.MediaPlayer / Player / PlayerError.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
22 {
23     internal enum PlayerErrorCode
24     {
25         None = ErrorCode.None,
26         InvalidArgument = ErrorCode.InvalidParameter,
27         OutOfMemory = ErrorCode.OutOfMemory,
28         NoSuchFile = ErrorCode.NoSuchFile,
29         InvalidOperation = ErrorCode.InvalidOperation,
30         NoSpaceOnDevice = ErrorCode.FileNoSpaceOnDevice,
31         FeatureNotSupported = ErrorCode.NotSupported,
32         PermissionDenied = ErrorCode.PermissionDenied,
33         NoBufferSpace = ErrorCode.BufferSpace,
34         TizenPlayerError = -0x01940000,
35         PlayerErrorClass = TizenPlayerError | 0x20,
36         SeekFailed = PlayerErrorClass | 0x01,
37         InvalidState = PlayerErrorClass | 0x02,
38         NotSupportedFile = PlayerErrorClass | 0x03,
39         InvalidUri = PlayerErrorClass | 0x04,
40         SoundPolicyError = PlayerErrorClass | 0x05,
41         ConnectionFailed = PlayerErrorClass | 0x06,
42         VideoCaptureFailed = PlayerErrorClass | 0x07,
43         DrmExpired = PlayerErrorClass | 0x08,
44         DrmNoLicense = PlayerErrorClass | 0x09,
45         DrmFutureUse = PlayerErrorClass | 0x0a,
46         DrmNotPermitted = PlayerErrorClass | 0x0b,
47         ResourceLimit = PlayerErrorClass | 0x0c,
48         ServiceDisconnected = PlayerErrorClass | 0x0d,
49         NotSupportedAudioCodec = PlayerErrorClass | 0x0e,
50         NotSupportedVideoCodec = PlayerErrorClass | 0x0f,
51         NotSupportedSubtitle = PlayerErrorClass | 0x10,
52         NotSupportedFormat = PlayerErrorClass | 0x11,
53         NotAvailable = PlayerErrorClass | 0x12
54     }
55
56     internal static class PlayerErrorCodeExtensions
57     {
58         internal static void ThrowIfFailed(this PlayerErrorCode err, Player player, string message)
59         {
60             if (err == PlayerErrorCode.None)
61             {
62                 return;
63             }
64
65             var ex = err.GetException(message);
66
67             if (ex == null)
68             {
69                 // Notify only when it can't be handled.
70                 player?.NotifyError((int)err, message);
71
72                 throw new InvalidOperationException($"{message} : Unknown error({err.ToString()}).");
73             }
74
75             throw ex;
76         }
77
78         internal static Exception GetException(this PlayerErrorCode err, string message)
79         {
80             if (err == PlayerErrorCode.None)
81             {
82                 return null;
83             }
84
85             string msg = $"{ (message ?? "Operation failed") } : { err.ToString() }.";
86
87             switch (err)
88             {
89                 case PlayerErrorCode.InvalidArgument:
90                 case PlayerErrorCode.InvalidUri:
91                     throw new ArgumentException(msg);
92
93                 case PlayerErrorCode.NoSuchFile:
94                     throw new FileNotFoundException(msg);
95
96                 case PlayerErrorCode.OutOfMemory:
97                     throw new OutOfMemoryException(msg);
98
99                 case PlayerErrorCode.NoSpaceOnDevice:
100                     throw new IOException(msg);
101
102                 case PlayerErrorCode.PermissionDenied:
103                     throw new UnauthorizedAccessException(msg);
104
105                 case PlayerErrorCode.NotSupportedFile:
106                     throw new FileFormatException(msg);
107
108                 case PlayerErrorCode.FeatureNotSupported:
109                     throw new NotSupportedException(msg);
110
111                 case PlayerErrorCode.DrmExpired:
112                 case PlayerErrorCode.DrmNoLicense:
113                 case PlayerErrorCode.DrmFutureUse:
114                 case PlayerErrorCode.DrmNotPermitted:
115                 // TODO consider another exception.
116                 case PlayerErrorCode.InvalidOperation:
117                 case PlayerErrorCode.InvalidState:
118                 case PlayerErrorCode.SeekFailed:
119                 case PlayerErrorCode.ConnectionFailed:
120                 case PlayerErrorCode.VideoCaptureFailed:
121                     throw new InvalidOperationException(msg);
122
123                 case PlayerErrorCode.NoBufferSpace:
124                     throw new NoBufferSpaceException(msg);
125
126                 case PlayerErrorCode.ResourceLimit:
127                     throw new ResourceLimitException(msg);
128
129                 case PlayerErrorCode.NotSupportedAudioCodec:
130                     throw new CodecNotSupportedException(CodecKind.Audio);
131
132                 case PlayerErrorCode.NotSupportedVideoCodec:
133                     throw new CodecNotSupportedException(CodecKind.Video);
134
135                 case PlayerErrorCode.NotSupportedFormat:
136                     throw new NotSupportedFormatException(msg);
137
138                 case PlayerErrorCode.NotAvailable:
139                     throw new NotAvailableException(msg);
140             }
141
142             return null;
143         }
144     }
145
146     /// <summary>
147     /// The exception that is thrown when there is no available space in a buffer.
148     /// </summary>
149     /// <since_tizen> 3 </since_tizen>
150     public class NoBufferSpaceException : InvalidOperationException
151     {
152         /// <summary>
153         /// Initializes a new instance of the NoBufferSpaceException class with a specified error message.
154         /// </summary>
155         /// <param name="message">Error description.</param>
156         /// <since_tizen> 3 </since_tizen>
157         public NoBufferSpaceException(string message) : base(message)
158         {
159         }
160     }
161
162     /// <summary>
163     /// The exception that is thrown when there is no available resource for internal use.
164     /// </summary>
165     /// <since_tizen> 3 </since_tizen>
166     public class ResourceLimitException : InvalidOperationException
167     {
168         /// <summary>
169         /// Initializes a new instance of the ResourceLimitException class with a specified error message.
170         /// </summary>
171         /// <param name="message">Error description.</param>
172         /// <since_tizen> 3 </since_tizen>
173         public ResourceLimitException(string message) : base(message)
174         {
175         }
176     }
177
178     /// <summary>
179     /// The exception that is thrown when it is not available.
180     /// </summary>
181     /// <since_tizen> 6 </since_tizen>
182     public class NotAvailableException : Exception
183     {
184         /// <summary>
185         /// Initializes a new instance of the NotAvailableException class with a specified error message.
186         /// </summary>
187         /// <param name="message">Error description.</param>
188         /// <since_tizen> 6 </since_tizen>
189         public NotAvailableException(string message) : base(message)
190         {
191         }
192     }
193
194     /// <summary>
195     /// The exception that is thrown when it is not supported format.
196     /// </summary>
197     /// <since_tizen> 8 </since_tizen>
198     public class NotSupportedFormatException : Exception
199     {
200         /// <summary>
201         /// Initializes a new instance of the NotSupportedFormatException class with a specified error message.
202         /// </summary>
203         /// <param name="message">Error description.</param>
204         /// <since_tizen> 8 </since_tizen>
205         public NotSupportedFormatException(string message) : base(message)
206         {
207         }
208     }
209 }
210