|
29 | 29 | import java.util.Collections; |
30 | 30 | import java.util.List; |
31 | 31 | import java.util.concurrent.CountDownLatch; |
| 32 | +import java.util.concurrent.ExecutorService; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import java.util.concurrent.Future; |
32 | 35 | import java.util.concurrent.TimeUnit; |
33 | 36 | import java.util.zip.CRC32; |
34 | 37 |
|
@@ -169,6 +172,240 @@ void seeksInHardware() throws Exception { |
169 | 172 | assertTrue(hardware.frames.size() < FRAMES, "frames: " + hardware.frames.size()); |
170 | 173 | } |
171 | 174 |
|
| 175 | + @Test |
| 176 | + void loopsInHardware() throws Exception { |
| 177 | + Result software = play(H264, false, 0); |
| 178 | + Result hardware = playLooped(H264, 2 * FRAMES + 5); |
| 179 | + |
| 180 | + requireHardware(hardware); |
| 181 | + |
| 182 | + // Every pass starts over from the first key frame, which flushes the |
| 183 | + // decoder, and the pictures of each pass are those of the first. |
| 184 | + assertTrue(hardware.frames.size() >= 2 * FRAMES, "frames: " + hardware.frames.size()); |
| 185 | + |
| 186 | + for (int i = 0; i < 2 * FRAMES; i++) { |
| 187 | + assertEquals(software.frames.get(i % FRAMES), hardware.frames.get(i), "frame " + i); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void seekBetweenKeyFrames() throws Exception { |
| 193 | + // Half a second in is not a key frame; the player has to start from |
| 194 | + // the key frame before it, however the reader gets there. |
| 195 | + Result software = play(H264, false, 500_000); |
| 196 | + Result hardware = play(H264, true, 500_000); |
| 197 | + |
| 198 | + assertNull(hardware.error); |
| 199 | + |
| 200 | + requireHardware(hardware); |
| 201 | + |
| 202 | + assertEquals(software.frames, hardware.frames); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + void seekAfterEndStartsOver() throws Exception { |
| 207 | + Result expected = play(VP9, false, 0); |
| 208 | + Result result = new Result(); |
| 209 | + CountDownLatch[] ended = { new CountDownLatch(1) }; |
| 210 | + |
| 211 | + CustomVideoSource source = new CustomVideoSource(); |
| 212 | + VideoTrack track = factory.createVideoTrack("video", source); |
| 213 | + VideoTrackSink sink = frame -> { |
| 214 | + result.frames.add(lumaChecksum(frame.buffer.toI420())); |
| 215 | + |
| 216 | + frame.release(); |
| 217 | + }; |
| 218 | + |
| 219 | + track.addSink(sink); |
| 220 | + |
| 221 | + try (MediaPlayer player = new MediaPlayer(new MediaReader(path(VP9)), source, null, true)) { |
| 222 | + player.setListener(new MediaPlayerListener() { |
| 223 | + |
| 224 | + @Override |
| 225 | + public void onEndOfStream() { |
| 226 | + ended[0].countDown(); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public void onError(String message) { |
| 231 | + result.error = message; |
| 232 | + ended[0].countDown(); |
| 233 | + } |
| 234 | + }); |
| 235 | + |
| 236 | + player.play(); |
| 237 | + |
| 238 | + assertTrue(ended[0].await(TIMEOUT_SECONDS, TimeUnit.SECONDS), "no end of stream"); |
| 239 | + |
| 240 | + requireHardware(player.isHardwareDecoding()); |
| 241 | + |
| 242 | + int firstPass = result.frames.size(); |
| 243 | + |
| 244 | + // A decoder that has drained at the end of the stream takes |
| 245 | + // packets again after a seek. |
| 246 | + ended[0] = new CountDownLatch(1); |
| 247 | + |
| 248 | + player.seek(0); |
| 249 | + player.play(); |
| 250 | + |
| 251 | + assertTrue(ended[0].await(TIMEOUT_SECONDS, TimeUnit.SECONDS), "no second end of stream"); |
| 252 | + assertNull(result.error); |
| 253 | + assertEquals(FRAMES, firstPass); |
| 254 | + assertEquals(2 * FRAMES, result.frames.size()); |
| 255 | + assertEquals(expected.frames, result.frames.subList(0, FRAMES)); |
| 256 | + assertEquals(expected.frames, result.frames.subList(FRAMES, 2 * FRAMES)); |
| 257 | + } |
| 258 | + finally { |
| 259 | + track.removeSink(sink); |
| 260 | + track.dispose(); |
| 261 | + source.dispose(); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + void pausesAndResumes() throws Exception { |
| 267 | + Result software = play(H264, false, 0); |
| 268 | + Result result = new Result(); |
| 269 | + CountDownLatch ended = new CountDownLatch(1); |
| 270 | + |
| 271 | + CustomVideoSource source = new CustomVideoSource(); |
| 272 | + VideoTrack track = factory.createVideoTrack("video", source); |
| 273 | + VideoTrackSink sink = frame -> { |
| 274 | + result.frames.add(lumaChecksum(frame.buffer.toI420())); |
| 275 | + |
| 276 | + frame.release(); |
| 277 | + }; |
| 278 | + |
| 279 | + track.addSink(sink); |
| 280 | + |
| 281 | + try (MediaPlayer player = new MediaPlayer(new MediaReader(path(H264)), source, null, true)) { |
| 282 | + player.setListener(new MediaPlayerListener() { |
| 283 | + |
| 284 | + @Override |
| 285 | + public void onEndOfStream() { |
| 286 | + ended.countDown(); |
| 287 | + } |
| 288 | + |
| 289 | + @Override |
| 290 | + public void onError(String message) { |
| 291 | + result.error = message; |
| 292 | + ended.countDown(); |
| 293 | + } |
| 294 | + }); |
| 295 | + |
| 296 | + player.play(); |
| 297 | + |
| 298 | + awaitFrames(result, 5); |
| 299 | + player.pause(); |
| 300 | + |
| 301 | + // Whatever was in flight arrives, then nothing does. |
| 302 | + Thread.sleep(300); |
| 303 | + |
| 304 | + int paused = result.frames.size(); |
| 305 | + |
| 306 | + Thread.sleep(300); |
| 307 | + |
| 308 | + assertEquals(paused, result.frames.size(), "frames while paused"); |
| 309 | + |
| 310 | + player.play(); |
| 311 | + |
| 312 | + assertTrue(ended.await(TIMEOUT_SECONDS, TimeUnit.SECONDS), "no end of stream"); |
| 313 | + assertNull(result.error); |
| 314 | + |
| 315 | + requireHardware(player.isHardwareDecoding()); |
| 316 | + |
| 317 | + // Pausing neither drops nor repeats a picture. |
| 318 | + assertEquals(software.frames, result.frames); |
| 319 | + } |
| 320 | + finally { |
| 321 | + track.removeSink(sink); |
| 322 | + track.dispose(); |
| 323 | + source.dispose(); |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + @Test |
| 328 | + void closesWhilePlaying() throws Exception { |
| 329 | + // Closing a player takes the hardware decoder down in the middle of |
| 330 | + // decoding, with frames still on the GPU. Do it again and again: a |
| 331 | + // session or a device that leaks would run out. |
| 332 | + for (int i = 0; i < 20; i++) { |
| 333 | + CustomVideoSource source = new CustomVideoSource(); |
| 334 | + VideoTrack track = factory.createVideoTrack("video", source); |
| 335 | + CountDownLatch first = new CountDownLatch(1); |
| 336 | + VideoTrackSink sink = frame -> { |
| 337 | + frame.release(); |
| 338 | + first.countDown(); |
| 339 | + }; |
| 340 | + |
| 341 | + track.addSink(sink); |
| 342 | + |
| 343 | + try (MediaPlayer player = new MediaPlayer(new MediaReader(path(H264)), source, null, true)) { |
| 344 | + player.play(); |
| 345 | + |
| 346 | + assertTrue(first.await(TIMEOUT_SECONDS, TimeUnit.SECONDS), "no frame in round " + i); |
| 347 | + |
| 348 | + requireHardware(player.isHardwareDecoding()); |
| 349 | + } |
| 350 | + finally { |
| 351 | + track.removeSink(sink); |
| 352 | + track.dispose(); |
| 353 | + source.dispose(); |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + |
| 358 | + @Test |
| 359 | + void opensManyWithoutPlaying() throws Exception { |
| 360 | + // A decoder that is opened and closed without a single packet, which |
| 361 | + // the hardware has to hand its session back for. |
| 362 | + for (int i = 0; i < 50; i++) { |
| 363 | + CustomVideoSource source = new CustomVideoSource(); |
| 364 | + |
| 365 | + try (MediaPlayer player = new MediaPlayer(new MediaReader(path(i % 2 == 0 ? H264 : VP9)), |
| 366 | + source, null, true)) { |
| 367 | + if (i == 0) { |
| 368 | + requireHardware(player.isHardwareDecoding()); |
| 369 | + } |
| 370 | + } |
| 371 | + finally { |
| 372 | + source.dispose(); |
| 373 | + } |
| 374 | + } |
| 375 | + } |
| 376 | + |
| 377 | + @Test |
| 378 | + void decodesConcurrently() throws Exception { |
| 379 | + Result h264 = play(H264, false, 0); |
| 380 | + Result vp9 = play(VP9, false, 0); |
| 381 | + |
| 382 | + int players = 4; |
| 383 | + ExecutorService executor = Executors.newFixedThreadPool(players); |
| 384 | + |
| 385 | + try { |
| 386 | + List<Future<Result>> results = new ArrayList<>(); |
| 387 | + |
| 388 | + // Several sessions on the one GPU at the same time, in both codecs. |
| 389 | + for (int i = 0; i < players; i++) { |
| 390 | + String asset = i % 2 == 0 ? H264 : VP9; |
| 391 | + |
| 392 | + results.add(executor.submit(() -> play(asset, true, 0))); |
| 393 | + } |
| 394 | + for (int i = 0; i < players; i++) { |
| 395 | + Result result = results.get(i).get(60, TimeUnit.SECONDS); |
| 396 | + |
| 397 | + assertNull(result.error); |
| 398 | + |
| 399 | + requireHardware(result); |
| 400 | + |
| 401 | + assertEquals(i % 2 == 0 ? h264.frames : vp9.frames, result.frames, "player " + i); |
| 402 | + } |
| 403 | + } |
| 404 | + finally { |
| 405 | + executor.shutdownNow(); |
| 406 | + } |
| 407 | + } |
| 408 | + |
172 | 409 | private void assertMatchesSoftware(String asset) throws Exception { |
173 | 410 | Result software = play(asset, false, 0); |
174 | 411 | Result hardware = play(asset, true, 0); |
@@ -254,6 +491,59 @@ public void onError(String message) { |
254 | 491 | return result; |
255 | 492 | } |
256 | 493 |
|
| 494 | + /** |
| 495 | + * Plays the asset in a loop, in hardware, until the sink has seen at least |
| 496 | + * the given number of frames, then stops. |
| 497 | + */ |
| 498 | + private Result playLooped(String asset, int minFrames) throws Exception { |
| 499 | + Result result = new Result(); |
| 500 | + |
| 501 | + CustomVideoSource source = new CustomVideoSource(); |
| 502 | + VideoTrack track = factory.createVideoTrack("video", source); |
| 503 | + VideoTrackSink sink = frame -> { |
| 504 | + result.frames.add(lumaChecksum(frame.buffer.toI420())); |
| 505 | + |
| 506 | + frame.release(); |
| 507 | + }; |
| 508 | + |
| 509 | + track.addSink(sink); |
| 510 | + |
| 511 | + try (MediaPlayer player = new MediaPlayer(new MediaReader(path(asset)), source, null, true)) { |
| 512 | + player.setListener(new MediaPlayerListener() { |
| 513 | + |
| 514 | + @Override |
| 515 | + public void onError(String message) { |
| 516 | + result.error = message; |
| 517 | + } |
| 518 | + }); |
| 519 | + |
| 520 | + player.setLooping(true); |
| 521 | + player.play(); |
| 522 | + |
| 523 | + awaitFrames(result, minFrames); |
| 524 | + |
| 525 | + result.hardware = player.isHardwareDecoding(); |
| 526 | + } |
| 527 | + finally { |
| 528 | + track.removeSink(sink); |
| 529 | + track.dispose(); |
| 530 | + source.dispose(); |
| 531 | + } |
| 532 | + |
| 533 | + return result; |
| 534 | + } |
| 535 | + |
| 536 | + private static void awaitFrames(Result result, int count) throws InterruptedException { |
| 537 | + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(TIMEOUT_SECONDS); |
| 538 | + |
| 539 | + while (result.frames.size() < count && result.error == null && System.nanoTime() < deadline) { |
| 540 | + Thread.sleep(10); |
| 541 | + } |
| 542 | + |
| 543 | + assertNull(result.error); |
| 544 | + assertTrue(result.frames.size() >= count, "frames: " + result.frames.size()); |
| 545 | + } |
| 546 | + |
257 | 547 | /** A checksum of the luma of a picture, row by row so that the stride does not matter. */ |
258 | 548 | private static long lumaChecksum(I420Buffer buffer) { |
259 | 549 | ByteBuffer luma = buffer.getDataY(); |
|
0 commit comments