Skip to content

Commit 46cd9a5

Browse files
committed
adding PlayerCache class
1 parent 23c6079 commit 46cd9a5

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

pyradio/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,8 @@ class PyRadioConfig(PyRadioStations):
13271327

13281328
start_colors_at = 0
13291329

1330+
buffering_data = []
1331+
13301332
def __init__(self, user_config_dir=None):
13311333
self.backup_player_params = None
13321334
self.player = ''

pyradio/player.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,8 @@ def _buildStartOpts(self, streamUrl, playList=False):
23192319
else:
23202320
opts = [self.PLAYER_CMD, '--no-video', '--quiet', self._url_to_use(streamUrl), '--input-unix-socket=' + self.mpvsocket]
23212321

2322+
if self._cnf.buffering_data:
2323+
opts.extend(self._cnf.buffering_data)
23222324

23232325
''' this will set the profile too '''
23242326
params = self.params[self.params[0]]
@@ -2765,6 +2767,8 @@ def save_volume(self):
27652767
def _buildStartOpts(self, streamUrl, playList=False):
27662768
''' Builds the options to pass to mplayer subprocess.'''
27672769
opts = [self.PLAYER_CMD, '-vo', 'null', '-msglevel', 'all=6']
2770+
if self._cnf.buffering_data:
2771+
opts.extend(self._cnf.buffering_data)
27682772
# opts = [self.PLAYER_CMD, '-vo', 'null']
27692773
monitor_opts = None
27702774

@@ -3137,6 +3141,9 @@ def _buildStartOpts(self, streamUrl, playList=False):
31373141
opts = [self.PLAYER_CMD, '--no-one-instance', '--no-volume-save',
31383142
'-Irc', '-vv', self._url_to_use(streamUrl)]
31393143

3144+
if self._cnf.buffering_data:
3145+
opts.extend(self._cnf.buffering_data)
3146+
31403147
''' this will set the profile too '''
31413148
if self.params[0] > 1:
31423149
params = self.params[self.params[0]]
@@ -3478,6 +3485,118 @@ def _win_get_playing_state(self, msg):
34783485
break
34793486
#self.print_response(rep)
34803487

3488+
3489+
class PlayerCache(object):
3490+
3491+
_dirty = False
3492+
3493+
_data = {
3494+
'mpv': [
3495+
'--cache-secs=30',
3496+
'--cache=yes',
3497+
'--cache-on-disk=yes',
3498+
'--demuxer-cache-wait=yes',
3499+
'--demuxer-readahead-secs=29',
3500+
],
3501+
'mplayer': [
3502+
'-cache=1024',
3503+
'-cache-min=80'
3504+
],
3505+
'vlc': [
3506+
'--network-caching',
3507+
'10000'
3508+
]
3509+
}
3510+
3511+
def __init__(self, player_name, data_dir, recording):
3512+
self._player_name = player_name
3513+
self._data_file = os.path.join(data_dir, 'buffers')
3514+
self_recording = recording
3515+
self._read()
3516+
3517+
def __del__(self):
3518+
self._save()
3519+
3520+
@property
3521+
def cache(self):
3522+
if self._player_name == 'mpv':
3523+
self._on_disk()
3524+
return self._data[self._player_name]
3525+
3526+
@property
3527+
def delay(self):
3528+
if self._player_name == 'mpv':
3529+
return int(self._data['mpv'][0].replace('--cache-secs=', ''))
3530+
elif self._player_name == 'mplayer':
3531+
return int(self._data['mplayer'][0].replace('-cache=', ''))
3532+
else:
3533+
return int(self._data['vlc'][1])
3534+
3535+
@delay.setter
3536+
def delay(self, a_delay):
3537+
try:
3538+
x = int(a_delay)
3539+
except ValueError:
3540+
return
3541+
if self._player_name == 'vlc':
3542+
x *= 1000
3543+
3544+
if self._player_name == 'vlc':
3545+
self._data['vlc'][1] = str(x)
3546+
else:
3547+
str_x = str(x)
3548+
if self._player_name == 'mpv':
3549+
self._data['mpv'][0] = '--cache-secs=' + str_x
3550+
x -= 1
3551+
self._data['mpv'][-1] = '--demuxer-readahead-secs=' + str(x)
3552+
elif self._player_name == 'mplayer':
3553+
self._data['mplayer'][0] = '-cache=' + str_x
3554+
self._dirty = True
3555+
3556+
def _read(self):
3557+
if os.path.exists(self._data_file):
3558+
try:
3559+
with open(self._data_file, 'r', encoding='utf-8') as f:
3560+
line = f.read()
3561+
sp = line.split(',')
3562+
orig_player_name = self._player_name
3563+
for i, a_player in enumerate(('mpv', 'mplayer', 'vlc')):
3564+
self._player_name = a_player
3565+
self.delay = sp[i]
3566+
self._player_name = orig_player_name
3567+
except:
3568+
pass
3569+
3570+
def _save(self):
3571+
if self._dirty:
3572+
mpl = int(self._data['vlc'][1])
3573+
i_mpl = int(int(mpl) / 1000)
3574+
msg = self._data['mpv'][0].replace('--cache-secs=', '') + ',' + \
3575+
self._data['mplayer'][0].replace('-cache=', '') + ',' + \
3576+
str(i_mpl)
3577+
try:
3578+
with open(self._data_file, 'w', encoding='utf-8') as f:
3579+
f.write(msg)
3580+
except:
3581+
pass
3582+
self._dirty = False
3583+
3584+
def _on_disk(self):
3585+
if self._recording():
3586+
self._data['mpv'][2] = '--cache-on-disk=no'
3587+
return
3588+
try:
3589+
vitr = psutil.virtual_memory()
3590+
except:
3591+
self._data['mpv'][2] = '--cache-on-disk=no'
3592+
return
3593+
3594+
if virt.available > 1073741824:
3595+
self._data['mpv'][2] = '--cache-on-disk=no'
3596+
else:
3597+
self._data['mpv'][2] = '--cache-on-disk=yes'
3598+
3599+
34813600
def probePlayer(config, requested_player=''):
34823601
''' Probes the multimedia players which are
34833602
available on the host system. '''

0 commit comments

Comments
 (0)