plex-api 0.0.12

Library for communication with Plex server. Work in progress, not ready for any use. See github for details.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
mod fixtures;

mod offline {
    use std::collections::HashMap;

    use super::fixtures::offline::{server::*, Mocked};
    use httpmock::{prelude::HttpMockRequest, Method::GET};
    use plex_api::{
        library::{Movie, Transcodable},
        media_container::server::library::{AudioCodec, VideoCodec},
        Server,
    };

    // Expands a profile query parameter into the list of settings.
    fn expand_profile(req: &HttpMockRequest) -> HashMap<String, Vec<HashMap<String, String>>> {
        let param = req
            .query_params()
            .into_iter()
            .filter_map(|(n, v)| {
                if n == "X-Plex-Client-Profile-Extra" {
                    Some(v)
                } else {
                    None
                }
            })
            .next()
            .unwrap();

        let mut settings: HashMap<String, Vec<HashMap<String, String>>> = HashMap::new();
        for setting in param.split('+') {
            if setting.ends_with(')') {
                if let Some(idx) = setting.find('(') {
                    let setting_name = setting[0..idx].to_string();
                    let params: HashMap<String, String> = setting[idx + 1..setting.len() - 1]
                        // Split up the parameters
                        .split('&')
                        .filter_map(|v| {
                            // Split into name=value
                            v.find('=')
                                .map(|index| (v[0..index].to_string(), v[index + 1..].to_string()))
                        })
                        .collect();

                    if let Some(list) = settings.get_mut(&setting_name) {
                        list.push(params);
                    } else {
                        settings.insert(setting_name, vec![params]);
                    }
                }
            }
        }

        settings
    }

    fn assert_setting_count(
        settings: &HashMap<String, Vec<HashMap<String, String>>>,
        name: &str,
        expected: usize,
    ) {
        if let Some(s) = settings.get(name) {
            assert_eq!(s.len(), expected);
        } else {
            assert_eq!(0, expected);
        }
    }

    fn assert_setting(
        settings: &HashMap<String, Vec<HashMap<String, String>>>,
        name: &str,
        values: &[(&str, &str)],
    ) {
        let settings = if let Some(s) = settings.get(name) {
            s
        } else {
            panic!("Failed to find match for {values:#?} in []")
        };

        for setting in settings {
            if setting.len() != values.len() {
                continue;
            }

            let mut matched = true;
            for (name, value) in values {
                if setting.get(*name) != Some(&value.to_string()) {
                    matched = false;
                }
            }

            if matched {
                return;
            }
        }

        panic!("Failed to find match for {values:#?} in {settings:#?}")
    }

