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