Release 4.0.0-preview1-00051
[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 using System;
17 using System.IO;
18 using Tizen.Internals.Errors;
19
20 namespace Tizen.Multimedia
21 {
22     internal enum PlayerErrorCode
23     {
24         None = ErrorCode.None,
25         InvalidArgument = ErrorCode.InvalidParameter,
26         OutOfMemory = ErrorCode.OutOfMemory,
27         NoSuchFile = ErrorCode.NoSuchFile,
28         InvalidOperation = ErrorCode.InvalidOperation,
29         NoSpaceOnDevice = ErrorCode.FileNoSpaceOnDevice,
30         FeatureNotSupported = ErrorCode.NotSupported,
31         PermissionDenied = ErrorCode.PermissionDenied,
32         NoBufferSpace = ErrorCode.BufferSpace,
33         TizenPlayerError = -0x01940000,
34         PlayerErrorClass = TizenPlayerError | 0x20,
35         SeekFailed = PlayerErrorClass | 0x01,
36         InvalidState = PlayerErrorClass | 0x02,
37         NotSupportedFile = PlayerErrorClass | 0x03,
38         InvalidUri = PlayerErrorClass | 0x04,
39         SoundPolicyError = PlayerErrorClass | 0x05,
40         ConnectionFailed = PlayerErrorClass | 0x06,
41         VideoCaptureFailed = PlayerErrorClass | 0x07,
42         DrmExpired = PlayerErrorClass | 0x08,
43         DrmNoLicense = PlayerErrorClass | 0x09,
44         DrmFutureUse = PlayerErrorClass | 0x0a,
45         DrmNotPermitted = PlayerErrorClass | 0x0b,
46         ResourceLimit = PlayerErrorClass | 0x0c,
47         ServiceDisconnected = PlayerErrorClass | 0x0d,
48         NotSupportedAudioCodec = PlayerErrorClass | 0x0e,
49         NotSupportedVideoCodec = PlayerErrorClass | 0x0f,
50         NotSupportedSubtitle = PlayerErrorClass | 0x10
51     }
52
53     internal static class PlayerErrorCodeExtensions
54     {
55         internal static void ThrowIfFailed(this PlayerErrorCode err, string message)
56         {
57             if (err == PlayerErrorCode.None)
58             {
59                 return;
60             }
61
62             throw err.GetException(message);
63         }
64
65         internal static Exception GetException(this PlayerErrorCode err, string message)
66         {
67             if (err == PlayerErrorCode.None)
68             {
69                 return null;
70             }
71
72             string msg = $"{ (message ?? "Operation failed") } : { err.ToString() }.";
73
74             switch (err)
75             {
76                 case PlayerErrorCode.InvalidArgument:
77                 case PlayerErrorCode.InvalidUri:
78                     throw new ArgumentException(msg);
79
80                 case PlayerErrorCode.NoSuchFile:
81                     throw new FileNotFoundException(msg);
82
83                 case PlayerErrorCode.OutOfMemory:
84                     throw new OutOfMemoryException(msg);
85
86                 case PlayerErrorCode.NoSpaceOnDevice:
87                     throw new IOException(msg);
88
89                 case PlayerErrorCode.PermissionDenied:
90                     throw new UnauthorizedAccessException(msg);
91
92                 case PlayerErrorCode.NotSupportedFile:
93                     throw new FileFormatException(msg);
94
95                 case PlayerErrorCode.FeatureNotSupported:
96                     throw new NotSupportedException(msg);
97
98                 case PlayerErrorCode.DrmExpired:
99                 case PlayerErrorCode.DrmNoLicense:
100                 case PlayerErrorCode.DrmFutureUse:
101                 case PlayerErrorCode.DrmNotPermitted:
102                 // TODO consider another exception.
103                 case PlayerErrorCode.InvalidOperation:
104                 case PlayerErrorCode.InvalidState:
105                 case PlayerErrorCode.SeekFailed:
106                 case PlayerErrorCode.ConnectionFailed:
107                 case PlayerErrorCode.VideoCaptureFailed:
108                     throw new InvalidOperationException(msg);
109
110                 case PlayerErrorCode.NoBufferSpace:
111                     throw new NoBufferSpaceException(msg);
112
113                 case PlayerErrorCode.ResourceLimit:
114                     throw new ResourceLimitException(msg);
115
116                 case PlayerErrorCode.NotSupportedAudioCodec:
117                     throw new CodecNotSupportedException(CodecKind.Audio);
118
119                 case PlayerErrorCode.NotSupportedVideoCodec:
120                     throw new CodecNotSupportedException(CodecKind.Video);
121
122             }
123
124             throw new InvalidOperationException(msg);
125         }
126     }
127
128     /// <summary>
129     /// The exception that is thrown when there is no available space in a buffer.
130     /// </summary>
131     public class NoBufferSpaceException : InvalidOperationException
132     {
133         /// <summary>
134         /// Initializes a new instance of the NoBufferSpaceException class with a specified error message.
135         /// </summary>
136         /// <param name="message">Error description.</param>
137         public NoBufferSpaceException(string message) : base(message)
138         {
139         }
140     }
141
142     /// <summary>
143     /// The exception that is thrown when there is no available resource for internal use.
144     /// </summary>
145     public class ResourceLimitException : InvalidOperationException
146     {
147         /// <summary>
148         /// Initializes a new instance of the ResourceLimitException class with a specified error message.
149         /// </summary>
150         /// <param name="message">Error description.</param>
151         public ResourceLimitException(string message) : base(message)
152         {
153         }
154     }
155 }
156