Author Topic: Screenshare  (Read 1050 times)

0 Members and 1 Guest are viewing this topic.

Offline Real2Two

  • Newbie
  • Posts: 18
    • View Profile
Screenshare
« on: July 09, 2020, 12:49:51 pm »
Thanks to Ashish for helping me fix a bug which made the website not work.

I made a little screenshare program... it isn't perfect but it works:

host.bas
Code: QB64: [Select]
  1. dd = _OPENHOST("TCP/IP:80")
  2. crlf$ = CHR$(13) + CHR$(10)
  3.     c = _OPENCONNECTION(dd)
  4.     IF c <> 0 THEN
  5.         addr$ = _CONNECTIONADDRESS(c)
  6.         COLOR 15: PRINT "Client connected: ";: COLOR 12: PRINT addr$
  7.  
  8.         GET #c, , x$
  9.  
  10.         a = INSTR(x$, "GET ")
  11.         b = INSTR(a + 5, x$, " ")
  12.         request$ = RIGHT$(LEFT$(x$, b - 1), b - a - 4)
  13.         COLOR 14, 8
  14.         PRINT "request = ";
  15.         COLOR 0, 7: PRINT request$
  16.         IF LEN(request$) = 0 THEN PRINT x$
  17.         COLOR 7, 0
  18.  
  19.         SELECT CASE request$
  20.             CASE "/"
  21.                 ff = FREEFILE
  22.                 OPEN "index.html" FOR BINARY AS #ff
  23.                 a$ = SPACE$(LOF(ff))
  24.                 GET #ff, 1, a$
  25.                 CLOSE ff
  26.                 reply$ = "HTTP/1.0 200 OK" + crlf$ + crlf$
  27.                 reply$ = reply$ + a$
  28.             CASE "/screen"
  29.                 SaveImage _SCREENIMAGE, "screen"
  30.                 ff = FREEFILE
  31.                 OPEN "screen.bmp" FOR BINARY AS #ff
  32.                 a$ = SPACE$(LOF(ff))
  33.                 GET #ff, 1, a$
  34.                 CLOSE ff
  35.                 reply$ = "HTTP/1.0 200 OK" + crlf$
  36.                 reply$ = reply$ + "Content-Length:" + STR$(LEN(a$)) + crlf$
  37.                 reply$ = reply$ + "Content-Type: image/bmp" + crlf$ + crlf$
  38.                 reply$ = reply$ + a$
  39.             CASE ELSE
  40.                 FourOhFour:
  41.                 reply$ = "HTTP/1.0 404 Not found" + crlf$
  42.                 reply$ = reply$ + "Content-Length: 103" + crlf$
  43.                 reply$ = reply$ + "Content-Type: text/html" + crlf$ + crlf$
  44.                 reply$ = reply$ + "<html><head><title>404 Not Found</title></head><body><p>This page does not exist.</p></body></html>" + crlf$ + crlf$
  45.         END SELECT
  46.         PUT #c, , reply$
  47.         CLOSE c
  48.         c = 0
  49.         x$ = ""
  50.         request$ = ""
  51.         reply$ = ""
  52.     END IF
  53.     _LIMIT 30
  54.  
  55. SUB SaveImage (image AS LONG, filename AS STRING)
  56.     bytesperpixel& = _PIXELSIZE(image&)
  57.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  58.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  59.     x& = _WIDTH(image&)
  60.     y& = _HEIGHT(image&)
  61.     b$ = "BM????QB64????" + MKL$(40) + MKL$(x&) + MKL$(y&) + MKI$(1) + MKI$(bpp&) + MKL$(0) + "????" + STRING$(16, 0) 'partial BMP header info(???? to be filled later)
  62.     IF bytesperpixel& = 1 THEN
  63.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  64.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  65.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  66.         NEXT
  67.     END IF
  68.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  69.     lastsource& = _SOURCE
  70.     _SOURCE image&
  71.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  72.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  73.         r$ = ""
  74.         FOR px& = 0 TO x& - 1
  75.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  76.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  77.         NEXT px&
  78.         d$ = d$ + r$ + padder$
  79.     NEXT py&
  80.     _SOURCE lastsource&
  81.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  82.     b$ = b$ + d$ ' total file data bytes to create file
  83.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  84.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  85.     f& = FREEFILE
  86.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  87.     OPEN filename$ + ext$ FOR BINARY AS #f&
  88.     PUT #f&, , b$
  89.     CLOSE #f&
  90.  

index.html
Code: Text: [Select]
  1. <html>
  2.     <head>
  3.         <title>Screenshare</title>
  4.         <style>
  5.             body {
  6.                 margin: 0;
  7.             }
  8.         </style>
  9.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  10.     </head>
  11.     <body>
  12.         <div id="screen">
  13.         </div>
  14.         <script>
  15.             imageLoop()
  16.             function imageLoop() {
  17.                 document.getElementById("screen").innerHTML = `<img id="imgvar" src="/screen">`;
  18.                 setTimeout(function(){
  19.                     location.reload();
  20.                 }, 8000);
  21.             }
  22.         </script>
  23.     </body>
  24. </html>

Offline Real2Two

  • Newbie
  • Posts: 18
    • View Profile
