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
use super::GenericCtl;
use crate::{
    error::try_map_opus_error, ffi, Application, Bandwidth, Bitrate, Channels, ErrorCode, Result,
    SampleRate, Signal, TryFrom,
};

/// `Encoder` calls to Opus and offers method to encode and issue
/// requests to Opus.
#[derive(Debug)]
pub struct Encoder {
    pointer: *mut ffi::OpusEncoder,
    channels: Channels,
}

/// The Opus encoder can be sent between threads unless the Opus library
/// has been compiled with `NONTHREADSAFE_PSEUDOSTACK` to disallow decoding in
/// parallel. This is not the case as embedded platforms are not supported
/// by the Audiopus Sys binding.
unsafe impl Send for Encoder {}

impl GenericCtl for Encoder {
    /// Gets the final state of the codec's entropy coder.
    ///
    /// This is used for testing purposes. The encoder state should
    /// be identical after coding a payload, assuming no data corruption or
    /// software bugs.
    fn final_range(&self) -> Result<u32> {
        self.encoder_ctl_request(ffi::OPUS_GET_FINAL_RANGE_REQUEST)
            .map(|v| v as u32)
    }

    /// Gets the encoder's configured phase inversion status.
    fn phase_inversion_disabled(&self) -> Result<bool> {
        self.encoder_ctl_request(ffi::OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST)
            .map(|b| b == 1)
    }

    /// If set to `true`, disables the use of phase inversion for intensity
    /// stereo, improving the quality of mono downmixes, but slightly reducing
    /// normal stereo quality.
    ///
    /// Disabling phase inversion in the decoder does not comply with RFC 6716,
    /// although it does not cause any interoperability issue and is expected
    /// to become part of the Opus standard once RFC 6716 is updated by
    /// draft-ietf-codec-opus-update.
    fn set_phase_inversion_disabled(&mut self, disabled: bool) -> Result<()> {
        let disable_phase_inversion = if disabled { 1 } else { 0 };
        self.set_encoder_ctl_request(
            ffi::OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST,
            disable_phase_inversion,
        )
        .map(|_| ())
    }

    /// Gets the sampling rate the encoder or decoder was initialized with.
    ///
    /// This simply returns the Fs value passed to [`Encoder::new`].
    ///
    /// [`Encoder::new`]: struct.Encoder.html#method.new
    fn sample_rate(&self) -> Result<SampleRate> {
        self.encoder_ctl_request(ffi::OPUS_GET_SAMPLE_RATE_REQUEST)
            .and_then(SampleRate::try_from)
    }

    /// Resets the codec state to be equivalent to a freshly initialized state.
    ///
    /// This should be called when switching streams in order to prevent the
    /// back to back decoding from giving different results from one at a
    /// time decoding.
    fn reset_state(&mut self) -> Result<()> {
        self.encoder_ctl_request(ffi::OPUS_RESET_STATE).map(|_| ())
    }
}

impl Encoder {
    /// Creates a new Opus encoder.
    ///
    /// **Warning**:
    /// If `channels` is set to [`Channels::Auto`] the function will
    /// return [`BadArgument`].
    ///
    /// [`Channels::Auto`]: ../enum.Channels.html#variant.Auto
    /// [`BadArgument`]: ../error/enum.ErrorCode.html#variant.BadArgument
    pub fn new(sample_rate: SampleRate, channels: Channels, mode: Application) -> Result<Encoder> {
        let mut opus_code = 0;

        let pointer = unsafe {
            ffi::opus_encoder_create(
                sample_rate as i32,
                channels as i32,
                mode as i32,
                &mut opus_code,
            )
        };

        if opus_code == ffi::OPUS_OK || !pointer.is_null() {
            return Ok(Encoder { pointer, channels });
        }

        Err(ErrorCode::from(opus_code))?
    }

