b.liu | f191eb7 | 2024-12-12 10:45:23 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <errno.h> |
| 5 | #include <unistd.h> |
| 6 | #include <time.h> |
| 7 | #include <sys/time.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <pthread.h> |
| 12 | |
| 13 | #include "mbtk_loop_buffer.h" |
| 14 | #include "mbtk_log.h" |
| 15 | #include "mbtk_utils.h" |
| 16 | |
| 17 | |
| 18 | static void* read_run(void* arg) |
| 19 | { |
| 20 | mbtk_loop_buff_handle *handle = (mbtk_loop_buff_handle*)arg; |
| 21 | int fd = open("/data/voip_playback.wav", O_RDONLY); |
| 22 | int read_count = 0; |
| 23 | int buff_count = 0; |
| 24 | if(fd > 0) { |
| 25 | char buff[1569]; |
| 26 | int len; |
| 27 | int temp_len; |
| 28 | while((len = read(fd, buff, sizeof(buff))) > 0) { |
| 29 | read_count += len; |
| 30 | |
| 31 | temp_len = mbtk_loopbuff_writen(handle, buff, len); |
| 32 | if(temp_len > 0) { |
| 33 | if(temp_len != len) { |
| 34 | printf("mbtk_loopbuff_writen() fail : %d/%d\n", temp_len, len); |
| 35 | } |
| 36 | buff_count += temp_len; |
| 37 | } else { |
| 38 | printf("mbtk_loopbuff_writen() fail.\n"); |
| 39 | } |
| 40 | } |
| 41 | close(fd); |
| 42 | } |
| 43 | printf("read complete : %d / %d\n", read_count, buff_count); |
| 44 | |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | static void* write_run(void* arg) |
| 49 | { |
| 50 | mbtk_loop_buff_handle *handle = (mbtk_loop_buff_handle*)arg; |
| 51 | int write_count = 0; |
| 52 | int buff_count = 0; |
| 53 | int fd = open("/data/voip_playback_copy.wav", O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 54 | if(fd > 0) { |
| 55 | char buff[999]; |
| 56 | int len; |
| 57 | while((len = mbtk_loopbuff_readn(handle, buff, sizeof(buff))) > 0) { |
| 58 | buff_count += len; |
| 59 | len = write(fd, buff, len); |
| 60 | if(len > 0) |
| 61 | write_count += len; |
| 62 | else |
| 63 | printf("mbtk_write() fail.\n"); |
| 64 | } |
| 65 | close(fd); |
| 66 | } |
| 67 | |
| 68 | printf("write complete : %d / %d\n", write_count, buff_count); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | int main(int argc, char *argv[]) |
| 73 | { |
| 74 | mbtk_log_init("radio", "LOOPBUFF"); |
| 75 | mbtk_loop_buff_handle *handle = mbtk_loopbuff_get(7 * 1001); |
| 76 | if(handle) { |
| 77 | pthread_t read_pid, write_pid; |
| 78 | if(pthread_create(&read_pid, NULL, read_run, handle)) |
| 79 | { |
| 80 | LOGE("pthread_create() fail."); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | if(pthread_create(&write_pid, NULL, write_run, handle)) |
| 85 | { |
| 86 | LOGE("pthread_create() fail."); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | sleep(1); |
| 91 | |
| 92 | if (pthread_join(read_pid, NULL)){ |
| 93 | printf("error join thread!\n"); |
| 94 | abort(); |
| 95 | } |
| 96 | |
| 97 | if (pthread_join(write_pid, NULL)){ |
| 98 | printf("error join thread!\n"); |
| 99 | abort(); |
| 100 | } |
| 101 | |
| 102 | mbtk_loopbuff_free(handle); |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | |