TcpClient or HttpWebRequest to Apple TV ending after 30 seconds?

后端 未结 1 1277
情深已故
情深已故 2021-01-13 07:41

I\'m working on creating an Library in C# to use the Airplay protocol to send Photos and Video to my Apple TV (Specifically working with Generation 3 but hopefully that shou

相关标签:
1条回答
  • 2021-01-13 08:26

    I figured it out finally. It wasn't the .Net code killing the connection, it was the Apple TV itself. With wireshark I was able to see the proper Ack and Fin messages which were from the AppleTV after 30 seconds of not recieving any new messages on that connection. To solve the problem I figured out by playing around with Telnet that the AppleTV doesn't seem to care what you send it as long as you send it SOMETHING on a periodic basis, which seems to keep the connection alive.

    With HttpWebRequest the send/recieve portion is pretty canned. Its designed for a standard Http request and response, and if you need to do anything else you simply start a new HttpWebRequest rather than use the existing one. Trying to send a 2nd message on the same HttpWebRequest errors out.

    So I had to use a TcpClient, and had to rework the end.

        /// <summary>
        /// Starts a video.
        /// </summary>
        /// <param name="url">The URL of the video to play.</param>
        /// <param name="startPosition">The start position of the video. This value must be between 0 and 1</param>
        public void StartVideo(Uri url, float startPosition = 0)
        {
            if (startPosition > 1)
            {
                throw new ArgumentException("Start Position must be between 0 and 1");
            }
    
            TcpClient tcpClient = new TcpClient("192.168.1.20", 7000);
            tcpClient.ReceiveTimeout = 100000;
            tcpClient.SendTimeout = 100000;
    
            //get the client stream to read data from.
            NetworkStream clientStream = tcpClient.GetStream();
    
            string body =
           "Content-Location: " + url + "\n" +
           "Start-Position: " + startPosition + "\n";
    
            string request = "POST /play HTTP/1.1\n" +
            "User-Agent: MediaControl/1.0\n" +
            "Content-Type: text/parameters\n" +
            "Content-Length: " + Encoding.ASCII.GetBytes(body).Length + "\n" +
            "X-Apple-Session-ID:" + _sessionGuid.ToString() + "\n\n";
    
            //Send the headers
            sendMessage(clientStream, request);
            //Send the body
            sendMessage(clientStream, body);
    
            //Get the response
            byte[] myReadBuffer = new byte[1024];
            StringBuilder myCompleteMessage = new StringBuilder();
            int numberOfBytesRead = 0;
            numberOfBytesRead = clientStream.Read(myReadBuffer, 0, myReadBuffer.Length);
            myCompleteMessage.Append(Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
    
            //Now start doing a "keepalive"
            while (true)
            {
                //Simply send the characters "ok" every two seconds
                sendMessage(clientStream, "ok");
                Thread.Sleep(2000);
            }                      
        }
    
        /// <summary>
        /// Sends a message across the NetworkStream
        /// </summary>
        /// <param name="clientStream">The stream to send the message down</param>
        /// <param name="message">The message to send</param>
        public void sendMessage(NetworkStream clientStream, string message)
        {
            byte[] buffer = new ASCIIEncoding().GetBytes(message);
            try
            {
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
            }
            catch (System.IO.IOException e)
            {
                Debug.WriteLine("IOException: " + e.Message);
            }
        }
    

    Obviously this is not the final answer, but this was the bare minimum to get it to work. If anybody figures out what the actual Apple hardware is sending in place of the "ok" please add a note.

    0 讨论(0)
提交回复
热议问题