 # RTMP specification is available at:
 # http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/rtmp/pdf/rtmp_specification_1.0.pdf
 
+fs            = require 'fs'
 net           = require 'net'
 crypto        = require 'crypto'
 Sequent       = require 'sequent'

  teardown: ->
    if (@streamPublisher)
      @streamPublisher = false
      @emit "stream_unpublished", @stream
    if @isTearedDown
      console.log "[rtmp] already teared down"

            when 9  # Video Message (incoming)
              videoData = parseVideoMessage @stream, rtmpMessage.body
              if videoData.nalUnitGlob?

                fileHeader = new Buffer(4 * 4)
                
                fileHeader.writeUInt32LE(9, 0)
                fileHeader.writeUInt32LE(rtmpMessage.timestamp, 4)
                fileHeader.writeUInt32LE(videoData.nalUnitGlob.length, 8)
                fileHeader.writeUInt32LE(0, 12)

                @stream.dump.write fileHeader

                @stream.dump.write videoData.nalUnitGlob
                if not @isFirstVideoReceived
                  @emit 'video_start', @stream
                  @isFirstVideoReceived = true
