1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "playerData.h"
23 #include <sys/types.h>
31 int buffered_before_play
;
34 #define DEFAULT_BUFFER_SIZE 2048
35 #define DEFAULT_BUFFER_BEFORE_PLAY 10
37 static PlayerData
*playerData_pd
;
39 void initPlayerData(void)
41 float perc
= DEFAULT_BUFFER_BEFORE_PLAY
;
45 size_t bufferSize
= DEFAULT_BUFFER_SIZE
;
46 size_t allocationSize
;
49 size_t device_array_size
= audio_device_count() * sizeof(mpd_sint8
);
51 param
= getConfigParam(CONF_AUDIO_BUFFER_SIZE
);
54 bufferSize
= strtol(param
->value
, &test
, 10);
55 if (*test
!= '\0' || bufferSize
<= 0) {
56 FATAL("buffer size \"%s\" is not a positive integer, "
57 "line %i\n", param
->value
, param
->line
);
63 buffered_chunks
= bufferSize
/ CHUNK_SIZE
;
65 if (buffered_chunks
>= 1 << 15) {
66 FATAL("buffer size \"%li\" is too big\n", (long)bufferSize
);
69 param
= getConfigParam(CONF_BUFFER_BEFORE_PLAY
);
72 perc
= strtod(param
->value
, &test
);
73 if (*test
!= '%' || perc
< 0 || perc
> 100) {
74 FATAL("buffered before play \"%s\" is not a positive "
75 "percentage and less than 100 percent, line %i"
76 "\n", param
->value
, param
->line
);
80 buffered_before_play
= (perc
/ 100) * buffered_chunks
;
81 if (buffered_before_play
> buffered_chunks
) {
82 buffered_before_play
= buffered_chunks
;
83 } else if (buffered_before_play
< 0)
84 buffered_before_play
= 0;
86 allocationSize
= buffered_chunks
* CHUNK_SIZE
; /*actual buffer */
87 allocationSize
+= buffered_chunks
* sizeof(float); /*for times */
88 allocationSize
+= buffered_chunks
* sizeof(mpd_sint16
); /*for chunkSize */
89 allocationSize
+= buffered_chunks
* sizeof(mpd_sint16
); /*for bitRate */
90 allocationSize
+= buffered_chunks
* sizeof(mpd_sint8
); /*for metaChunk */
91 allocationSize
+= sizeof(PlayerData
); /*for playerData struct */
93 /* for audioDeviceStates[] */
94 allocationSize
+= device_array_size
;
96 if ((shmid
= shmget(IPC_PRIVATE
, allocationSize
, IPC_CREAT
| 0600)) < 0)
97 FATAL("problems shmget'ing\n");
98 if (!(playerData_pd
= shmat(shmid
, NULL
, 0)))
99 FATAL("problems shmat'ing\n");
100 if (shmctl(shmid
, IPC_RMID
, NULL
) < 0)
101 FATAL("problems shmctl'ing\n");
103 playerData_pd
->audioDeviceStates
= (mpd_uint8
*)playerData_pd
+
104 allocationSize
- device_array_size
;
105 buffer
= &(playerData_pd
->buffer
);
107 memset(&buffer
->convState
, 0, sizeof(ConvState
));
108 buffer
->chunks
= ((char *)playerData_pd
) + sizeof(PlayerData
);
109 buffer
->chunkSize
= (mpd_uint16
*) (((char *)buffer
->chunks
) +
110 buffered_chunks
* CHUNK_SIZE
);
111 buffer
->bitRate
= (mpd_uint16
*) (((char *)buffer
->chunkSize
) +
112 buffered_chunks
* sizeof(mpd_sint16
));
113 buffer
->metaChunk
= (mpd_sint8
*) (((char *)buffer
->bitRate
) +
117 (float *)(((char *)buffer
->metaChunk
) +
118 buffered_chunks
* sizeof(mpd_sint8
));
119 buffer
->acceptMetadata
= 0;
121 playerData_pd
->playerControl
.wait
= 0;
122 playerData_pd
->playerControl
.stop
= 0;
123 playerData_pd
->playerControl
.pause
= 0;
124 playerData_pd
->playerControl
.play
= 0;
125 playerData_pd
->playerControl
.error
= PLAYER_ERROR_NOERROR
;
126 playerData_pd
->playerControl
.lockQueue
= 0;
127 playerData_pd
->playerControl
.unlockQueue
= 0;
128 playerData_pd
->playerControl
.state
= PLAYER_STATE_STOP
;
129 playerData_pd
->playerControl
.queueState
= PLAYER_QUEUE_BLANK
;
130 playerData_pd
->playerControl
.queueLockState
= PLAYER_QUEUE_UNLOCKED
;
131 playerData_pd
->playerControl
.seek
= 0;
132 playerData_pd
->playerControl
.closeAudio
= 0;
133 memset(playerData_pd
->playerControl
.utf8url
, 0, MAXPATHLEN
+ 1);
134 memset(playerData_pd
->playerControl
.erroredUrl
, 0, MAXPATHLEN
+ 1);
135 memset(playerData_pd
->playerControl
.currentUrl
, 0, MAXPATHLEN
+ 1);
136 playerData_pd
->playerControl
.crossFade
= crossfade
;
137 playerData_pd
->playerControl
.softwareVolume
= 1000;
138 playerData_pd
->playerControl
.totalPlayTime
= 0;
139 playerData_pd
->playerControl
.decode_pid
= 0;
140 playerData_pd
->playerControl
.metadataState
=
141 PLAYER_METADATA_STATE_WRITE
;
143 playerData_pd
->decoderControl
.stop
= 0;
144 playerData_pd
->decoderControl
.start
= 0;
145 playerData_pd
->decoderControl
.state
= DECODE_STATE_STOP
;
146 playerData_pd
->decoderControl
.seek
= 0;
147 playerData_pd
->decoderControl
.error
= DECODE_ERROR_NOERROR
;
148 memset(playerData_pd
->decoderControl
.utf8url
, 0, MAXPATHLEN
+ 1);
151 PlayerData
*getPlayerData(void)
153 return playerData_pd
;
156 void freePlayerData(void)
158 /* We don't want to release this memory until we know our player and
159 * decoder have exited. Otherwise, their signal handlers will want to
160 * access playerData_pd and we need to keep it available for them */
161 waitpid(-1, NULL
, 0);
162 shmdt(playerData_pd
);