I am trying to send audio through RTP too. I can hear my voice but cannot play a WAV file by splitting it into several chunks. Interestingly, I can echo my voice and hear that clearly. I use the header of the received chunk for the output chunk. thank you for your help.
the below code is echoing and works,
packet_info = self.packet_queue.get(timeout=0.1)
# Extract packet info
addr = packet_info['addr']
payload = packet_info['payload']
payload_type = packet_info['payload_type']
timestamp = packet_info['timestamp']
is_silent = packet_info['is_silent']
counter = packet_info['counter']
# Create RTP header with carefully managed timestamp
echo_rtp_header = struct.pack(
"!BBHII",
(self.RTP_VERSION << 6) | 0,
payload_type,
self.seq_num,
timestamp, # Using our managed timestamp
self.RTP_SSRC
)
# Send appropriate packet based on silence state (not just current packet)
if not silence_state:
echo_packet = echo_rtp_header + payload
self.sock.sendto(echo_packet, addr)
but when playing wav file like below (of course this not the whole code) i hear noise, also attached wireshark screenshot:
def load_wav_file(self):
"""Load and preprocess WAV file"""
try:
# Path to the WAV file
audio_dir = os.path.join(settings.MEDIA_ROOT, 'uploads/reserved_audio')
os.makedirs(audio_dir, exist_ok=True)
wav_path = os.path.join(audio_dir, 'welcome_voice.wav')
# Check if file exists, if not, return silence
if not os.path.exists(wav_path):
return self.silence_payload_ulaw * 50 # 1 second of silence
with wave.open(wav_path, 'rb') as wav_file:
# Read the entire file
pcm_data = wav_file.readframes(wav_file.getnframes())
# Convert to mono and 8kHz if needed
if wav_file.getnchannels() == 2:
pcm_data = audioop.tomono(pcm_data, wav_file.getsampwidth(), 0.5, 0.5)
if wav_file.getsampwidth() != 2:
pcm_data = audioop.lin2lin(pcm_data, wav_file.getsampwidth(), 2)
if wav_file.getframerate() != 8000:
pcm_data, _ = audioop.ratecv(pcm_data, 2, 1, wav_file.getframerate(), 8000, None)
alaw_data = audioop.lin2alaw(pcm_data, 2)
return alaw_data
##------------------- new method for generating and sending rtp packets**
last_timestamp = 0
timestamp_increment = 160 # 20ms at 8kHz
# WAV playback position
audio_position = 0
max_position = max(1, len(self.wav_data) // self.packet_size)
while self.running:
try:
# Get packet from queue with timeout
packet_info = self.packet_queue.get(timeout=0.1)
addr = packet_info['addr']
timestamp = packet_info['timestamp']
payload_type = 0
# Ensure smooth timestamp progression
if timestamp > last_timestamp:
last_timestamp = timestamp
else:
last_timestamp += timestamp_increment
timestamp = last_timestamp
# Create RTP header
rtp_header = struct.pack(
"!BBHII",
(self.RTP_VERSION << 6), # Version 2, no padding, no extension, no CSRC
payload_type, # μ-law (8)
self.seq_num, # Sequence number
timestamp, # Timestamp
self.RTP_SSRC # SSRC identifier
)
# Get the next chunk of audio data
start_pos = (audio_position % max_position) * self.packet_size
# Extract the chunk
if start_pos + self.packet_size <= len(self.wav_data):
audio_chunk = self.wav_data[start_pos:start_pos + self.packet_size]
else:
# Handle end of file wrap-around
audio_chunk = self.wav_data[start_pos:]
# Pad with silence if needed
if len(audio_chunk) < self.packet_size:
padding = self.silence_payload_ulaw[:self.packet_size - len(audio_chunk)]
audio_chunk += padding
# Increment position
audio_position = (audio_position + 1) % max_position
# Create packet and send it
packet = rtp_header + audio_chunk
self.sock.sendto(packet, addr)
# Increment sequence number
self.seq_num = (self.seq_num + 1) % 65536
Asterisk CLI:
-- Called 0.0.0.0:6000/c(alaw)
-- UnicastRTP/0.0.0.0:6000-0x5ec7053ed440 answered
> Launching Stasis(python_app) on UnicastRTP/0.0.0.0:6000-0x5ec7053ed440
-- Channel PJSIP/myvoip_endpoint-0000001a joined 'simple_bridge' stasis-bridge <e9904607-d530-49f3-96f9-3ae192d8da3c>
-- Channel UnicastRTP/0.0.0.0:6000-0x5ec7053ed440 joined 'simple_bridge' stasis-bridge <e9904607-d530-49f3-96f9-3ae192d8da3c>
-- Channel Snoop/1745932639.148-00000018 joined 'simple_bridge' stasis-bridge <e9904607-d530-49f3-96f9-3ae192d8da3c>
> Bridge e9904607-d530-49f3-96f9-3ae192d8da3c: switching from simple_bridge technology to softmix
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007373, ts 000160, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029469, ts 000160, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029470, ts 000320, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007374, ts 000320, len 000160)
> 0x5ec7053e8a50 -- Strict RTP qualifying stream type: <unknown>
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029471, ts 000480, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007375, ts 000480, len 000160)
> 0x5ec7053e8a50 -- Strict RTP qualifying stream type: <unknown>
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029472, ts 000640, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007376, ts 000640, len 000160)
> 0x5ec7053e8a50 -- Strict RTP qualifying stream type: <unknown>
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007377, ts 000800, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029473, ts 000800, len 000160)
> 0x5ec7053e8a50 -- Strict RTP qualifying stream type: <unknown>
> 0x5ec7053e8a50 -- Strict RTP switching source address to 127.0.0.1:6000
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000004, ts 000800, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007378, ts 000960, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029474, ts 000960, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000005, ts 000960, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007379, ts 001120, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029475, ts 001120, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000006, ts 001120, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007380, ts 001280, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029476, ts 001280, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000007, ts 001280, len 000160)
> 0x7477c404ba60 -- Strict RTP switching to RTP target address 148.122.250.9:11092 as source
Got RTP packet from 148.122.250.9:11092 (type 08, seq 000001, ts 000480, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029477, ts 001440, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007381, ts 001440, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000008, ts 001440, len 000160)
Got RTP packet from 148.122.250.9:11092 (type 08, seq 000002, ts 000640, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029478, ts 001600, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007382, ts 001600, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000009, ts 001600, len 000160)
Got RTP packet from 148.122.250.9:11092 (type 08, seq 000003, ts 000800, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007383, ts 001760, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029479, ts 001760, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000010, ts 001760, len 000160)
Got RTP packet from 148.122.250.9:11092 (type 08, seq 000004, ts 000960, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029480, ts 001920, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007384, ts 001920, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000011, ts 001920, len 000160)
Got RTP packet from 148.122.250.9:11092 (type 08, seq 000005, ts 001120, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007385, ts 002080, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029481, ts 002080, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000012, ts 002080, len 000160)
Got RTP packet from 148.122.250.9:11092 (type 08, seq 000006, ts 001280, len 000160)
Sent RTP packet to 148.122.250.9:11092 (type 08, seq 007386, ts 002240, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029482, ts 002240, len 000160)
Got RTP packet from 127.0.0.1:6000 (type 08, seq 000013, ts 002240, len 000160)
Got RTP packet from 148.122.250.9:11092 (type 08, seq 000007, ts 001440, len 000160)
Sent RTP packet to 0.0.0.0:6000 (type 08, seq 029483, ts 002400, len 000160) ```