Ejemplo n.º 1
0
    def roundtrip_video(self, codec, width, height):
        """
        Round-trip a VideoFrame through encoder then decoder.
        """
        encoder = get_encoder(codec)
        decoder = get_decoder(codec)

        for timestamp in range(0, 90000, 3000):
            # encode
            frame = VideoFrame(width=width, height=height)
            frame.pts = timestamp
            frame.time_base = VIDEO_TIME_BASE
            packages, timestamp = encoder.encode(frame)

            # depacketize
            data = b''
            for package in packages:
                data += depayload(codec, package)

            # decode
            frames = decoder.decode(JitterFrame(data=data, timestamp=timestamp))
            self.assertEqual(len(frames), 1)
            self.assertEqual(frames[0].width, width)
            self.assertEqual(frames[0].height, height)
            self.assertEqual(frames[0].pts, timestamp)
            self.assertEqual(frames[0].time_base, VIDEO_TIME_BASE)
Ejemplo n.º 2
0
    def roundtrip_audio(self, codec, output_layout, output_sample_rate, drop=[]):
        """
        Round-trip an AudioFrame through encoder then decoder.
        """
        encoder = get_encoder(codec)
        decoder = get_decoder(codec)

        input_frames = self.create_audio_frames(
            layout="mono", sample_rate=8000, count=10
        )

        output_sample_count = int(output_sample_rate * AUDIO_PTIME)

        for i, frame in enumerate(input_frames):
            # encode
            packages, timestamp = encoder.encode(frame)

            if i not in drop:
                # depacketize
                data = b""
                for package in packages:
                    data += depayload(codec, package)

                # decode
                frames = decoder.decode(JitterFrame(data=data, timestamp=timestamp))
                self.assertEqual(len(frames), 1)
                self.assertEqual(frames[0].format.name, "s16")
                self.assertEqual(frames[0].layout.name, output_layout)
                self.assertEqual(frames[0].samples, output_sample_rate * AUDIO_PTIME)
                self.assertEqual(frames[0].sample_rate, output_sample_rate)
                self.assertEqual(frames[0].pts, i * output_sample_count)
                self.assertEqual(
                    frames[0].time_base, fractions.Fraction(1, output_sample_rate)
                )
Ejemplo n.º 3
0
    def roundtrip_video(self, codec, width, height, time_base=VIDEO_TIME_BASE):
        """
        Round-trip a VideoFrame through encoder then decoder.
        """
        encoder = get_encoder(codec)
        decoder = get_decoder(codec)

        input_frames = self.create_video_frames(
            width=width, height=height, count=30, time_base=time_base
        )
        for i, frame in enumerate(input_frames):
            # encode
            packages, timestamp = encoder.encode(frame)

            # depacketize
            data = b""
            for package in packages:
                data += depayload(codec, package)

            # decode
            frames = decoder.decode(JitterFrame(data=data, timestamp=timestamp))
            self.assertEqual(len(frames), 1)
            self.assertEqual(frames[0].width, frame.width)
            self.assertEqual(frames[0].height, frame.height)
            self.assertEqual(frames[0].pts, i * 3000)
            self.assertEqual(frames[0].time_base, VIDEO_TIME_BASE)
Ejemplo n.º 4
0
    def roundtrip_audio(self, codec, output_channels, output_sample_rate):
        """
        Round-trip an AudioFrame through encoder then decoder.
        """
        encoder = get_encoder(codec)
        decoder = get_decoder(codec)

        # encode
        frame = AudioFrame(channels=1,
                           data=b'\x00\x00' * 160,
                           sample_rate=8000,
                           timestamp=0)
        self.assertEqual(len(frame.data), 320)
        data = encoder.encode(frame)

        # decode
        frames = decoder.decode(JitterFrame(data=data, timestamp=0))
        self.assertEqual(len(frames), 1)
        self.assertEqual(
            len(frames[0].data),
            output_sample_rate * AUDIO_PTIME * output_channels * 2)
        self.assertEqual(frames[0].channels, output_channels)
        self.assertEqual(frames[0].sample_rate, output_sample_rate)
        self.assertEqual(frames[0].sample_width, 2)
        self.assertEqual(frames[0].timestamp, 0)
Ejemplo n.º 5
0
    def test_decoder(self):
        decoder = get_decoder(H264_CODEC)
        self.assertTrue(isinstance(decoder, H264Decoder))

        # decode junk
        with redirect_stderr(io.StringIO()):
            frames = decoder.decode(JitterFrame(data=b"123", timestamp=0))
        self.assertEqual(frames, [])
Ejemplo n.º 6
0
    def test_decoder(self):
        decoder = get_decoder(OPUS_CODEC)
        self.assertTrue(isinstance(decoder, OpusDecoder))

        frames = decoder.decode(JitterFrame(data=b'\xfc\xff\xfe', timestamp=0))
        self.assertEqual(len(frames), 1)
        frame = frames[0]
        self.assertEqual(frame.channels, 2)
        self.assertEqual(frame.data, b'\x00' * 4 * 960)
        self.assertEqual(frame.sample_rate, 48000)
        self.assertEqual(frame.timestamp, 0)
Ejemplo n.º 7
0
    def test_decoder(self):
        decoder = get_decoder(PCMU_CODEC)
        self.assertTrue(isinstance(decoder, PcmuDecoder))

        frames = decoder.decode(JitterFrame(data=b'\xff' * 160, timestamp=0))
        self.assertEqual(len(frames), 1)
        frame = frames[0]
        self.assertEqual(frame.channels, 1)
        self.assertEqual(frame.data, b'\x00\x00' * 160)
        self.assertEqual(frame.sample_rate, 8000)
        self.assertEqual(frame.timestamp, 0)
