7bc6eee8ff7c4c01bcc3bce94b299c05082683f2
[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         NotAvailable = PlayerErrorClass | 0x12
53     }
54
55     internal static class PlayerErrorCodeExtensions
56     {
57         internal static void ThrowIfFailed(this PlayerErrorCode err, Player player, string message)
58         {
59             if (err == PlayerErrorCode.None)
60             {
61                 return;
62             }
63
64             var ex = err.GetException(message);
65
66             if (ex == null)
67             {
68                 // Notify only when it can't be handled.
69                 player?.NotifyError((int)err, message);
70
71                 throw new InvalidOperationException($"{message} : Unknown error({err.ToString()}).");
72             }
73
74             throw ex;
75         }
76
77         internal static Exception GetException(this PlayerErrorCode err, string message)
78         {
79             if (err == PlayerErrorCode.None)
80             {
81                 return null;
82             }
83
84             string msg = $"{ (message ?? "Operation failed") } : { err.ToString() }.";
85
86             switch (err)
87             {
88                 case PlayerErrorCode.InvalidArgument:
89                 case PlayerErrorCode.InvalidUri:
90                     throw new ArgumentException(msg);
91
92                 case PlayerErrorCode.NoSuchFile:
93                     throw new FileNotFoundException(msg);
94
95                 case PlayerErrorCode.OutOfMemory:
96                     throw new OutOfMemoryException(msg);
97
98                 case PlayerErrorCode.NoSpaceOnDevice:
99                     throw new IOException(msg);
100
101                 case PlayerErrorCode.PermissionDenied:
102                     throw new UnauthorizedAccessException(msg);
103
104                 case PlayerErrorCode.NotSupportedFile:
105                     throw new FileFormatException(msg);
106
107                 case PlayerErrorCode.FeatureNotSupported:
108                     throw new NotSupportedException(msg);
109
110                 case PlayerErrorCode.DrmExpired:
111                 case PlayerErrorCode.DrmNoLicense:
112                 case PlayerErrorCode.DrmFutureUse:
113                 case PlayerErrorCode.DrmNotPermitted:
114                 // TODO consider another exception.
115                 case PlayerErrorCode.InvalidOperation:
116                 case PlayerErrorCode.InvalidState:
117                 case PlayerErrorCode.SeekFailed:
118                 case PlayerErrorCode.ConnectionFailed:
119                 case PlayerErrorCode.VideoCaptureFailed:
120                     throw new InvalidOperationException(msg);
121
122                 case PlayerErrorCode.NoBufferSpace:
123                     throw new NoBufferSpaceException(msg);
124
125                 case PlayerErrorCode.ResourceLimit:
126                     throw new ResourceLimitException(msg);
127
128                 case PlayerErrorCode.NotSupportedAudioCodec:
129                     throw new CodecNotSupportedException(CodecKind.Audio);
130
131                 case PlayerErrorCode.NotSupportedVideoCodec:
132                     throw new CodecNotSupportedException(CodecKind.Video);
133
134                 case PlayerErrorCode.NotAvailable:
135                     throw new NotAvailableException(msg);
136             }
137
138             return null;
139         }
140     }
141
142     /// <summary>
143     /// The exception that is thrown when there is no available space in a buffer.
144     /// </summary>
145     /// <since_tizen> 3 </since_tizen>
146     public class NoBufferSpaceException : InvalidOperationException
147     {
148         /// <summary>
149         /// Initializes a new instance of the NoBufferSpaceException class with a specified error message.
150         /// </summary>
151         /// <param name="message">Error description.</param>
152         /// <since_tizen> 3 </since_tizen>
153         public NoBufferSpaceException(string message) : base(message)
154         {
155         }
156     }
157
158     /// <summary>
159     /// The exception that is thrown when there is no available resource for internal use.
160     /// </summary>
161     /// <since_tizen> 3 </since_tizen>
162     public class ResourceLimitException : InvalidOperationException
163     {
164         /// <summary>
165         /// Initializes a new instance of the ResourceLimitException class with a specified error message.
166         /// </summary>
167         /// <param name="message">Error description.</param>
168         /// <since_tizen> 3 </since_tizen>
169         public ResourceLimitException(string message) : base(message)
170         {
171         }
172     }
173
174     /// <summary>
175     /// The exception that is thrown when it is not available.
176     /// </summary>
177     /// <since_tizen> 6 </since_tizen>
178     public class NotAvailableException : Exception
179     {
180         /// <summary>
181         /// Initializes a new instance of the NotAvailableException class with a specified error message.
182         /// </summary>
183         /// <param name="message">Error description.</param>
184         /// <since_tizen> 6 </since_tizen>
185         public NotAvailableException(string message) : base(message)
186         {
187         }
188     }
189 }
190