| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <pjmedia-audiodev/errno.h> |
| 19 |
#include <pj/string.h> |
| 20 |
#include <pj/unicode.h> |
| 21 |
#if PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO |
| 22 |
# include <portaudio.h> |
| 23 |
#endif |
| 24 |
#if PJMEDIA_AUDIO_DEV_HAS_WMME |
| 25 |
# ifdef _MSC_VER |
| 26 |
# pragma warning(push, 3) |
| 27 |
# endif |
| 28 |
# include <windows.h> |
| 29 |
# include <mmsystem.h> |
| 30 |
# ifdef _MSC_VER |
| 31 |
# pragma warning(pop) |
| 32 |
# endif |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0) |
| 41 |
|
| 42 |
static const struct |
| 43 |
{ |
| 44 |
int code; |
| 45 |
const char *msg; |
| 46 |
} err_str[] = |
| 47 |
{ |
| 48 |
PJ_BUILD_ERR( PJMEDIA_EAUD_ERR, "Unspecified audio device error" ), |
| 49 |
PJ_BUILD_ERR( PJMEDIA_EAUD_SYSERR, "Unknown error from audio driver" ), |
| 50 |
PJ_BUILD_ERR( PJMEDIA_EAUD_INIT, "Audio subsystem not initialized" ), |
| 51 |
PJ_BUILD_ERR( PJMEDIA_EAUD_INVDEV, "Invalid audio device" ), |
| 52 |
PJ_BUILD_ERR( PJMEDIA_EAUD_NODEV, "Found no audio devices" ), |
| 53 |
PJ_BUILD_ERR( PJMEDIA_EAUD_NODEFDEV, "Unable to find default audio device" ), |
| 54 |
PJ_BUILD_ERR( PJMEDIA_EAUD_NOTREADY, "Audio device not ready" ), |
| 55 |
PJ_BUILD_ERR( PJMEDIA_EAUD_INVCAP, "Invalid or unsupported audio capability" ), |
| 56 |
PJ_BUILD_ERR( PJMEDIA_EAUD_INVOP, "Invalid or unsupported audio device operation" ), |
| 57 |
PJ_BUILD_ERR( PJMEDIA_EAUD_BADFORMAT, "Bad or invalid audio device format" ), |
| 58 |
PJ_BUILD_ERR( PJMEDIA_EAUD_SAMPFORMAT, "Invalid audio device sample format"), |
| 59 |
PJ_BUILD_ERR( PJMEDIA_EAUD_BADLATENCY, "Bad audio latency setting") |
| 60 |
|
| 61 |
}; |
| 62 |
|
| 63 |
#endif |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
PJ_DEF(pj_str_t) pjmedia_audiodev_strerror(pj_status_t statcode, |
| 71 |
char *buf, pj_size_t bufsize ) |
| 72 |
{ |
| 73 |
pj_str_t errstr; |
| 74 |
|
| 75 |
#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#if PJMEDIA_AUDIO_DEV_HAS_COREAUDIO |
| 80 |
if (statcode >= PJMEDIA_AUDIODEV_COREAUDIO_ERRNO_START && |
| 81 |
statcode <= PJMEDIA_AUDIODEV_COREAUDIO_ERRNO_END) |
| 82 |
{ |
| 83 |
int ca_err = PJMEDIA_AUDIODEV_COREAUDIO_ERRNO_START - statcode; |
| 84 |
pj_str_t msg = pj_str("Core audio error"); |
| 85 |
|
| 86 |
PJ_UNUSED_ARG(ca_err); |
| 87 |
|
| 88 |
errstr.ptr = buf; |
| 89 |
pj_strncpy_with_null(&errstr, &msg, bufsize); |
| 90 |
return errstr; |
| 91 |
} else |
| 92 |
#endif |
| 93 |
|
| 94 |
|
| 95 |
#if PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO |
| 96 |
if (statcode >= PJMEDIA_AUDIODEV_PORTAUDIO_ERRNO_START && |
| 97 |
statcode <= PJMEDIA_AUDIODEV_PORTAUDIO_ERRNO_END) |
| 98 |
{ |
| 99 |
|
| 100 |
|
| 101 |
int pa_err = PJMEDIA_AUDIODEV_PORTAUDIO_ERRNO_START - statcode; |
| 102 |
pj_str_t msg; |
| 103 |
|
| 104 |
msg.ptr = (char*)Pa_GetErrorText(pa_err); |
| 105 |
msg.slen = pj_ansi_strlen(msg.ptr); |
| 106 |
|
| 107 |
errstr.ptr = buf; |
| 108 |
pj_strncpy_with_null(&errstr, &msg, bufsize); |
| 109 |
return errstr; |
| 110 |
|
| 111 |
} else |
| 112 |
#endif |
| 113 |
|
| 114 |
|
| 115 |
#if PJMEDIA_AUDIO_DEV_HAS_WMME |
| 116 |
if ((statcode >= PJMEDIA_AUDIODEV_WMME_IN_ERROR_START && |
| 117 |
statcode < PJMEDIA_AUDIODEV_WMME_IN_ERROR_END) || |
| 118 |
(statcode >= PJMEDIA_AUDIODEV_WMME_OUT_ERROR_START && |
| 119 |
statcode < PJMEDIA_AUDIODEV_WMME_OUT_ERROR_END)) |
| 120 |
{ |
| 121 |
MMRESULT native_err, mr; |
| 122 |
MMRESULT (WINAPI *waveGetErrText)(UINT mmrError, LPTSTR pszText, UINT cchText); |
| 123 |
PJ_DECL_UNICODE_TEMP_BUF(wbuf, 80) |
| 124 |
|
| 125 |
if (statcode >= PJMEDIA_AUDIODEV_WMME_IN_ERROR_START && |
| 126 |
statcode <= PJMEDIA_AUDIODEV_WMME_IN_ERROR_END) |
| 127 |
{ |
| 128 |
native_err = statcode - PJMEDIA_AUDIODEV_WMME_IN_ERROR_START; |
| 129 |
waveGetErrText = &waveInGetErrorText; |
| 130 |
} else { |
| 131 |
native_err = statcode - PJMEDIA_AUDIODEV_WMME_OUT_ERROR_START; |
| 132 |
waveGetErrText = &waveOutGetErrorText; |
| 133 |
} |
| 134 |
|
| 135 |
#if PJ_NATIVE_STRING_IS_UNICODE |
| 136 |
mr = (*waveGetErrText)(native_err, wbuf, PJ_ARRAY_SIZE(wbuf)); |
| 137 |
if (mr == MMSYSERR_NOERROR) { |
| 138 |
int len = wcslen(wbuf); |
| 139 |
pj_unicode_to_ansi(wbuf, len, buf, bufsize); |
| 140 |
} |
| 141 |
#else |
| 142 |
mr = (*waveGetErrText)(native_err, buf, (UINT)bufsize); |
| 143 |
#endif |
| 144 |
|
| 145 |
if (mr==MMSYSERR_NOERROR) { |
| 146 |
errstr.ptr = buf; |
| 147 |
errstr.slen = pj_ansi_strlen(buf); |
| 148 |
return errstr; |
| 149 |
} else { |
| 150 |
pj_ansi_snprintf(buf, bufsize, "MMSYSTEM native error %d", |
| 151 |
native_err); |
| 152 |
return pj_str(buf); |
| 153 |
} |
| 154 |
|
| 155 |
} else |
| 156 |
#endif |
| 157 |
|
| 158 |
|
| 159 |
#if PJMEDIA_AUDIO_DEV_HAS_BDIMAD |
| 160 |
|
| 161 |
if (statcode >= PJMEDIA_AUDIODEV_BDIMAD_ERROR_START && |
| 162 |
statcode < PJMEDIA_AUDIODEV_BDIMAD_ERROR_END) |
| 163 |
{ |
| 164 |
pj_status_t native_err; |
| 165 |
native_err = statcode - PJMEDIA_AUDIODEV_BDIMAD_ERROR_START; |
| 166 |
|
| 167 |
pj_ansi_snprintf(buf, bufsize, "BDIMAD native error %d", native_err); |
| 168 |
return pj_str(buf); |
| 169 |
} else |
| 170 |
#endif |
| 171 |
|
| 172 |
|
| 173 |
if (statcode >= PJMEDIA_AUDIODEV_ERRNO_START && |
| 174 |
statcode < PJMEDIA_AUDIODEV_ERRNO_END) |
| 175 |
{ |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
int first = 0; |
| 180 |
int n = PJ_ARRAY_SIZE(err_str); |
| 181 |
|
| 182 |
while (n > 0) { |
| 183 |
int half = n/2; |
| 184 |
int mid = first + half; |
| 185 |
|
| 186 |
if (err_str[mid].code < statcode) { |
| 187 |
first = mid+1; |
| 188 |
n -= (half+1); |
| 189 |
} else if (err_str[mid].code > statcode) { |
| 190 |
n = half; |
| 191 |
} else { |
| 192 |
first = mid; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
if (PJ_ARRAY_SIZE(err_str) && err_str[first].code == statcode) { |
| 199 |
pj_str_t msg; |
| 200 |
|
| 201 |
msg.ptr = (char*)err_str[first].msg; |
| 202 |
msg.slen = pj_ansi_strlen(err_str[first].msg); |
| 203 |
|
| 204 |
errstr.ptr = buf; |
| 205 |
pj_strncpy_with_null(&errstr, &msg, bufsize); |
| 206 |
return errstr; |
| 207 |
|
| 208 |
} |
| 209 |
} |
| 210 |
#endif |
| 211 |
|
| 212 |
|
| 213 |
errstr.ptr = buf; |
| 214 |
errstr.slen = pj_ansi_snprintf(buf, bufsize, |
| 215 |
"Unknown pjmedia-audiodev error %d", |
| 216 |
statcode); |
| 217 |
if (errstr.slen < 1 || errstr.slen >= (pj_ssize_t)bufsize) |
| 218 |
errstr.slen = bufsize - 1; |
| 219 |
return errstr; |
| 220 |
} |
| 221 |
|