    /// Issues a CTL get-`request` to Opus.
    /// If Opus returns a negative value it indicates an error.
    ///
    /// **Info**:
    /// As [`Encoder`]'s methods cover all possible CTLs, it is recommended
    /// to use them instead.
    ///
    /// [`Encoder`]: struct.Encoder.html
    pub fn encoder_ctl_request(&self, request: i32) -> Result<i32> {
        let mut value = 0;

        let ffi_result = unsafe { ffi::opus_encoder_ctl(self.pointer, request, &mut value) };
        try_map_opus_error(ffi_result)?;

        Ok(value)
    }

    /// Issues a CTL set-`request` to Opus and sets the `Encoder`'s setting to
    /// `value` based on sent `request`.
    /// If Opus returns a negative value it indicates an error.
    ///
    /// **Info**:
    /// As [`Encoder`]'s methods cover all possible CTLs, it is recommended
    /// to use them instead.
    ///
    /// [`Encoder`]: struct.Encoder.html
    pub fn set_encoder_ctl_request(&mut self, request: i32, value: i32) -> Result<()> {
        try_map_opus_error(unsafe { ffi::opus_encoder_ctl(self.pointer, request, value) })?;

        Ok(())
    }

    /// Encodes an Opus frame.
    ///
    /// The `input` signal (interleaved if 2 channels) will be encoded into the
    /// `output` payload and on success returns the length of the
    /// encoded packet.
    pub fn encode(&self, input: &[i16], output: &mut [u8]) -> Result<usize> {
        try_map_opus_error(unsafe {
            ffi::opus_encode(
                self.pointer,
                input.as_ptr(),
                input.len() as i32 / self.channels as i32,
                output.as_mut_ptr(),
                output.len() as i32,
            )
        })
        .map(|n| n as usize)
    }

    /// Encodes an Opus frame from floating point input.
    ///
    /// The `input` signal (interleaved if 2 channels) will be encoded into the
    /// `output` payload and on success, returns the length of the
    /// encoded packet.
    pub fn encode_float(&self, input: &[f32], output: &mut [u8]) -> Result<usize> {
        try_map_opus_error(unsafe {
            ffi::opus_encode_float(
                self.pointer,
                input.as_ptr(),
                input.len() as i32 / self.channels as i32,
                output.as_mut_ptr(),
                output.len() as i32,
            )
        })
        .map(|n| n as usize)
    }

    /// Gets the encoder's complexity configuration.
    pub fn complexity(&self) -> Result<u8> {
        self.encoder_ctl_request(ffi::OPUS_GET_COMPLEXITY_REQUEST)
            .map(|v| v as u8)
    }

    /// Configures the encoder's computational complexity.
    ///
    /// **Warning**:
    /// If `complexity` exceeds 10, [`BadArgument`] will be returned.
    ///
    /// [`BadArgument`]: ../error/enum.ErrorCode.html#variant.BadArgument.html
    pub fn set_complexity(&mut self, complexity: u8) -> Result<()> {
        self.set_encoder_ctl_request(ffi::OPUS_SET_COMPLEXITY_REQUEST, i32::from(complexity))
    }

    /// Gets the encoder's configured application.
    pub fn application(&self) -> Result<Application> {
        self.encoder_ctl_request(ffi::OPUS_GET_APPLICATION_REQUEST)
            .and_then(Application::try_from)
    }

    /// Configures the encoder's intended application.
    ///
    /// The initial value is a mandatory argument in the [`new`]-function.
    ///
    /// [`new`]: struct.Encoder.html#method.new
    pub fn set_application(&mut self, application: Application) -> Result<()> {
        self.set_encoder_ctl_request(ffi::OPUS_SET_APPLICATION_REQUEST, application as i32)
            .map(|_| ())
    }

    /// Configures the bitrate in the encoder.
    ///
    /// Rates from 500 to 512000 bits per second are meaningful,
    /// as well as the special values [`Bitrate::Auto`] and [`Bitrate::Max`].
    /// [`Bitrate::Max`] can be used to cause the codec to use
    /// as much rate as it can, which is useful for controlling the rate by
    /// adjusting the output buffer size.
    pub fn set_bitrate(&mut self, bitrate: Bitrate) -> Result<()> {
        self.set_encoder_ctl_request(ffi::OPUS_SET_BITRATE_REQUEST, bitrate.into())?;

        Ok(())
    }

