Skip to content

Commit e89841c

Browse files
authored
Merge pull request #224 from tobexyz/feat/issue213
Feat/issue213
2 parents 119fd19 + af4b7e2 commit e89841c

4 files changed

Lines changed: 76 additions & 17 deletions

File tree

yaacc/src/main/java/de/yaacc/upnp/UpnpClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,11 @@ public void addUpnpClientListener(UpnpClientListener listener) {
498498
return getLocalDummyDevice();
499499
}
500500
if (isInitialized()) {
501-
return getRegistry().getDevice(new UDN(identifier), false);
501+
// Normalize UDN by stripping uuid: prefix if present
502+
UDN udn = UDN.valueOf(identifier);
503+
Device<?, ?, ?> device = getRegistry().getDevice(udn, false);
504+
YaaccLogger.d(getClass().getName(), "getDevice() identifier='" + identifier + "' -> UDN='" + udn.getIdentifierString() + "' found=" + (device != null));
505+
return device;
502506
}
503507
return null;
504508
}

yaacc/src/main/java/de/yaacc/upnp/server/media/SystemAudioCaptureService.java

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ public boolean startCapture(MediaProjection mediaProjection) {
6464

6565
try {
6666
int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);
67-
bufferSize *= BUFFER_SIZE_MULTIPLIER; // Increase buffer for stability
67+
if (bufferSize <= 0) {
68+
YaaccLogger.e(getClass().getName(), "Invalid buffer size: " + bufferSize + ", using default");
69+
bufferSize = 4096;
70+
}
71+
bufferSize *= BUFFER_SIZE_MULTIPLIER;
6872

6973
AudioPlaybackCaptureConfiguration config = new AudioPlaybackCaptureConfiguration.Builder(mediaProjection)
7074
.addMatchingUsage(android.media.AudioAttributes.USAGE_MEDIA)
@@ -82,23 +86,72 @@ public boolean startCapture(MediaProjection mediaProjection) {
8286
.setBufferSizeInBytes(bufferSize)
8387
.build();
8488

89+
int state = audioRecord.getState();
90+
if (state != AudioRecord.STATE_INITIALIZED) {
91+
YaaccLogger.e(getClass().getName(), "AudioRecord not initialized, state: " + state);
92+
// Try with smaller buffer as fallback
93+
return tryStartWithSmallerBuffer(config, mediaProjection);
94+
}
95+
96+
audioRecord.startRecording();
97+
isCapturing = true;
98+
99+
captureThread = new Thread(this::captureLoop);
100+
captureThread.setPriority(Thread.MAX_PRIORITY);
101+
captureThread.start();
102+
103+
YaaccLogger.i(getClass().getName(), "Audio capture started, buffer: " + bufferSize);
104+
return true;
105+
106+
} catch (IllegalArgumentException e) {
107+
YaaccLogger.e(getClass().getName(), "AudioRecord creation failed (invalid parameters)", e);
108+
stopCapture();
109+
return false;
110+
} catch (Exception e) {
111+
YaaccLogger.e(getClass().getName(), "Failed to start audio capture", e);
112+
stopCapture();
113+
return false;
114+
}
115+
}
116+
117+
/**
118+
* Fallback: try with smaller buffer if initial attempt fails.
119+
*/
120+
private boolean tryStartWithSmallerBuffer(AudioPlaybackCaptureConfiguration config, MediaProjection mediaProjection) {
121+
try {
122+
YaaccLogger.w(getClass().getName(), "Trying with minimum buffer size...");
123+
int minBuffer = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);
124+
if (minBuffer <= 0) {
125+
minBuffer = 4096;
126+
}
127+
128+
audioRecord = new AudioRecord.Builder()
129+
.setAudioPlaybackCaptureConfig(config)
130+
.setAudioFormat(new AudioFormat.Builder()
131+
.setEncoding(AUDIO_FORMAT)
132+
.setSampleRate(SAMPLE_RATE)
133+
.setChannelMask(CHANNEL_CONFIG)
134+
.build())
135+
.setBufferSizeInBytes(minBuffer)
136+
.build();
137+
85138
if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
86-
YaaccLogger.e(getClass().getName(), "AudioRecord not initialized");
139+
YaaccLogger.e(getClass().getName(), "AudioRecord still not initialized with min buffer");
87140
return false;
88141
}
89142

90143
audioRecord.startRecording();
91144
isCapturing = true;
92145

93146
captureThread = new Thread(this::captureLoop);
94-
captureThread.setPriority(Thread.MAX_PRIORITY); // High priority for audio
147+
captureThread.setPriority(Thread.MAX_PRIORITY);
95148
captureThread.start();
96149

97-
YaaccLogger.i(getClass().getName(), "Audio capture started with buffer size: " + bufferSize);
150+
YaaccLogger.i(getClass().getName(), "Audio capture started with fallback buffer");
98151
return true;
99152

100153
} catch (Exception e) {
101-
YaaccLogger.e(getClass().getName(), "Failed to start audio capture", e);
154+
YaaccLogger.e(getClass().getName(), "Fallback audio capture also failed", e);
102155
stopCapture();
103156
return false;
104157
}

yaacc/src/main/java/de/yaacc/util/FileDownloader.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,18 @@ protected Void doInBackground(DIDLObject... didlObjects) {
8585
}
8686
}
8787

88-
try {
89-
90-
InputStream is = new URL(playableItem.getUri().toString()).openStream();
91-
FileOutputStream outputStream = new FileOutputStream(file);
92-
byte[] b = new byte[1024];
88+
try (InputStream is = new URL(playableItem.getUri().toString()).openStream();
89+
FileOutputStream outputStream = new FileOutputStream(file)) {
90+
byte[] b = new byte[8192];
9391
int len;
9492
while ((len = is.read(b)) != -1) {
9593
outputStream.write(b, 0, len);
9694
}
97-
is.close();
98-
outputStream.close();
99-
100-
} catch (Exception e) {
101-
throw new RuntimeException(e);
95+
YaaccLogger.d(getClass().getName(), "Downloaded: " + file.getAbsolutePath());
96+
} catch (java.io.FileNotFoundException e) {
97+
YaaccLogger.e(getClass().getName(), "File not found: " + playableItem.getUri());
98+
} catch (java.io.IOException e) {
99+
YaaccLogger.e(getClass().getName(), "Download failed: " + playableItem.getUri());
102100
}
103101

104102
}

yaacc/src/main/java/org/fourthline/cling/support/model/PositionInfo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ public void setRelTime(String relTime) {
142142
}
143143

144144
public long getTrackDurationSeconds() {
145-
return getTrackDuration() == null ? 0 : ModelUtil.fromTimeString(getTrackDuration());
145+
String duration = getTrackDuration();
146+
if (duration == null || duration.equals("NOT_IMPLEMENTED")) {
147+
return 0;
148+
}
149+
return ModelUtil.fromTimeString(duration);
146150
}
147151

148152
public long getTrackElapsedSeconds() {

0 commit comments

Comments
 (0)