         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


  for session in allSessions
    msgs = [];
    msgs2 = []
    for rtmpMessage, i in rtmpMessagesToSend
      msgs.push rtmpMessage if session.from_packet(rtmpMessage)

    if not msgs? || msgs.length == 0
      continue

    #console.log "lets stream"
    #fixme -> remove the ! and test again, this was a hack for stream switching
    if session.isWaitingForKeyFrame
      if session.stream.isVideoStarted  # has video stream
        for rtmpMessage, i in msgs
          if rtmpMessage.avType is 'video' and rtmpMessage.isKeyFrame
            session.isPlaying = true
            session.playStartTimestamp = rtmpMessage.originalTimestamp
            session.isWaitingForKeyFrame = false
            msgs2 = msgs[i..]
            break
      else  # audio only
        session.isPlaying = true
        session.playStartTimestamp = msgs[0].originalTimestamp
        session.isWaitingForKeyFrame = false
        msgs2 = msgs
    else
      msgs2 = msgs

    msgs2 = msgs;

    if not msgs? || msgs.length == 0
      continue
