www.gusucode.com > 嵌入式linux系统的网络编程源码程序 > 嵌入式linux系统的网络编程源码程序/视频会议源码/audio_play_thread.cpp
/////////////////////////////////////////////////////// // FileName: audio_play_thread.cpp // Author: b1gm0use // Project: myaudio #include <iostream> #include <unistd.h> #include <sys/ioctl.h> #include <linux/soundcard.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <qsemaphore.h> #include "audio_play_thread.h" #include "audio_player.h" #include "audio.h" #include "common.h" #include "network.h" #include "avi.h" using namespace std; /////////////////////////////////////////////////////// // Public Functions /////////////////////////////////////////////////////// // 构造函数 // 传入参数为 // ap_in 上层AudioPlayer对象指针 // img 图像参数 // msg_in 为一个字符串类型的参数,用于传递IP或设备名 // type_in AudioPlayer类型 // stackSize QThread所用的参数 audio_play_thread::audio_play_thread ( avi * avi_ptr_in, AudioPlayer * ap_in, unsigned int stackSize ) :QThread( stackSize ) // {{{ { verbose_output( 2, "create audio_play_thread." ); ap = ap_in; fd_w = 0; frame = 0; avi_ptr = avi_ptr_in; } // }}} void audio_play_thread::init( void ) // {{{ { verbose_output( 2, "init audio_play_thread." ); int arg = 0; int status = 0; // 打开回放 fd_w = open( avi_ptr->audio_dev, O_WRONLY); if (fd_w < 0) { cerr << "open device [" << avi_ptr->audio_dev << "] failed." << endl; ::exit(1); } // 设置采样时的量化位数 arg = SIZE; status = ioctl(fd_w, SOUND_PCM_WRITE_BITS, &arg); if (status == -1) { cerr << "SOUND_PCM_WRITE_BITS ioctl failed" << endl;; ::exit( 1 ); } if (arg != SIZE) { cerr << "Warning! unable to set sample size" << endl;; } verbose_output( 2, "use sample size: ", arg ); // 设置采样时的声道数目 arg = CHANNELS; status = ioctl(fd_w, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1) { cerr << "SOUND_PCM_WRITE_CHANNELS ioctl failed" << endl;; ::exit( 1 ); } if (arg != CHANNELS) { cerr << "Warning! unable to set number of channels" << endl;; } verbose_output( 2, "use audio channels: ", arg ); // 设置采样时的采样频率 arg = RATE; status = ioctl(fd_w, SOUND_PCM_WRITE_RATE, &arg); if (status == -1) { cerr << "SOUND_PCM_WRITE_WRITE ioctl failed" << endl;; ::exit( 1 ); } if (arg != RATE ) { cerr << "Warning! unable to set sample rate" << endl;; } verbose_output( 2, "use sample rate: ", arg ); } // }}} // 运行部分,线程代码在这里 void audio_play_thread::run ( void ) // {{{ { int status = 0; verbose_output( 2, "audio_play_thread begin running." ); //(*(avi_ptr->audio_can_play_semaphore))++; while ( 1 ) { ap->ready_to_play_audio->wait(); verbose_output( 4, "Playing...lock frame No.", frame ); (*(avi_ptr->snd_buff_semaphore[frame]))++; //回放 status = write( fd_w, avi_ptr->snd_buff[frame], BUFF_SIZE * LENGTH ); if ( status == -1 ) { cerr << "Write to device error." << endl; ::exit( 1 ); } verbose_output( 4, "Playing...unlock frame No.", frame ); (*(avi_ptr->snd_buff_semaphore[frame]))--; // 等待回放结束 //status = ioctl( fd_w, SOUND_PCM_SYNC, 0 ); if ( status == -1 ) { cerr << "SOUND_PCM_SYNC ioctl failed" << endl; ::exit( 1 ); } frame++; frame %= 3; } return; } // }}}