<commit_before># RTSP/HTTP/RTMPT hybrid server
#
# RTSP spec:
#   RFC 2326  http://www.ietf.org/rfc/rfc2326.txt

# TODO: clear old sessioncookies

net = require 'net'
dgram = require 'dgram'
os = require 'os'
crypto = require 'crypto'
url = require 'url'
Sequent = require 'sequent'

rtp = require './rtp'
sdp = require './sdp'
h264 = require './h264'
aac = require './aac'
http = require './http'
avstreams = require './avstreams'
Bits = require './bits'
logger = require './logger'
config = require './config'

TAG = 'rtsp/http'

# Default server name for RTSP and HTTP responses
DEFAULT_SERVER_NAME = 'node-rtsp-rtmp-server'

# Start playing from keyframe
ENABLE_START_PLAYING_FROM_KEYFRAME = false

# Maximum single NAL unit size
SINGLE_NAL_UNIT_MAX_SIZE = 1358

DAY_NAMES = [
  'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
]

MONTH_NAMES = [
  'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
]

# If true, RTSP requests/response will be printed to the console
DEBUG_RTSP = false
DEBUG_RTSP_HEADERS_ONLY = false

# If true, outgoing video/audio packets are printed to the console
DEBUG_OUTGOING_PACKET_DATA = false

# If true, outgoing RTCP packets (sender reports) are printed to the console
DEBUG_OUTGOING_RTCP = false

# If true, RTSP requests/responses tunneled in HTTP will be
# printed to the console
DEBUG_HTTP_TUNNEL = false

# If true, UDP transport will always be disabled and
# clients will be forced to use TCP transport.
DEBUG_DISABLE_UDP_TRANSPORT = false

# Two CRLFs
CRLF_CRLF = [ 0x0d, 0x0a, 0x0d, 0x0a ]

TIMESTAMP_ROUNDOFF = 4294967296  # 32 bits

if DEBUG_OUTGOING_PACKET_DATA
  logger.enableTag 'rtsp:out'

zeropad = (columns, num) ->
  num += ''
  while num.length < columns
    num = '0' + num
  num

pad = (digits, n) ->
  n = n + ''
  while n.length < digits
    n = '0' + n
  n

# Generate new random session ID
# NOTE: Samsung SC-02B doesn't work with some hex string
generateNewSessionID = (callback) ->
  id = ''
  for i in [0..7]
    id += parseInt(Math.random() * 9) + 1
  callback null, id

# Generate random 32 bit unsigned integer.
# Return value is intended to be used as an SSRC identifier.
generateRandom32 = ->
  str = "#{new Date().getTime()}#{process.pid}#{os.hostname()}" + \
        "#{process.getuid()}#{process.getgid()}" + \
        (1 + Math.random() * 1000000000)

  md5sum = crypto.createHash 'md5'
  md5sum.update str
  md5sum.digest()[0..3].readUInt32BE(0)

resetStreamParams = (stream) ->
  stream.rtspUploadingClient = null
  stream.videoSequenceNumber = 0
  stream.audioSequenceNumber = 0
  stream.lastVideoRTPTimestamp = null
  stream.lastAudioRTPTimestamp = null
  stream.videoRTPTimestampInterval = Math.round(90000 / stream.videoFrameRate)
  stream.audioRTPTimestampInterval = stream.audioPeriodSize

avstreams.on 'update_frame_rate', (stream, frameRate) -><commit_msg>Make it work on non-POSIX OSes<commit_after># RTSP/HTTP/RTMPT hybrid server
#
# RTSP spec:
#   RFC 2326  http://www.ietf.org/rfc/rfc2326.txt

# TODO: clear old sessioncookies

net = require 'net'
dgram = require 'dgram'
os = require 'os'
crypto = require 'crypto'
url = require 'url'
Sequent = require 'sequent'

rtp = require './rtp'
sdp = require './sdp'
h264 = require './h264'
aac = require './aac'
http = require './http'
avstreams = require './avstreams'
Bits = require './bits'
logger = require './logger'
config = require './config'

TAG = 'rtsp/http'

# Default server name for RTSP and HTTP responses
DEFAULT_SERVER_NAME = 'node-rtsp-rtmp-server'

# Start playing from keyframe
ENABLE_START_PLAYING_FROM_KEYFRAME = false

# Maximum single NAL unit size
SINGLE_NAL_UNIT_MAX_SIZE = 1358

DAY_NAMES = [
  'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
]

MONTH_NAMES = [
  'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
]

# If true, RTSP requests/response will be printed to the console
DEBUG_RTSP = false
DEBUG_RTSP_HEADERS_ONLY = false

# If true, outgoing video/audio packets are printed to the console
DEBUG_OUTGOING_PACKET_DATA = false

# If true, outgoing RTCP packets (sender reports) are printed to the console
DEBUG_OUTGOING_RTCP = false

# If true, RTSP requests/responses tunneled in HTTP will be
# printed to the console
DEBUG_HTTP_TUNNEL = false

# If true, UDP transport will always be disabled and
# clients will be forced to use TCP transport.
DEBUG_DISABLE_UDP_TRANSPORT = false

# Two CRLFs
CRLF_CRLF = [ 0x0d, 0x0a, 0x0d, 0x0a ]

TIMESTAMP_ROUNDOFF = 4294967296  # 32 bits

if DEBUG_OUTGOING_PACKET_DATA
  logger.enableTag 'rtsp:out'

zeropad = (columns, num) ->
  num += ''
  while num.length < columns
    num = '0' + num
  num

pad = (digits, n) ->
  n = n + ''
  while n.length < digits
    n = '0' + n
  n

# Generate new random session ID
# NOTE: Samsung SC-02B doesn't work with some hex string
generateNewSessionID = (callback) ->
  id = ''
  for i in [0..7]
    id += parseInt(Math.random() * 9) + 1
  callback null, id

# Generate random 32 bit unsigned integer.
# Return value is intended to be used as an SSRC identifier.
generateRandom32 = ->
  str = "#{new Date().getTime()}#{process.pid}#{os.hostname()}" + \
        (1 + Math.random() * 1000000000)

  md5sum = crypto.createHash 'md5'
  md5sum.update str
  md5sum.digest()[0..3].readUInt32BE(0)

resetStreamParams = (stream) ->
  stream.rtspUploadingClient = null
  stream.videoSequenceNumber = 0
  stream.audioSequenceNumber = 0
  stream.lastVideoRTPTimestamp = null
  stream.lastAudioRTPTimestamp = null
  stream.videoRTPTimestampInterval = Math.round(90000 / stream.videoFrameRate)
  stream.audioRTPTimestampInterval = stream.audioPeriodSize

avstreams.on 'update_frame_rate', (stream, frameRate) ->
{'fraction_non_alphanumeric': 0.12285313704872064, 'fraction_numerical': 0.02698913424465475, 'mean_word_length': 4.010535557506585, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4570628', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1998, 'n_words': 596}