/* * Convert 32-bit value from host byte order to network byte order. */ PJ_DEF(pj_uint32_t) pj_htonl(pj_uint32_t hostlong) { #if PJ_IS_LITTLE_ENDIAN return pj_swap32(hostlong); #else return netlong; #endif }
/* * Convert 32-bit value from network byte order to host byte order. */ PJ_DEF(pj_uint32_t) pj_ntohl(pj_uint32_t netlong) { #if PJ_IS_LITTLE_ENDIAN return pj_swap32(netlong); #else return netlong; #endif }
static void data_to_host(void *data, pj_uint8_t bits, unsigned count) { unsigned i; pj_int32_t *data32 = (pj_int32_t *)data; pj_int16_t *data16 = (pj_int16_t *)data; count /= (bits == 32? 4 : 2); for (i=0; i<count; ++i) { if (bits == 32) data32[i] = pj_swap32(data32[i]); else data16[i] = pj_swap16(data16[i]); } }
/* * Change the endianness of WAVE header fields. */ static void wave_hdr_swap_bytes( pjmedia_wave_hdr *hdr ) { #if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0 hdr->riff_hdr.riff = pj_swap32(hdr->riff_hdr.riff); hdr->riff_hdr.file_len = pj_swap32(hdr->riff_hdr.file_len); hdr->riff_hdr.wave = pj_swap32(hdr->riff_hdr.wave); hdr->fmt_hdr.fmt = pj_swap32(hdr->fmt_hdr.fmt); hdr->fmt_hdr.len = pj_swap32(hdr->fmt_hdr.len); hdr->fmt_hdr.fmt_tag = pj_swap16(hdr->fmt_hdr.fmt_tag); hdr->fmt_hdr.nchan = pj_swap16(hdr->fmt_hdr.nchan); hdr->fmt_hdr.sample_rate = pj_swap32(hdr->fmt_hdr.sample_rate); hdr->fmt_hdr.bytes_per_sec = pj_swap32(hdr->fmt_hdr.bytes_per_sec); hdr->fmt_hdr.block_align = pj_swap16(hdr->fmt_hdr.block_align); hdr->fmt_hdr.bits_per_sample = pj_swap16(hdr->fmt_hdr.bits_per_sample); hdr->data_hdr.data = pj_swap32(hdr->data_hdr.data); hdr->data_hdr.len = pj_swap32(hdr->data_hdr.len); #else PJ_UNUSED_ARG(hdr); #endif }
/* * 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; }