Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
[WIP] Should fix #53
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias4217 committed May 15, 2019
1 parent 1561c0d commit 6b7a99d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
21 changes: 14 additions & 7 deletions jukebox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,31 @@ def player_worker(self):
while len(self.playlist) > 0:
url = self.playlist[0]["url"]
self.currently_played = url
self.mpv = MyMPV(None) # we start the track
with app.mpv_lock:
if self.mpv:
del self.mpv
self.mpv = MyMPV(None) # we start the track
start = time.time()
end = start
with self.database_lock:
track = Track.import_from_url(app.config["DATABASE_PATH"], url)
counter = 0
app.logger.info(track.duration)
app.logger.info(end - start)
while counter < 5 and track.duration is not None and end - start < track.duration: # duration of track
# duration of track
while counter < 5 and track.duration is not None and end - start < min(track.duration, 10):
# note for the future : what if track is passed with a timestamp ?
start = time.time()
self.mpv.play(self.currently_played)
with app.mpv_lock:
self.mpv.play(self.currently_played)
# the next instruction should be the only one without a lock
# but it causes a segfault
# I fear fixing it may be dirty
# we could switch to mpv playlists though
self.mpv.wait_for_playback() # it's stuck here while it's playing
end = time.time()
counter += 1
with self.mpv_lock:
self.mpv.terminate() # the track is finished
self.mpv = "unavailable" # or del app.mpv
del self.mpv # the track is finished
# self.mpv = "unavailable" # or del app.mpv
with self.playlist_lock:
if len(self.playlist) > 0 and url == self.currently_played:
del self.playlist[0]
Expand Down
2 changes: 1 addition & 1 deletion jukebox/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def sync():
volume = re.findall("Playback \d+ \[(\d+)%\]", amixer_out)[0]
# segfault was here
with app.mpv_lock:
if app.mpv is not None and app.mpv != "unavailable" and hasattr(app.mpv, 'time_pos') \
if hasattr(app, 'mpv') and app.mpv is not None and hasattr(app.mpv, 'time_pos') \
and app.mpv.time_pos is not None:
time_pos = app.mpv.time_pos
else:
Expand Down
34 changes: 20 additions & 14 deletions jukebox/src/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def add():
from track_info
where url = ?;
""",
(track["url"],))
(track["url"],))
r = c.fetchall()
if r == []:
if not r:
c.execute("""INSERT INTO track_info
(url, track, artist, album, duration, albumart_url,
source) VALUES
Expand Down Expand Up @@ -77,7 +77,11 @@ def remove():
if track_p["url"] == track["url"]:
if app.playlist.index(track_p) == 0:
#app.logger.info("Removing currently playing track")
app.mpv.terminate()
with app.mpv_lock:
# app.mpv.stop()
app.mpv.event_callback('shutdown')
# app.mpv.terminate()
# app.mpv = "unavailable"
app.playlist.remove(track_p)
#app.playlist_skip.set()
return "ok"
Expand All @@ -104,20 +108,22 @@ def suggest():
#if n > 20:
# n = 20
result = []
conn = sqlite3.connect(app.config["DATABASE_PATH"])
c = conn.cursor()
with app.database_lock:
conn = sqlite3.connect(app.config["DATABASE_PATH"])
c = conn.cursor()
nbr = 0
while nbr < n: # we use a while to be able not to add a song
# if it is blacklisted
c.execute("SELECT * FROM log ORDER BY RANDOM() LIMIT 1;")
r_log = c.fetchall()
track_id = r_log[0][1]
user_id = r_log[0][2]
c.execute("SELECT user FROM users WHERE id = ?;", (user_id,))
r = c.fetchall()
user = r[0][0]
c.execute("SELECT * FROM track_info WHERE id = ?;", (track_id,))
r = c.fetchall()
with app.database_lock:
c.execute("SELECT * FROM log ORDER BY RANDOM() LIMIT 1;")
r_log = c.fetchall()
track_id = r_log[0][1]
user_id = r_log[0][2]
c.execute("SELECT user FROM users WHERE id = ?;", (user_id,))
r = c.fetchall()
user = r[0][0]
c.execute("SELECT * FROM track_info WHERE id = ?;", (track_id,))
r = c.fetchall()
#track_tuple = r[0]
for track_tuple in r:
#app.logger.info("nbr : " + str(nbr))
Expand Down

0 comments on commit 6b7a99d

Please sign in to comment.