Example #1
0
    def make(self, channel_id, packet, timestamp=0, stream_id=0):
        type = packet[0]
        header = [channel_id, timestamp, None, type, stream_id]

        data = BytesOutput(True)

        # 0x01 - Chunk Size
        if type == RTMP_TYPE_CHUNK_SIZE:
            data.writeUInt(packet[1])

        # 0x03 - Bytes Read
        elif type == RTMP_TYPE_BYTES_READ:
            data.writeUInt(packet[1])

        # 0x04 - Control
        elif type == RTMP_TYPE_CONTROL:
            control_stream_id, control = packet[1]
            control_type = control[0]
            value1 = None
            value2 = None

            # 0x00 - Clear
            if control_type == RTMP_CONTROL_CLEAR:
                pass

            # 0x01 - Clear Play
            elif control_type == RTMP_CONTROL_CLEAR_PLAY:
                pass

            # 0x03 - Client Buffer
            elif control_type == RTMP_CONTROL_CLIENT_BUFFER:
                value1 = control[1]

            # 0x04 - Reset
            elif control_type == RTMP_CONTROL_RESET:
                pass

            # 0x06 - Ping
            elif control_type == RTMP_CONTROL_PING:
                value1 = control[1]

            # 0x07 - Pong
            elif control_type == RTMP_CONTROL_PONG:
                value1 = control[1]

            # Unknown
            elif control_type == RTMP_CONTROL_UNKNOWN:
                control_type = control[1]
                value1 = control[2]
                value2 = control[3]

            data.writeUInt16(control_type)
            data.writeUInt(control_stream_id)

            if value1 is not None:
                data.writeUInt(value1)
            if value2 is not None:
                data.writeUInt(value2)

        # 0x05 - Server BW
        elif type == RTMP_TYPE_SERVER_BW:
            data.writeUInt(0x002625A0)

        # 0x06 - Client BW
        elif type == RTMP_TYPE_CLIENT_BW:
            data.writeUInt(0x002625A0)
            data.writeByte(0x02)

        # 0x08 - Audio
        elif type == RTMP_TYPE_AUDIO:
            data.write(packet[1])

        # 0x09 - Video
        elif type == RTMP_TYPE_VIDEO:
            data.write(packet[1])

        # 0x12 - Notify
        elif type == RTMP_TYPE_NOTIFY:
            data.writeString(packet[1])

        # 0x13 - Shared
        elif type == RTMP_TYPE_SHARED:
            raise Exception, "Not implemented"  #TODO -> Shared Object functions

        # 0x14 - Command
        elif type == RTMP_TYPE_COMMAND:
            name, id, args = packet[1]
            Amf.write(data, (3, name))  #Command name
            Amf.write(data, (1, id))  #Command id
            for arg in args:  # Args (Amf Values)
                Amf.write(data, arg)

        # Unknown
        elif type == RTMP_TYPE_UNKNOWN:
            type, body = packet[1]
            header[4] = type
            data.write(body)

        # Other
        else:
            header[4] = None

        data = data.getBytes()
        length = len(data)
        header[2] = length

        output = BytesOutput(True)

        output.write(self.makeHeader(header))
        if length > 0:
            pos = self.write_chunk_size
            if length <= pos:
                output.write(data)
            else:
                output.write(buffer(data, 0, pos))
                length -= pos
                while length > 0:
                    output.writeByte(header[0] | (headerSizeToByte(1) << 6))
                    n = (length,
                         self.write_chunk_size)[length > self.write_chunk_size]
                    output.write(buffer(data, pos, n))
                    pos += n
                    length -= n

        return output.getBytes()
Example #2
0
	def make(self, channel_id, packet, timestamp = 0, stream_id = 0):
		type = packet[0]
		header = [channel_id, timestamp, None, type, stream_id]
		
		data = BytesOutput(True)
		
		# 0x01 - Chunk Size
		if type == RTMP_TYPE_CHUNK_SIZE:
			data.writeUInt(packet[1])
			
		# 0x03 - Bytes Read
		elif type == RTMP_TYPE_BYTES_READ:
			data.writeUInt(packet[1])
			
		# 0x04 - Control
		elif type == RTMP_TYPE_CONTROL:
			control_stream_id, control = packet[1]
			control_type = control[0]
			value1 = None
			value2 = None
			
			# 0x00 - Clear
			if control_type == RTMP_CONTROL_CLEAR:
				pass
				
			# 0x01 - Clear Play
			elif control_type == RTMP_CONTROL_CLEAR_PLAY:
				pass
				
			# 0x03 - Client Buffer
			elif control_type == RTMP_CONTROL_CLIENT_BUFFER:
				value1 = control[1]
				
			# 0x04 - Reset
			elif control_type == RTMP_CONTROL_RESET:
				pass
				
			# 0x06 - Ping
			elif control_type == RTMP_CONTROL_PING:
				value1 = control[1]
				
			# 0x07 - Pong
			elif control_type == RTMP_CONTROL_PONG:
				value1 = control[1]
				
			# Unknown
			elif control_type == RTMP_CONTROL_UNKNOWN:
				control_type = control[1]
				value1 = control[2]
				value2 = control[3]

			data.writeUInt16(control_type)
			data.writeUInt(control_stream_id)
			
			if value1 is not None:
				data.writeUInt(value1)
			if value2 is not None:
				data.writeUInt(value2)
				
		# 0x05 - Server BW
		elif type == RTMP_TYPE_SERVER_BW:
			data.writeUInt(0x002625A0)
			
		# 0x06 - Client BW
		elif type == RTMP_TYPE_CLIENT_BW:
			data.writeUInt(0x002625A0)
			data.writeByte(0x02)
			
		# 0x08 - Audio
		elif type == RTMP_TYPE_AUDIO:
			data.write(packet[1])
			
		# 0x09 - Video
		elif type == RTMP_TYPE_VIDEO:
			data.write(packet[1])
			
		# 0x12 - Notify
		elif type == RTMP_TYPE_NOTIFY:
			data.writeString(packet[1])
			
		# 0x13 - Shared
		elif type == RTMP_TYPE_SHARED:
			raise Exception, "Not implemented" #TODO -> Shared Object functions
			
		# 0x14 - Command
		elif type == RTMP_TYPE_COMMAND:
			name, id, args = packet[1]
			Amf.write(data, (3, name)) #Command name
			Amf.write(data, (1, id)) #Command id
			for arg in args: # Args (Amf Values)
				Amf.write(data, arg)
				
		# Unknown
		elif type == RTMP_TYPE_UNKNOWN: 
			type, body = packet[1]
			header[4] = type
			data.write(body)
			
		# Other
		else:
			header[4] = None
			
		data = data.getBytes()
		length = len(data)
		header[2] = length

		
		output = BytesOutput(True)
		
		output.write(self.makeHeader(header))
		if length > 0:
			pos = self.write_chunk_size
			if length <= pos:
				output.write(data)
			else:
				output.write(buffer(data, 0, pos))
				length -= pos
				while length > 0:
					output.writeByte(header[0] | (headerSizeToByte(1) << 6))
					n = (length, self.write_chunk_size)[length > self.write_chunk_size]
					output.write(buffer(data, pos, n))
					pos += n
					length -= n

		return output.getBytes()