#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private WiFi As MLwifi
Public Stream1 As AsyncStreams
Public Client1 As Socket
Public local As Boolean
Private mHost As String
Private mPath As String= "/?action=stream"
Private Data(1000000) As Byte
Private index As Int
Private bc As ByteConverter
Private boundary As String
Public curpos As Int =90
End Sub
Sub Connect
If Stream1.IsInitialized Then Stream1.Close
If Client1.IsInitialized Then Client1.Close
If WiFi.IsWifiConnected= True And (WiFi.SSID="JetStream" Or WiFi.SSID="JetStream1") Then
local=True
Client1.Initialize("Client1")
Client1.Connect("192.168.2.4",8080, 20000)
mHost="192.168.2.4:8080"
Else
local=False
Client1.Initialize("Client1")
Client1.Connect("00.000.000.00",44000, 20000)
mHost=00.000.000.00:44000"
End If
End Sub
Sub Service_Create
Connect
End Sub
Sub Service_Start (StartingIntent As Intent)
'Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub
Sub Service_Destroy
End Sub
Sub Client1_Connected (Successful As Boolean)
If Successful Then
If Stream1.IsInitialized Then Stream1.Close
Stream1.Initialize(Client1.InputStream,Client1.OutputStream,"Client1")
If local =True Then
ToastMessageShow("Verbinding via WiFi", True)
Else
ToastMessageShow("Verbinding via internet", True)
End If
boundary = ""
Dim sTmp As String = $"GET ${mPath} HTTP/1.1
Host: ${mHost}
Connection: keep-alive
"$
Successful= Stream1.Write(sTmp.Replace(Chr(10), CRLF).GetBytes("UTF8"))
Else
Log("Connection error")
Return
End If
End Sub
Private Sub Client1_NewData (Buffer() As Byte)
If IsPaused(Main) =False Then
bc.ArrayCopy(Buffer, 0, Data, index, Buffer.Length)
index = index + Buffer.Length
If boundary = "" Then
Dim i1 As Int = IndexOfString("Content-Type", 0)
If i1 > -1 Then
Dim i2 As Int = IndexOfString(CRLF, i1 + 1)
If i2 > -1 Then
Dim ct As String = BytesToString(Data, i1, i2 - i1, "ASCII")
Dim b As Int = ct.IndexOf("=")
boundary = ct.SubString(b + 1)
End If
End If
Else
Dim b1 As Int = IndexOfString(boundary, 0)
If b1 > -1 Then
Dim b2 As Int = IndexOfString(boundary, b1 + 1)
If b2 > -1 Then
Dim startframe As Int = IndexOf(Array As Byte(0xff, 0xd8), b1)
Dim endframe As Int = IndexOf(Array As Byte(0xff, 0xd9), b2 - 10)
If startframe > -1 And endframe > -1 Then
Dim In As InputStream
In.InitializeFromBytesArray(Data, startframe, endframe - startframe + 2)
CallSub2 (Main,"Display",In)
End If
TrimArray(b2)
End If
End If
End If
End If
End Sub
Private Sub TrimArray (i As Int)
bc.ArrayCopy(Data, i, Data, 0, index - i)
index = index - i
End Sub
Private Sub Stream1_Error
Log("Stream1 Error")
End Sub