"""Serves a throttled silent WAV that forces a second window to make its own mid-file media range request. The resource is a long silent WAV whose body is streamed slowly (throttled). The WAV header is sent immediately so a media element can read metadata right away, but the audio samples trickle out over time. A window that only wants metadata (preload="metadata") therefore caches just the beginning of the file. When a second, same-principal window clones that media resource and seeks close to the end, the bytes it needs have not been downloaded yet, so it must issue its own range request starting well past byte 0 (a non-first-partial 206). Opaque Response Blocking only allows such a request if the resource has already been recorded as media. This is what lets the test tell apart per-window and per-principal recording (bug 2057272). """ import re import struct import time from wptserve.utils import isomorphic_decode SAMPLE_RATE = 8000 BIT_DEPTH = 8 CHANNELS = 1 # Long enough that, while throttled, the tail of the file is nowhere near # downloaded by the time the second window seeks to it. DURATION_SECONDS = 300 # Bytes handed out per throttle tick, and the delay between ticks. CHUNK = SAMPLE_RATE THROTTLE_SECONDS = 0.5 def wav_header(data_size): block_align = int(BIT_DEPTH / 8) * CHANNELS byte_rate = SAMPLE_RATE * block_align header = b"" header += b"RIFF" header += struct.pack("