File: mp3play.cc

package info (click to toggle)
mp3info 0.2.9-1
  • links: PTS
  • area: non-free
  • in suites: hamm, slink
  • size: 104 kB
  • ctags: 130
  • sloc: cpp: 1,013; makefile: 84; sh: 59
file content (156 lines) | stat: -rw-r--r-- 3,356 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <sys/soundcard.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "mp3info.h"
#include <termio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

static int decpid;
static int decinpipe;

static int dsp_speed, dsp_stereo;
static int samplesize;
static int audio;

static char audio_name[20] = AUDIO;

static int canplay,lastsig;

int initpipe(void) {
   int dec_inpipe[2];
   int devnull;
   
   devnull=open("/dev/null",O_WRONLY);
   
   if (pipe(dec_inpipe)!=0) {
      return(0);
   }
   
   if ((decpid = fork()) == 0) {
      close(dec_inpipe[1]); 
      dup2(dec_inpipe[0], fileno(stdin));
      close(dec_inpipe[0]);
      
      dup2(audio,fileno(stdout));
      dup2(devnull,fileno(stderr));
      
      execlp(DECODER, DECODER, DECODEOPTS, NULL);
      fprintf(stderr, "Error: Decoder failed.\n");
      exit(1);
   }
   
   close(dec_inpipe[0]);
   decinpipe = dec_inpipe[1];
   close(devnull);
   return(1);
}


int initsound() {
   int tmp;
   
   audio = open (audio_name, O_WRONLY, 0);
   
   if (audio == -1) {
      perror (audio_name);
      return(0);
   }
   
   tmp = 0x7FFF000C;
   if (ioctl(audio, SNDCTL_DSP_SETFRAGMENT, &tmp)) {
      fprintf(stderr,"Error: Unable to optimize fragment size.\n");
   }
   
   tmp = samplesize;
   ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &samplesize);
   
   if (tmp != samplesize) {
      fprintf(stderr, "Error: Unable to set the sample size.\n");
      return(0);
   }
   
   if (ioctl (audio, SNDCTL_DSP_STEREO, &dsp_stereo)==-1)     {
      fprintf (stderr, "Error: Unable to set mono/stereo.\n");
      perror (audio_name);
      return(0);
   }
   
   if (ioctl (audio, SNDCTL_DSP_SPEED, &dsp_speed) == -1)     {
      fprintf (stderr, "Error: Unable to set audio speed.\n");
      perror (audio_name);
      return(0);
   }
   
   ioctl (audio, SNDCTL_DSP_GETBLKSIZE, &tmp);
   if (tmp!=4096) {
      fprintf (stderr, "Error: Fragment size is not 4096.\n");
   }     
   return(1);
}

void end(int wait) {
   if (decpid) {
      if (wait) ioctl(audio,SNDCTL_DSP_POST,0);
      close(decinpipe);
      if (wait) ioctl(audio, SNDCTL_DSP_SYNC, 0);
      kill(decpid, SIGKILL);
      close(audio);
      decpid = 0;
      decinpipe = 0;
   }
}

void sighandler(int sig) {
   canplay=0;
   lastsig=sig;
   return;
}

bool Mpfile::play(int threads) {
   char buffer[PLAY_BUF_LEN];
   size_t size;
   struct sigaction sigstruc;
   
   dsp_speed=layer->sfreq();
   samplesize=16;
   if (layer->stereo) dsp_stereo=1;
   else dsp_stereo=0;
   if (!initsound()) {
      fprintf(stderr,"Error: Failed to init sound device.\n");
      return(0);
   }
   if (!initpipe()) {
      fprintf(stderr,"Error: Failed to open Decoder Pipe.\n");
      return(0);
   }
   
#ifdef L3UNREG
   write(decinpipe,"N",1);
#endif
   
   canplay=1;
   lastsig=0;

   sigstruc.sa_handler=sighandler;
   memset(&sigstruc.sa_mask,0,sizeof(sigstruc.sa_mask));
   sigstruc.sa_flags=SA_RESTART;

   sigaction(SIGHUP,&sigstruc,NULL);
   sigaction(SIGTERM,&sigstruc,NULL);
   signal(SIGCHLD,SIG_IGN);

   fseek(file,0,SEEK_SET);
   while ((size=fread(&buffer,1,PLAY_BUF_LEN,file)) && canplay) {
      write(decinpipe,&buffer,size);
   }
   end(canplay);
   if (lastsig==SIGTERM) exit(0);
   return(1);
}