From 35ee0fdd12a9737d4a1876657fe78b38153b0ba4 Mon Sep 17 00:00:00 2001 From: badaix Date: Mon, 6 Feb 2017 19:30:33 +0100 Subject: [PATCH] mute group --- .../java/de/badaix/snapcast/GroupItem.java | 19 +++++----- .../java/de/badaix/snapcast/MainActivity.java | 6 +++- .../snapcast/control/RemoteControl.java | 14 +++++++- .../badaix/snapcast/control/json/Group.java | 15 ++++++-- server/config.h | 5 ++- server/streamServer.cpp | 35 ++++++++++++++++--- 6 files changed, 76 insertions(+), 18 deletions(-) diff --git a/android/Snapcast/src/main/java/de/badaix/snapcast/GroupItem.java b/android/Snapcast/src/main/java/de/badaix/snapcast/GroupItem.java index 0d17e848..91f954e4 100644 --- a/android/Snapcast/src/main/java/de/badaix/snapcast/GroupItem.java +++ b/android/Snapcast/src/main/java/de/badaix/snapcast/GroupItem.java @@ -25,7 +25,6 @@ import android.view.MotionEvent; import android.view.View; import android.widget.ImageButton; import android.widget.LinearLayout; -import android.widget.RelativeLayout; import android.widget.SeekBar; import android.widget.TextView; @@ -100,6 +99,11 @@ public class GroupItem extends LinearLayout implements SeekBar.OnSeekBarChangeLi llClient.addView(clientItem); } + if (group.isMuted()) + ibMute.setImageResource(R.drawable.ic_mute_icon); + else + ibMute.setImageResource(R.drawable.ic_speaker_icon); + if (clientItems.size() >= 2) llVolume.setVisibility(VISIBLE); else @@ -200,14 +204,11 @@ public class GroupItem extends LinearLayout implements SeekBar.OnSeekBarChangeLi @Override public void onClick(View v) { -/* TODO: group if (v == ibMute) { - Volume volume = client.getConfig().getVolume(); - volume.setMuted(!volume.isMuted()); + if (v == ibMute) { + group.setMuted(!group.isMuted()); update(); - listener.onMute(this, volume.isMuted()); - } else -*/ - if (v == ibSettings) { + listener.onMute(this, group.isMuted()); + } else if (v == ibSettings) { listener.onPropertiesClicked(this); } } @@ -245,6 +246,8 @@ public class GroupItem extends LinearLayout implements SeekBar.OnSeekBarChangeLi public interface GroupItemListener { void onGroupVolumeChanged(GroupItem group); + void onMute(GroupItem group, boolean mute); + void onVolumeChanged(GroupItem group, ClientItem clientItem, int percent, boolean mute); void onDeleteClicked(GroupItem group, ClientItem clientItem); diff --git a/android/Snapcast/src/main/java/de/badaix/snapcast/MainActivity.java b/android/Snapcast/src/main/java/de/badaix/snapcast/MainActivity.java index b2fa57ca..7da5233b 100644 --- a/android/Snapcast/src/main/java/de/badaix/snapcast/MainActivity.java +++ b/android/Snapcast/src/main/java/de/badaix/snapcast/MainActivity.java @@ -58,7 +58,6 @@ import de.badaix.snapcast.control.json.Client; import de.badaix.snapcast.control.json.Group; import de.badaix.snapcast.control.json.ServerStatus; import de.badaix.snapcast.control.json.Stream; -import de.badaix.snapcast.control.json.Volume; import de.badaix.snapcast.utils.NsdHelper; import de.badaix.snapcast.utils.Settings; import de.badaix.snapcast.utils.Setup; @@ -570,6 +569,11 @@ public class MainActivity extends AppCompatActivity implements GroupItem.GroupIt remoteControl.setGroupVolume(groupItem.getGroup()); } + @Override + public void onMute(GroupItem groupItem, boolean mute) { + remoteControl.setGroupMuted(groupItem.getGroup(), mute); + } + @Override public void onVolumeChanged(GroupItem groupItem, ClientItem clientItem, int percent, boolean mute) { remoteControl.setVolume(clientItem.getClient(), percent, mute); diff --git a/android/Snapcast/src/main/java/de/badaix/snapcast/control/RemoteControl.java b/android/Snapcast/src/main/java/de/badaix/snapcast/control/RemoteControl.java index b5447727..e5210e04 100644 --- a/android/Snapcast/src/main/java/de/badaix/snapcast/control/RemoteControl.java +++ b/android/Snapcast/src/main/java/de/badaix/snapcast/control/RemoteControl.java @@ -94,7 +94,7 @@ public class RemoteControl implements TcpClient.TcpClientListener { try { if (message.trim().startsWith("[")) { JSONArray jsonArray = new JSONArray(message); - for (int i=0; i { private String name = ""; private String id = ""; private String streamId = ""; + private boolean muted = false; private ArrayList clients = new ArrayList(); public Group(JSONObject json) { @@ -52,7 +53,7 @@ public class Group implements JsonSerialisable, Comparable { name = json.getString("name"); id = json.getString("id"); streamId = json.getString("stream"); - + muted = json.optBoolean("muted", false); JSONArray jClients = json.optJSONArray("clients"); if (jClients != null) { for (int i = 0; i < jClients.length(); i++) @@ -71,6 +72,7 @@ public class Group implements JsonSerialisable, Comparable { json.put("name", name); json.put("id", id); json.put("stream", streamId); + json.put("muted", muted); json.put("clients", getJsonClients()); } catch (JSONException e) { e.printStackTrace(); @@ -111,6 +113,13 @@ public class Group implements JsonSerialisable, Comparable { return clients; } + public boolean isMuted() { + return muted; + } + + public void setMuted(boolean muted) { + this.muted = muted; + } public boolean removeClient(String id) { if (TextUtils.isEmpty(id)) @@ -150,7 +159,7 @@ public class Group implements JsonSerialisable, Comparable { if (TextUtils.isEmpty(id)) return null; - for (Client c: clients) { + for (Client c : clients) { if (c == null) continue; @@ -176,6 +185,7 @@ public class Group implements JsonSerialisable, Comparable { if (name != null ? !name.equals(group.name) : group.name != null) return false; if (id != null ? !id.equals(group.id) : group.id != null) return false; + if (muted != group.muted) return false; if (streamId != null ? !streamId.equals(group.streamId) : group.streamId != null) return false; return clients != null ? clients.equals(group.clients) : group.clients == null; @@ -187,6 +197,7 @@ public class Group implements JsonSerialisable, Comparable { int result = name != null ? name.hashCode() : 0; result = 31 * result + (id != null ? id.hashCode() : 0); result = 31 * result + (streamId != null ? streamId.hashCode() : 0); + result = 31 * result + (muted ? 1 : 0); result = 31 * result + (clients != null ? clients.hashCode() : 0); return result; } diff --git a/server/config.h b/server/config.h index 3d34a95b..db0d9718 100644 --- a/server/config.h +++ b/server/config.h @@ -257,7 +257,7 @@ struct ClientInfo struct Group { - Group(const ClientInfoPtr client = nullptr) + Group(const ClientInfoPtr client = nullptr) : muted(false) { if (client) id = client->id; @@ -269,6 +269,7 @@ struct Group name = trim_copy(jGet(j, "name", "")); id = trim_copy(jGet(j, "id", "")); streamId = trim_copy(jGet(j, "stream", "")); + muted = jGet(j, "muted", false); clients.clear(); if (j.count("clients")) { @@ -288,6 +289,7 @@ struct Group j["name"] = trim_copy(name); j["id"] = trim_copy(id); j["stream"] = trim_copy(streamId); + j["muted"] = muted; json jClients = json::array(); for (auto client: clients) @@ -355,6 +357,7 @@ struct Group std::string name; std::string id; std::string streamId; + bool muted; std::vector clients; }; diff --git a/server/streamServer.cpp b/server/streamServer.cpp index 31c3ff88..ae127721 100644 --- a/server/streamServer.cpp +++ b/server/streamServer.cpp @@ -155,7 +155,8 @@ void StreamServer::ProcessRequest(const jsonrpcpp::request_ptr request, jsonrpcp msg::ServerSettings serverSettings; serverSettings.setBufferMs(settings_.bufferMs); serverSettings.setVolume(clientInfo->config.volume.percent); - serverSettings.setMuted(clientInfo->config.volume.muted); + GroupPtr group = Config::instance().getGroupFromClient(clientInfo); + serverSettings.setMuted(clientInfo->config.volume.muted || group->muted); serverSettings.setLatency(clientInfo->config.latency); session->send(&serverSettings); } @@ -174,6 +175,30 @@ void StreamServer::ProcessRequest(const jsonrpcpp::request_ptr request, jsonrpcp { result = group->toJson(); } + else if (request->method == "Group.SetMuted") + { + bool muted = request->params.get("muted"); + group->muted = muted; + + /// Update clients + for (auto client: group->clients) + { + session_ptr session = getStreamSession(client->id); + if (session != nullptr) + { + msg::ServerSettings serverSettings; + serverSettings.setBufferMs(settings_.bufferMs); + serverSettings.setVolume(client->config.volume.percent); + GroupPtr group = Config::instance().getGroupFromClient(client); + serverSettings.setMuted(client->config.volume.muted || group->muted); + serverSettings.setLatency(client->config.latency); + session->send(&serverSettings); + } + } + + /// Notify others + notification.reset(new jsonrpcpp::Notification("Group.OnUpdate", group->toJson()));; + } else if (request->method == "Group.SetStream") { string streamId = request->params.get("id"); @@ -304,7 +329,7 @@ void StreamServer::ProcessRequest(const jsonrpcpp::request_ptr request, jsonrpcp void StreamServer::onMessageReceived(ControlSession* controlSession, const std::string& message) { - logO << "onMessageReceived: " << message << "\n"; + logD << "onMessageReceived: " << message << "\n"; jsonrpcpp::entity_ptr entity(nullptr); try { @@ -328,7 +353,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std:: if (entity->is_request()) { jsonrpcpp::request_ptr request = dynamic_pointer_cast(entity); - logO << "isRequest: " << request->to_json().dump() << "\n"; + logD << "isRequest: " << request->to_json().dump() << "\n"; ProcessRequest(request, response, notification); if (response) controlSession->send(response->to_json().dump()); @@ -338,7 +363,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std:: else if (entity->is_batch()) { jsonrpcpp::batch_ptr batch = dynamic_pointer_cast(entity); - logO << "isBatch: " << batch->to_json().dump() << "\n"; + logD << "isBatch: " << batch->to_json().dump() << "\n"; jsonrpcpp::Batch responseBatch; jsonrpcpp::Batch notificationBatch; for (const auto& batch_entity: batch->entities) @@ -406,7 +431,7 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM logD << "request kServerSettings\n"; msg::ServerSettings* serverSettings = new msg::ServerSettings(); serverSettings->setVolume(client->config.volume.percent); - serverSettings->setMuted(client->config.volume.muted); + serverSettings->setMuted(client->config.volume.muted || group->muted); serverSettings->setLatency(client->config.latency); serverSettings->setBufferMs(settings_.bufferMs); serverSettings->refersTo = helloMsg.id;