This commit is contained in:
Koen 2024-03-08 08:17:38 +01:00
commit f6cc00f471
3 changed files with 46 additions and 34 deletions

View file

@ -163,24 +163,25 @@ class AirPlayCastingDevice : CastingDevice {
} }
connectionState = CastConnectionState.CONNECTED; connectionState = CastConnectionState.CONNECTED;
delay(1000);
val progressIndex = progressInfo.lowercase().indexOf("position: "); val progressIndex = progressInfo.lowercase().indexOf("position: ");
if (progressIndex == -1) { if (progressIndex == -1) {
delay(1000);
continue; continue;
} }
val progress = progressInfo.substring(progressIndex + "position: ".length).toDoubleOrNull() ?: continue; val progress = progressInfo.substring(progressIndex + "position: ".length).toDoubleOrNull() ?: continue;
setTime(progress); setTime(progress);
val durationIndex = progressInfo.lowercase().indexOf("duration: "); val durationIndex = progressInfo.lowercase().indexOf("duration: ");
if (durationIndex == -1) { if (durationIndex == -1) {
delay(1000);
continue; continue;
} }
val duration = progressInfo.substring(durationIndex + "duration: ".length).toDoubleOrNull() ?: continue; val duration = progressInfo.substring(durationIndex + "duration: ".length).toDoubleOrNull() ?: continue;
setDuration(duration); setDuration(duration);
delay(1000);
} catch (e: Throwable) { } catch (e: Throwable) {
Logger.w(TAG, "Failed to get server info from AirPlay device.", e) Logger.w(TAG, "Failed to get server info from AirPlay device.", e)
} }

View file

@ -44,7 +44,9 @@ class ChromecastCastingDevice : CastingDevice {
private var _socket: SSLSocket? = null; private var _socket: SSLSocket? = null;
private var _outputStream: DataOutputStream? = null; private var _outputStream: DataOutputStream? = null;
private var _outputStreamLock = Object();
private var _inputStream: DataInputStream? = null; private var _inputStream: DataInputStream? = null;
private var _inputStreamLock = Object();
private var _scopeIO: CoroutineScope? = null; private var _scopeIO: CoroutineScope? = null;
private var _requestId = 1; private var _requestId = 1;
private var _started: Boolean = false; private var _started: Boolean = false;
@ -383,22 +385,26 @@ class ChromecastCastingDevice : CastingDevice {
getStatus(); getStatus();
val buffer = ByteArray(4096); val buffer = ByteArray(409600);
Logger.i(TAG, "Started receiving."); Logger.i(TAG, "Started receiving.");
while (_scopeIO?.isActive == true) { while (_scopeIO?.isActive == true) {
try { try {
val inputStream = _inputStream ?: break; val inputStream = _inputStream ?: break;
synchronized(_inputStreamLock)
{
Log.d(TAG, "Receiving next packet..."); Log.d(TAG, "Receiving next packet...");
val b1 = inputStream.readUnsignedByte(); val b1 = inputStream.readUnsignedByte();
val b2 = inputStream.readUnsignedByte(); val b2 = inputStream.readUnsignedByte();
val b3 = inputStream.readUnsignedByte(); val b3 = inputStream.readUnsignedByte();
val b4 = inputStream.readUnsignedByte(); val b4 = inputStream.readUnsignedByte();
val size = ((b1.toLong() shl 24) or (b2.toLong() shl 16) or (b3.toLong() shl 8) or b4.toLong()).toInt(); val size =
((b1.toLong() shl 24) or (b2.toLong() shl 16) or (b3.toLong() shl 8) or b4.toLong()).toInt();
if (size > buffer.size) { if (size > buffer.size) {
Logger.w(TAG, "Skipping packet that is too large $size bytes.") Logger.w(TAG, "Skipping packet that is too large $size bytes.")
inputStream.skip(size.toLong()); inputStream.skip(size.toLong());
continue; return@synchronized
} }
Log.d(TAG, "Received header indicating $size bytes. Waiting for message."); Log.d(TAG, "Received header indicating $size bytes. Waiting for message.");
@ -414,9 +420,10 @@ class ChromecastCastingDevice : CastingDevice {
try { try {
handleMessage(message); handleMessage(message);
} catch (e:Throwable) { } catch (e: Throwable) {
Logger.w(TAG, "Failed to handle message.", e); Logger.w(TAG, "Failed to handle message.", e);
} }
}
} catch (e: java.net.SocketException) { } catch (e: java.net.SocketException) {
Logger.e(TAG, "Socket exception while receiving.", e); Logger.e(TAG, "Socket exception while receiving.", e);
break; break;
@ -588,6 +595,8 @@ class ChromecastCastingDevice : CastingDevice {
return; return;
} }
synchronized(_outputStreamLock)
{
val serializedSizeBE = ByteArray(4); val serializedSizeBE = ByteArray(4);
serializedSizeBE[0] = (data.size shr 24 and 0xff).toByte(); serializedSizeBE[0] = (data.size shr 24 and 0xff).toByte();
serializedSizeBE[1] = (data.size shr 16 and 0xff).toByte(); serializedSizeBE[1] = (data.size shr 16 and 0xff).toByte();
@ -595,6 +604,7 @@ class ChromecastCastingDevice : CastingDevice {
serializedSizeBE[3] = (data.size and 0xff).toByte(); serializedSizeBE[3] = (data.size and 0xff).toByte();
outputStream.write(serializedSizeBE); outputStream.write(serializedSizeBE);
outputStream.write(data); outputStream.write(data);
}
//Log.d(TAG, "Sent ${data.size} bytes."); //Log.d(TAG, "Sent ${data.size} bytes.");
} }

View file

@ -242,6 +242,7 @@ class StateCasting {
jmDNS.addServiceListener("_googlecast._tcp.local.", _chromecastServiceListener); jmDNS.addServiceListener("_googlecast._tcp.local.", _chromecastServiceListener);
jmDNS.addServiceListener("_airplay._tcp.local.", _airPlayServiceListener); jmDNS.addServiceListener("_airplay._tcp.local.", _airPlayServiceListener);
jmDNS.addServiceListener("_fastcast._tcp.local.", _fastCastServiceListener); jmDNS.addServiceListener("_fastcast._tcp.local.", _fastCastServiceListener);
jmDNS.addServiceListener("_fcast._tcp.local.", _fastCastServiceListener);
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
jmDNS.addServiceTypeListener(_serviceTypeListener); jmDNS.addServiceTypeListener(_serviceTypeListener);