PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/csrc/com/xuggle/xuggler/Error.cpp

https://github.com/extremolo/xuggle-xuggler
C++ | 193 lines | 148 code | 20 blank | 25 comment | 15 complexity | 769d597650ff17e41cd661482736686c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0, BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright (c) 2008, 2010 Xuggle Inc. All rights reserved.
  3. *
  4. * This file is part of Xuggle-Xuggler-Main.
  5. *
  6. * Xuggle-Xuggler-Main is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Xuggle-Xuggler-Main is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Xuggle-Xuggler-Main. If not, see <http://www.gnu.org/licenses/>.
  18. *******************************************************************************/
  19. /*
  20. * Error.cpp
  21. *
  22. * Created on: Mar 20, 2009
  23. * Author: aclarke
  24. */
  25. #include <com/xuggle/ferry/Logger.h>
  26. #include <com/xuggle/xuggler/Error.h>
  27. #include <com/xuggle/xuggler/FfmpegIncludes.h>
  28. #include <cstring>
  29. #include <stdexcept>
  30. VS_LOG_SETUP(VS_CPP_PACKAGE);
  31. namespace com { namespace xuggle { namespace xuggler
  32. {
  33. struct ErrorMappingTable {
  34. int32_t mFfmpegError;
  35. IError::Type mXugglerError;
  36. } ;
  37. static struct ErrorMappingTable sErrorMappingTable[] = {
  38. { AVERROR(EIO), IError::ERROR_IO },
  39. { AVERROR(EDOM), IError::ERROR_NUMEXPECTED },
  40. { AVERROR(EINVAL), IError::ERROR_INVALIDDATA },
  41. { AVERROR(ENOMEM), IError::ERROR_NOMEM },
  42. { AVERROR(EILSEQ), IError::ERROR_NOFMT },
  43. { AVERROR(ENOSYS), IError::ERROR_NOTSUPPORTED },
  44. { AVERROR(ENOENT), IError::ERROR_NOENT },
  45. { AVERROR(EPIPE), IError::ERROR_EOF },
  46. { AVERROR_PATCHWELCOME, IError::ERROR_PATCHWELCOME },
  47. { AVERROR(EAGAIN), IError::ERROR_AGAIN },
  48. { AVERROR(ERANGE), IError::ERROR_RANGE },
  49. { AVERROR(EINTR), IError::ERROR_INTERRUPTED },
  50. { AVERROR_EOF, IError::ERROR_EOF },
  51. { AVERROR_DECODER_NOT_FOUND, IError::ERROR_NOFMT },
  52. { AVERROR_DEMUXER_NOT_FOUND, IError::ERROR_NOFMT },
  53. { AVERROR_ENCODER_NOT_FOUND, IError::ERROR_NOFMT },
  54. { AVERROR_MUXER_NOT_FOUND, IError::ERROR_NOFMT },
  55. { AVERROR_OPTION_NOT_FOUND, IError::ERROR_NOFMT },
  56. };
  57. static int32_t sErrorMappingTableSize = sizeof(sErrorMappingTable)/sizeof(struct ErrorMappingTable);
  58. Error :: Error()
  59. {
  60. mType = IError::ERROR_UNKNOWN;
  61. mErrorNo = 0;
  62. *mErrorStr = 0;
  63. mErrorStr[sizeof(mErrorStr)-1]=0;
  64. }
  65. Error :: ~Error()
  66. {
  67. }
  68. const char*
  69. Error :: getDescription()
  70. {
  71. const char* retval = 0;
  72. if (!*mErrorStr && mErrorNo != 0)
  73. {
  74. #ifdef HAVE_STRERROR_R
  75. #ifdef STRERROR_R_CHAR_P
  76. retval = strerror_r(AVUNERROR(mErrorNo), mErrorStr, sizeof(mErrorStr));
  77. #else
  78. strerror_r(AVUNERROR(mErrorNo), mErrorStr, sizeof(mErrorStr));
  79. retval = mErrorStr;
  80. #endif
  81. #else
  82. retval = strerror(AVUNERROR(mErrorNo));
  83. #endif // HAVE_STRERROR_R
  84. if (retval != (const char*) mErrorStr)
  85. strncpy(mErrorStr, retval, sizeof(mErrorStr)-1);
  86. }
  87. return *mErrorStr ? mErrorStr : 0;
  88. }
  89. int32_t
  90. Error :: getErrorNumber()
  91. {
  92. return mErrorNo;
  93. }
  94. IError::Type
  95. Error :: getType()
  96. {
  97. return mType;
  98. }
  99. Error*
  100. Error :: make(int32_t aErrorNo)
  101. {
  102. if (aErrorNo >= 0)
  103. return 0;
  104. return make(aErrorNo, errorNumberToType(aErrorNo));
  105. }
  106. Error*
  107. Error :: make(Type aType)
  108. {
  109. return make(typeToErrorNumber(aType), aType);
  110. }
  111. Error*
  112. Error :: make(int32_t aErrorNo, Type aType)
  113. {
  114. Error* retval = 0;
  115. try
  116. {
  117. retval = make();
  118. if (!retval)
  119. throw std::bad_alloc();
  120. retval->mErrorNo = aErrorNo;
  121. retval->mType = aType;
  122. // null out and don't fill unless description is asked for
  123. *(retval->mErrorStr) = 0;
  124. }
  125. catch (std::bad_alloc & e)
  126. {
  127. VS_REF_RELEASE(retval);
  128. throw e;
  129. }
  130. catch (std::exception & e)
  131. {
  132. VS_LOG_TRACE("Error: %s", e.what());
  133. VS_REF_RELEASE(retval);
  134. }
  135. return retval;
  136. }
  137. IError::Type
  138. Error :: errorNumberToType(int32_t errNo)
  139. {
  140. IError::Type retval = IError::ERROR_UNKNOWN;
  141. int i = 0;
  142. for(; i < sErrorMappingTableSize; i++)
  143. {
  144. if (sErrorMappingTable[i].mFfmpegError == errNo)
  145. {
  146. retval = sErrorMappingTable[i].mXugglerError;
  147. break;
  148. }
  149. }
  150. if (i >= sErrorMappingTableSize) {
  151. retval = IError::ERROR_UNKNOWN;
  152. }
  153. return retval;
  154. }
  155. int32_t
  156. Error :: typeToErrorNumber(Type type)
  157. {
  158. int32_t retval = AVERROR(EINVAL);
  159. int i = 0;
  160. for(; i < sErrorMappingTableSize; i++)
  161. {
  162. if (sErrorMappingTable[i].mXugglerError == type)
  163. {
  164. retval = sErrorMappingTable[i].mFfmpegError;
  165. break;
  166. }
  167. }
  168. if (i >= sErrorMappingTableSize) {
  169. retval = AVERROR(EINVAL);
  170. }
  171. return retval;
  172. }
  173. }}}