Ejemplo n.º 8
0
    def test_decoder(self):
        decoder = get_decoder(PCMA_CODEC)
        self.assertTrue(isinstance(decoder, PcmaDecoder))

        frames = decoder.decode(JitterFrame(data=b'\xd5' * 160, timestamp=0))
        self.assertEqual(len(frames), 1)
        frame = frames[0]
        self.assertEqual(frame.channels, 1)
        self.assertEqual(frame.data, b'\x08\x00' * 160)
        self.assertEqual(frame.sample_rate, 8000)
        self.assertEqual(frame.pts, 0)
        self.assertEqual(frame.time_base, fractions.Fraction(1, 8000))
Ejemplo n.º 9
0
    def test_decoder(self):
        decoder = get_decoder(OPUS_CODEC)
        self.assertTrue(isinstance(decoder, OpusDecoder))

        frames = decoder.decode(JitterFrame(data=b'\xfc\xff\xfe', timestamp=0))
        self.assertEqual(len(frames), 1)
        frame = frames[0]
        self.assertEqual(frame.format.name, 's16')
        self.assertEqual(frame.layout.name, 'stereo')
        self.assertEqual(bytes(frame.planes[0]), b'\x00' * 4 * 960)
        self.assertEqual(frame.sample_rate, 48000)
        self.assertEqual(frame.pts, 0)
        self.assertEqual(frame.time_base, fractions.Fraction(1, 48000))
Ejemplo n.º 10
0
    def test_decoder(self):
        decoder = get_decoder(PCMU_CODEC)
        self.assertTrue(isinstance(decoder, PcmuDecoder))

        frames = decoder.decode(JitterFrame(data=b"\xff" * 160, timestamp=0))
        self.assertEqual(len(frames), 1)
        frame = frames[0]
        self.assertEqual(frame.format.name, "s16")
        self.assertEqual(frame.layout.name, "mono")
        self.assertEqual(bytes(frame.planes[0]), b"\x00\x00" * 160)
        self.assertEqual(frame.pts, 0)
        self.assertEqual(frame.samples, 160)
        self.assertEqual(frame.sample_rate, 8000)
        self.assertEqual(frame.time_base, fractions.Fraction(1, 8000))
Ejemplo n.º 11
0
    def test_decoder(self):
        decoder = get_decoder(PCMA_CODEC)
        self.assertTrue(isinstance(decoder, PcmaDecoder))

        frames = decoder.decode(JitterFrame(data=b'\xd5' * 160, timestamp=0))
        self.assertEqual(len(frames), 1)
        frame = frames[0]
        self.assertEqual(frame.format.name, 's16')
        self.assertEqual(frame.layout.name, 'mono')
        self.assertEqual(bytes(frame.planes[0]), b'\x08\x00' * 160)
        self.assertEqual(frame.pts, 0)
        self.assertEqual(frame.samples, 160)
        self.assertEqual(frame.sample_rate, 8000)
        self.assertEqual(frame.time_base, fractions.Fraction(1, 8000))
Ejemplo n.º 12
0
    def test_decoder(self):
        decoder = get_decoder(PCMA_CODEC)
        self.assertTrue(isinstance(decoder, PcmaDecoder))

        frames = decoder.decode(JitterFrame(data=b"\xd5" * 160, timestamp=0))
        self.assertEqual(len(frames), 1)
        frame = frames[0]
        self.assertEqual(frame.format.name, "s16")
        self.assertEqual(frame.layout.name, "mono")
        self.assertEqual(
            bytes(frame.planes[0]),
            (b"\x08\x00" if sys.byteorder == "little" else b"\x00\x08") * 160,
        )
        self.assertEqual(frame.pts, 0)
        self.assertEqual(frame.samples, 160)
        self.assertEqual(frame.sample_rate, 8000)
        self.assertEqual(frame.time_base, fractions.Fraction(1, 8000))
Ejemplo n.º 13
0
    def roundtrip_audio(self,
                        codec,
                        output_channels,
                        output_sample_rate,
                        drop=[]):
        """
        Round-trip an AudioFrame through encoder then decoder.
        """
        encoder = get_encoder(codec)
        decoder = get_decoder(codec)

        input_frames = self.create_audio_frames(channels=1,
                                                sample_rate=8000,
                                                count=10)

        output_sample_count = int(output_sample_rate * AUDIO_PTIME)

        for i, frame in enumerate(input_frames):
            # encode
            self.assertEqual(len(frame.data), 320)
            packages, timestamp = encoder.encode(frame)

            if i not in drop:
                # depacketize
                data = b''
                for package in packages:
                    data += depayload(codec, package)

                # decode
                frames = decoder.decode(
                    JitterFrame(data=data, timestamp=timestamp))
                self.assertEqual(len(frames), 1)
                self.assertEqual(
                    len(frames[0].data),
                    output_sample_rate * AUDIO_PTIME * output_channels * 2)
                self.assertEqual(frames[0].channels, output_channels)
                self.assertEqual(frames[0].sample_rate, output_sample_rate)
                self.assertEqual(frames[0].sample_width, 2)
                self.assertEqual(frames[0].pts, i * output_sample_count)
                self.assertEqual(frames[0].time_base,
                                 fractions.Fraction(1, output_sample_rate))