一种选择是将流分割成消息,以交错时钟和音频数据。消息可以是一个头,其中包含类型指示符和随后使用struct模块编码的字节数。这段代码是未经测试的,实际上是伪代码,但类似。。。在import struct
def send_audio(audio_socket, audio_data_fragment):
"""send the next bit of audio. if fragment size is large,
the next clock send will be delayed"""
audio_socket.sendall(struct,pack('!bL', 0, len(audio_data_fragment))
audio_socket.sendall(audio_data_fragment)
def send_clock(audio_socket, clock_val):
"""send the next clock info. just guessing that its an 8 byte
binary number as an example"""
audio_socket.sendall(struct.pack('!bQ', 1, clock_val))
def _recvall(sock, sz):
buf = ''
while len(buf) < sz:
buf += sock.recv(sz - len(buf))
return buf
def recv(audio_socket):
"""recv next message and return a tuple of (type, data)"""
msg_type = struct.unpack('b', audio_socket.recv(1)))
if msg_type == 0:
audio_len = struct.unpack('!L', _recvall(audio_socket, 4))
audio = _recvall(audio_socket, audio_len)
return 'audio', audio
elif msg_type == 1:
clock = struct.unpack('!Q', _recvall(audio_socket, 8))
return 'clock', clock
另一个选择是选择一个为您生成消息的现有系统。它可以是python标准xmlrpc模块,也可以是zeromq、协议缓冲区或trift。有很多选择。在