    /// Gets the encoder's configured bandpass.
    pub fn bitrate(&self) -> Result<Bitrate> {
        self.encoder_ctl_request(ffi::OPUS_GET_BITRATE_REQUEST)
            .and_then(Bitrate::try_from)
    }

    /// Enables variable bitrate (VBR) in the encoder.
    ///
    /// The configured bitrate may not be met exactly because frames must be an
    /// integer number of bytes in length.
    ///
    /// **Warning**:
    /// Only the MDCT mode of Opus currently heeds the constraint.
    /// Speech mode ignores it completely,
    /// hybrid mode may fail to obey it if the LPC layer uses more bitrate
    /// than the constraint would have permitted.
    pub fn enable_vbr_constraint(&mut self) -> Result<()> {
        self.set_vbr_constraint(true)
    }

    /// Disables variable bitrate (VBR) in the encoder.
    ///
    /// The configured bitrate may not be met exactly because frames must be an
    /// integer number of bytes in length.
    ///
    /// **Warning**:
    /// Only the MDCT mode of Opus currently heeds the constraint.
    /// Speech mode ignores it completely,
    /// hybrid mode may fail to obey it if the LPC layer uses more bitrate
    /// than the constraint would have permitted.
    pub fn disable_vbr_constraint(&mut self) -> Result<()> {
        self.set_vbr_constraint(false)
    }

    /// Sets variable bitrate (VBR) in the encoder.
    ///
    /// The configured bitrate may not be met exactly because frames must be an
    /// integer number of bytes in length.
    ///
    /// **Warning**:
    /// Only the MDCT mode of Opus currently heeds the constraint.
    /// Speech mode ignores it completely,
    /// hybrid mode may fail to obey it if the LPC layer uses more bitrate
    /// than the constraint would have permitted.
    pub fn set_vbr_constraint(&mut self, enable: bool) -> Result<()> {
        let if_vbr_shall_be_enabled = if enable { 1 } else { 0 };

        self.set_encoder_ctl_request(
            ffi::OPUS_SET_VBR_CONSTRAINT_REQUEST,
            if_vbr_shall_be_enabled,
        )
        .map(|_| ())
    }

    /// Determine if constrained VBR is enabled in the encoder.
    pub fn vbr_constraint(&self) -> Result<bool> {
        self.encoder_ctl_request(ffi::OPUS_GET_VBR_CONSTRAINT_REQUEST)
            .map(|b| b == 1)
    }

    /// Enables variable bitrate (VBR) in the encoder.
    ///
    /// The configured bitrate may not be met exactly because frames must be an
    /// integer number of bytes in length.
    pub fn enable_vbr(&mut self) -> Result<()> {
        self.set_vbr(true)
    }

    /// Disables variable bitrate (VBR) in the encoder.
    ///
    /// The configured bitrate may not be met exactly because frames must be an
    /// integer number of bytes in length.
    pub fn disable_vbr(&mut self) -> Result<()> {
        self.set_vbr(false)
    }

    /// Sets variable bitrate (VBR) in the encoder.
    ///
    /// The configured bitrate may not be met exactly because frames must be an
    /// integer number of bytes in length.
    pub fn set_vbr(&mut self, enable: bool) -> Result<()> {
        let if_vbr_shall_be_enabled = if enable { 1 } else { 0 };

        self.set_encoder_ctl_request(ffi::OPUS_SET_VBR_REQUEST, if_vbr_shall_be_enabled)
            .map(|_| ())
    }

    /// Determine if variable bitrate (VBR) is enabled in the encoder.
    pub fn vbr(&self) -> Result<bool> {
        self.encoder_ctl_request(ffi::OPUS_GET_VBR_REQUEST)
            .map(|b| b == 1)
    }

