Update.
[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 (defcustom media/mplayer/capture-dir nil
36   "States where to save the dumped streams."
37   :type 'string
38   :group 'media)
39
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41
42 ;; It is impossible to tell mplayer to send information every time dt
43 ;; or so, hence this mess with a timer to avoid overloading emacs with
44 ;; the processing of the information
45
46 (defvar media/mplayer/timer nil
47   "A timer to request the timing position.")
48
49 (defun media/mplayer/timing-request ()
50   (if media/mplayer/process
51       (unless media/mplayer/paused
52         (media/mplayer/write "get_time_pos\n")
53         )
54     (media/mplayer/stop-timing-requests)
55     ))
56
57 (defun media/mplayer/start-timing-requests ()
58   (when media/mplayer/timing-request-period
59     (media/mplayer/stop-timing-requests)
60     (setq media/mplayer/timer
61           (run-at-time nil
62                        media/mplayer/timing-request-period
63                        'media/mplayer/timing-request))
64     )
65   )
66
67 (defun media/mplayer/stop-timing-requests ()
68   (when media/mplayer/timer
69     (cancel-timer media/mplayer/timer)
70     (setq media/mplayer/timer nil)
71     ))
72
73 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
74
75 (setq media/mplayer/protocol-regexp
76       "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$")
77
78 (defun media/mplayer/filter-subfunctions (cmd param)
79   ;; (unless (string= cmd "A:")
80   ;; (message "cmd=%s param=%s" cmd param)
81   ;; )
82   (eval
83    (cons 'progn
84          (cdr
85           (assoc cmd
86
87                  '(
88                    ("ICY Info:"
89                     ;; (message "ICY Info \"%s\"" param)
90
91                     (if (string-match "StreamTitle='\\([^;]*\\)';" param)
92
93                         (setq media/current-song-in-stream
94                               (let ((s (match-string 1 param)))
95                                 (concat (if (string= s "")
96                                             "<no title>"
97                                           ;; (encode-coding-string s 'latin-1)
98                                           s
99                                           )
100                                         " | "
101                                         (format-time-string "%a %b %d %H:%M:%S")
102                                         )
103                                 )
104                               )
105
106                       ;; If we did not parse it properly, reset the
107                       ;; song name, and display the ICY string raw
108                       (setq media/current-song-in-stream nil)
109                       (message "ICY Info \"%s\"" param)
110                       )
111
112                     (when media/mplayer/capture-dir
113                       (with-temp-buffer
114                         (insert
115                          (concat media/current-song-in-stream "\n"))
116                         (write-region nil nil (concat media/mplayer/capture-dir "/log") t)))
117
118                     (if (and media/current-song-in-stream media/current-information)
119                         (media/show-current-information))
120                     )
121
122                    ;; ----------------------------------------
123
124                    ("ANS_LENGTH"
125
126                     (setq media/song-duration
127                           (string-to-number (substring param 1))))
128
129                    ;; ----------------------------------------
130
131                    ("ANS_TIME_POSITION"
132
133                     (setq media/song-current-time
134                           (string-to-number (substring param 1)))
135
136                     (when (and media/duration-to-history
137                                (< media/mplayer/cumulated-duration media/duration-to-history))
138
139                       (when media/mplayer/last-current-time
140                         (setq media/mplayer/cumulated-duration
141                               (+ media/mplayer/cumulated-duration
142                                  (- media/song-current-time media/mplayer/last-current-time))))
143
144                       (when (>= media/mplayer/cumulated-duration media/duration-to-history)
145                         (media/put-in-history)
146                         )
147
148                       (setq media/mplayer/last-current-time media/song-current-time)
149                       )
150                     )
151
152                    ;; ----------------------------------------
153
154                    ("AUDIO:"
155
156                     ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
157                     (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
158                                         param)
159                       (setq media/current-information
160                             (list media/mplayer/url
161                                   (string-to-number (match-string 1 param))
162                                   (string-to-number (match-string 2 param))
163                                   (string-to-number (match-string 3 param))))
164                       )
165                     (run-hooks 'media/play-hook)
166                     )
167
168                    ;; ----------------------------------------
169
170                    ("Starting"
171                     (media/mplayer/write "get_time_length\n")
172                     (when media/mplayer/capture-dir
173                       (media/mplayer/write "capturing\n")
174                       ;; (message "Capturing stream in %s" media/mplayer/capture-dir)
175                       )
176                     )
177
178                    ;; ----------------------------------------
179
180                    ("Cache fill:"
181                     (when (string-match "(\\([0-9]+\\) bytes" param)
182                       (message "Caching stream (%dkb)"
183                                (/ (string-to-number (match-string 1 param)) 1024))))
184
185                    ;; ----------------------------------------
186
187                    ("Exiting..."
188                     (setq media/mplayer/exit-type
189                           (cdr (assoc param '(("(End of file)" . file-finished)
190                                               ("(Quit)" . quit))))
191                           media/current-information nil
192                           media/song-duration nil
193                           media/song-current-time nil)
194
195                     (when media/mplayer/process (kill-process media/mplayer/process))
196
197                     (force-mode-line-update)
198                     )
199
200                    ;; ----------------------------------------
201
202                    )
203                  )
204           )
205          )
206    )
207   )
208
209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210
211 (defun media/mplayer/filter (process str)
212   (setq media/mplayer/buffer (concat media/mplayer/buffer str))
213   (let ((start 0))
214     (while (and (< start (length media/mplayer/buffer))
215                 (string-match "\\(.*\\)[\n\r]+" media/mplayer/buffer start))
216       (setq start (1+ (match-end 1)))
217       (let ((line (match-string 1 media/mplayer/buffer)))
218         (when (string-match media/mplayer/protocol-regexp line)
219           (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line))))
220       )
221     (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
222   )
223
224 (defun media/mplayer/sentinel (process str) ()
225   ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
226   (unless (eq (process-status media/mplayer/process) 'run)
227     (setq media/current-information nil
228           media/mplayer/process nil
229           media/song-current-time nil
230           media/song-duration nil)
231
232     (media/mplayer/stop-timing-requests)
233
234     (if (eq media/mplayer/exit-type 'file-finished)
235         (run-hooks 'media/finished-hook)
236       (run-hooks 'media/error-hook))
237
238     (force-mode-line-update))
239   )
240
241 (defun media/mplayer/write (&rest l)
242   ;;   (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
243   (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
244     (error "No mplayer process"))
245   )
246
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250
251 (defun media/api/init () "Called once when the media application starts"
252   (setq media/player-id "MPlayer"
253         media/mplayer/url nil
254         media/mplayer/buffer "" ;; Used as read buffer
255         media/mplayer/process nil
256         media/mplayer/exit-type nil
257         media/mplayer/paused nil
258         media/song-duration nil
259         media/song-current-time nil
260         media/mplayer/cumulated-duration 0
261         media/mplayer/last-current-time nil
262         )
263 )
264
265 (defun media/api/cleanup () "Called when killing the application's buffer"
266   (when media/mplayer/process
267     (delete-process media/mplayer/process)
268     (media/mplayer/stop-timing-requests)
269     (setq media/mplayer/process nil)))
270
271 (defun media/api/play (url) (interactive)
272   (setq media/mplayer/url url)
273
274   (when media/mplayer/process (kill-process media/mplayer/process))
275
276   ;; (if media/mplayer/process
277   ;; (media/mplayer/write (concat "loadfile "
278   ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
279   ;; "\n"))
280
281   (setq media/mplayer/process
282         (apply
283          'start-process
284          (append
285           '("mplayer" nil "mplayer" "-slave" "-quiet")
286           media/mplayer/args
287           (when (string-match  "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
288             (if media/mplayer/capture-dir
289                 (list "-dumpfile"
290                       (concat media/mplayer/capture-dir
291                               "/"
292                               (replace-regexp-in-string "[^a-zA-Z0-9\.]" "_" media/mplayer/url)
293                               (format-time-string "-%Y-%m-%d-%H:%M:%S"))
294                       "-capture"
295                       "-playlist"
296                       )
297               (list "-playlist"))
298             )
299           (list (replace-regexp-in-string "^file://" "" media/mplayer/url)))
300          )
301         media/mplayer/exit-type 'unknown
302         media/mplayer/paused nil
303         media/song-duration nil
304         media/song-current-time nil
305         media/mplayer/cumulated-duration 0
306         media/mplayer/last-current-time nil
307         )
308
309   (set-process-filter media/mplayer/process 'media/mplayer/filter)
310   (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
311   (process-kill-without-query media/mplayer/process)
312   (media/mplayer/start-timing-requests)
313   (media/mplayer/write "get_time_pos\n")
314   )
315
316 (defun media/api/stop () (interactive)
317   (media/mplayer/write "quit\n")
318   )
319
320 (defun media/api/pause () (interactive)
321   (media/mplayer/write "pause\n")
322   (setq media/mplayer/paused (not media/mplayer/paused))
323   )
324
325 (defun media/api/set-volume (mode value) (interactive)
326   (if (eq mode 'absolute)
327       (media/mplayer/write "volume %s 1\n" value)
328     (if (>= value 0)
329         (media/mplayer/write "volume +%s\n" value)
330       (media/mplayer/write "volume %s\n" value))))
331
332 (defun media/api/jump-at-percent (percent) (interactive)
333   (setq media/song-current-time nil)
334   (when (< media/mplayer/cumulated-duration media/duration-to-history)
335     (setq media/mplayer/cumulated-duration 0
336           media/mplayer/last-current-time nil))
337   (media/mplayer/write "seek %s 1\n" percent)
338   (media/mplayer/write "get_time_pos\n")
339   )
340
341 (defun media/api/jump-at-time (mode time) (interactive)
342   (setq media/song-current-time nil)
343   (when (< media/mplayer/cumulated-duration media/duration-to-history)
344     (setq media/mplayer/cumulated-duration 0
345           media/mplayer/last-current-time nil))
346   (if (eq mode 'absolute)
347       (media/mplayer/write "seek %s 2\n" time)
348     (media/mplayer/write "seek %s 0\n" time))
349   (media/mplayer/write "get_time_pos\n")
350   )
351
352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;