# Path: twimp/amf0.py
# class Object(OrderedObject):
#     """AMF Object class, with chronological attribute ordering, for
#     increased compatibility.
#     """
# 
# Path: twimp/chunks.py
# class Header(object):
# class ChunkStreamParseError(ValueError):
# class ChunkStreamValueError(ValueError):
# class Demuxer(object):
# class Chunker(object):
# class SimpleChunkProducer(object):
# class Muxer(object):
#     def __init__(self, cs_id, time, size, type, ms_id, **kw):
#     def __repr__(self):
#         def _repr_maybe(v, provided):
# def absolutize(h, base):
# def read_header_head(s):
#     def __init__(self, protocol):
#     def doSetChunkSize(self, header, new_size):
#     def doAbortMessage(self, header, cs_id):
#     def doACK(self, header, seq_num):
#     def doUserControlMessage(self, header, evt_type, body):
#     def doWindowSize(self, header, window_size):
#     def doSetBandwidth(self, header, window_size, limit_type):
#     def controlMessageUnknown(self, header, body):
#     def controlMessageReceived(self, header, body):
#     def gen_handler(self):
# def _encode_basic_header(h_type, cs_id, time=None):
# def encode_full_header(cs_id, time, size, msg_type, ms_id):
# def encode_comp_header(h_type, cs_id, time, size, msg_type, ms_id):
#     def set_chunk_size(self, new_chunk_size):
#     def __call__(self, cs_id, first_header, body, time=None):
#     def __init__(self, transport):
#     def queue_chunker(self, priority, chunker):
#     def sync(self, priority):
#     def pauseProducing(self):
#     def resumeProducing(self):
#     def stopProducing(self):
#     def __init__(self, transport):
#     def _precompute_dispatch(self, amf_ver):
#     def _make_adhoc_csid(self, mt):
#     def set_chunk_size(self, new_chunk_size):
#     def sendMessage(self, time, type_, ms_id, body, absolute=False):
# DEFAULT_CHUNK_SIZE = 128
# (PROTO_SET_CHUNK_SIZE, PROTO_ABORT_MESSAGE, PROTO_ACK, PROTO_USER_CONTROL,
#  PROTO_WINDOW_SIZE, PROTO_SET_BANDWIDTH, MSG_COMMAND, MSG_DATA, MSG_SO,
#  MSG_AUDIO, MSG_VIDEO, MSG_AGGREGATE) = range(12)
# 
# Path: twimp/primitives.py

# 
# Path: twimp/helpers.py
# def vb_clone(vb):
#     # TODO: add this functionality to the vecbuf/VecBuf itself
#     return VecBuf(vb.peek_seq(len(vb)))
# 
# def vb(s):
#     # TODO: add this functionality to the vecbuf/VecBuf itself
#     return VecBuf([s])


from collections import deque
from twisted.internet import defer
from twimp.amf0 import Object
from twimp import chunks
from twimp.primitives import _s_uchar, _s_double_uchar
from twimp.helpers import vb_clone, vb
import twimp.log

    def _add_headers_cb(self, _result):
        d = defer.succeed(None)

        def debug_cb(result, msg):
            log.debug(msg)
            return result

        def header_callback_maker(type_):
            def header_callback(ts, flags, data):
                return self.on_header_data_added(type_, ts, flags, data)
            return header_callback

        def do_read_headers(_result, s, type_):
            return s.read_headers(header_callback_maker(type_))

        log.debug('[adding headers]')
        for s, type_ in self._tracks:
            d.addCallback(do_read_headers, s, type_)

        d.addCallback(debug_cb, '[headers done..]')
        return d

    def _send_meta_cb(self, _result):
        def got_meta(meta):
            if meta:
                self._stream_meta = meta
                self._nstream.asend(0, chunks.MSG_DATA, 'onStatus',
                                    Object(code='NetStream.Data.Start'))
                log.debug('sending meta: %r', meta)

