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