Re: Screenshare
« Reply #1 on: July 09, 2020, 12:52:10 pm »
I tried to use JQuery instead of reload but I gave up.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Screenshare
« Reply #2 on: July 11, 2020, 12:22:55 am »
Awesome! It will work faster if you use png images instead of bmp, as bmp are usually larger in size.
I tried using Steve SaveImage 2.3 for saving image in .png format but the browser is not accepting it. IDK why, lol.

Good work! :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Screenshare
« Reply #3 on: July 11, 2020, 12:29:49 am »
Oh, I made it to share program own's screen. :P


 
Screenshot_1_.png



Here's the code

Code: QB64: [Select]
  1. 'to use color names
  2. SCREEN _NEWIMAGE(500, 500, 32)
  3. dd = _OPENHOST("TCP/IP:80")
  4. crlf$ = CHR$(13) + CHR$(10)
  5.     c = _OPENCONNECTION(dd)
  6.     IF c <> 0 THEN
  7.         addr$ = _CONNECTIONADDRESS(c)
  8.         COLOR White: PRINT "Client connected: ";: COLOR 12: PRINT addr$
  9.  
  10.         GET #c, , x$
  11.  
  12.         a = INSTR(x$, "GET ")
  13.         b = INSTR(a + 5, x$, " ")
  14.         request$ = RIGHT$(LEFT$(x$, b - 1), b - a - 4)
  15.         COLOR Yellow, DarkRed
  16.         PRINT "request = ";
  17.         COLOR Black, Silver: PRINT request$
  18.         IF LEN(request$) = 0 THEN PRINT x$
  19.         COLOR Yellow, Black
  20.  
  21.         SELECT CASE request$
  22.             CASE "/"
  23.                 ff = FREEFILE
  24.                 OPEN "index.html" FOR BINARY AS #ff
  25.                 a$ = SPACE$(LOF(ff))
  26.                 GET #ff, 1, a$
  27.                 CLOSE ff
  28.                 reply$ = "HTTP/1.0 200 OK" + crlf$ + crlf$
  29.                 reply$ = reply$ + a$
  30.             CASE "/screen"
  31.                 SaveImage _COPYIMAGE(0), "screen"
  32.                 ff = FREEFILE
  33.                 OPEN "screen.bmp" FOR BINARY AS #ff
  34.                 a$ = SPACE$(LOF(ff))
  35.                 GET #ff, 1, a$
  36.                 CLOSE ff
  37.                 reply$ = "HTTP/1.0 200 OK" + crlf$
  38.                 reply$ = reply$ + "Content-Length:" + STR$(LEN(a$)) + crlf$
  39.                 reply$ = reply$ + "Content-Type: image/bmp" + crlf$ + crlf$
  40.                 reply$ = reply$ + a$
  41.             CASE ELSE
  42.                 FourOhFour:
  43.                 reply$ = "HTTP/1.0 404 Not found" + crlf$
  44.                 reply$ = reply$ + "Content-Length: 103" + crlf$
  45.                 reply$ = reply$ + "Content-Type: text/html" + crlf$ + crlf$
  46.                 reply$ = reply$ + "<html><head><title>404 Not Found</title></head><body><p>This page does not exist.</p></body></html>" + crlf$ + crlf$
  47.         END SELECT
  48.         PUT #c, , reply$
  49.         CLOSE c
  50.         c = 0
  51.         x$ = ""
  52.         request$ = ""
  53.         reply$ = ""
  54.     END IF
  55.     _LIMIT 30
  56.  
  57. SUB SaveImage (image AS LONG, filename AS STRING)
  58.     bytesperpixel& = _PIXELSIZE(image&)
  59.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  60.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  61.     x& = _WIDTH(image&)
  62.     y& = _HEIGHT(image&)
  63.     b$ = "BM????QB64????" + MKL$(40) + MKL$(x&) + MKL$(y&) + MKI$(1) + MKI$(bpp&) + MKL$(0) + "????" + STRING$(16, 0) 'partial BMP header info(???? to be filled later)
  64.     IF bytesperpixel& = 1 THEN
  65.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  66.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  67.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  68.         NEXT
  69.     END IF
  70.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  71.     lastsource& = _SOURCE
  72.     _SOURCE image&
  73.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  74.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  75.         r$ = ""
  76.         FOR px& = 0 TO x& - 1
  77.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  78.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  79.         NEXT px&
  80.         d$ = d$ + r$ + padder$
  81.     NEXT py&
  82.     _SOURCE lastsource&
  83.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  84.     b$ = b$ + d$ ' total file data bytes to create file
  85.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  86.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  87.     f& = FREEFILE
  88.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  89.     OPEN filename$ + ext$ FOR BINARY AS #f&
  90.     PUT #f&, , b$
  91.     CLOSE #f&
  92.  


index.html

Code: Text: [Select]
  1. <html>
  2.     <head>
  3.         <title>Screenshare</title>
  4.         <style>
  5.             body {
  6.                 margin: 0;
  7.             }
  8.         </style>
  9.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  10.     </head>
  11.     <body>
  12.         <div id="screen">
  13.         </div>
  14.         <script>
  15.             imageLoop()
  16.             function imageLoop() {
  17.                 document.getElementById("screen").innerHTML = `<img id="imgvar" src="/screen">`;
  18.                 setTimeout(function(){
  19.                     location.reload();
  20.                 }, 4000);
  21.             }
  22.         </script>
  23.     </body>
  24. </html>
  25.  

I don't know, it still delays a little.
« Last Edit: July 11, 2020, 12:31:33 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials