|
6 | 6 | from os.path import expanduser
|
7 | 7 | from sys import platform, version_info
|
8 | 8 | from sys import exit
|
| 9 | +from time import sleep |
| 10 | +import json |
| 11 | +import socket |
9 | 12 |
|
10 | 13 | logger = logging.getLogger(__name__)
|
11 | 14 |
|
@@ -216,18 +219,20 @@ def updateStatus(self, *args):
|
216 | 219 | logger.debug("User input: {}".format(subsystemOut))
|
217 | 220 | self.oldUserInput['Input'] = subsystemOut
|
218 | 221 | if self.volume_string in subsystemOut:
|
219 |
| - #logger.error("***** volume") |
220 |
| - if self.oldUserInput['Volume'] != subsystemOut: |
221 |
| - self.oldUserInput['Volume'] = subsystemOut |
222 |
| - self.volume = ''.join(c for c in subsystemOut if c.isdigit()) |
223 |
| - |
224 |
| - # IMPORTANT: do this here, so that cvlc actual_volume |
225 |
| - # gets updated in _format_volume_string |
226 |
| - string_to_show = self._format_volume_string(subsystemOut) + self._format_title_string(self.oldUserInput['Title']) |
227 |
| - |
228 |
| - if self.show_volume and self.oldUserInput['Title']: |
229 |
| - self.outputStream.write(string_to_show, args[0]) |
230 |
| - self.threadUpdateTitle(args[0]) |
| 222 | + # disable volume for mpv |
| 223 | + if self.PLAYER_CMD != "mpv": |
| 224 | + logger.error("***** volume") |
| 225 | + if self.oldUserInput['Volume'] != subsystemOut: |
| 226 | + self.oldUserInput['Volume'] = subsystemOut |
| 227 | + self.volume = ''.join(c for c in subsystemOut if c.isdigit()) |
| 228 | + |
| 229 | + # IMPORTANT: do this here, so that cvlc actual_volume |
| 230 | + # gets updated in _format_volume_string |
| 231 | + string_to_show = self._format_volume_string(subsystemOut) + self._format_title_string(self.oldUserInput['Title']) |
| 232 | + |
| 233 | + if self.show_volume and self.oldUserInput['Title']: |
| 234 | + self.outputStream.write(string_to_show, args[0]) |
| 235 | + self.threadUpdateTitle(args[0]) |
231 | 236 | elif self._is_in_playback_token(subsystemOut):
|
232 | 237 | if self.connection_timeout_thread is not None:
|
233 | 238 | self.connection_timeout_thread.cancel()
|
@@ -576,19 +581,99 @@ def _stop(self):
|
576 | 581 | def _volume_up(self):
|
577 | 582 | """ increase mpv's volume """
|
578 | 583 | os.system("echo 'cycle volume' | socat - " + self.mpvsocket + " 2>/dev/null");
|
| 584 | + self._diaplay_mpv_volume_value() |
579 | 585 |
|
580 | 586 | def _volume_down(self):
|
581 | 587 | """ decrease mpv's volume """
|
582 | 588 | os.system("echo 'cycle volume down' | socat - " + self.mpvsocket + " 2>/dev/null");
|
| 589 | + self._diaplay_mpv_volume_value() |
583 | 590 |
|
584 | 591 | def _format_title_string(self, title_string):
|
585 | 592 | """ format mpv's title """
|
586 | 593 | return self._title_string_format_text_tag(title_string.replace(self.icy_tokkens[0], self.icy_title_prefix))
|
587 | 594 |
|
588 | 595 | def _format_volume_string(self, volume_string):
|
589 |
| - """ format mplayer's volume """ |
| 596 | + """ format mpv's volume """ |
590 | 597 | return '[' + volume_string[volume_string.find(self.volume_string):].replace('ume', '')+'] '
|
591 | 598 |
|
| 599 | + def _connect_to_socket(self, server_address): |
| 600 | + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 601 | + try: |
| 602 | + sock.connect(server_address) |
| 603 | + return sock |
| 604 | + except socket.error as err: |
| 605 | + sock.close() |
| 606 | + return None |
| 607 | + |
| 608 | + def _diaplay_mpv_volume_value(self): |
| 609 | + """ Display volume for MPV |
| 610 | +
|
| 611 | + Currently working with python 2 and 3 |
| 612 | + Eventually will be used for python 2 only |
| 613 | +
|
| 614 | + Python 2 cannot correctly read icy-title from |
| 615 | + the socket (unidoce issue), so it has to read |
| 616 | + it from stdout. |
| 617 | + """ |
| 618 | + |
| 619 | + #if version_info > (3, 0): |
| 620 | + # return |
| 621 | + vol = 0 |
| 622 | + while True: |
| 623 | + sock = self._connect_to_socket(self.mpvsocket) |
| 624 | + if sock: |
| 625 | + break |
| 626 | + sleep(.25) |
| 627 | + |
| 628 | + # Send data |
| 629 | + message = b'{ "command": ["get_property", "volume"] }\n' |
| 630 | + sock.sendall(message) |
| 631 | + |
| 632 | + # wait for response |
| 633 | + got_it = True |
| 634 | + while got_it: |
| 635 | + try: |
| 636 | + if version_info < (3, 0): |
| 637 | + data = sock.recv(4096) |
| 638 | + else: |
| 639 | + data = sock.recvmsg(4096) |
| 640 | + if isinstance(data, tuple): |
| 641 | + a_data = data[0] |
| 642 | + else: |
| 643 | + a_data = data |
| 644 | + #logger.error('DE Received: "{!r}"'.format(a_data)) |
| 645 | + |
| 646 | + if a_data == b'': |
| 647 | + break |
| 648 | + |
| 649 | + if data: |
| 650 | + |
| 651 | + all_data = a_data.split(b'\n') |
| 652 | + for n in all_data: |
| 653 | + try: |
| 654 | + d = json.loads(n) |
| 655 | + if d['error'] == 'success': |
| 656 | + try: |
| 657 | + vol = int(d['data']) |
| 658 | + got_it = False |
| 659 | + break |
| 660 | + except: |
| 661 | + pass |
| 662 | + except: |
| 663 | + pass |
| 664 | + finally: |
| 665 | + pass |
| 666 | + sock.close() |
| 667 | + if self.oldUserInput['Title']: |
| 668 | + info_string = self._format_title_string(self.oldUserInput['Title']) |
| 669 | + else: |
| 670 | + info_string = self._format_title_string(self.oldUserInput['Input']) |
| 671 | + string_to_show = self._format_volume_string('Volume: ' + str(vol) + '%') + info_string |
| 672 | + self.outputStream.write(string_to_show) |
| 673 | + self.threadUpdateTitle(self.status_update_lock) |
| 674 | + self.volume = str(vol) |
| 675 | + |
| 676 | + |
592 | 677 | class MpPlayer(Player):
|
593 | 678 | """Implementation of Player object for MPlayer"""
|
594 | 679 |
|
|
0 commit comments