Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set packet_time = 125 msec instead of 20 msec (both side) #1080

Closed
el07694 opened this issue Mar 29, 2024 · 4 comments
Closed

Set packet_time = 125 msec instead of 20 msec (both side) #1080

el07694 opened this issue Mar 29, 2024 · 4 comments

Comments

@el07694
Copy link

el07694 commented Mar 29, 2024

Hello i am wondering how can i change the packet time to be 125 msec instead of 20 msec.
Also one more question: Is there any suggested port the aiohttp server will run for aiortc purposes?

@el07694
Copy link
Author

el07694 commented Mar 29, 2024

As for the main question i fixed by splitting the 125 msec sec to 20 msec (I use buffer variable) and by merging 20mesc packets to 125msec (I also use buffer variable).

As for the first question which port should you suggest me to use for radio broadcasting and live ip-calls?

@Chappie74
Copy link

As for the main question i fixed by splitting the 125 msec sec to 20 msec (I use buffer variable) and by merging 20mesc packets to 125msec (I also use buffer variable).

Please can you share some code on how?

@el07694
Copy link
Author

el07694 commented Apr 17, 2024

Sure, i will share with you tomorrow morning.

Good night!

@el07694
Copy link
Author

el07694 commented Apr 21, 2024

Here it is the code snippets (i use pydub.AudioSegment in both cases):

'''
code to convert 125 msec packets to 20 msec packets
'''
class Server_Audio_Stream_Offer(MediaStreamTrack):
    kind = "audio"

    def __init__(self,q):
        super().__init__()  # don't forget this!

        self.speackers_deck_queue = q
        self.q = Simple_Queue()

        self.codec = av.CodecContext.create('pcm_s16le', 'r')
        self.codec.sample_rate = 8000
        self.codec.channels = 2

        self.audio_samples = 0

        self.run = True
        self.mp3_q = AudioSegment.empty()
        self.packetize_correct_thread = threading.Thread(target=self.packetize_correct)
        self.packetize_correct_thread.start()

    async def recv(self):
        packet = av.Packet(self.q.get())
        frame = self.codec.decode(packet)[0]
        frame.pts = self.audio_samples
        frame.time_base = fractions.Fraction(1, self.codec.sample_rate)
        self.audio_samples += frame.samples
        return frame

    def packetize_correct(self):
        while (self.run):
            try:
                slice_125 = self.speackers_deck_queue.get()["slice"].set_frame_rate(8000)
                slice_full = self.mp3_q + slice_125
                len_slice_full = len(slice_full)
                desired_slice_len = 20
                packets = int(len_slice_full/desired_slice_len)
                for i in range(0,packets):
                    self.q.put(slice_full[i*desired_slice_len:(i+1)*desired_slice_len].raw_data)
                self.mp3_q = slice_full[packets*desired_slice_len:]
            except:
                print(traceback.format_exc())

    def stop_offering(self):
        try:
            self.run = False
            self.packetize_correct_thread.join()
        except Exception as e:
            pass



'''
code to convert 20 msec packets to 125 msec packets
'''
if len(self.ip_call_1_mp3_q) < self.packet_time:#125msec packet time
	while (len(self.ip_call_1_mp3_q) < self.packet_time):
		chunk = self.ip_call_1_packet_queue.get()["packet"]
		chunk_slice = AudioSegment(chunk, sample_width=2, frame_rate=48000, channels=2)
		self.ip_call_1_mp3_q = self.ip_call_1_mp3_q + chunk_slice
		time.sleep(0.020)
	slice = self.ip_call_1_mp3_q[0:self.packet_time]
	self.ip_call_1_mp3_q = self.ip_call_1_mp3_q[self.packet_time:]
else:
	slice = self.ip_call_1_mp3_q[0:self.packet_time]
	self.ip_call_1_mp3_q = self.ip_call_1_mp3_q[self.packet_time:]


@aiortc aiortc locked and limited conversation to collaborators May 21, 2024
@jlaine jlaine converted this issue into discussion #1099 May 21, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants