Added a comment.
[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                     (if (and media/current-song-in-stream media/current-information)
113                         (media/show-current-information))
114                     )
115
116                    ;; ----------------------------------------
117
118                    ("ANS_LENGTH"
119
120                     (setq media/song-duration
121                           (string-to-number (substring param 1))))
122
123                    ;; ----------------------------------------
124
125                    ("ANS_TIME_POSITION"
126
127                     (setq media/song-current-time
128                           (string-to-number (substring param 1)))
129
130                     (when (and media/duration-to-history
131                                (< media/mplayer/cumulated-duration media/duration-to-history))
132
133                       (when media/mplayer/last-current-time
134                         (setq media/mplayer/cumulated-duration
135                               (+ media/mplayer/cumulated-duration
136                                  (- media/song-current-time media/mplayer/last-current-time))))
137
138                       (when (>= media/mplayer/cumulated-duration media/duration-to-history)
139                         (media/put-in-history)
140                         )
141
142                       (setq media/mplayer/last-current-time media/song-current-time)
143                       )
144                     )
145
146                    ;; ----------------------------------------
147
148                    ("AUDIO:"
149
150                     ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
151                     (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
152                                         param)
153                       (setq media/current-information
154                             (list media/mplayer/url
155                                   (string-to-number (match-string 1 param))
156                                   (string-to-number (match-string 2 param))
157                                   (string-to-number (match-string 3 param))))
158                       )
159                     (run-hooks 'media/play-hook)
160                     )
161
162                    ;; ----------------------------------------
163
164                    ("Starting"
165                     (media/mplayer/write "get_time_length\n")
166                     (when media/mplayer/capture-dir
167                       (media/mplayer/write "capturing\n")
168                       ;; (message "Capturing stream in %s" media/mplayer/capture-dir)
169                       )
170                     )
171
172                    ;; ----------------------------------------
173
174                    ("Cache fill:"
175                     (when (string-match "(\\([0-9]+\\) bytes" param)
176                       (message "Caching stream (%dkb)"
177                                (/ (string-to-number (match-string 1 param)) 1024))))
178
179                    ;; ----------------------------------------
180
181                    ("Exiting..."
182                     (setq media/mplayer/exit-type
183                           (cdr (assoc param '(("(End of file)" . file-finished)
184                                               ("(Quit)" . quit))))
185                           media/current-information nil
186                           media/song-duration nil
187                           media/song-current-time nil)
188
189                     (when media/mplayer/process (kill-process media/mplayer/process))
190
191                     (force-mode-line-update)
192                     )
193
194                    ;; ----------------------------------------
195
196                    )
197                  )
198           )
199          )
200    )
201   )
202
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204
205 (defun media/mplayer/filter (process str)
206   (setq media/mplayer/buffer (concat media/mplayer/buffer str))
207   (let ((start 0))
208     (while (and (< start (length media/mplayer/buffer))
209                 (string-match "\\(.*\\)[\n\r]+" media/mplayer/buffer start))
210       (setq start (1+ (match-end 1)))
211       (let ((line (match-string 1 media/mplayer/buffer)))
212         (when (string-match media/mplayer/protocol-regexp line)
213           (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line))))
214       )
215     (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
216   )
217
218 (defun media/mplayer/sentinel (process str) ()
219   ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
220   (unless (eq (process-status media/mplayer/process) 'run)
221     (setq media/current-information nil
222           media/mplayer/process nil
223           media/song-current-time nil
224           media/song-duration nil)
225
226     (media/mplayer/stop-timing-requests)
227
228     (if (eq media/mplayer/exit-type 'file-finished)
229         (run-hooks 'media/finished-hook)
230       (run-hooks 'media/error-hook))
231
232     (force-mode-line-update))
233   )
234
235 (defun media/mplayer/write (&rest l)
236   ;;   (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
237   (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
238     (error "No mplayer process"))
239   )
240
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244
245 (defun media/api/init () "Called once when the media application starts"
246   (setq media/player-id "MPlayer"
247         media/mplayer/url nil
248         media/mplayer/buffer "" ;; Used as read buffer
249         media/mplayer/process nil
250         media/mplayer/exit-type nil
251         media/mplayer/paused nil
252         media/song-duration nil
253         media/song-current-time nil
254         media/mplayer/cumulated-duration 0
255         media/mplayer/last-current-time nil
256         )
257 )
258
259 (defun media/api/cleanup () "Called when killing the application's buffer"
260   (when media/mplayer/process
261     (delete-process media/mplayer/process)
262     (media/mplayer/stop-timing-requests)
263     (setq media/mplayer/process nil)))
264
265 (defun media/api/play (url) (interactive)
266   (setq media/mplayer/url url)
267
268   (when media/mplayer/process (kill-process media/mplayer/process))
269
270   ;; (if media/mplayer/process
271   ;; (media/mplayer/write (concat "loadfile "
272   ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
273   ;; "\n"))
274
275   (setq media/mplayer/process
276         (apply
277          'start-process
278          (append
279           '("mplayer" nil "mplayer" "-slave" "-quiet")
280           media/mplayer/args
281           (when (string-match  "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
282             (if media/mplayer/capture-dir
283                 (list "-dumpfile"
284                       (concat media/mplayer/capture-dir
285                               "/"
286                               (replace-regexp-in-string "[^a-zA-Z0-9\.]" "_" media/mplayer/url)
287                               (format-time-string "-%Y-%m-%d-%H:%M:%S"))
288                       "-capture"
289                       "-playlist"
290                       )
291               (list "-playlist"))
292             )
293           (list (replace-regexp-in-string "^file://" "" media/mplayer/url)))
294          )
295         media/mplayer/exit-type 'unknown
296         media/mplayer/paused nil
297         media/song-duration nil
298         media/song-current-time nil
299         media/mplayer/cumulated-duration 0
300         media/mplayer/last-current-time nil
301         )
302
303   (set-process-filter media/mplayer/process 'media/mplayer/filter)
304   (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
305   (process-kill-without-query media/mplayer/process)
306   (media/mplayer/start-timing-requests)
307   (media/mplayer/write "get_time_pos\n")
308   )
309
310 (defun media/api/stop () (interactive)
311   (media/mplayer/write "quit\n")
312   )
313
314 (defun media/api/pause () (interactive)
315   (media/mplayer/write "pause\n")
316   (setq media/mplayer/paused (not media/mplayer/paused))
317   )
318
319 (defun media/api/set-volume (mode value) (interactive)
320   (if (eq mode 'absolute)
321       (media/mplayer/write "volume %s 1\n" value)
322     (if (>= value 0)
323         (media/mplayer/write "volume +%s\n" value)
324       (media/mplayer/write "volume %s\n" value))))
325
326 (defun media/api/jump-at-percent (percent) (interactive)
327   (setq media/song-current-time nil)
328   (when (< media/mplayer/cumulated-duration media/duration-to-history)
329     (setq media/mplayer/cumulated-duration 0
330           media/mplayer/last-current-time nil))
331   (media/mplayer/write "seek %s 1\n" percent)
332   (media/mplayer/write "get_time_pos\n")
333   )
334
335 (defun media/api/jump-at-time (mode time) (interactive)
336   (setq media/song-current-time nil)
337   (when (< media/mplayer/cumulated-duration media/duration-to-history)
338     (setq media/mplayer/cumulated-duration 0
339           media/mplayer/last-current-time nil))
340   (if (eq mode 'absolute)
341       (media/mplayer/write "seek %s 2\n" time)
342     (media/mplayer/write "seek %s 0\n" time))
343   (media/mplayer/write "get_time_pos\n")
344   )
345
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;