Esempio n. 1
0
        public static CompactResponse GetAudio(CompactRequest req)
        {
            //foreach (KeyValuePair<string, string> kvp in req.Headers) {
            //    Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
            //}
            if (!req.Headers.ContainsKey("x-video"))
            {
                return(NoFileResponse());
            }

            string videoPath = req.Headers["x-video"].Trim();

            if (!videoPath.EndsWith(".avi") && !videoPath.EndsWith(".mp4"))
            {
                return(NotVideoFileResponse());
            }

            byte[] buf;
            try
            {
                AudioHandler audio       = new AudioHandler(videoPath);
                string       tmpFilePath = audio.StartProcess();
                buf = System.IO.File.ReadAllBytes(tmpFilePath);
            }
            catch (Exception e)
            {
                return(NotVideoFileResponse());
            }

            return(new CompactResponse()
            {
                ContentType = "audio/ogg",
                Data = buf
            });
        }
Esempio n. 2
0
        public static CompactResponse GetVideo(CompactRequest req)
        {
            if (!req.Headers.ContainsKey("x-video"))
            {
                return(NoFileResponse());
            }

            string videoPath = req.Headers["x-video"].Trim();

            if (!videoPath.EndsWith(".avi") && !videoPath.EndsWith(".mp4"))
            {
                return(NotVideoFileResponse());
            }

            VideoReader reader;

            try
            {
                reader = new VideoReader(videoPath);
            }
            catch (Exception e)
            {
                return(NoFileResponse());
            }

            int startOffset = req.Headers.ContainsKey("x-skip-frame") ? Int32.Parse(req.Headers["x-skip-frame"]) : 0;
            int readFrames  = req.Headers.ContainsKey("x-frames") ? Int32.Parse(req.Headers["x-frames"]) : 0;

            reader.SkipFrame(startOffset);

            VideoUdpSender sender = new VideoUdpSender(reader, readFrames, req.ipAddress, 10005);
            //Console.WriteLine("IP Address = " + req.ipAddress.ToString());

            /*string response = reader.FileProperties().FrameRate+"\n"
             + reader.FileProperties().FrameCount + "\n"
             + reader.FileProperties().Width + "\n"
             + reader.FileProperties().Height;*/
            string response = "OK";

            // Hold the HTTP session until everything is sent
            while (!sender.isAllSent())
            {
                ;
            }

            return(new CompactResponse()
            {
                ContentType = "text/plain",
                Data = Encoding.UTF8.GetBytes(response)
            });
        }
Esempio n. 3
0
        //呼叫端要準備一個函數,接收CompactRequest,回傳CompactResponse
        public MicroHttpServer(int port,
                               Func <CompactRequest, CompactResponse> reqProc)
        {
            listener = new TcpListener(IPAddress.Any, port);
            //另建Thread執行
            serverThread = new Thread(() =>
            {
                listener.Start();
                while (true)
                {
                    Socket s         = listener.AcceptSocket();
                    NetworkStream ns = new NetworkStream(s);

                    //解讀Request內容
                    StreamReader sr    = new StreamReader(ns);
                    CompactRequest req = new CompactRequest(sr, (IPEndPoint)s.RemoteEndPoint);
                    //呼叫自訂的處理邏輯,得到要回傳的Response
                    CompactResponse resp = reqProc(req);
                    //傳回Response
                    StreamWriter sw = new StreamWriter(ns);
                    sw.WriteLine("HTTP/1.1 {0}", resp.StatusText);
                    sw.WriteLine("Content-Type: " + resp.ContentType);
                    foreach (string k in resp.Headers.Keys)
                    {
                        sw.WriteLine("{0}: {1}", k, resp.Headers[k]);
                    }
                    sw.WriteLine("Content-Length: {0}", resp.Data.Length);
                    sw.WriteLine();
                    sw.Flush();
                    //寫入資料本體
                    s.Send(resp.Data);
                    //結束連線
                    s.Shutdown(SocketShutdown.Both);
                    ns.Close();
                }
            });
            serverThread.Start();
        }