Fix more threading and file errors in Mod_Audio
parent
6de154b22e
commit
e7024c0883
|
@ -7,19 +7,16 @@ require 'streamio-ffmpeg'
|
|||
# @!visibility private
|
||||
class QueueItem
|
||||
attr_accessor :next, :prev
|
||||
attr_reader :time, :ptime, :size, :fname, :descr, :is_stream, :is_file
|
||||
attr_reader :time, :ptime, :size, :fname, :descr, :is_stream
|
||||
|
||||
def initialize uri, stream = false
|
||||
@is_stream = stream
|
||||
if uri.scheme == "file"
|
||||
@fname = uri.path
|
||||
@is_file = true
|
||||
elsif uri.host and check_youtube_dl uri.host
|
||||
@fname = get_yt_vid_from uri
|
||||
@is_file = true
|
||||
else
|
||||
@fname = uri.to_s
|
||||
@is_file = false
|
||||
end
|
||||
|
||||
@descr = uri.to_s
|
||||
|
@ -30,15 +27,11 @@ class QueueItem
|
|||
@size = mov.size
|
||||
@ptime = "%.2i:%.2i" % [@time / 60, @time % 60]
|
||||
else
|
||||
throw ArgumentError, "File is invalid"
|
||||
throw ArgumentError, "File is invalid or is a stream."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def file
|
||||
open @fname
|
||||
end
|
||||
|
||||
def cleanup
|
||||
File.delete @tmpf if @tmpf
|
||||
end
|
||||
|
@ -104,7 +97,7 @@ class Mod_Audio < Vrobot4::Module::Module
|
|||
if argv.empty?
|
||||
list_queue m
|
||||
elsif push_queue m, argv
|
||||
if @queue_mtx.synchronize {@queue.size == 1 or not @running}
|
||||
if @queue_mtx.synchronize {not @running}
|
||||
start_playback m, 0
|
||||
end
|
||||
end
|
||||
|
@ -129,7 +122,7 @@ class Mod_Audio < Vrobot4::Module::Module
|
|||
end
|
||||
|
||||
def c_play m, argv
|
||||
m.serv.bot.voice(m.chan.real).play
|
||||
m.serv.bot.voice(m.chan.real).continue
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -148,10 +141,17 @@ class Mod_Audio < Vrobot4::Module::Module
|
|||
end
|
||||
end
|
||||
|
||||
def rm_queue qi
|
||||
qi.prev.next = qi.next if qi.prev
|
||||
qi.next.prev = qi.prev if qi.next
|
||||
qi.cleanup
|
||||
@queue.delete qi
|
||||
end
|
||||
|
||||
def push_queue m, argv, stream = false
|
||||
uri = URI.parse argv
|
||||
|
||||
m.reply "Queueing <" + uri.to_s + ">..."
|
||||
m.reply "Queueing <#{uri.to_s}>..."
|
||||
m.chan.real.start_typing if m.serv.class.type == "Discord"
|
||||
|
||||
begin
|
||||
|
@ -162,12 +162,12 @@ class Mod_Audio < Vrobot4::Module::Module
|
|||
qi.prev = @queue.last
|
||||
end
|
||||
@queue.push qi
|
||||
end
|
||||
if qi.is_stream
|
||||
m.reply "Stream <" + uri.to_s + "> queued"
|
||||
else
|
||||
m.reply "<" + uri.to_s + "> loaded (%s, %i bytes)" %
|
||||
[qi.ptime, qi.size]
|
||||
if qi.is_stream
|
||||
m.reply "Stream <#{qi.descr}> queued"
|
||||
else
|
||||
m.reply "<#{qi.descr}> loaded (%s, %i bytes)" %
|
||||
[qi.ptime, qi.size]
|
||||
end
|
||||
end
|
||||
return true
|
||||
rescue
|
||||
|
@ -177,13 +177,6 @@ class Mod_Audio < Vrobot4::Module::Module
|
|||
end
|
||||
end
|
||||
|
||||
def rm_queue qi
|
||||
@queue.delete qi
|
||||
qi.prev.next = qi.next if qi.prev
|
||||
qi.next.prev = qi.prev if qi.next
|
||||
qi.cleanup
|
||||
end
|
||||
|
||||
def start_playback m, start
|
||||
qi = nil
|
||||
|
||||
|
@ -192,25 +185,22 @@ class Mod_Audio < Vrobot4::Module::Module
|
|||
qi = @queue[start]
|
||||
end
|
||||
|
||||
Thread.new do
|
||||
loop do
|
||||
begin
|
||||
if qi.is_file then m.serv.voice_play_io m, qi.file
|
||||
else m.serv.voice_play m, qi.fname end
|
||||
rescue
|
||||
p $!
|
||||
m.reply "Error playing queue item."
|
||||
end
|
||||
while qi
|
||||
begin
|
||||
m.serv.voice_play(m, @queue_mtx.synchronize {qi.fname})
|
||||
rescue
|
||||
p $!
|
||||
m.reply "Error playing queue item."
|
||||
end
|
||||
|
||||
@queue_mtx.synchronize {rm_queue qi}
|
||||
|
||||
unless (qi = qi.next)
|
||||
m.reply "End of queue reached."
|
||||
@queue_mtx.synchronize {@running = false}
|
||||
break
|
||||
end
|
||||
@queue_mtx.synchronize do
|
||||
rm_queue qi
|
||||
qi = qi.next
|
||||
end
|
||||
end
|
||||
|
||||
m.reply "End of queue reached."
|
||||
@queue_mtx.synchronize {@running = false}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -58,6 +58,7 @@ class Sv_Discord < Vrobot4::Server::AudioServer
|
|||
sleep 5
|
||||
end
|
||||
end
|
||||
|
||||
# (see Vrobot4::Server::Server#connect)
|
||||
def connect
|
||||
@bot.run
|
||||
|
|
Loading…
Reference in New Issue