From 6d624e96a6a1fa533b89898c06398c884a0f3992 Mon Sep 17 00:00:00 2001 From: Sheldon Knuth <4120133-sheldon_knuth@users.noreply.gitlab.com> Date: Wed, 3 Jul 2019 17:23:24 +0000 Subject: [PATCH] fix the missing set_playing(true) in AudioPlayer::load() to prevent infinity thread loop & adopt std::sync::{Mutex, Condvar} for accurate thread blocking. --- Cargo.lock | 38 +++++++++++++-------------- common/src/assets/mod.rs | 1 - voxygen/src/audio/base.rs | 55 +++++++++++++++++++++------------------ 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8103edea98..d7f998a273 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,7 +15,7 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -123,7 +123,7 @@ name = "backtrace" version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "backtrace-sys 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", + "backtrace-sys 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -131,7 +131,7 @@ dependencies = [ [[package]] name = "backtrace-sys" -version = "0.1.29" +version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", @@ -618,7 +618,7 @@ dependencies = [ [[package]] name = "deflate" -version = "0.7.19" +version = "0.7.20" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -710,7 +710,7 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1834,7 +1834,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "deflate 0.7.19 (registry+https://github.com/rust-lang/crates.io-index)", + "deflate 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)", "inflate 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1845,7 +1845,7 @@ version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "deflate 0.7.19 (registry+https://github.com/rust-lang/crates.io-index)", + "deflate 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)", "inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1864,7 +1864,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2105,7 +2105,7 @@ name = "regex" version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aho-corasick 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "aho-corasick 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2191,7 +2191,7 @@ dependencies = [ [[package]] name = "ryu" -version = "0.2.8" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -2274,11 +2274,11 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.39" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", - "ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2620,7 +2620,7 @@ dependencies = [ "rayon 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", "specs 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)", "sphynx 0.1.0 (git+https://gitlab.com/veloren/sphynx.git)", "threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2895,7 +2895,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c" "checksum aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5" -"checksum aho-corasick 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e6f484ae0c99fec2e858eb6134949117399f222608d84cadb3f58c1f97c2364c" +"checksum aho-corasick 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "36b7aa1ccb7d7ea3f437cf025a2ab1c47cc6c1bc9fc84918ff449def12f5e282" "checksum alsa-sys 0.1.1 (git+https://github.com/desttinghim/cpal?rev=e7c086d0afc368a888ad133c3b1d928b16986130)" = "" "checksum andrew 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9b7f09f89872c2b6b29e319377b1fbe91c6f5947df19a25596e121cf19a7b35e" "checksum android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "000444226fcff248f2bc4c7625be32c63caccfecc2723a2b9f78a7487a49c407" @@ -2909,7 +2909,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" "checksum backtrace 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "18b50f5258d1a9ad8396d2d345827875de4261b158124d4c819d9b351454fae5" -"checksum backtrace-sys 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)" = "12cb9f1eef1d1fc869ad5a26c9fa48516339a15e54a227a25460fc304815fdb3" +"checksum backtrace-sys 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "5b3a000b9c543553af61bc01cbfc403b04b5caa9e421033866f2e98061eb3e61" "checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" "checksum bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9f04a5e50dc80b3d5d35320889053637d15011aed5e66b66b37ae798c65da6f7" "checksum bindgen 0.32.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8b242e11a8f446f5fc7b76b37e81d737cabca562a927bd33766dac55b5f1177f" @@ -2963,7 +2963,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "677d453a17e8bd2b913fa38e8b9cf04bcdbb5be790aa294f2389661d72036015" "checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" "checksum daggy 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9293a0da7d1bc1f30090ece4d9f9de79a07be7302ddb00e5eb1fefb6ee6409e2" -"checksum deflate 0.7.19 (registry+https://github.com/rust-lang/crates.io-index)" = "8a6abb26e16e8d419b5c78662aa9f82857c2386a073da266840e474d5055ec86" +"checksum deflate 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = "707b6a7b384888a70c8d2e8650b3e60170dfc6a67bb4aa67b6dfca57af4bedb4" "checksum derivative 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6073e9676dbebdddeabaeb63e3b7cefd23c86f5c41d381ee1237cc77b1079898" "checksum directories 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "72d337a64190607d4fcca2cb78982c5dd57f4916e19696b48a575fa746b6cb0f" "checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" @@ -2974,7 +2974,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b" "checksum enum_primitive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "be4551092f4d519593039259a9ed8daedf0da12e5109c5280338073eaeb81180" "checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" -"checksum env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b61fa891024a945da30a9581546e8cfaf5602c7b3f4c137a2805cf388f92075a" +"checksum env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" "checksum euc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0151594c4feeeb99ff35ac1b467383a46fcb2705275615bed0a47f25ffe2ccf8" "checksum euclid 0.19.9 (registry+https://github.com/rust-lang/crates.io-index)" = "596b99621b9477e7a5f94d2d8dd13a9c5c302ac358b822c67a42b6f1054450e1" "checksum euclid_macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdcb84c18ea5037a1c5a23039b4ff29403abce2e0d6b1daa11cf0bde2b30be15" @@ -3135,7 +3135,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum rusttype 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)" = "654103d61a05074b268a107cf6581ce120f0fc0115f2610ed9dfea363bb81139" -"checksum ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b96a9549dc8d48f2c283938303c4b5a77aa29bfbc5b54b084fb1630408899a8f" +"checksum ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997" "checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267" "checksum scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8b87497427f9fbe539ee6b9626f5a5e899331fdf1c1d62f14c637a462969db30" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" @@ -3146,7 +3146,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)" = "076a696fdea89c19d3baed462576b8f6d663064414b5c793642da8dfeb99475b" "checksum serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)" = "ef45eb79d6463b22f5f9e16d283798b7c0175ba6050bc25c1a946c122727fe7b" -"checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d" +"checksum serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "051c49229f282f7c6f3813f8286cc1e3323e8051823fce42c7ea80fe13521704" "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" "checksum shred 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6ea122e6133568144fcfb5888737d4ac776ebc959f989dd65b907136ac22bfed" "checksum shred-derive 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fcf34e5e5302d3024aba7afc291f6d1ca7573ed035d3c0796976ba3f10691a1" diff --git a/common/src/assets/mod.rs b/common/src/assets/mod.rs index 1a0298f31d..d4c2d83dda 100644 --- a/common/src/assets/mod.rs +++ b/common/src/assets/mod.rs @@ -179,7 +179,6 @@ pub fn load_from_path(name: &str) -> Result, Error> { /// Read directory from `veloren/assets/*` pub fn read_from_assets(dir_name: &str) -> Result { let mut entry = assets_folder(); - entry.push("../assets/"); entry.push(dir_name); match Path::new(&entry).exists() { true => Ok(read_dir(entry).expect("`read_dir` failed.")), diff --git a/voxygen/src/audio/base.rs b/voxygen/src/audio/base.rs index a667a1c598..16f708a300 100644 --- a/voxygen/src/audio/base.rs +++ b/voxygen/src/audio/base.rs @@ -7,7 +7,7 @@ use crossbeam::{ sync::{ShardedLock, WaitGroup}, }; use rodio::{Decoder, Device, Sink, SpatialSink}; -use std::sync::Arc; +use std::sync::{Arc, Condvar, Mutex}; use std::thread; trait AudioConfig { @@ -56,7 +56,7 @@ pub enum Action { #[derive(Clone)] struct EventLoop { - condition: Arc<(ShardedLock, WaitGroup)>, + condvar: Arc<(Mutex, Condvar)>, queue: Arc>, playing: Arc>, } @@ -64,7 +64,7 @@ struct EventLoop { impl EventLoop { fn new() -> Self { Self { - condition: Arc::new((ShardedLock::new(false), WaitGroup::new())), + condvar: Arc::new((Mutex::new(false), Condvar::new())), queue: Arc::new(SegQueue::new()), playing: Arc::new(ShardedLock::new(false)), } @@ -89,14 +89,7 @@ impl AudioPlayer { pub(crate) fn load(&self, path: &str) { self.emit(Action::Load(path.to_string())); - } - - pub(crate) fn set_volume(&self, value: f32) { - self.emit(Action::AdjustVolume(value)); - } - - pub(crate) fn set_device(&self, device: &str) { - self.emit(Action::ChangeDevice(device.to_string())); + self.set_playing(true); } pub(crate) fn pause(&mut self) { @@ -111,10 +104,6 @@ impl AudioPlayer { self.set_playing(true); } - pub(crate) fn is_paused(&self) -> bool { - self.paused.load() - } - pub(crate) fn stop(&mut self) { self.paused.store(false); self.send(AudioPlayerMsg::AudioStop); @@ -122,6 +111,18 @@ impl AudioPlayer { self.set_playing(false); } + pub(crate) fn is_paused(&self) -> bool { + self.paused.load() + } + + pub(crate) fn set_volume(&self, value: f32) { + self.emit(Action::AdjustVolume(value)); + } + + pub(crate) fn set_device(&self, device: &str) { + self.emit(Action::ChangeDevice(device.to_string())); + } + fn emit(&self, action: Action) { self.event_loop.queue.push(action); } @@ -132,12 +133,11 @@ impl AudioPlayer { fn set_playing(&self, playing: bool) { *self.event_loop.playing.write().unwrap() = playing; - let &(ref lock, ref wg) = &*self.event_loop.condition; - let mut started = lock.write().unwrap(); + let &(ref lock, ref condvar) = &*self.event_loop.condvar; + let mut started = lock.lock().unwrap(); *started = playing; - let wg = wg.clone(); if playing { - wg.wait(); + condvar.notify_one(); } } } @@ -148,24 +148,29 @@ impl MonoMode for AudioPlayer { let event_loop = EventLoop::new(); { - let mut tx = tx.clone(); + //let mut tx = tx.clone(); let event_loop = event_loop.clone(); - let condition = event_loop.condition.clone(); + let condition = event_loop.condvar.clone(); thread::spawn(move || { let block = || { - let &(ref lock, ref wg) = &*condition; - let mut started = lock.write().unwrap(); + let (ref lock, ref condvar) = *condition; + let mut started = lock.lock().unwrap(); *started = false; - drop(wg); + while !*started { + started = condvar.wait(started).unwrap(); + } }; let mut playback = MonoEmitter::new(&Settings::load().audio); + + // Start the thread if set_playing(true). loop { if let Ok(action) = event_loop.queue.pop() { match action { Action::Load(path) => { if playback.stream.empty() { playback.play_from(&path); + //send_msg(&mut tx, AudioPlayerMsg::AudioPlay); } } Action::Stop => playback.stream.stop(), @@ -415,7 +420,7 @@ pub(crate) fn select_random_music(genre: &Genre) -> String { } fn send_msg(tx: &mut Sender, msg: AudioPlayerMsg) { - tx.try_send(msg) + tx.send(msg) .expect("Failed on attempting to send a message into audio channel."); }