    #[plex_api_test_helper::offline_test]
    async fn download_queue(#[future] server_authenticated: Mocked<Server>) {
        let (server, mock_server) = server_authenticated.split();

        // Test getting/creating the queue
        let mut m = mock_server.mock(|when, then| {
            when.method(httpmock::Method::POST).path("/downloadQueue");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/queue_created.json");
        });

        let queue = server.download_queue().await.unwrap();
        m.assert();
        m.delete();

        // Test listing items when queue is empty
        let mut m = mock_server.mock(|when, then| {
            when.method(GET).path("/downloadQueue/1/items");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/empty_items.json");
        });

        let items = queue.items().await.unwrap();
        assert_eq!(items.len(), 0);
        m.assert();
        m.delete();

        // Test listing items when queue has one item
        let mut m = mock_server.mock(|when, then| {
            when.method(GET).path("/downloadQueue/1/items");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/items_with_one.json");
        });

        let items = queue.items().await.unwrap();
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].id(), 123);
        assert_eq!(items[0].key(), "/library/metadata/159637");
        m.assert();
        m.delete();

        // Test getting a specific item
        let mut m = mock_server.mock(|when, then| {
            when.method(GET).path("/downloadQueue/1/items/123");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/item_waiting.json");
        });

        let item = queue.item(123).await.unwrap();
        assert_eq!(item.id(), 123);
        assert_eq!(item.key(), "/library/metadata/159637");
        m.assert();
        m.delete();
    }

    #[plex_api_test_helper::offline_test]
    async fn queue_item(#[future] server_authenticated: Mocked<Server>) {
        let (server, mock_server) = server_authenticated.split();

        let mut m = mock_server.mock(|when, then| {
            when.method(GET).path("/library/metadata/159637");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/transcode/metadata_159637.json");
        });

        let item: Movie = server
            .item_by_id("159637")
            .await
            .unwrap()
            .try_into()
            .unwrap();
        m.assert();
        m.delete();

        // Mock the queue creation
        let mut m = mock_server.mock(|when, then| {
            when.method(httpmock::Method::POST).path("/downloadQueue");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/queue_created.json");
        });

        // Mock adding the item to the queue
        let mut m2 = mock_server.mock(|when, then| {
            when.method(httpmock::Method::POST)
                .path("/downloadQueue/1/add")
                .query_param_exists("session")
                .query_param_exists("transcodeSessionId")
                .query_param("transcodeType", "video")
                .query_param("path", "/library/metadata/159637")
                .query_param("keys", "/library/metadata/159637")
                .query_param_exists("mediaIndex")
                .query_param_exists("partIndex")
                .query_param_exists("directPlay")
                .query_param_exists("directStream")
                .query_param_exists("directStreamAudio")
                .query_param("context", "static")
                .query_param("maxVideoBitrate", "2000")
                .query_param("videoBitrate", "2000")
                .query_param("videoResolution", "1280x720")
                .query_param_exists("subtitles")
                .query_param_exists("subtitleSize")
                .query_param_exists("X-Plex-Client-Profile-Extra")
                .is_true(|req| {
                    // Verify that the transcode options are correctly passed via the profile
                    let settings = expand_profile(req);

                    // Verify we have only one transcode target (for Mp4) and one direct play profile
                    assert_setting_count(&settings, "add-transcode-target", 1);
                    assert_setting_count(&settings, "add-direct-play-profile", 1);

                    // Verify MP4 transcode target
                    assert_setting(
                        &settings,
                        "add-transcode-target",
                        &[
                            ("type", "videoProfile"),
                            ("context", "static"),
                            ("protocol", "http"),
                            ("container", "mp4"),
                            ("videoCodec", "h264"),
                            ("audioCodec", "aac"),
                            ("subtitleCodec", ""),
                            ("replace", "true"),
                        ],
                    );

                    // Verify direct play profile
                    assert_setting(
                        &settings,
                        "add-direct-play-profile",
                        &[
                            ("type", "videoProfile"),
                            ("container", "mp4"),
                            ("videoCodec", "h264"),
                            ("audioCodec", "aac"),
                            ("subtitleCodec", ""),
                            ("replace", "true"),
                        ],
                    );

                    true
                });
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/add_item_response.json");
        });

        // Mock fetching the item in Deciding state
        let mut m3 = mock_server.mock(|when, then| {
            when.method(GET).path("/downloadQueue/1/items/123");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/item_deciding.json");
        });

        let mut queue_item = item
            .queue_download(
                plex_api::transcode::VideoTranscodeOptions {
                    bitrate: 2000,
                    width: 1280,
                    height: 720,
                    containers: vec![
                        plex_api::media_container::server::library::ContainerFormat::Mp4,
                    ],
                    video_codecs: vec![VideoCodec::H264],
                    audio_codecs: vec![AudioCodec::Aac],
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();

        m.assert();
        m.delete();
        m2.assert();
        m2.delete();
        m3.assert();
        m3.delete();

        // Verify initial state is Deciding
        assert!(matches!(
            queue_item.status(),
            plex_api::transcode::QueueItemStatus::Deciding
        ));
        assert_eq!(queue_item.id(), 123);
        assert_eq!(queue_item.key(), "/library/metadata/159637");

        // Update to Waiting state
        let mut m = mock_server.mock(|when, then| {
            when.method(GET).path("/downloadQueue/1/items/123");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/item_waiting.json");
        });

        queue_item.update().await.unwrap();
        m.assert();
        m.delete();

        assert!(matches!(
            queue_item.status(),
            plex_api::transcode::QueueItemStatus::Waiting
        ));

        // Update to Processing state
        let mut m = mock_server.mock(|when, then| {
            when.method(GET).path("/downloadQueue/1/items/123");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/item_processing.json");
        });

        queue_item.update().await.unwrap();
        m.assert();
        m.delete();

        assert!(matches!(
            queue_item.status(),
            plex_api::transcode::QueueItemStatus::Processing
        ));
        assert!(queue_item.is_transcode());
        let stats = queue_item.stats().unwrap();
        assert_eq!(stats.progress, 25.5_f32);
        assert_eq!(stats.speed, Some(2.5_f32));

        // Update to Available state
        let mut m = mock_server.mock(|when, then| {
            when.method(GET).path("/downloadQueue/1/items/123");
            then.status(200)
                .header("content-type", "text/json")
                .body_from_file("tests/mocks/download_queue/item_available.json");
        });

        queue_item.update().await.unwrap();
        m.assert();
        m.delete();

        assert!(matches!(
            queue_item.status(),
            plex_api::transcode::QueueItemStatus::Available
        ));
    }
}

mod online {
    use plex_api::{
        media_container::server::library::{
            AudioCodec, ContainerFormat, Decision, Protocol, VideoCodec,
        },
        transcode::{QueueItem, QueueItemStatus},
        Server,
    };
    use std::time::Duration;
    use tokio::time::sleep;

    /// Waits for an item to start transcoding.
    async fn wait_for_transcode_start(item: &mut QueueItem) {
        let mut count = 0;
        loop {
            if !matches!(
                item.status(),
                QueueItemStatus::Deciding | QueueItemStatus::Waiting
            ) {
                break;
            }
            sleep(Duration::from_millis(250)).await;
            item.update().await.unwrap();
            count += 1;

            if count > 480 {
                panic!("Waited too long for transcode to start");
            }
        }
    }

    /// Waits for an item to become available.
    async fn wait_for_available(item: &mut QueueItem) {
        let mut count = 0;
        loop {
            if matches!(item.status(), QueueItemStatus::Available) {
                break;
            }
            sleep(Duration::from_millis(250)).await;
            item.update().await.unwrap();
            count += 1;

            if count > 480 {
                panic!("Waited too long for item to become available");
            }
        }
    }

    /// Checks the item was correct.
    fn verify_transcoded_item(
        item: &QueueItem,
        container: ContainerFormat,
        audio: (Decision, AudioCodec),
        video: Option<(Decision, VideoCodec)>,
    ) {
        assert!(item.is_transcode());

        match item.status() {
            QueueItemStatus::Waiting => {
                // It may have taken too long to start transcoding. This isn't
                // an error, but it is unfortunate as we then can't check the
                // transcode options.
            }
            QueueItemStatus::Available => {
                // If the server is too fast the item may have completed
                // already. This isn't an error, but it is unfortunate as we
                // then can't check the transcode options.
            }
            QueueItemStatus::Processing => {
                let stats = item
                    .stats()
                    .expect("Stats should be available when processing");

                assert_eq!(stats.protocol, Protocol::Http);
                assert_eq!(stats.container, container);
                assert_eq!(stats.audio_decision, Some(audio.0));
                assert_eq!(stats.audio_codec, Some(audio.1));
                assert_eq!(stats.video_decision, video.map(|v| v.0));
                assert_eq!(stats.video_codec, video.map(|v| v.1));
            }
            status => panic!("Unexpected status: {status:?}"),
        }
    }

    mod movie {
        use super::{super::fixtures::online::server::server, *};
        use mp4::{AvcProfile, MediaType, Mp4Reader, TrackType};
        use plex_api::{
            library::{MediaItem, MetadataItem, Movie, Transcodable},
            transcode::VideoTranscodeOptions,
        };
        use std::io::Cursor;

        #[plex_api_test_helper::online_test_claimed_server]
        async fn queue_transcode(
            #[future]
            #[with("Generic".to_owned())]
            server: Server,
        ) {
            let queue = server.download_queue().await.unwrap();

            let movie: Movie = server.item_by_id("57").await.unwrap().try_into().unwrap();
            assert_eq!(movie.title(), "Sintel");

            let media = &movie.media()[0];
            let part = &media.parts()[0];

            let mut item = part
                .queue_download(
                    // These settings will force transcoding as the original has
                    // higher bitrate and has a different audio codec.
                    VideoTranscodeOptions {
                        bitrate: 110,
                        containers: vec![ContainerFormat::Mp4],
                        video_codecs: vec![VideoCodec::H264],
                        audio_codecs: vec![AudioCodec::Mp3],
                        ..Default::default()
                    },
                    Some(&queue),
                )
                .await
                .unwrap();

            assert_eq!(item.queue(), queue);

            assert!(matches!(item.status(), QueueItemStatus::Deciding));

            wait_for_transcode_start(&mut item).await;

            assert!(item.is_transcode());

            verify_transcoded_item(
                &item,
                ContainerFormat::Mp4,
                (Decision::Transcode, AudioCodec::Mp3),
                Some((Decision::Transcode, VideoCodec::H264)),
            );

            item.delete().await.unwrap();

            assert!(queue.items().await.unwrap().is_empty());

            let movie: Movie = server.item_by_id("55").await.unwrap().try_into().unwrap();
            assert_eq!(movie.title(), "Big Buck Bunny");

            let media = &movie.media()[0];
            assert_eq!(media.parts().len(), 2);

            let mut item = movie
                .queue_download(
                    // These settings will force transcoding as the original has
                    // higher bitrate and has a different audio codec.
                    VideoTranscodeOptions {
                        bitrate: 110,
                        containers: vec![ContainerFormat::Mp4],
                        video_codecs: vec![VideoCodec::H264],
                        audio_codecs: vec![AudioCodec::Mp3],
                        ..Default::default()
                    },
                    None,
                )
                .await
                .unwrap();

            assert!(matches!(item.status(), QueueItemStatus::Deciding));

            wait_for_transcode_start(&mut item).await;

            assert!(item.is_transcode());

            verify_transcoded_item(
                &item,
                ContainerFormat::Mp4,
                (Decision::Transcode, AudioCodec::Mp3),
                Some((Decision::Transcode, VideoCodec::H264)),
            );

            item.delete().await.unwrap();

            assert!(queue.items().await.unwrap().is_empty());
        }

        #[plex_api_test_helper::online_test_claimed_server]
        async fn queue_change_container(
            #[future]
            #[with("Generic".to_owned())]
            server: Server,
        ) {
            let queue = server.download_queue().await.unwrap();

            let movie: Movie = server.item_by_id("57").await.unwrap().try_into().unwrap();
            assert_eq!(movie.title(), "Sintel");

            let media = &movie.media()[0];
            let part = &media.parts()[0];

            let mut item = part
                .queue_download(
                    // These settings should allow for direct streaming of the video
                    // and audio but into a different container format.
                    VideoTranscodeOptions {
                        bitrate: 200000000,
                        width: 1280,
                        height: 720,
                        containers: vec![ContainerFormat::Mp4],
                        video_codecs: vec![VideoCodec::H264],
                        audio_codecs: vec![AudioCodec::Aac],
                        ..Default::default()
                    },
                    Some(&queue),
                )
                .await
                .unwrap();

            assert_eq!(item.queue(), queue);

            assert!(matches!(item.status(), QueueItemStatus::Deciding));

            wait_for_transcode_start(&mut item).await;

            assert!(item.is_transcode());

            verify_transcoded_item(
                &item,
                ContainerFormat::Mp4,
                (Decision::Copy, AudioCodec::Aac),
                Some((Decision::Copy, VideoCodec::H264)),
            );

            // As this transcode is just copying the existing streams into a new
            // container format it should complete quickly allowing us to download
            // the transcoded file.

            wait_for_available(&mut item).await;

            let mut buf = Vec::<u8>::new();
            item.download(&mut buf, ..).await.unwrap();

            item.delete().await.unwrap();

            assert!(queue.items().await.unwrap().is_empty());

            // Verify that the file is a valid MP4 container and the tracks are
            // expected.
            let len = buf.len();
            let cursor = Cursor::new(buf);
            let mp4 = Mp4Reader::read_header(cursor, len as u64).unwrap();

            let mut videos = mp4
                .tracks()
                .values()
                .filter(|t| matches!(t.track_type(), Ok(TrackType::Video)));

            let video = videos.next().unwrap();
            assert!(matches!(video.media_type(), Ok(MediaType::H264)));
            assert_eq!(video.width(), 1280);
            assert_eq!(video.height(), 720);
            // Allow some slop in the durations
            assert!(
                video
                    .duration()
                    .as_millis()
                    .abs_diff(part.duration().unwrap() as u128)
                    < 200,
            );
            assert!(matches!(video.video_profile(), Ok(AvcProfile::AvcHigh)));
            assert!(videos.next().is_none());

            let mut audios = mp4
                .tracks()
                .values()
                .filter(|t| matches!(t.track_type(), Ok(TrackType::Audio)));

            let audio = audios.next().unwrap();
            assert_eq!(audio.media_type().unwrap(), MediaType::AAC);
            assert!(audios.next().is_none());

            let movie: Movie = server.item_by_id("55").await.unwrap().try_into().unwrap();
            assert_eq!(movie.title(), "Big Buck Bunny");

            let media = &movie.media()[0];
            assert_eq!(media.parts().len(), 2);

            let mut item = movie
                .queue_download(
                    // These settings should allow for direct streaming of the video
                    // and audio but into a different container format.
                    VideoTranscodeOptions {
                        bitrate: 200000000,
                        width: 1280,
                        height: 720,
                        containers: vec![ContainerFormat::Mp4],
                        video_codecs: vec![VideoCodec::H264],
                        audio_codecs: vec![AudioCodec::Aac],
                        ..Default::default()
                    },
                    None,
                )
                .await
                .unwrap();

            assert!(matches!(item.status(), QueueItemStatus::Deciding));

            wait_for_transcode_start(&mut item).await;

            assert!(item.is_transcode());

            verify_transcoded_item(
                &item,
                ContainerFormat::Mp4,
                (Decision::Transcode, AudioCodec::Aac),
                Some((Decision::Transcode, VideoCodec::H264)),
            );

            // As this transcode is just copying the existing streams into a new
            // container format it should complete quickly allowing us to download
            // the transcoded file.

            wait_for_available(&mut item).await;

            let mut buf = Vec::<u8>::new();
            item.download(&mut buf, ..).await.unwrap();

            item.delete().await.unwrap();

            assert!(queue.items().await.unwrap().is_empty());

            // Verify that the file is a valid MP4 container and the tracks are
            // expected.
            let len = buf.len();
            let cursor = Cursor::new(buf);
            let mp4 = Mp4Reader::read_header(cursor, len as u64).unwrap();

            let mut videos = mp4
                .tracks()
                .values()
                .filter(|t| matches!(t.track_type(), Ok(TrackType::Video)));

            let video = videos.next().unwrap();
            assert!(matches!(video.media_type(), Ok(MediaType::H264)));
            assert_eq!(video.width(), 1280);
            assert_eq!(video.height(), 720);
            // Allow some slop in the durations
            assert!(
                video
                    .duration()
                    .as_millis()
                    .abs_diff(media.duration().unwrap() as u128)
                    < 200,
            );
            assert_eq!(video.video_profile().unwrap(), AvcProfile::AvcMain);
            assert!(videos.next().is_none());

            let mut audios = mp4
                .tracks()
                .values()
                .filter(|t| matches!(t.track_type(), Ok(TrackType::Audio)));

            let audio = audios.next().unwrap();
            assert!(matches!(audio.media_type(), Ok(MediaType::AAC)));
            assert!(audios.next().is_none());
        }

        #[plex_api_test_helper::online_test_claimed_server]
        async fn queue_direct_play(
            #[future]
            #[with("Generic".to_owned())]
            server: Server,
        ) {
            let queue = server.download_queue().await.unwrap();

            let movie: Movie = server.item_by_id("57").await.unwrap().try_into().unwrap();
            assert_eq!(movie.title(), "Sintel");

            let media = &movie.media()[0];
            let part = &media.parts()[0];

            let mut item = part
                .queue_download(
                    // Here we ask to transcode into a format the movie is already
                    // in so the server denies the request.
                    VideoTranscodeOptions {
                        bitrate: 200000000,
                        width: 1280,
                        height: 720,
                        containers: vec![ContainerFormat::Mkv],
                        video_codecs: vec![VideoCodec::H264],
                        audio_codecs: vec![AudioCodec::Aac],
                        ..Default::default()
                    },
                    Some(&queue),
                )
                .await
                .unwrap();

            assert_eq!(item.queue(), queue);

            assert!(matches!(item.status(), QueueItemStatus::Deciding));

            wait_for_transcode_start(&mut item).await;

            assert!(!item.is_transcode());

            assert_eq!(item.status(), QueueItemStatus::Available);

            let mut buf = Vec::<u8>::new();
            item.download(&mut buf, ..).await.unwrap();

            item.delete().await.unwrap();

            assert!(queue.items().await.unwrap().is_empty());

            assert_eq!(buf.len(), part.len().unwrap() as usize);
        }
    }

    mod music {
        use super::{super::fixtures::online::server::server, *};
        use plex_api::{
            library::{MediaItem, MetadataItem, Track, Transcodable},
            transcode::MusicTranscodeOptions,
        };

        #[plex_api_test_helper::online_test_claimed_server]
        async fn queue_transcode(
            #[future]
            #[with("Generic".to_owned())]
            server: Server,
        ) {
            let queue = server.download_queue().await.unwrap();

            let track: Track = server.item_by_id("158").await.unwrap().try_into().unwrap();
            assert_eq!(track.title(), "Try It Out (Neon mix)");

            let media = &track.media()[0];
            let part = &media.parts()[0];

            assert!(queue.items().await.unwrap().is_empty());

            let mut item = part
                .queue_download(
                    // These settings will force transcoding as the original is too
                    // high a bitrate and has a different audio codec.
                    MusicTranscodeOptions {
                        bitrate: 92,
                        containers: vec![ContainerFormat::Mp3],
                        codecs: vec![AudioCodec::Mp3],
                        ..Default::default()
                    },
                    None,
                )
                .await
                .unwrap();

            assert_eq!(item.queue(), queue);

            assert!(matches!(item.status(), QueueItemStatus::Deciding));

            wait_for_transcode_start(&mut item).await;

            assert!(item.is_transcode());

            verify_transcoded_item(
                &item,
                ContainerFormat::Mp3,
                (Decision::Transcode, AudioCodec::Mp3),
                None,
            );

            // Audio transcoding should be reasonably fast...

            wait_for_available(&mut item).await;

            let mut buf = Vec::<u8>::new();
            item.download(&mut buf, ..).await.unwrap();

            item.delete().await.unwrap();

            assert!(queue.items().await.unwrap().is_empty());

            // Check a few unlikely to change properties about the stream.
            let metadata = mp3_metadata::read_from_slice(&buf).unwrap();
            assert_eq!(metadata.duration.as_secs(), 5);
            let frame = metadata.frames.first().unwrap();
            assert_eq!(frame.layer, mp3_metadata::Layer::Layer3);
            assert_eq!(frame.chan_type, mp3_metadata::ChannelType::SingleChannel);
        }

        #[plex_api_test_helper::online_test_claimed_server]
        async fn queue_direct_play(
            #[future]
            #[with("Generic".to_owned())]
            server: Server,
        ) {
            let queue = server.download_queue().await.unwrap();

            let track: Track = server.item_by_id("158").await.unwrap().try_into().unwrap();
            assert_eq!(track.title(), "Try It Out (Neon mix)");

            let media = &track.media()[0];
            let part = &media.parts()[0];

            let mut item = part
                .queue_download(
                    // Here we ask to transcode into a format the music is already
                    // in so the server denies the request.
                    MusicTranscodeOptions {
                        bitrate: 200000000,
                        containers: vec![ContainerFormat::Aac],
                        codecs: vec![AudioCodec::Aac],
                        ..Default::default()
                    },
                    Some(&queue),
                )
                .await
                .unwrap();

            assert_eq!(item.queue(), queue);

            assert!(matches!(item.status(), QueueItemStatus::Deciding));

            wait_for_transcode_start(&mut item).await;

            assert!(!item.is_transcode());

            assert_eq!(item.status(), QueueItemStatus::Available);

            let mut buf = Vec::<u8>::new();
            item.download(&mut buf, ..).await.unwrap();

            item.delete().await.unwrap();

            assert!(queue.items().await.unwrap().is_empty());

            assert_eq!(buf.len(), part.len().unwrap() as usize);
        }
    }
}