Search code examples
c#video-streamingh.264apple-tv

Streaming h.264 packetized to Apple Tv in C#


Im currently on an endeavour to create an application in C# to interface with my Apple TV.

The following link is the AirPlay protocol that the Apple TV uses(Unofficial): http://nto.github.io/AirPlay.html

Starting from the top of the document I managed to, so far, make my application send photo/slideshows/video to the appletv successfully using http requests back and forth.

However, Im up to the part of the document about mirroring. According to the (unofficial) Protocol, the Client connects to the Apple TV on Port 7100 and sends a typical HTTP POST request followed by a binary PLIST of parameters about the session. I have no trouble with this part. I am a little stumped as to what to do on the next part due to lack of experience.

After sending the initial HTTP header, the Apple TV expects an H.264 packetized stream, this I have absolutely no idea about, particularly because I dont have a clue about payload/NAL structures with this type of stream, and how to construct these byte arrays to adhere to the protocol (mainly because ive only ever dealt with your typical data types.

My main question is: How do I go about setting up the right values to send to adhere to the following:

6.2. Stream Packets

The video stream is packetized using 128-byte headers, followed by an optional payload. Only the first 64 bytes of headers seem to be used. Headers start with the following little-endian fields:

size description
4 bytes payload size
2 bytes payload type
2 bytes 0x1e if type = 2, else 6
8 bytes NTP timestamp
There are 3 types of packets:

type description
0 video bitstream
1 codec data
2 heartbeat

Any help steering me in the right direction would be very appreciated as I feel like i've hit a bit of a brick wall.


Solution

  • Sounds like a custom header format. There are typically 2 formats that H.264 is transported in as described here:

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd757808%28v=vs.85%29.aspx Either it consists of a sequence of network abstraction layer units (NALUs), each of which is prefixed with a start code equal to 0x000001 or 0x00000001

    OR

    each NALU is prefixed by a length field, which gives the length of the NALU in bytes. The size of the length field can vary, but is typically 1, 2, or 4 bytes

    Download the H.264 spec here: http://www.itu.int/rec/T-REC-H.264-201304-S