Skip to content

Commit

Permalink
feat: vp9 encoding by default
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuagruenstein committed Jun 2, 2024
1 parent fc4bb12 commit ca91ba4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
17 changes: 10 additions & 7 deletions examples/flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from vpx_rtp.codecs.vpx import (
VIDEO_CLOCK_RATE,
VIDEO_TIME_BASE,
VP8_CODEC,
Vp8Decoder,
Vp8Encoder,
VpxCodec,
vp8_depayload,
)
from vpx_rtp.jitterbuffer import JitterBuffer
Expand All @@ -23,6 +23,7 @@
DUCK_JPEG_PATH = Path(__file__).parent / "duck.jpg"

VIDEO_PTIME = 1 / 30 # 30fps
DROPPED_PACKET_PERCENTAGE = 0


def generate_flag_frames() -> list[VideoFrame]:
Expand Down Expand Up @@ -57,8 +58,10 @@ def generate_flag_frames() -> list[VideoFrame]:

DUCK_FLAG_FRAMES = generate_flag_frames()

video_encoder = Vp8Encoder()
video_decoder = Vp8Decoder()
codec = VpxCodec.VP9

video_encoder = Vp8Encoder(codec)
video_decoder = Vp8Decoder(codec)

pts_timestamp = 0
_ssrc = random32()
Expand All @@ -67,8 +70,8 @@ def generate_flag_frames() -> list[VideoFrame]:
_jitter_buffer = JitterBuffer(capacity=128, is_video=True)

received_frame_num = 0
max_simulated_time = 20
FORCE_KEYFRAME_EVERY_N = 30
max_simulated_time = 5
FORCE_KEYFRAME_EVERY_N = 100

script_start_time = time.perf_counter()
for send_frame_num in range(500):
Expand All @@ -94,7 +97,7 @@ def generate_flag_frames() -> list[VideoFrame]:
wire_packet_bytes = []
for i, payload in enumerate(_mid_encoding_video_packets):
outgoing_packet = RtpPacket(
payload_type=VP8_CODEC.payloadType,
payload_type=codec.value.payloadType,
sequence_number=sequence_number,
timestamp=timestamp,
)
Expand All @@ -120,7 +123,7 @@ def generate_flag_frames() -> list[VideoFrame]:
start = time.perf_counter()
received_video_frames: list[VideoFrame] = []
for incoming_packet_bytes in wire_packet_bytes:
if random.random() < 0.01:
if random.random() < DROPPED_PACKET_PERCENTAGE:
print("\n\n\nSIMULATED DROPPED PACKET\n\n\n")
continue
incoming_rtp_packet = RtpPacket.parse(incoming_packet_bytes)
Expand Down
33 changes: 27 additions & 6 deletions vpx_rtp/codecs/vpx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fractions
import multiprocessing
import random
from enum import Enum
from struct import pack, unpack_from
from typing import List, Tuple, Type, TypeVar, cast

Expand All @@ -27,6 +28,12 @@
mimeType="video/VP8", clockRate=VIDEO_CLOCK_RATE, payloadType=100
)

VP9_CODEC = RTCRtpCodecParameters(
mimeType="video/VP9",
clockRate=VP8_CODEC.clockRate,
payloadType=VP8_CODEC.payloadType,
)


def convert_timebase(
pts: int, from_base: fractions.Fraction, to_base: fractions.Fraction
Expand Down Expand Up @@ -182,12 +189,22 @@ def _vpx_assert(err: int) -> None:
raise Exception("libvpx error: " + reason.decode("utf8"))


class VpxCodec(Enum):
VP8 = VP8_CODEC
VP9 = VP9_CODEC


class Vp8Decoder:
def __init__(self) -> None:
def __init__(self, codec: VpxCodec = VpxCodec.VP9) -> None:
self.codec = ffi.new("vpx_codec_ctx_t *")
_vpx_assert(
lib.vpx_codec_dec_init(self.codec, lib.vpx_codec_vp8_dx(), ffi.NULL, 0)
)

match codec:
case VpxCodec.VP8:
dx = lib.vpx_codec_vp8_dx()
case VpxCodec.VP9:
dx = lib.vpx_codec_vp9_dx()

_vpx_assert(lib.vpx_codec_dec_init(self.codec, dx, ffi.NULL, 0))

ppcfg = ffi.new("vp8_postproc_cfg_t *")
ppcfg.post_proc_flag = lib.VP8_DEMACROBLOCK | lib.VP8_DEBLOCK
Expand Down Expand Up @@ -241,8 +258,12 @@ def decode(self, encoded_frame: JitterFrame) -> list[VideoFrame]:


class Vp8Encoder:
def __init__(self) -> None:
self.cx = lib.vpx_codec_vp8_cx()
def __init__(self, codec: VpxCodec = VpxCodec.VP9) -> None:
match codec:
case VpxCodec.VP8:
self.cx = lib.vpx_codec_vp8_cx()
case VpxCodec.VP9:
self.cx = lib.vpx_codec_vp9_cx()

self.cfg = ffi.new("vpx_codec_enc_cfg_t *")
lib.vpx_codec_enc_config_default(self.cx, self.cfg, 0)
Expand Down

0 comments on commit ca91ba4

Please sign in to comment.