                 stream.emit 'end'
               seq.done()
             when 15  # AMF3 data message
-              dataMessage = parseAMF0DataMessage rtmpMessage.body[1..]
-              if DEBUG_INCOMING_RTMP_PACKETS
-                logger.info "[rtmp:receive] AMF3 data:"
-                logger.info dataMessage
-              @handleAMFDataMessage dataMessage, (err, output) ->
-                if err?
-                  logger.error "[rtmp:receive] packet error: #{err}"
-                if output?
-                  outputs.push output
-                seq.done()
+              try
+                dataMessage = parseAMF0DataMessage rtmpMessage.body[1..]
+              catch e
+                logger.error "[rtmp] error: failed to parse AMF0 data message: #{e.stack}"
+                logger.error "messageTypeID=#{rtmpMessage.messageTypeID} body:"
+                Bits.hexdump rtmpMessage.body
+                seq.done e
+              if dataMessage?
+                if DEBUG_INCOMING_RTMP_PACKETS
+                  logger.info "[rtmp:receive] AMF3 data:"
+                  logger.info dataMessage
+                @handleAMFDataMessage dataMessage, (err, output) ->
+                  if err?
+                    logger.error "[rtmp:receive] packet error: #{err}"
+                  if output?
+                    outputs.push output
+                  seq.done()
             when 17  # AMF3 command (0x11)
               # Does the first byte == 0x00 mean AMF0?
               commandMessage = parseAMF0CommandMessage rtmpMessage.body[1..]

                  outputs.push output
                seq.done()
            when 18  # AMF0 data message
              dataMessage = parseAMF0DataMessage rtmpMessage.body
              if DEBUG_INCOMING_RTMP_PACKETS
                logger.info "[rtmp:receive] AMF0 data:"
                logger.info dataMessage
              @handleAMFDataMessage dataMessage, (err, output) ->
                if err?
                  logger.error "[rtmp:receive] packet error: #{err}"
                if output?
                  outputs.push output
                seq.done()
            when 20  # AMF0 command
              commandMessage = parseAMF0CommandMessage rtmpMessage.body
              if DEBUG_INCOMING_RTMP_PACKETS

flv = require './flv'
avstreams = require './avstreams'
logger = require './logger'
Bits = require './bits'

# enum
SESSION_STATE_NEW               = 1
