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. ;;
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. ;;
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/>. ;;
17 ;; Written by and Copyright (C) Francois Fleuret ;;
18 ;; Contact <francois@fleuret.org> for comments & bug reports ;;
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; Is it me, or the slave mode of mplayer is ugly to parse? Did I miss
24 (defcustom media/mplayer/args nil
25 "List of arguments for mplayer."
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."
35 (defcustom media/mplayer/capture-dir nil
36 "States where to save the dumped streams."
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
46 (defvar media/mplayer/timer nil
47 "A timer to request the timing position.")
49 (defun media/mplayer/timing-request ()
50 (if media/mplayer/process
51 (unless media/mplayer/paused
52 (media/mplayer/write "get_time_pos\n")
54 (media/mplayer/stop-timing-requests)
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
62 media/mplayer/timing-request-period
63 'media/mplayer/timing-request))
67 (defun media/mplayer/stop-timing-requests ()
68 (when media/mplayer/timer
69 (cancel-timer media/mplayer/timer)
70 (setq media/mplayer/timer nil)
73 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75 (setq media/mplayer/protocol-regexp
76 "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$")
78 (defun media/mplayer/filter-subfunctions (cmd param)
79 ;; (unless (string= cmd "A:")
80 ;; (message "cmd=%s param=%s" cmd param)
89 ;; (message "ICY Info \"%s\"" param)
91 (if (string-match "StreamTitle='\\([^;]*\\)';" param)
93 (setq media/current-song-in-stream
94 (let ((s (match-string 1 param)))
95 (concat (if (string= s "")
97 ;; (encode-coding-string s 'latin-1)
101 (format-time-string "%a %b %d %H:%M:%S")
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)
112 (when media/mplayer/capture-dir
115 (concat media/current-song-in-stream "\n"))
116 (write-region nil nil (concat media/mplayer/capture-dir "/log") t)))
118 (if (and media/current-song-in-stream media/current-information)
119 (media/show-current-information))
122 ;; ----------------------------------------
126 (setq media/song-duration
127 (string-to-number (substring param 1))))
129 ;; ----------------------------------------
133 (setq media/song-current-time
134 (string-to-number (substring param 1)))
136 (when (and media/duration-to-history
137 (< media/mplayer/cumulated-duration media/duration-to-history))
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))))
144 (when (>= media/mplayer/cumulated-duration media/duration-to-history)
145 (media/put-in-history)
148 (setq media/mplayer/last-current-time media/song-current-time)
152 ;; ----------------------------------------
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"
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))))
165 (run-hooks 'media/play-hook)
168 ;; ----------------------------------------
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)
178 ;; ----------------------------------------
181 (when (string-match "(\\([0-9]+\\) bytes" param)
182 (message "Caching stream (%dkb)"
183 (/ (string-to-number (match-string 1 param)) 1024))))
185 ;; ----------------------------------------
188 (setq media/mplayer/exit-type
189 (cdr (assoc param '(("(End of file)" . file-finished)
191 media/current-information nil
192 media/song-duration nil
193 media/song-current-time nil)
195 (when media/mplayer/process (kill-process media/mplayer/process))
197 (force-mode-line-update)
200 ;; ----------------------------------------
209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 (defun media/mplayer/filter (process str)
212 (setq media/mplayer/buffer (concat media/mplayer/buffer str))
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))))
221 (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
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)
232 (media/mplayer/stop-timing-requests)
234 (if (eq media/mplayer/exit-type 'file-finished)
235 (run-hooks 'media/finished-hook)
236 (run-hooks 'media/error-hook))
238 (force-mode-line-update))
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"))
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
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)))
271 (defun media/api/play (url) (interactive)
272 (setq media/mplayer/url url)
274 (when media/mplayer/process (kill-process media/mplayer/process))
276 ;; (if media/mplayer/process
277 ;; (media/mplayer/write (concat "loadfile "
278 ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
281 (setq media/mplayer/process
285 '("mplayer" nil "mplayer" "-slave" "-quiet")
287 (when (string-match "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
288 (if media/mplayer/capture-dir
290 (concat media/mplayer/capture-dir
292 (replace-regexp-in-string "[^a-zA-Z0-9\.]" "_" media/mplayer/url)
293 (format-time-string "-%Y-%m-%d-%H:%M:%S"))
299 (list (replace-regexp-in-string "^file://" "" media/mplayer/url)))
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
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")
316 (defun media/api/stop () (interactive)
317 (media/mplayer/write "quit\n")
320 (defun media/api/pause () (interactive)
321 (media/mplayer/write "pause\n")
322 (setq media/mplayer/paused (not media/mplayer/paused))
325 (defun media/api/set-volume (mode value) (interactive)
326 (if (eq mode 'absolute)
327 (media/mplayer/write "volume %s 1\n" value)
329 (media/mplayer/write "volume +%s\n" value)
330 (media/mplayer/write "volume %s\n" value))))
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")
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")
352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;