Changeset 319 for pjproject/trunk/pjmedia/src/pjmedia/pasound.c
- Timestamp:
- Mar 15, 2006 8:56:04 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia/pasound.c
r245 r319 18 18 */ 19 19 #include <pjmedia/sound.h> 20 #include <pjmedia/errno.h> 21 #include <pj/log.h> 22 #include <pj/os.h> 20 23 #include <pj/string.h> 21 #include <pj/os.h>22 #include <pj/log.h>23 24 #include <portaudio.h> 24 25 … … 38 39 int bytes_per_sample; 39 40 pj_uint32_t samples_per_sec; 41 int channel_count; 40 42 pj_uint32_t timestamp; 41 43 pj_uint32_t underflow; … … 189 191 * Open stream. 190 192 */ 191 PJ_DEF(pj_snd_stream*) pj_snd_open_recorder( int index, 192 const pj_snd_stream_info *param, 193 pj_snd_rec_cb rec_cb, 194 void *user_data) 193 PJ_DEF(pj_status_t) pj_snd_open_recorder( int index, 194 unsigned clock_rate, 195 unsigned channel_count, 196 unsigned samples_per_frame, 197 unsigned bits_per_sample, 198 pj_snd_rec_cb rec_cb, 199 void *user_data, 200 pj_snd_stream **p_snd_strm) 195 201 { 196 202 pj_pool_t *pool; … … 209 215 } 210 216 if (index == count) { 211 PJ_LOG(2,(THIS_FILE, "Error: unable to find recorder device"));212 return NULL;217 /* No such device. */ 218 return PJ_ENOTFOUND; 213 219 } 214 220 } else { 215 221 paDevInfo = Pa_GetDeviceInfo(index); 216 if (!paDevInfo) 217 return NULL; 218 } 219 220 if (param->bits_per_sample == 8) 222 if (!paDevInfo) { 223 /* Assumed it is "No such device" error. */ 224 return PJ_ENOTFOUND; 225 } 226 } 227 228 if (bits_per_sample == 8) 221 229 sampleFormat = paUInt8; 222 else if ( param->bits_per_sample == 16)230 else if (bits_per_sample == 16) 223 231 sampleFormat = paInt16; 224 else if ( param->bits_per_sample == 32)232 else if (bits_per_sample == 32) 225 233 sampleFormat = paInt32; 226 234 else 227 return NULL;235 return PJ_ENOTSUP; 228 236 229 237 pool = pj_pool_create( snd_mgr.factory, "sndstream", 1024, 1024, NULL); 230 238 if (!pool) 231 return NULL;232 233 stream = pj_pool_ calloc(pool, 1, sizeof(*stream));239 return PJ_ENOMEM; 240 241 stream = pj_pool_zalloc(pool, sizeof(*stream)); 234 242 stream->pool = pool; 235 stream->name = pj_str(" recorder");243 stream->name = pj_str("snd-rec"); 236 244 stream->user_data = user_data; 237 245 stream->dev_index = index; 238 stream->samples_per_sec = param->samples_per_frame; 239 stream->bytes_per_sample = param->bits_per_sample / 8; 246 stream->samples_per_sec = samples_per_frame; 247 stream->bytes_per_sample = bits_per_sample / 8; 248 stream->channel_count = channel_count; 240 249 stream->rec_cb = rec_cb; 241 250 242 251 pj_memset(&inputParam, 0, sizeof(inputParam)); 243 252 inputParam.device = index; 244 inputParam.channelCount = 1;253 inputParam.channelCount = channel_count; 245 254 inputParam.hostApiSpecificStreamInfo = NULL; 246 255 inputParam.sampleFormat = sampleFormat; … … 248 257 249 258 err = Pa_OpenStream( &stream->stream, &inputParam, NULL, 250 param->samples_per_sec, 251 param->samples_per_frame * param->frames_per_packet, 252 0, 253 &PaRecorderCallback, stream ); 259 clock_rate, samples_per_frame, 260 0, &PaRecorderCallback, stream ); 254 261 if (err != paNoError) { 255 262 pj_pool_release(pool); 256 PJ_LOG(2,(THIS_FILE, "Error opening player: %s", Pa_GetErrorText(err))); 257 return NULL; 263 return PJMEDIA_ERRNO_FROM_PORTAUDIO(err); 258 264 } 259 265 260 266 PJ_LOG(5,(THIS_FILE, "%s opening device %s for recording, sample rate=%d, " 267 "channel count=%d, " 261 268 "%d bits per sample, %d samples per buffer", 262 269 (err==0 ? "Success" : "Error"), 263 paDevInfo->name, param->samples_per_sec, 264 param->bits_per_sample, 265 param->samples_per_frame * param->frames_per_packet)); 266 267 return stream; 268 } 269 270 271 PJ_DEF(pj_snd_stream*) pj_snd_open_player(int index, 272 const pj_snd_stream_info *param, 273 pj_snd_play_cb play_cb, 274 void *user_data) 270 paDevInfo->name, clock_rate, channel_count, 271 bits_per_sample, samples_per_frame)); 272 273 *p_snd_strm = stream; 274 return PJ_SUCCESS; 275 } 276 277 278 PJ_DEF(pj_status_t) pj_snd_open_player( int index, 279 unsigned clock_rate, 280 unsigned channel_count, 281 unsigned samples_per_frame, 282 unsigned bits_per_sample, 283 pj_snd_play_cb play_cb, 284 void *user_data, 285 pj_snd_stream **p_snd_strm) 275 286 { 276 287 pj_pool_t *pool; … … 289 300 } 290 301 if (index == count) { 291 PJ_LOG(2,(THIS_FILE, "Error: unable to find player device"));292 return NULL;302 /* No such device. */ 303 return PJ_ENOTFOUND; 293 304 } 294 305 } else { 295 306 paDevInfo = Pa_GetDeviceInfo(index); 296 if (!paDevInfo) 297 return NULL; 298 } 299 300 if (param->bits_per_sample == 8) 307 if (!paDevInfo) { 308 /* Assumed it is "No such device" error. */ 309 return PJ_ENOTFOUND; 310 } 311 } 312 313 if (bits_per_sample == 8) 301 314 sampleFormat = paUInt8; 302 else if ( param->bits_per_sample == 16)315 else if (bits_per_sample == 16) 303 316 sampleFormat = paInt16; 304 else if ( param->bits_per_sample == 32)317 else if (bits_per_sample == 32) 305 318 sampleFormat = paInt32; 306 319 else 307 return NULL;320 return PJ_ENOTSUP; 308 321 309 322 pool = pj_pool_create( snd_mgr.factory, "sndstream", 1024, 1024, NULL); 310 323 if (!pool) 311 return NULL;324 return PJ_ENOMEM; 312 325 313 326 stream = pj_pool_calloc(pool, 1, sizeof(*stream)); … … 315 328 stream->name = pj_str("player"); 316 329 stream->user_data = user_data; 317 stream->samples_per_sec = param->samples_per_frame; 318 stream->bytes_per_sample = param->bits_per_sample / 8; 330 stream->samples_per_sec = samples_per_frame; 331 stream->bytes_per_sample = bits_per_sample / 8; 332 stream->channel_count = channel_count; 319 333 stream->dev_index = index; 320 334 stream->play_cb = play_cb; … … 322 336 pj_memset(&outputParam, 0, sizeof(outputParam)); 323 337 outputParam.device = index; 324 outputParam.channelCount = 1;338 outputParam.channelCount = channel_count; 325 339 outputParam.hostApiSpecificStreamInfo = NULL; 326 340 outputParam.sampleFormat = sampleFormat; … … 328 342 329 343 err = Pa_OpenStream( &stream->stream, NULL, &outputParam, 330 param->samples_per_sec, 331 param->samples_per_frame * param->frames_per_packet, 332 0, 333 &PaPlayerCallback, stream ); 344 clock_rate, samples_per_frame, 345 0, &PaPlayerCallback, stream ); 334 346 if (err != paNoError) { 335 347 pj_pool_release(pool); 336 PJ_LOG(2,(THIS_FILE, "Error opening player: %s", Pa_GetErrorText(err))); 337 return NULL; 348 return PJMEDIA_ERRNO_FROM_PORTAUDIO(err); 338 349 } 339 350 340 351 PJ_LOG(5,(THIS_FILE, "%s opening device %s for playing, sample rate=%d, " 352 "channel count=%d, " 341 353 "%d bits per sample, %d samples per buffer", 342 354 (err==0 ? "Success" : "Error"), 343 paDevInfo->name, param->samples_per_sec, 344 param->bits_per_sample, 345 param->samples_per_frame * param->frames_per_packet)); 346 347 return stream; 355 paDevInfo->name, clock_rate, channel_count, 356 bits_per_sample, samples_per_frame)); 357 358 *p_snd_strm = stream; 359 360 return PJ_SUCCESS; 348 361 } 349 362
Note: See TracChangeset
for help on using the changeset viewer.