public static void AddVideoLog(PiSpyVideoLog log) { using (SqlConnection cn = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand("INSERT INTO PiSpyVideoLogs(TimeStamp, IpAddress, FilePath, PiSpySerialNumber, TimeReceived) VALUES (@TimeStamp, @IpAddress, @FilePath, @PiSpySerialNumber, @TimeReceived);", cn)) { cmd.Parameters.Add("@TimeStamp", SqlDbType.DateTime).Value = log.TimeStamp; cmd.Parameters.Add("@IpAddress", SqlDbType.NVarChar).Value = log.IpAddress; cmd.Parameters.Add("@FilePath", SqlDbType.NVarChar).Value = log.FilePath; cmd.Parameters.Add("@PiSpySerialNumber", SqlDbType.NVarChar).Value = log.PiSpySerialNumber; cmd.Parameters.Add("@TimeReceived", SqlDbType.DateTime).Value = log.TimeReceived; cn.Open(); cmd.ExecuteNonQuery(); } } }
private static void StartVideoServer(object clientParameter) { TcpClient client = (TcpClient)clientParameter; NetworkStream networkStream = client.GetStream(); string ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); while (client.Connected) { try { //Format at time this comment was written: // 4-4-24-<size given as int in first 4 bytes> // videoLen-HeaderLength-Header-Video // Header contains an 8 byte double for the timestamp and 16 byte UTF-8 Pi serial number byte[] videoLenBytes = TcpStreamReader.ReadBytesBlocking(networkStream, 4, 30000);// This one waits 30 seconds for data incase client wants to send more video clips. byte[] headerLenBytes = TcpStreamReader.ReadBytesBlocking(networkStream, 4); if (videoLenBytes.Length < 4 || headerLenBytes.Length < 4) { Trace.TraceError("Message not recieved Quickly enough or the message was not long enough!" + "There was more than 1 second in which data was expected but not recievied in the first 8 bytes!"); continue; } int videoLen = System.BitConverter.ToInt32(videoLenBytes, 0); int headerLen = System.BitConverter.ToInt32(headerLenBytes, 0); byte[] messageHeader = TcpStreamReader.ReadBytesBlocking(networkStream, headerLen); byte[] h264File = TcpStreamReader.ReadBytesBlocking(networkStream, videoLen); if (messageHeader.Length < headerLen || h264File.Length < videoLen) { Trace.TraceError("Message not recieved Quickly enough, the message was not long enough, or the first 8 bytes were incorrect!" + "There was more than 1 second in which data was expected but not recievied in the header or the video block."); continue; } //Use the serial number to determine what video sequence number is next for that pi byte[] serialnumbytes = new byte[16]; System.Array.Copy(messageHeader, headerLen-16, serialnumbytes, 0, 16); string SerialNum = System.Text.Encoding.UTF8.GetString(serialnumbytes); int filenum = SqlHelper.GetVideoNum(SerialNum); string videoPath = BlobHelper.StoreVideoFile(h264File, filenum, SerialNum); // Encode video in mp4 and get it's blob URI PiSpyVideoLog log = new PiSpyVideoLog(messageHeader, ip, videoPath); SqlHelper.AddVideoLog(log); } catch (System.Exception e) { Trace.TraceError(e.ToString()); } } }