Cosmetics.
[elisp.git] / media-mplayer.el
1 ;; -*-Emacs-Lisp-*-
2
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ;; This program is free software; you can redistribute it and/or         ;;
5 ;; modify it under the terms of the GNU General Public License as        ;;
6 ;; published by the Free Software Foundation; either version 3, or (at   ;;
7 ;; your option) any later version.                                       ;;
8 ;;                                                                       ;;
9 ;; This program is distributed in the hope that it will be useful, but   ;;
10 ;; WITHOUT ANY WARRANTY; without even the implied warranty of            ;;
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      ;;
12 ;; General Public License for more details.                              ;;
13 ;;                                                                       ;;
14 ;; You should have received a copy of the GNU General Public License     ;;
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.  ;;
16 ;;                                                                       ;;
17 ;; Written by and Copyright (C) Francois Fleuret                         ;;
18 ;; Contact <francois@fleuret.org> for comments & bug reports             ;;
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20
21 ;; Is it me, or the slave mode of mplayer is ugly to parse? Did I miss
22 ;; something?
23
24 (defcustom media/mplayer/args nil
25   "List of arguments for mplayer."
26   :type 'list
27   :group 'media)
28
29 (defcustom media/mplayer/timing-request-period 0.25
30   "Period for the timing requests in second(s). Larger values
31 load Emacs less. Nil means no timing."
32   :type 'float
33   :group 'media)
34
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36
37 ;; It is impossible to tell mplayer to send information every time dt
38 ;; or so, hence this mess with a timer to avoid overloading emacs with
39 ;; the processing of the information
40
41 (defvar media/mplayer/timer nil
42   "A timer to request the timing position.")
43
44 (defun media/mplayer/timing-request ()
45   (if media/mplayer/process
46       (unless media/mplayer/paused
47         (media/mplayer/write "get_time_pos\n")
48         )
49     (media/mplayer/stop-timing-requests)
50     ))
51
52 (defun media/mplayer/start-timing-requests ()
53   (when media/mplayer/timing-request-period
54     (media/mplayer/stop-timing-requests)
55     (setq media/mplayer/timer
56           (run-at-time nil
57                        media/mplayer/timing-request-period
58                        'media/mplayer/timing-request))
59     )
60   )
61
62 (defun media/mplayer/stop-timing-requests ()
63   (when media/mplayer/timer
64     (cancel-timer media/mplayer/timer)
65     (setq media/mplayer/timer nil)
66     ))
67
68 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
69
70 (defun media/mplayer/filter-subfunctions (cmd param)
71   ;; (unless (string= cmd "A:")
72     ;; (message "cmd=%s param=%s" cmd param)
73     ;; )
74   (eval
75    (cdr
76     (assoc cmd
77
78            '(
79
80              ;; ----------------------------------------
81
82              ("ICY Info:" .
83               (progn
84                 ;; (message "ICY Info \"%s\"" param)
85                 (if (string-match "StreamTitle='\\([^;]*\\)';" param)
86                     (setq media/current-song-in-stream
87                           (let ((s (match-string 1 param)))
88                             (concat (if (string= s "") "<no title>" s)
89                                     " | "
90                                     ;; (current-time-string)
91                                     (format-time-string "%a %b %d %H:%M:%S")
92                                     )))
93                   (setq media/current-song-in-stream nil)
94                   (message "ICY Info \"%s\"" param))
95                 (if (and media/current-song-in-stream media/current-information)
96                     (media/show-current-information)))
97               )
98
99              ;; ----------------------------------------
100
101              ("ANS_LENGTH" .
102
103               (setq media/song-duration
104                     (string-to-number (substring param 1))))
105
106              ;; ----------------------------------------
107
108              ("ANS_TIME_POSITION" .
109
110               (progn
111                 (setq media/song-current-time
112                       (string-to-number (substring param 1)))
113
114                 (when (and media/duration-to-history
115                            (< media/mplayer/cumulated-duration media/duration-to-history))
116
117                   (when media/mplayer/last-current-time
118                     (setq media/mplayer/cumulated-duration
119                           (+ media/mplayer/cumulated-duration
120                              (- media/song-current-time media/mplayer/last-current-time))))
121
122                   (when (>= media/mplayer/cumulated-duration media/duration-to-history)
123                     (media/put-in-history)
124                     )
125
126                   (setq media/mplayer/last-current-time media/song-current-time)
127                   )
128
129
130                 )
131               )
132
133              ;; ----------------------------------------
134
135              ("AUDIO:" .
136
137               (progn
138                 ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
139                 (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
140                                     param)
141                   (setq media/current-information
142                         (list media/mplayer/url
143                               (string-to-number (match-string 1 param))
144                               (string-to-number (match-string 2 param))
145                               (string-to-number (match-string 3 param))))
146                   )
147                 (run-hooks 'media/play-hook)
148                 ))
149
150              ;; ----------------------------------------
151
152              ("Starting" .
153               (media/mplayer/write "get_time_length\n"))
154
155              ;; ----------------------------------------
156
157              ("Cache fill:" .
158
159               (when (string-match "(\\([0-9]+\\) bytes" param)
160                 (message "Caching stream (%dkb)"
161                          (/ (string-to-number (match-string 1 param)) 1024))))
162
163              ;; ----------------------------------------
164
165              ("Exiting..." .
166
167               (progn
168                 (setq media/mplayer/exit-type
169                       (cdr (assoc param '(("(End of file)" . file-finished)
170                                           ("(Quit)" . quit))))
171                       media/current-information nil
172                       media/song-duration nil
173                       media/song-current-time nil)
174
175                 (when media/mplayer/process (kill-process media/mplayer/process))
176
177                 (force-mode-line-update)))
178
179              ;; ----------------------------------------
180
181              )
182            ))))
183
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
185
186 (defun media/mplayer/filter (process str)
187   (setq media/mplayer/buffer (concat media/mplayer/buffer str))
188   (let ((start 0))
189     (while (and (< start (length media/mplayer/buffer))
190                 (string-match "\\(.*\\)[\n\r]+" media/mplayer/buffer start))
191       (setq start (1+ (match-end 1)))
192       (let ((line (match-string 1 media/mplayer/buffer)))
193         (when (string-match "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$" line)
194           (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line))))
195       )
196     (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
197   )
198
199 (defun media/mplayer/sentinel (process str) ()
200   ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
201   (unless (eq (process-status media/mplayer/process) 'run)
202     (setq media/current-information nil
203           media/mplayer/process nil
204           media/song-current-time nil
205           media/song-duration nil)
206
207     (media/mplayer/stop-timing-requests)
208
209     (if (eq media/mplayer/exit-type 'file-finished)
210         (run-hooks 'media/finished-hook)
211       (run-hooks 'media/error-hook))
212
213     (force-mode-line-update))
214   )
215
216 (defun media/mplayer/write (&rest l)
217   ;;   (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
218   (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
219     (error "No mplayer process"))
220   )
221
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225
226 (defun media/api/init () "Called once when the media application starts"
227   (setq media/player-id "MPlayer"
228         media/mplayer/url nil
229         media/mplayer/buffer "" ;; Used as read buffer
230         media/mplayer/process nil
231         media/mplayer/exit-type nil
232         media/mplayer/paused nil
233         media/song-duration nil
234         media/song-current-time nil
235         media/mplayer/cumulated-duration 0
236         media/mplayer/last-current-time nil
237         ))
238
239 (defun media/api/cleanup () "Called when killing the application's buffer"
240   (when media/mplayer/process
241     (delete-process media/mplayer/process)
242     (media/mplayer/stop-timing-requests)
243     (setq media/mplayer/process nil)))
244
245 (defun media/api/play (url) (interactive)
246   (setq media/mplayer/url url)
247
248   (when media/mplayer/process (kill-process media/mplayer/process))
249
250   ;; (if media/mplayer/process
251   ;; (media/mplayer/write (concat "loadfile "
252   ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
253   ;; "\n"))
254
255   (setq media/mplayer/process
256         (apply
257          'start-process
258          (append
259           '("mplayer" nil "mplayer" "-slave" "-quiet")
260           media/mplayer/args
261           (if (string-match  "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
262               (list "-playlist"))
263           (list (replace-regexp-in-string "^file://" "" media/mplayer/url))))
264         media/mplayer/exit-type 'unknown
265         media/mplayer/paused nil
266         media/song-duration nil
267         media/song-current-time nil
268         media/mplayer/cumulated-duration 0
269         media/mplayer/last-current-time nil
270         )
271
272   (set-process-filter media/mplayer/process 'media/mplayer/filter)
273   (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
274   (process-kill-without-query media/mplayer/process)
275   (media/mplayer/start-timing-requests)
276   (media/mplayer/write "get_time_pos\n")
277
278   )
279
280 (defun media/api/stop () (interactive)
281   (media/mplayer/write "quit\n")
282   )
283
284 (defun media/api/pause () (interactive)
285   (media/mplayer/write "pause\n")
286   (setq media/mplayer/paused (not media/mplayer/paused))
287   )
288
289 (defun media/api/set-volume (mode value) (interactive)
290   (if (eq mode 'absolute)
291       (media/mplayer/write "volume %s 1\n" value)
292     (if (>= value 0)
293         (media/mplayer/write "volume +%s\n" value)
294       (media/mplayer/write "volume %s\n" value))))
295
296 (defun media/api/jump-at-percent (percent) (interactive)
297   (setq media/song-current-time nil)
298   (when (< media/mplayer/cumulated-duration media/duration-to-history)
299     (setq media/mplayer/cumulated-duration 0
300           media/mplayer/last-current-time nil))
301   (media/mplayer/write "seek %s 1\n" percent)
302   (media/mplayer/write "get_time_pos\n")
303   )
304
305 (defun media/api/jump-at-time (mode time) (interactive)
306   (setq media/song-current-time nil)
307   (when (< media/mplayer/cumulated-duration media/duration-to-history)
308     (setq media/mplayer/cumulated-duration 0
309           media/mplayer/last-current-time nil))
310   (if (eq mode 'absolute)
311       (media/mplayer/write "seek %s 2\n" time)
312     (media/mplayer/write "seek %s 0\n" time))
313   (media/mplayer/write "get_time_pos\n")
314   )
315
316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;