    /// Configures the encoder's use of inband forward error correction (FEC).
    pub fn set_inband_fec(&mut self, enable: bool) -> Result<()> {
        let if_inband_fec_shall_be_enabled = if enable { 1 } else { 0 };

        self.set_encoder_ctl_request(
            ffi::OPUS_SET_INBAND_FEC_REQUEST,
            if_inband_fec_shall_be_enabled,
        )
        .map(|_| ())
    }

    /// Enables the encoder's use of inband forward error correction (FEC).
    pub fn enable_inband_fec(&mut self) -> Result<()> {
        self.set_inband_fec(true)
    }

    /// Disables the encoder's use of inband forward error correction (FEC).
    pub fn disable_inband_fec(&mut self) -> Result<()> {
        self.set_inband_fec(false)
    }

    /// Gets encoder's configured use of inband forward error correction.
    pub fn inband_fec(&self) -> Result<bool> {
        self.encoder_ctl_request(ffi::OPUS_GET_INBAND_FEC_REQUEST)
            .map(|n| n == 1)
    }

    /// Gets the encoder's configured packet loss percentage.
    pub fn packet_loss_perc(&self) -> Result<u8> {
        self.encoder_ctl_request(ffi::OPUS_GET_PACKET_LOSS_PERC_REQUEST)
            .map(|n| n as u8)
    }

    /// Higher values trigger progressively more loss resistant behavior in the
    /// encoder at the expense of quality at a given bitrate in the absence of
    /// packet loss, but greater quality under loss.
    ///
    /// Configures the encoder's expected packet loss percentage.
    pub fn set_packet_loss_perc(&mut self, percentage: u8) -> Result<()> {
        self.set_encoder_ctl_request(
            ffi::OPUS_SET_PACKET_LOSS_PERC_REQUEST,
            i32::from(percentage),
        )
        .map(|_| ())
    }

    /// Gets the total samples of delay added by the entire codec.
    ///
    /// This can be queried by the encoder and then the provided number of
    /// samples can be skipped on from the start of the decoder's output to
    /// provide time aligned input and output. From the perspective of a
    /// decoding application the real data begins this many samples late.
    ///
    /// The decoder contribution to this delay is identical for all decoders,
    /// but the encoder portion of the delay may vary from implementation to
    /// implementation, version to version, or even depend on the encoder's
    /// initial configuration.
    /// Applications needing delay compensation should call this method
    /// rather than hard-coding a value.
    pub fn lookahead(&self) -> Result<u32> {
        self.encoder_ctl_request(ffi::OPUS_GET_LOOKAHEAD_REQUEST)
            .map(|n| n as u32)
    }

    /// Configures mono/stereo forcing in the encoder.
    ///
    /// This can force the encoder to produce packets encoded as either
    /// mono or stereo, regardless of the format of the input audio.
    /// This is useful when the caller knows that the input signal is
    /// currently a mono source embedded in a stereo stream.
    pub fn set_force_channels(&mut self, channels: Channels) -> Result<()> {
        self.set_encoder_ctl_request(ffi::OPUS_SET_FORCE_CHANNELS_REQUEST, channels as i32)
            .map(|_| ())
    }

    /// Gets the encoder's forced channel configuration.
    pub fn force_channels(&self) -> Result<Channels> {
        self.encoder_ctl_request(ffi::OPUS_GET_FORCE_CHANNELS_REQUEST)
            .and_then(Channels::try_from)
    }

    /// Gets the encoder's configured maximum allowed bandpass.
    pub fn max_bandwidth(&self) -> Result<Bandwidth> {
        self.encoder_ctl_request(ffi::OPUS_GET_MAX_BANDWIDTH_REQUEST)
            .and_then(Bandwidth::try_from)
    }

