         console.log "metadata re-sent"
 
       for rtmpMessage in msgs
-        session.lastTimestamp = rtmpMessage.timestamp = session.offsetTimestamp + session.getScaledTimestamp(rtmpMessage.originalTimestamp) % TIMESTAMP_ROUNDOFF 
+        session.lastTimestamp[rtmpMessage.avType] = rtmpMessage.timestamp = session.offsetTimestamp[rtmpMessage.avType] + session.getScaledTimestamp(rtmpMessage.originalTimestamp) % TIMESTAMP_ROUNDOFF 
 
       if msgs.length > 1
         buf = createRTMPAggregateMessage msgs, session.chunkSize

      consumeNextRTMPMessage()

class RTMPServer
  setBroadcastStream: (name) ->
    console.log "setBroadcastStream", name
    broadcasted_stream = name
    stream = getStreamByName(name)
    stream_ts = 0
    for clientID, session of sessions
      if session.stream == stream
        stream_ts = session.lastTimestamp

    @receivedBytes = 0
    @stream_switched = false
    @server = server
    @lastTimestamp = { 'audio': 0, 'video': 0 }
    @offsetTimestamp = { 'audio': 0, 'video': 0 }

  from_packet: (pkt) ->
    if (pkt.avType == 'audio')
      if @audio_stream
        console.log 'Audio filter'
        return (@audio_stream == pkt.stream)
    return @stream == pkt.stream

  clearTimeout: ->
    if @timeoutTimer?