# Path: twimp/amf0.py
# class Object(OrderedObject):
#     """AMF Object class, with chronological attribute ordering, for
#     increased compatibility.
#     """
# 
# Path: twimp/chunks.py
# class Header(object):
# class ChunkStreamParseError(ValueError):
# class ChunkStreamValueError(ValueError):
# class Demuxer(object):
# class Chunker(object):
# class SimpleChunkProducer(object):
# class Muxer(object):
#     def __init__(self, cs_id, time, size, type, ms_id, **kw):
#     def __repr__(self):
#         def _repr_maybe(v, provided):
# def absolutize(h, base):
# def read_header_head(s):
#     def __init__(self, protocol):
#     def doSetChunkSize(self, header, new_size):
#     def doAbortMessage(self, header, cs_id):
#     def doACK(self, header, seq_num):
#     def doUserControlMessage(self, header, evt_type, body):
#     def doWindowSize(self, header, window_size):
#     def doSetBandwidth(self, header, window_size, limit_type):
#     def controlMessageUnknown(self, header, body):
#     def controlMessageReceived(self, header, body):
#     def gen_handler(self):
# def _encode_basic_header(h_type, cs_id, time=None):
# def encode_full_header(cs_id, time, size, msg_type, ms_id):
# def encode_comp_header(h_type, cs_id, time, size, msg_type, ms_id):
#     def set_chunk_size(self, new_chunk_size):
#     def __call__(self, cs_id, first_header, body, time=None):
#     def __init__(self, transport):
#     def queue_chunker(self, priority, chunker):
#     def sync(self, priority):
#     def pauseProducing(self):
#     def resumeProducing(self):
#     def stopProducing(self):
#     def __init__(self, transport):
#     def _precompute_dispatch(self, amf_ver):
#     def _make_adhoc_csid(self, mt):
#     def set_chunk_size(self, new_chunk_size):
#     def sendMessage(self, time, type_, ms_id, body, absolute=False):
# DEFAULT_CHUNK_SIZE = 128
# (PROTO_SET_CHUNK_SIZE, PROTO_ABORT_MESSAGE, PROTO_ACK, PROTO_USER_CONTROL,
#  PROTO_WINDOW_SIZE, PROTO_SET_BANDWIDTH, MSG_COMMAND, MSG_DATA, MSG_SO,
#  MSG_AUDIO, MSG_VIDEO, MSG_AGGREGATE) = range(12)
# 
# Path: twimp/primitives.py

# 
# Path: twimp/helpers.py
# def vb_clone(vb):
#     # TODO: add this functionality to the vecbuf/VecBuf itself
#     return VecBuf(vb.peek_seq(len(vb)))
# 
# def vb(s):
#     # TODO: add this functionality to the vecbuf/VecBuf itself
#     return VecBuf([s])


# Path: twimp/server/controllers.py
from collections import deque
from twisted.internet import defer
from twimp.amf0 import Object
from twimp import chunks
from twimp.primitives import _s_uchar, _s_double_uchar
from twimp.helpers import vb_clone, vb
import twimp.log


    def _add_headers_cb(self, _result):
        d = defer.succeed(None)

        def debug_cb(result, msg):
            log.debug(msg)
            return result

        def header_callback_maker(type_):
            def header_callback(ts, flags, data):
                return self.on_header_data_added(type_, ts, flags, data)
            return header_callback

        def do_read_headers(_result, s, type_):
            return s.read_headers(header_callback_maker(type_))

        log.debug('[adding headers]')
        for s, type_ in self._tracks:
            d.addCallback(do_read_headers, s, type_)

        d.addCallback(debug_cb, '[headers done..]')
        return d

    def _send_meta_cb(self, _result):
        def got_meta(meta):
            if meta:
                self._stream_meta = meta
                self._nstream.asend(0, chunks.MSG_DATA, 'onStatus',
                                    Object(code='NetStream.Data.Start'))
                log.debug('sending meta: %r', meta)

                self._nstream.asend(0, chunks.MSG_DATA, 'onMetaData',