    /// Configures the maximum bandpass that the encoder will select automatically.
    ///
    /// Applications should normally use this instead of [`set_bandwidth`]
    /// (leaving that set to the default, [`Bandwidth::Auto`]).
    ///
    /// This allows the application to set an upper bound based on the type of
    /// input it is providing, but still gives the encoder the freedom to reduce
    /// the bandpass when the bitrate becomes too low,
    /// for better overall quality.
    ///
    /// **Warning**:
    /// [`Bandwidth::Auto`] will return [`BadArgument`] as it is not
    /// accepted by Opus as `bandwidth` value.
    ///
    /// [`set_bandwidth`]: struct.Encoder.html#method.set_bandwidth.html
    /// [`Bandwidth::Auto`]: ../enum.Bandwidth.html#variant.Auto
    /// [`BadArgument`]: ../error/enum.ErrorCode.html#variant.BadArgument.html
    pub fn set_max_bandwidth(&mut self, bandwidth: Bandwidth) -> Result<()> {
        self.set_encoder_ctl_request(ffi::OPUS_SET_MAX_BANDWIDTH_REQUEST, bandwidth as i32)
    }

    /// Gets the encoder's configured prediction status.
    pub fn prediction_disabled(&self) -> Result<bool> {
        self.encoder_ctl_request(ffi::OPUS_GET_PREDICTION_DISABLED_REQUEST)
            .map(|n| n == 1)
    }

    /// If set `precition_disabled` to `true`, disables almost all use of
    /// prediction, making frames almost completely independent.
    ///
    /// This reduces quality.
    pub fn set_prediction_disabled(&mut self, precition_disabled: bool) -> Result<()> {
        let precition_disabled = if precition_disabled { 1 } else { 0 };

        self.set_encoder_ctl_request(
            ffi::OPUS_SET_PREDICTION_DISABLED_REQUEST,
            precition_disabled,
        )
        .map(|_| ())
    }

    /// Gets the encoder's configured signal type.
    pub fn signal(&self) -> Result<Signal> {
        self.encoder_ctl_request(ffi::OPUS_GET_SIGNAL_REQUEST)
            .and_then(Signal::try_from)
    }

    /// Configures the type of signal being encoded.
    ///
    /// This is a hint which helps the encoder's mode selection.
    pub fn set_signal(&mut self, signal: Signal) -> Result<()> {
        self.set_encoder_ctl_request(ffi::OPUS_SET_SIGNAL_REQUEST, signal as i32)
            .map(|_| ())
    }

    /// Gets the encoder's configured bandpass.
    pub fn bandwidth(&self) -> Result<Bandwidth> {
        self.encoder_ctl_request(ffi::OPUS_GET_BANDWIDTH_REQUEST)
            .and_then(Bandwidth::try_from)
    }

    /// Sets the encoder's bandpass to a specific value.
    ///
    /// This prevents the encoder from automatically selecting the bandpass
    /// based on the available bitrate.
    /// If an application knows the bandpass of the input audio it is providing,
    /// it should normally use [`set_max_bandwidth`] instead,
    /// which still gives the encoder the freedom to reduce the bandpass when
    /// the bitrate becomes too low, for better overall quality.
    ///
    /// [`set_max_bandwidth::Max`]: struct.Encoder.html#method.set_max_bandwidth
    pub fn set_bandwidth(&mut self, bandwidth: Bandwidth) -> Result<()> {
        self.set_encoder_ctl_request(ffi::OPUS_SET_BANDWIDTH_REQUEST, bandwidth as i32)
    }
}

impl Drop for Encoder {
    /// We have to ensure that the resource our wrapping Opus-struct is pointing
    /// to is deallocated properly.
    fn drop(&mut self) {
        unsafe { ffi::opus_encoder_destroy(self.pointer) }
    }
}

#[cfg(test)]
mod tests {
    use super::Encoder;
    use crate::{Application, Bandwidth, Bitrate, Channels, Error, ErrorCode, SampleRate, Signal};
    use matches::assert_matches;

    #[test]
    fn set_get_inband_fec() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        assert_matches!(encoder.inband_fec(), Ok(false));

