static pj_status_t skip(pj_oshandle_t fd, pj_off_t bytes) { pj_status_t status; status = pj_file_setpos(fd, bytes, PJ_SEEK_CUR); if (status != PJ_SUCCESS) return status; return PJ_SUCCESS; }
// // Set file position. // pj_status_t setpos(pj_off_t offset, Offset_Type whence) { return pj_file_setpos(hnd_, offset, (enum pj_file_seek_type)whence); }
static int file_test_internal(void) { enum { FILE_MAX_AGE = 1000 }; pj_oshandle_t fd = 0; pj_status_t status; char readbuf[sizeof(buffer)+16]; pj_file_stat stat; pj_time_val start_time; pj_ssize_t size; pj_off_t pos; PJ_LOG(3,("", "..file io test..")); /* Get time. */ pj_gettimeofday(&start_time); /* Delete original file if exists. */ if (pj_file_exists(FILENAME)) pj_file_delete(FILENAME); /* * Write data to the file. */ status = pj_file_open(NULL, FILENAME, PJ_O_WRONLY, &fd); if (status != PJ_SUCCESS) { app_perror("...file_open() error", status); return -10; } size = sizeof(buffer); status = pj_file_write(fd, buffer, &size); if (status != PJ_SUCCESS) { app_perror("...file_write() error", status); pj_file_close(fd); return -20; } if (size != sizeof(buffer)) return -25; status = pj_file_close(fd); if (status != PJ_SUCCESS) { app_perror("...file_close() error", status); return -30; } /* Check the file existance and size. */ if (!pj_file_exists(FILENAME)) return -40; if (pj_file_size(FILENAME) != sizeof(buffer)) return -50; /* Get file stat. */ status = pj_file_getstat(FILENAME, &stat); if (status != PJ_SUCCESS) return -60; /* Check stat size. */ if (stat.size != sizeof(buffer)) return -70; #if INCLUDE_FILE_TIME_TEST /* Check file creation time >= start_time. */ if (!PJ_TIME_VAL_GTE(stat.ctime, start_time)) return -80; /* Check file creation time is not much later. */ PJ_TIME_VAL_SUB(stat.ctime, start_time); if (stat.ctime.sec > FILE_MAX_AGE) return -90; /* Check file modification time >= start_time. */ if (!PJ_TIME_VAL_GTE(stat.mtime, start_time)) return -80; /* Check file modification time is not much later. */ PJ_TIME_VAL_SUB(stat.mtime, start_time); if (stat.mtime.sec > FILE_MAX_AGE) return -90; /* Check file access time >= start_time. */ if (!PJ_TIME_VAL_GTE(stat.atime, start_time)) return -80; /* Check file access time is not much later. */ PJ_TIME_VAL_SUB(stat.atime, start_time); if (stat.atime.sec > FILE_MAX_AGE) return -90; #endif /* * Re-open the file and read data. */ status = pj_file_open(NULL, FILENAME, PJ_O_RDONLY, &fd); if (status != PJ_SUCCESS) { app_perror("...file_open() error", status); return -100; } size = 0; while (size < (pj_ssize_t)sizeof(readbuf)) { pj_ssize_t read; read = 1; status = pj_file_read(fd, &readbuf[size], &read); if (status != PJ_SUCCESS) { PJ_LOG(3,("", "...error reading file after %d bytes (error follows)", size)); app_perror("...error", status); return -110; } if (read == 0) { // EOF break; } size += read; } if (size != sizeof(buffer)) return -120; /* if (!pj_file_eof(fd, PJ_O_RDONLY)) return -130; */ if (pj_memcmp(readbuf, buffer, size) != 0) return -140; /* Seek test. */ status = pj_file_setpos(fd, 4, PJ_SEEK_SET); if (status != PJ_SUCCESS) { app_perror("...file_setpos() error", status); return -141; } /* getpos test. */ status = pj_file_getpos(fd, &pos); if (status != PJ_SUCCESS) { app_perror("...file_getpos() error", status); return -142; } if (pos != 4) return -143; status = pj_file_close(fd); if (status != PJ_SUCCESS) { app_perror("...file_close() error", status); return -150; } /* * Rename test. */ status = pj_file_move(FILENAME, NEWNAME); if (status != PJ_SUCCESS) { app_perror("...file_move() error", status); return -160; } if (pj_file_exists(FILENAME)) return -170; if (!pj_file_exists(NEWNAME)) return -180; if (pj_file_size(NEWNAME) != sizeof(buffer)) return -190; /* Delete test. */ status = pj_file_delete(NEWNAME); if (status != PJ_SUCCESS) { app_perror("...file_delete() error", status); return -200; } if (pj_file_exists(NEWNAME)) return -210; PJ_LOG(3,("", "...success")); return PJ_SUCCESS; }
/* * Close the port, modify file header with updated file length. */ static pj_status_t file_on_destroy(pjmedia_port *this_port) { enum { FILE_LEN_POS = 4, DATA_LEN_POS = 40 }; struct file_port *fport = (struct file_port *)this_port; pj_off_t file_size; pj_ssize_t bytes; pj_uint32_t wave_file_len; pj_uint32_t wave_data_len; pj_status_t status; pj_uint32_t data_len_pos = DATA_LEN_POS; /* Flush remaining buffers. */ if (fport->writepos != fport->buf) flush_buffer(fport); /* Get file size. */ status = pj_file_getpos(fport->fd, &file_size); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Calculate wave fields */ wave_file_len = (pj_uint32_t)(file_size - 8); wave_data_len = (pj_uint32_t)(file_size - sizeof(pjmedia_wave_hdr)); #if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0 wave_file_len = pj_swap32(wave_file_len); wave_data_len = pj_swap32(wave_data_len); #endif /* Seek to the file_len field. */ status = pj_file_setpos(fport->fd, FILE_LEN_POS, PJ_SEEK_SET); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Write file_len */ bytes = sizeof(wave_file_len); status = pj_file_write(fport->fd, &wave_file_len, &bytes); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Write samples_len in FACT chunk */ if (fport->fmt_tag != PJMEDIA_WAVE_FMT_TAG_PCM) { enum { SAMPLES_LEN_POS = 44}; pj_uint32_t wav_samples_len; /* Adjust wave_data_len & data_len_pos since there is FACT chunk */ wave_data_len -= 12; data_len_pos += 12; wav_samples_len = wave_data_len; /* Seek to samples_len field. */ status = pj_file_setpos(fport->fd, SAMPLES_LEN_POS, PJ_SEEK_SET); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Write samples_len */ bytes = sizeof(wav_samples_len); status = pj_file_write(fport->fd, &wav_samples_len, &bytes); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); } /* Seek to data_len field. */ status = pj_file_setpos(fport->fd, data_len_pos, PJ_SEEK_SET); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Write file_len */ bytes = sizeof(wave_data_len); status = pj_file_write(fport->fd, &wave_data_len, &bytes); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Close file */ status = pj_file_close(fport->fd); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Done. */ return PJ_SUCCESS; }
PJ_DEF(pj_status_t) pj_file_open( pj_pool_t *pool, const char *pathname, unsigned flags, pj_oshandle_t *fd) { PJ_DECL_UNICODE_TEMP_BUF(wpathname,256) HANDLE hFile; DWORD dwDesiredAccess = 0; DWORD dwShareMode = 0; DWORD dwCreationDisposition = 0; DWORD dwFlagsAndAttributes = 0; PJ_UNUSED_ARG(pool); PJ_ASSERT_RETURN(pathname!=NULL, PJ_EINVAL); if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) { dwDesiredAccess |= GENERIC_WRITE; if ((flags & PJ_O_APPEND) == PJ_O_APPEND) { #if !defined(PJ_WIN32_WINCE) || !PJ_WIN32_WINCE /* FILE_APPEND_DATA is invalid on WM2003 and WM5, but it seems * to be working on WM6. All are tested on emulator though. * Removing this also seem to work (i.e. data is appended), so * I guess this flag is "optional". * See http://trac.pjsip.org/repos/ticket/825 */ dwDesiredAccess |= FILE_APPEND_DATA; #endif dwCreationDisposition |= OPEN_ALWAYS; } else { dwDesiredAccess &= ~(FILE_APPEND_DATA); dwCreationDisposition |= CREATE_ALWAYS; } } if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) { dwDesiredAccess |= GENERIC_READ; if (flags == PJ_O_RDONLY) dwCreationDisposition |= OPEN_EXISTING; } if (dwDesiredAccess == 0) { pj_assert(!"Invalid file open flags"); return PJ_EINVAL; } dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; hFile = CreateFile(PJ_STRING_TO_NATIVE(pathname,wpathname,sizeof(wpathname)), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); if (hFile == INVALID_HANDLE_VALUE) { *fd = 0; return PJ_RETURN_OS_ERROR(GetLastError()); } if ((flags & PJ_O_APPEND) == PJ_O_APPEND) { pj_status_t status; status = pj_file_setpos(hFile, 0, PJ_SEEK_END); if (status != PJ_SUCCESS) { pj_file_close(hFile); return status; } } *fd = hFile; return PJ_SUCCESS; }
PJ_DEF(pj_status_t) pj_file_open( pj_pool_t *pool, const char *pathname, unsigned flags, pj_oshandle_t *fd) { PJ_DECL_UNICODE_TEMP_BUF(wpathname,256) HANDLE hFile; DWORD dwDesiredAccess = 0; DWORD dwShareMode = 0; DWORD dwCreationDisposition = 0; DWORD dwFlagsAndAttributes = 0; PJ_UNUSED_ARG(pool); PJ_ASSERT_RETURN(pathname!=NULL, PJ_EINVAL); if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) { dwDesiredAccess |= GENERIC_WRITE; if ((flags & PJ_O_APPEND) == PJ_O_APPEND) { dwDesiredAccess |= FILE_APPEND_DATA; dwCreationDisposition |= OPEN_ALWAYS; } else { dwDesiredAccess &= ~(FILE_APPEND_DATA); dwCreationDisposition |= CREATE_ALWAYS; } } if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) { dwDesiredAccess |= GENERIC_READ; if (flags == PJ_O_RDONLY) dwCreationDisposition |= OPEN_EXISTING; } if (dwDesiredAccess == 0) { pj_assert(!"Invalid file open flags"); return PJ_EINVAL; } dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; hFile = CreateFile(PJ_STRING_TO_NATIVE(pathname,wpathname,sizeof(wpathname)), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); if (hFile == INVALID_HANDLE_VALUE) { *fd = 0; return PJ_RETURN_OS_ERROR(GetLastError()); } if ((flags & PJ_O_APPEND) == PJ_O_APPEND) { pj_status_t status; status = pj_file_setpos(hFile, 0, PJ_SEEK_END); if (status != PJ_SUCCESS) { pj_file_close(hFile); return status; } } *fd = hFile; return PJ_SUCCESS; }
/* * Create wave list player. */ PJ_DEF(pj_status_t) pjmedia_wav_playlist_create(pj_pool_t *pool, const pj_str_t *port_label, const pj_str_t file_list[], int file_count, unsigned ptime, unsigned options, pj_ssize_t buff_size, pjmedia_port **p_port) { struct playlist_port *fport; pj_off_t pos; pj_status_t status; int index; pj_bool_t has_wave_info = PJ_FALSE; pj_str_t tmp_port_label; char filename[PJ_MAXPATH]; /* filename for open operations. */ /* Check arguments. */ PJ_ASSERT_RETURN(pool && file_list && file_count && p_port, PJ_EINVAL); /* Normalize port_label */ if (port_label == NULL || port_label->slen == 0) { tmp_port_label = pj_str("WAV playlist"); port_label = &tmp_port_label; } /* Be sure all files exist */ for (index=0; index<file_count; index++) { PJ_ASSERT_RETURN(file_list[index].slen < PJ_MAXPATH, PJ_ENAMETOOLONG); pj_memcpy(filename, file_list[index].ptr, file_list[index].slen); filename[file_list[index].slen] = '\0'; /* Check the file really exists. */ if (!pj_file_exists(filename)) { PJ_LOG(4,(THIS_FILE, "WAV playlist error: file '%s' not found", filename)); return PJ_ENOTFOUND; } } /* Normalize ptime */ if (ptime == 0) ptime = 20; /* Create fport instance. */ fport = create_file_list_port(pool, port_label); if (!fport) { return PJ_ENOMEM; } /* start with the first file. */ fport->current_file = 0; fport->max_file = file_count; /* Create file descriptor list */ fport->fd_list = pj_pool_zalloc(pool, sizeof(pj_oshandle_t)*file_count); if (!fport->fd_list) { return PJ_ENOMEM; } /* Create file size list */ fport->fsize_list = pj_pool_alloc(pool, sizeof(pj_off_t)*file_count); if (!fport->fsize_list) { return PJ_ENOMEM; } /* Create start of WAVE data list */ fport->start_data_list = pj_pool_alloc(pool, sizeof(unsigned)*file_count); if (!fport->start_data_list) { return PJ_ENOMEM; } /* Create file position list */ fport->fpos_list = pj_pool_alloc(pool, sizeof(pj_off_t)*file_count); if (!fport->fpos_list) { return PJ_ENOMEM; } /* Create file buffer once for this operation. */ if (buff_size < 1) buff_size = PJMEDIA_FILE_PORT_BUFSIZE; fport->bufsize = buff_size; /* Create buffer. */ fport->buf = pj_pool_alloc(pool, fport->bufsize); if (!fport->buf) { return PJ_ENOMEM; } /* Initialize port */ fport->options = options; fport->readpos = fport->buf; /* ok run this for all files to be sure all are good for playback. */ for (index=file_count-1; index>=0; index--) { pjmedia_wave_hdr wavehdr; pj_ssize_t size_to_read, size_read; /* we end with the last one so we are good to go if still in function*/ pj_memcpy(filename, file_list[index].ptr, file_list[index].slen); filename[file_list[index].slen] = '\0'; /* Get the file size. */ fport->current_file = index; fport->fsize_list[index] = pj_file_size(filename); /* Size must be more than WAVE header size */ if (fport->fsize_list[index] <= sizeof(pjmedia_wave_hdr)) { status = PJMEDIA_ENOTVALIDWAVE; goto on_error; } /* Open file. */ status = pj_file_open( pool, filename, PJ_O_RDONLY, &fport->fd_list[index]); if (status != PJ_SUCCESS) goto on_error; /* Read the file header plus fmt header only. */ size_read = size_to_read = sizeof(wavehdr) - 8; status = pj_file_read( fport->fd_list[index], &wavehdr, &size_read); if (status != PJ_SUCCESS) { goto on_error; } if (size_read != size_to_read) { status = PJMEDIA_ENOTVALIDWAVE; goto on_error; } /* Normalize WAVE header fields values from little-endian to host * byte order. */ pjmedia_wave_hdr_file_to_host(&wavehdr); /* Validate WAVE file. */ if (wavehdr.riff_hdr.riff != PJMEDIA_RIFF_TAG || wavehdr.riff_hdr.wave != PJMEDIA_WAVE_TAG || wavehdr.fmt_hdr.fmt != PJMEDIA_FMT_TAG) { TRACE_((THIS_FILE, "actual value|expected riff=%x|%x, wave=%x|%x fmt=%x|%x", wavehdr.riff_hdr.riff, PJMEDIA_RIFF_TAG, wavehdr.riff_hdr.wave, PJMEDIA_WAVE_TAG, wavehdr.fmt_hdr.fmt, PJMEDIA_FMT_TAG)); status = PJMEDIA_ENOTVALIDWAVE; goto on_error; } /* Must be PCM with 16bits per sample */ if (wavehdr.fmt_hdr.fmt_tag != 1 || wavehdr.fmt_hdr.bits_per_sample != 16) { status = PJMEDIA_EWAVEUNSUPP; goto on_error; } /* Block align must be 2*nchannels */ if (wavehdr.fmt_hdr.block_align != wavehdr.fmt_hdr.nchan * BYTES_PER_SAMPLE) { status = PJMEDIA_EWAVEUNSUPP; goto on_error; } /* If length of fmt_header is greater than 16, skip the remaining * fmt header data. */ if (wavehdr.fmt_hdr.len > 16) { size_to_read = wavehdr.fmt_hdr.len - 16; status = pj_file_setpos(fport->fd_list[index], size_to_read, PJ_SEEK_CUR); if (status != PJ_SUCCESS) { goto on_error; } } /* Repeat reading the WAVE file until we have 'data' chunk */ for (;;) { pjmedia_wave_subchunk subchunk; size_read = 8; status = pj_file_read(fport->fd_list[index], &subchunk, &size_read); if (status != PJ_SUCCESS || size_read != 8) { status = PJMEDIA_EWAVETOOSHORT; goto on_error; } /* Normalize endianness */ PJMEDIA_WAVE_NORMALIZE_SUBCHUNK(&subchunk); /* Break if this is "data" chunk */ if (subchunk.id == PJMEDIA_DATA_TAG) { wavehdr.data_hdr.data = PJMEDIA_DATA_TAG; wavehdr.data_hdr.len = subchunk.len; break; } /* Otherwise skip the chunk contents */ size_to_read = subchunk.len; status = pj_file_setpos(fport->fd_list[index], size_to_read, PJ_SEEK_CUR); if (status != PJ_SUCCESS) { goto on_error; } } /* Current file position now points to start of data */ status = pj_file_getpos(fport->fd_list[index], &pos); fport->start_data_list[index] = (unsigned)pos; /* Validate length. */ if (wavehdr.data_hdr.len != fport->fsize_list[index] - fport->start_data_list[index]) { status = PJMEDIA_EWAVEUNSUPP; goto on_error; } if (wavehdr.data_hdr.len < 400) { status = PJMEDIA_EWAVETOOSHORT; goto on_error; } /* It seems like we have a valid WAVE file. */ /* Update port info if we don't have one, otherwise check * that the WAV file has the same attributes as previous files. */ if (!has_wave_info) { fport->base.info.channel_count = wavehdr.fmt_hdr.nchan; fport->base.info.clock_rate = wavehdr.fmt_hdr.sample_rate; fport->base.info.bits_per_sample = wavehdr.fmt_hdr.bits_per_sample; fport->base.info.samples_per_frame = fport->base.info.clock_rate * wavehdr.fmt_hdr.nchan * ptime / 1000; fport->base.info.bytes_per_frame = fport->base.info.samples_per_frame * fport->base.info.bits_per_sample / 8; has_wave_info = PJ_TRUE; } else { /* Check that this file has the same characteristics as the other * files. */ if (wavehdr.fmt_hdr.nchan != fport->base.info.channel_count || wavehdr.fmt_hdr.sample_rate != fport->base.info.clock_rate || wavehdr.fmt_hdr.bits_per_sample != fport->base.info.bits_per_sample) { /* This file has different characteristics than the other * files. */ PJ_LOG(4,(THIS_FILE, "WAV playlist error: file '%s' has differrent number" " of channels, sample rate, or bits per sample", filename)); status = PJMEDIA_EWAVEUNSUPP; goto on_error; } } /* Set initial position of the file. */ fport->fpos_list[index] = fport->start_data_list[index]; } /* Fill up the buffer. */ status = file_fill_buffer(fport); if (status != PJ_SUCCESS) { goto on_error; } /* Done. */ *p_port = &fport->base; PJ_LOG(4,(THIS_FILE, "WAV playlist '%.*s' created: samp.rate=%d, ch=%d, bufsize=%uKB", (int)port_label->slen, port_label->ptr, fport->base.info.clock_rate, fport->base.info.channel_count, fport->bufsize / 1000)); return PJ_SUCCESS; on_error: for (index=0; index<file_count; ++index) { if (fport->fd_list[index] != 0) pj_file_close(fport->fd_list[index]); } return status; }
/* * Fill buffer for file_list operations. */ static pj_status_t file_fill_buffer(struct playlist_port *fport) { pj_ssize_t size_left = fport->bufsize; unsigned size_to_read; pj_ssize_t size; pj_status_t status; int current_file = fport->current_file; /* Can't read file if EOF and loop flag is disabled */ if (fport->eof) return PJ_EEOF; while (size_left > 0) { /* Calculate how many bytes to read in this run. */ size = size_to_read = size_left; status = pj_file_read(fport->fd_list[current_file], &fport->buf[fport->bufsize-size_left], &size); if (status != PJ_SUCCESS) return status; if (size < 0) { /* Should return more appropriate error code here.. */ return PJ_ECANCELLED; } size_left -= size; fport->fpos_list[current_file] += size; /* If size is less than size_to_read, it indicates that we've * encountered EOF. Rewind the file. */ if (size < (pj_ssize_t)size_to_read) { /* Rewind the file for the next iteration */ fport->fpos_list[current_file] = fport->start_data_list[current_file]; pj_file_setpos(fport->fd_list[current_file], fport->fpos_list[current_file], PJ_SEEK_SET); /* Move to next file */ current_file++; fport->current_file = current_file; if (fport->current_file == fport->max_file) { /* All files have been played. Call callback, if any. */ if (fport->cb) { PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, calling callback", (int)fport->base.info.name.slen, fport->base.info.name.ptr)); fport->eof = PJ_TRUE; status = (*fport->cb)(&fport->base, fport->base.port_data.pdata); if (status != PJ_SUCCESS) { /* This will crash if file port is destroyed in the * callback, that's why we set the eof flag before * calling the callback: fport->eof = PJ_TRUE; */ return status; } fport->eof = PJ_FALSE; } if (fport->options & PJMEDIA_FILE_NO_LOOP) { PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, stopping..", (int)fport->base.info.name.slen, fport->base.info.name.ptr)); fport->eof = PJ_TRUE; return PJ_EEOF; } else { PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, rewinding..", (int)fport->base.info.name.slen, fport->base.info.name.ptr)); /* start with first file again. */ fport->current_file = current_file = 0; fport->fpos_list[0] = fport->start_data_list[0]; pj_file_setpos(fport->fd_list[0], fport->fpos_list[0], PJ_SEEK_SET); } } /* if current_file == max_file */ } /* size < size_to_read */ } /* while () */ /* Convert samples to host rep */ samples_to_host((pj_int16_t*)fport->buf, fport->bufsize/BYTES_PER_SAMPLE); return PJ_SUCCESS; }