/* ************************************************************************
// Last Change: 2011-01-19 15:56:14
* Filename: mplayer_main.c
* Description: mplayer maintance file
* Version: 1.0
* Created: 2011-1-13 11:27:45
* Revision: none
* Compiler: gcc
* Author: panda
* Company: sunplusapp
* ************************************************************************/
#include"mplayer_include.h"
#define FIFO "fifo"
int fd;
int pipedes[2];
char msg_buf[REC_MSG_CHNUM ] ;/*接收到信息缓冲区*/
int my_lock=0;//发送命令标志位
/*****************************************************************************
* Function name: send_cmd
* Description: send commend to mplayer by fifo
* parameter input: cmd
* parameter output: None
* return: None
*****************************************************************************/
void send_cmd(char *cmd)//通过有名管道向mplayer发送命令
{
if((write(fd,cmd,strlen(cmd)))!=strlen(cmd))
{
perror("write cmd");
}
}
/*****************************************************************************
* Function name: touch_pthread
* Description: a pthread that checking toucher status all times
* parameter input: None
* parameter output: None
* return: None
*****************************************************************************/
void *touch_pthread()
{
int key=0;
while(1)
{
usleep(100*MS);//延时处理触摸屏键值
if(ts_read(&ts)==-1)//读取按下的点
continue;
if((key=Touch_Trans(ts.x,ts.y))==-1)//将按下的点转化成设定的键值
continue;
key_dispose(key);//处理键值
}
return NULL;
}
/*****************************************************************************
* Function name: pipe_read_pthread
* Description: a pthread that ciculate reading msg from pipe data that reciver to mplayer
* parameter input: None
* parameter output: None
* return: None
*****************************************************************************/
void *pipe_read_pthread()
{
int size;
char buf[REC_MSG_CHNUM];
while(1)
{
memset(buf, 0 , REC_MSG_CHNUM) ;
if((size = read(pipedes[0],buf,sizeof(buf))) == -1)//读取mplayer发过来的歌曲信息
{
perror("read pipe");
exit(1);
}
if( size == 0)//如果没有读到信息,则返回继续读取
continue;
buf[size]='/0';//使信息变成字符串,便于处理
// printf("******************msg_buf=%s/n/n",buf);
strcpy(msg_buf,buf);
if(strncmp(buf,"ANS_META",8) ==0) //获取歌曲信息
{
buf[strlen(buf)-2]='/0';//多减一个去掉引号
msg_dispose(buf);
}
sem_post(&cmd_sem) ;
}
return NULL;
}
/*****************************************************************************
* Function name: pipe_read_dispose_pthread
* Description: a pthread that ciculate resolving msg
* parameter input: None
* parameter output: None
* return: None
*****************************************************************************/
void *pipe_read_dispose_pthread()
{
char buf[REC_MSG_CHNUM];
while(1)
{
sem_wait(&cmd_sem) ;
strcpy(buf,msg_buf);
if(strncmp(buf,"ANS_PERCENT_POSITION", 20)==0) //获取进度信息
{
percent_dispose(buf);
}
else if(strncmp(buf,"ANS_TIME_POSITION", 17) ==0) //获取歌曲当前播放时间
{
time_dispose(buf);
}
else if(strncmp(buf,"ANS_LENGTH",10) ==0) //获得歌的总长度
{
length_dispose(buf);
}
}
return NULL;
}
/*****************************************************************************
* Function name: get_percent_pos_pthread
* Description: a pthread that geting song time and percent of mplayer
* parameter input: None
* parameter output: None
* return: None
*****************************************************************************/
void *get_pos_pthread()
{
while(1)
{
if(my_lock==1 )
{
usleep(500*MS);
send_cmd("get_percent_pos/n");
usleep(500*MS);
send_cmd("get_time_pos/n");
}
}
return NULL;
}
/*****************************************************************************
* Function name: display_lrc_pthread
* Description: a pthread that displaying lyric
* parameter input: None
* parameter output: None
* return: None
*****************************************************************************/
void *display_lrc_pthread()
{
LRC *temp;
char oldtime[10]="00:00";
sleep(1);
while(1)
{
if(strcmp(song_msg.cur_time,oldtime)!=0)//时间变化了进入相应的歌词显示
{
temp=head;
while(temp!=NULL)
{
if(display_lrc(temp)==1)//如果找到该时间的歌词,则跳出循环,等待下一次时间的到来
{
break;
}
temp=temp->next;
}
strcpy(oldtime, song_msg.cur_time);
}
}
free_link(head);//释放链表空间
return NULL;
}
/*****************************************************************************
* Function name: main
* Description: maintance function
* parameter input: None
* parameter output: None
* return: None
*****************************************************************************/
int main(int argc,char *argv[])
{
pid_t pid;
unlink(FIFO);//如果管道存在,先删除
tft_init();//初始化触摸屏
get_song_list();//得到歌曲列表
unlink(FIFO);//如果管道存在,先删除
if(mkfifo("fifo",IPC_CREAT|0x744)==-1)//创建有名管道
{
perror("mkfifo");
exit(1);
}
if(pipe(pipedes)==-1)//创建无名管道用于从mplayer读取歌曲信息
{
perror("pipe");
exit(1);
}
if((pid=fork())==-1)
{
perror("fork");
exit(1);
}
else if(pid==0)//在子进程中播放歌曲
{
char song[SONG_CHNUM];
close(pipedes[0]);
dup2(pipedes[1],1);
sprintf(song,"%s%s","./song/",song_list[0]);//得到整个歌曲路径
execlp("./mplayer","","-ac","mad","-slave","-quiet","-input","file=fifo",song,NULL);
}
else if(pid>0)
{
pthread_t tid_touch;
pthread_t tid_pr;
pthread_t tid_prd;
pthread_t tid_gpp;
pthread_t tid_plp;
usleep(500*MS);//等待让歌曲播放之后在获取信息
if((fd=open(FIFO,O_RDWR))==-1)
{
perror("open");
exit(1);
}
pthread_create(&tid_touch,NULL,touch_pthread,NULL);//检测触摸屏
/*循环读管道把读到的消息保存在字符数组中*/
pthread_create(&tid_pr,NULL,pipe_read_pthread,NULL);
/*解析读到的消息,把有用的消息解析出来,做相应的处理*/
pthread_create(&tid_pr,NULL,pipe_read_dispose_pthread,NULL);
/*每隔一段时间发一条检测时间的命令,获取当前播放时间*/
pthread_create(&tid_gpp,NULL,get_pos_pthread,NULL);
pthread_create(&tid_plp,NULL,display_lrc_pthread,NULL);//显示歌词
slice_song(); //切歌后要执行的函数
pthread_join(tid_touch,NULL);
pthread_join(tid_pr,NULL);
pthread_join(tid_prd,NULL);
pthread_join(tid_gpp,NULL);
pthread_join(tid_plp,NULL);
}
return 0;
}