• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.googlecode.android_scripting.facade.webcam;
18 
19 import com.googlecode.android_scripting.Log;
20 import com.googlecode.android_scripting.SimpleServer;
21 
22 import java.io.OutputStream;
23 import java.net.Socket;
24 
25 class MjpegServer extends SimpleServer {
26 
27     private final JpegProvider mProvider;
28 
MjpegServer(JpegProvider provider)29     MjpegServer(JpegProvider provider) {
30         mProvider = provider;
31     }
32 
33     @Override
handleConnection(Socket socket)34     protected void handleConnection(Socket socket) throws Exception {
35         Log.d("handle Mjpeg connection");
36         byte[] data = mProvider.getJpeg();
37         if (data == null) {
38             return;
39         }
40         OutputStream outputStream = socket.getOutputStream();
41         outputStream.write((
42                 "HTTP/1.0 200 OK\r\n"
43                         + "Server: SL4A\r\n"
44                         + "Connection: close\r\n"
45                         + "Max-Age: 0\r\n"
46                         + "Expires: 0\r\n"
47                         + "Cache-Control: no-cache, private\r\n"
48                         + "Pragma: no-cache\r\n"
49                         + "Content-Type: multipart/x-mixed-replace; "
50                         + "boundary=--BoundaryString\r\n\r\n").getBytes());
51         while (true) {
52             data = mProvider.getJpeg();
53             if (data == null) {
54                 return;
55             }
56             outputStream.write("--BoundaryString\r\n".getBytes());
57             outputStream.write("Content-type: image/jpg\r\n".getBytes());
58             outputStream.write(("Content-Length: " + data.length + "\r\n\r\n").getBytes());
59             outputStream.write(data);
60             outputStream.write("\r\n\r\n".getBytes());
61             outputStream.flush();
62         }
63     }
64 }
65