        encoder
            .set_inband_fec(true)
            .expect("Could not set inband FEC to true.");
        assert_matches!(encoder.inband_fec(), Ok(true));

        encoder
            .set_inband_fec(false)
            .expect("Could not set inband FEC to false.");
        assert_matches!(encoder.inband_fec(), Ok(false));

        encoder
            .enable_inband_fec()
            .expect("Could not set inband FEC to true.");
        assert_matches!(encoder.inband_fec(), Ok(true));

        encoder
            .disable_inband_fec()
            .expect("Could not set inband FEC to false.");
        assert_matches!(encoder.inband_fec(), Ok(false));
    }

    #[test]
    fn set_get_vbr_constraint() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        assert_matches!(encoder.vbr_constraint(), Ok(true));

        encoder
            .set_vbr_constraint(false)
            .expect("Could not disable VBR constraint.");
        assert_matches!(encoder.vbr_constraint(), Ok(false));

        encoder
            .set_vbr_constraint(true)
            .expect("Could not enable VBR constraint.");
        assert_matches!(encoder.vbr_constraint(), Ok(true));

        encoder
            .enable_vbr_constraint()
            .expect("Could not enable VBR constraint.");
        assert_matches!(encoder.vbr_constraint(), Ok(true));

        encoder
            .disable_vbr_constraint()
            .expect("Could not disable VBR constraint.");
        assert_matches!(encoder.vbr_constraint(), Ok(false));
    }

    #[test]
    fn set_get_vbr() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        assert_matches!(encoder.vbr(), Ok(true));

        encoder.set_vbr(false).expect("Could not disable VBR.");
        assert_matches!(encoder.vbr(), Ok(false));

        encoder.set_vbr(true).expect("Could not enable VBR.");
        assert_matches!(encoder.vbr(), Ok(true));

        encoder.enable_vbr().expect("Could not enable VBR.");
        assert_matches!(encoder.vbr(), Ok(true));

        encoder.disable_vbr().expect("Could not disable VBR.");
        assert_matches!(encoder.vbr(), Ok(false));
    }

    #[test]
    fn set_get_packet_loss_perc() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        assert_matches!(encoder.packet_loss_perc(), Ok(0));

        encoder
            .set_packet_loss_perc(10)
            .expect("Could not set packet loss perc to 10%.");
        assert_matches!(encoder.packet_loss_perc(), Ok(10));

        encoder
            .set_packet_loss_perc(100)
            .expect("Could not set packet loss perc to 100%.");
        assert_matches!(encoder.packet_loss_perc(), Ok(100));

        assert_matches!(
            encoder.set_packet_loss_perc(101),
            Err(Error::Opus(ErrorCode::BadArgument))
        );
        assert_matches!(encoder.packet_loss_perc(), Ok(100));
    }

    #[test]
    fn set_get_force_channels() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        assert_matches!(encoder.force_channels(), Ok(Channels::Auto));

        encoder
            .set_force_channels(Channels::Mono)
            .expect("Could not set force channels to mono.");
        assert_matches!(encoder.force_channels(), Ok(Channels::Mono));

        encoder
            .set_force_channels(Channels::Stereo)
            .expect("Could not set force channels to stereo.");
        assert_matches!(encoder.force_channels(), Ok(Channels::Stereo));

        encoder
            .set_force_channels(Channels::Auto)
            .expect("Could not set force channels to mono.");
        assert_matches!(encoder.force_channels(), Ok(Channels::Auto));
    }

    #[test]
    fn set_get_prediction_disabled() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        assert_matches!(encoder.prediction_disabled(), Ok(false));

        encoder
            .set_prediction_disabled(true)
            .expect("Could not set prediction disabled to true.");
        assert_matches!(encoder.prediction_disabled(), Ok(true));

        encoder
            .set_prediction_disabled(false)
            .expect("Could not set prediction disabled to false.");
        assert_matches!(encoder.prediction_disabled(), Ok(false));
    }

    #[test]
    fn set_get_signal() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        assert_matches!(encoder.signal(), Ok(Signal::Auto));

        encoder
            .set_signal(Signal::Music)
            .expect("Could not set signal to music.");
        assert_matches!(encoder.signal(), Ok(Signal::Music));

        encoder
            .set_signal(Signal::Voice)
            .expect("Could not set signal to voice.");
        assert_matches!(encoder.signal(), Ok(Signal::Voice));

        encoder
            .set_signal(Signal::Auto)
            .expect("Could not set signal back to.");
        assert_matches!(encoder.signal(), Ok(Signal::Auto));
    }

    #[test]
    fn encoder_construction() {
        assert_matches!(
            Encoder::new(SampleRate::Hz48000, Channels::Auto, Application::Audio),
            Err(Error::Opus(ErrorCode::BadArgument))
        );

        Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio)
            .expect("Could not create stereo audio encoder");

        Encoder::new(SampleRate::Hz48000, Channels::Mono, Application::Audio)
            .expect("Could not create mono audio encoder");
    }

    #[test]
    fn encoding() {
        let stereo_encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        // 48000Hz * 1 channel * 20 ms / 1000
        const STEREO_20MS: usize = 48000 * 2 * 20 / 1000;
        let input = [0_i16; STEREO_20MS];
        let mut output = [0; 256];

        let len = stereo_encoder.encode(&input, &mut output).unwrap();
        assert_eq!(&output[..len], &[252, 255, 254]);

        let mono_encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Mono, Application::Audio).unwrap();

        // 48000Hz * 1 channel * 20 ms / 1000
        const MONO_20MS: usize = 48000 * 1 * 20 / 1000;
        let input = [0_i16; MONO_20MS];
        let mut output = [0; 256];

        let len = mono_encoder.encode(&input, &mut output).unwrap();
        assert_eq!(&output[..len], &[248, 255, 254]);
    }

    #[test]
    fn set_max_bandwidth() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        assert_matches!(encoder.max_bandwidth(), Ok(Bandwidth::Fullband));

        let bandwidth_to_set = Bandwidth::Narrowband;

        encoder.set_max_bandwidth(bandwidth_to_set).unwrap();
        let bandwidth_got = encoder.max_bandwidth().unwrap();

        assert_eq!(bandwidth_to_set, bandwidth_got);
    }

    #[test]
    fn get_set_complexity() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, Application::Audio).unwrap();

        encoder
            .set_complexity(10)
            .expect("Could not set complexity to 10.");
        assert_matches!(encoder.complexity(), Ok(10));

        encoder
            .set_complexity(0)
            .expect("Could not set complexity to 0.");
        assert_matches!(encoder.complexity(), Ok(0));

        assert_matches!(
            encoder.set_complexity(11),
            Err(Error::Opus(ErrorCode::BadArgument))
        );
    }

    #[test]
    fn set_get_application() {
        let application_to_set = Application::Audio;
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Stereo, application_to_set).unwrap();
        let current_application = encoder.application().unwrap();
        assert_eq!(current_application, application_to_set);

        let application_to_set = Application::Voip;
        encoder.set_application(application_to_set).unwrap();
        let current_application = encoder.application().unwrap();
        assert_eq!(current_application, application_to_set);

        let application_to_set = Application::LowDelay;
        encoder.set_application(application_to_set).unwrap();
        let current_application = encoder.application().unwrap();
        assert_eq!(current_application, application_to_set);

        let application_to_set = Application::Audio;
        encoder.set_application(application_to_set).unwrap();
        let current_application = encoder.application().unwrap();
        assert_eq!(current_application, application_to_set);
    }

    #[test]
    fn set_get_bitrate() {
        let mut encoder =
            Encoder::new(SampleRate::Hz48000, Channels::Mono, Application::Audio).unwrap();

        let bitrate = 512000;

        encoder
            .set_bitrate(Bitrate::BitsPerSecond(bitrate))
            .expect("Could not set bitrate to 512000.");

        assert_matches!(encoder.bitrate(), _bitrate);
    }
}