Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;                                             ;
  3. ;    Tiny MP3 Shoutcast Server v0.1 (vt)      ;
  4. ;                                             ;
  5. ;    Compile with FASM for Menuet             ;
  6. ;                                             ;
  7. ;    Listening to port 8008                   ;
  8. ;    Connect with eg: 192.168.1.22:8008       ;
  9. ;                                             ;
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11.  
  12. version  equ  '0.3'
  13.  
  14.   use32
  15.   org     0x0
  16.   db      'MENUET01'    ; 8 byte id
  17.   dd      0x01          ; header version
  18.   dd      START         ; program start
  19.   dd      I_END         ; program image size
  20.   dd      0x80000       ; memory usage
  21.   dd      0x20000       ; stack
  22.   dd      0,0
  23.  
  24. include 'lang.inc'
  25. include 'macros.inc'
  26.  
  27. ; 0x0+      program image
  28. ; 0x1ffff   stack
  29. ; 0x20000   work area for file read
  30. ; 0x40000+  file send buffer ( 100 kb )
  31.  
  32.  
  33. START:                          ; start of execution
  34.  
  35.     mov  [status],0
  36.     call clear_input
  37.     call draw_window            ; at first, draw the window
  38.  
  39. still:
  40.  
  41.     mov  eax,23                 ; wait here for event
  42.     mov  ebx,2
  43.     int  0x40
  44.  
  45.     call check_events
  46.  
  47.     call check_connection_status
  48.  
  49.     cmp  [status],4
  50.     je   start_transmission
  51.  
  52.     jmp  still
  53.  
  54.  
  55. check_events:
  56.  
  57.     cmp  eax,1                  ; redraw request ?
  58.     jz   red
  59.     cmp  eax,2                  ; key in buffer ?
  60.     jz   key
  61.     cmp  eax,3                  ; button in buffer ?
  62.     jz   button
  63.  
  64.     ret
  65.  
  66. red:                           ; redraw
  67.     call draw_window
  68.     ret
  69.  
  70. key:
  71.     mov  eax,2                 ; Just read it and ignore
  72.     int  0x40
  73.     ret
  74.  
  75. button:                         ; button
  76.  
  77.     mov  eax,17                 ; get id
  78.     int  0x40
  79.  
  80.     cmp  ah,1                   ; close
  81.     jne  no_close
  82.     mov  eax,-1
  83.     int  0x40
  84.   no_close:
  85.  
  86.     cmp  ah,2                   ; button id=2 ?
  87.     jnz  tst3
  88.     ; open socket
  89.     mov  eax,53
  90.     mov  ebx,5
  91.     mov  ecx,8008   ; local port # - http
  92.     mov  edx,0      ; no remote port specified
  93.     mov  esi,0      ; no remote ip specified
  94.     mov  edi,0      ; PASSIVE open
  95.     int  0x40
  96.     mov  [socket], eax
  97.     mov  [posy],1
  98.     mov  [posx],0
  99.     mov  [read_on],1
  100.     call check_for_incoming_data
  101.     call draw_window
  102.     ret
  103.   tst3:
  104.  
  105.     cmp  ah,4
  106.     je   close_socket
  107.     cmp  ah,6
  108.     je   close_socket
  109.     jmp  no_socket_close
  110.   close_socket:
  111.     mov  edx,eax
  112.     ; Close socket
  113.     mov  eax, 53
  114.     mov  ebx, 8
  115.     mov  ecx, [socket]
  116.     int  0x40
  117.     mov  esp,0x1fff0
  118.     cmp  dh,6
  119.     je   read_string
  120.     jmp  still
  121.   no_socket_close:
  122.  
  123.     cmp  ah,9
  124.     jne  no_bps_add
  125.     add  [bps],8*1000
  126.     call draw_window
  127.     ret
  128.   no_bps_add:
  129.  
  130.     cmp  ah,8
  131.     jne  no_bps_sub
  132.     sub  [bps],8*1000
  133.     call draw_window
  134.     ret
  135.   no_bps_sub:
  136.  
  137.  
  138.     ret
  139.  
  140.  
  141. clear_input:
  142.  
  143.     mov  edi,input_text
  144.     mov  eax,0
  145.     mov  ecx,60*40
  146.     cld
  147.     rep  stosb
  148.  
  149.     ret
  150.  
  151.  
  152. read_string:
  153.  
  154.     mov  [addr],dword filename
  155.     mov  [ya],dword 95
  156.  
  157.     mov  edi,[addr]
  158.     mov  eax,32
  159.     mov  ecx,30
  160.     cld
  161.     rep  stosb
  162.  
  163.     call print_text
  164.  
  165.     mov  edi,[addr]
  166.  
  167.   f11:
  168.     mov  eax,10
  169.     int  0x40
  170.     cmp  eax,2
  171.     jne  read_done
  172.     mov  eax,2
  173.     int  0x40
  174.     shr  eax,8
  175.     cmp  eax,13
  176.     je   read_done
  177.     cmp  eax,8
  178.     jnz  nobsl
  179.     cmp  edi,[addr]
  180.     jz   f11
  181.     sub  edi,1
  182.     mov  [edi],byte 32
  183.     call print_text
  184.     jmp  f11
  185.   nobsl:
  186.     cmp  eax,dword 31
  187.     jbe  f11
  188.     cmp  eax,dword 95
  189.     jb   keyok
  190.     sub  eax,32
  191.   keyok:
  192.     mov  [edi],al
  193.  
  194.     call print_text
  195.  
  196.     add  edi,1
  197.     mov  esi,[addr]
  198.     add  esi,30
  199.     cmp  esi,edi
  200.     jnz  f11
  201.  
  202.   read_done:
  203.  
  204.     mov  ecx,40
  205.     mov  eax,0
  206.     cld
  207.     rep  movsb
  208.  
  209.     call print_text
  210.  
  211.     jmp  still
  212.  
  213.  
  214. print_text:
  215.  
  216.     pusha
  217.  
  218.     mov  eax,13
  219.     mov  ebx,56*65536+30*6
  220.     mov  ecx,[ya]
  221.     shl  ecx,16
  222.     mov  cx,8
  223.     mov  edx,0xffffff
  224.     int  0x40
  225.  
  226.     mov  eax,4
  227.     mov  edx,[addr]
  228.     mov  ebx,56*65536
  229.     add  ebx,[ya]
  230.     mov  ecx,0x000000
  231.     mov  esi,30
  232.     int  0x40
  233.  
  234.     popa
  235.     ret
  236.  
  237.  
  238. wait_for  dd 0x0
  239.  
  240. transmission_start  dd  0x0
  241. sentbytes           dd  0x0
  242.  
  243. start_transmission:
  244.  
  245.     call clear_input
  246.  
  247.     mov  eax,5
  248.     mov  ebx,50
  249.     int  0x40
  250.  
  251.     call check_for_incoming_data
  252.     call draw_window
  253.  
  254.     call send_header
  255.  
  256.     mov  [fileinfo+4],dword 0   ; start from beginning
  257.     mov  [read_to],0x40000
  258.     mov  [playpos],0x40000
  259.  
  260.     mov  ecx,1024 / 512
  261.  
  262.   new_buffer:
  263.  
  264.     mov  eax,[read_to]
  265.     mov  ebx,1
  266.     call read_file
  267.  
  268.     loop new_buffer
  269.  
  270.  
  271.   newpart:
  272.  
  273.     call check_connection_status
  274.     call draw_window
  275.  
  276.     mov  eax,26
  277.     mov  ebx,9
  278.     int  0x40
  279.     mov  [transmission_start],eax
  280.     mov  [sentbytes],0
  281.  
  282.   newblock:
  283.  
  284.     mov  eax,[read_to]
  285.     mov  ebx,2
  286.     call read_file
  287.  
  288.   wait_more:
  289.  
  290.     mov  eax,26
  291.     mov  ebx,9
  292.     int  0x40
  293.  
  294.     cmp  eax,[wait_for]
  295.     jge  nomw
  296.  
  297.     mov  eax,5
  298.     mov  ebx,1
  299.     int  0x40
  300.  
  301.     jmp  wait_more
  302.  
  303.   nomw:
  304.  
  305.     add  eax,2
  306.     mov  [wait_for],eax
  307.  
  308.     mov  eax,11
  309.     int  0x40
  310.     call check_events
  311.  
  312.     mov  eax,53
  313.     mov  ebx,255
  314.     mov  ecx,103
  315.     int  0x40
  316.  
  317.     cmp  eax,0
  318.     jne  wait_more
  319.  
  320.     ; write to socket
  321.     mov  eax,53
  322.     mov  ebx,7
  323.     mov  ecx,[socket]
  324.     mov  edx,[playadd]
  325.     mov  esi,[playpos]
  326.     int  0x40
  327.  
  328.     add  [sentbytes],edx
  329.  
  330.     mov  esi,[playpos]
  331.     add  esi,[playadd]
  332.     mov  edi,0x40000
  333.     mov  ecx,110000 / 4
  334.     cld
  335.     rep  movsd
  336.  
  337.     mov  eax,[playadd]
  338.     sub  [read_to],eax
  339.  
  340.     call check_for_incoming_data
  341.     call show_progress
  342.     call check_rate
  343.  
  344.     mov  eax, 53
  345.     mov  ebx, 6
  346.     mov  ecx, [socket]
  347.     int  0x40
  348.     cmp  eax,4
  349.     jne  end_stream
  350.  
  351.     cmp  [read_to],0x40000
  352.     jge  newblock
  353.  
  354.   end_stream:
  355.  
  356.     ; Close socket
  357.  
  358.     mov  eax, 53
  359.     mov  ebx, 8
  360.     mov  ecx, [socket]
  361.     int  0x40
  362.  
  363.     mov  eax,5
  364.     mov  ebx,5
  365.     int  0x40
  366.  
  367.     ; Open socket
  368.  
  369.     mov  eax,53
  370.     mov  ebx,5
  371.     mov  ecx,8008   ; local port # - http
  372.     mov  edx,0      ; no remote port specified
  373.     mov  esi,0      ; no remote ip specified
  374.     mov  edi,0      ; PASSIVE open
  375.     int  0x40
  376.     mov  [socket], eax
  377.     mov  [posy],1
  378.     mov  [posx],0
  379.     mov  [read_on],0
  380.  
  381.     call draw_window
  382.  
  383.     jmp  still
  384.  
  385.  
  386. check_rate:
  387.  
  388.     pusha
  389.  
  390.     mov  eax,[bps]
  391.     xor  edx,edx
  392.     mov  ebx,8*100
  393.     div  ebx
  394.     shl  eax,1
  395.     mov  [playadd],eax
  396.  
  397.     mov  eax,26
  398.     mov  ebx,9
  399.     int  0x40
  400.  
  401.     sub  eax,[transmission_start]
  402.     shr  eax,1
  403.  
  404.     imul eax,[playadd]
  405.  
  406.     mov  edx,0x00dd00
  407.  
  408.     cmp  [sentbytes],eax
  409.     jge  sendok
  410.  
  411.     sub  eax,20000
  412.     cmp  [sentbytes],eax        ; a long buffer underrun correction
  413.     jge  no_buffer_overrun      ; actually leads to overrun
  414.     mov  [sentbytes],eax
  415.   no_buffer_overrun:
  416.  
  417.     add  [playadd],150
  418.     mov  edx,0xdd0000
  419.  
  420.   sendok:
  421.  
  422.     mov  eax,13
  423.     mov  ebx,320*65536+10
  424.     mov  ecx,105*65536+10
  425.     int  0x40
  426.  
  427.     mov  eax,47
  428.     mov  ebx,4*65536
  429.     mov  ecx,[playadd]
  430.     mov  edx,322*65536+106
  431.     mov  esi,0x000000
  432. ;    int  0x40
  433.  
  434.     popa
  435.  
  436.     ret
  437.  
  438.  
  439. show_progress:
  440.  
  441.     pusha
  442.  
  443.     mov  eax,13
  444.     mov  ebx,236*65536+10*6
  445.     mov  ecx,107*65536+8
  446.     mov  edx,0xffffff
  447.     int  0x40
  448.  
  449.     mov  ecx,[fileinfo+4]
  450.     imul ecx,512
  451.  
  452.     mov  eax,47               ; file read
  453.     mov  ebx,9*65536
  454.     mov  edx,236*65536+107
  455.     mov  esi,0x000000
  456.     int  0x40
  457.  
  458.     popa
  459.     ret
  460.  
  461.  
  462. playpos  dd  0x100000
  463. playadd  dd  256000 / 8 / 100
  464.  
  465.  
  466. send_header:
  467.  
  468.     pusha
  469.  
  470.     mov   [playpos],0x40000
  471.  
  472.     mov   esi,fileinfo+5*4
  473.     mov   edi,transname
  474.     mov   ecx,30
  475.     cld
  476.     rep   movsb
  477.  
  478.     mov   eax, 53
  479.     mov   ebx, 7
  480.     mov   ecx, [socket]
  481.     mov   edx, headere-headers
  482.     mov   esi, headers
  483.     int   0x40
  484.  
  485.     popa
  486.     ret
  487.  
  488.  
  489. read_file:
  490.  
  491.     cmp  [read_to],0x40000+2000
  492.     jg   cache_ok
  493.     mov  [read_on],1
  494.   cache_ok:
  495.  
  496.     cmp  [read_to],0x40000+95500
  497.     jg   no_read_1
  498.  
  499.     mov  [fileinfo+12],eax
  500.     mov  [fileinfo+8],ebx
  501.  
  502.     mov  eax,58
  503.     mov  ebx,fileinfo
  504.     int  0x40
  505.  
  506.     cmp  eax,0
  507.     jne  no_read_1
  508.  
  509.     mov  eax,[fileinfo+8]
  510.     add  [fileinfo+4],eax
  511.  
  512.     add  [read_to],512*2
  513.  
  514.     ret
  515.  
  516.   no_read_1:
  517.  
  518.     mov  [read_on],0
  519.  
  520.     ret
  521.  
  522.  
  523.  
  524. check_for_incoming_data:
  525.  
  526.     pusha
  527.  
  528.     mov  eax, 53
  529.     mov  ebx, 2
  530.     mov  ecx, [socket]
  531.     int  0x40
  532.  
  533.     cmp  eax,0
  534.     je   _ret_now
  535.  
  536.   new_data:
  537.  
  538.     mov  eax, 53
  539.     mov  ebx, 2
  540.     mov  ecx, [socket]
  541.     int  0x40
  542.  
  543.     cmp  eax,0
  544.     je   _ret
  545.  
  546.     mov  eax,53
  547.     mov  ebx,3
  548.     mov  ecx,[socket]
  549.     int  0x40
  550.  
  551.     cmp  bl,10
  552.     jne  no_lf
  553.     inc  [posy]
  554.     mov  [posx],0
  555.     jmp  new_data
  556.   no_lf:
  557.  
  558.     cmp  bl,20
  559.     jb   new_data
  560.  
  561.     inc  [posx]
  562.     cmp  [posx],60
  563.     jbe  xok
  564.     inc  [posy]
  565.     mov  [posx],0
  566.   xok:
  567.  
  568.     cmp  [posy],12
  569.     jbe  yok
  570.     mov  [posy],1
  571.   yok:
  572.  
  573.     mov  eax,[posy]
  574.     imul eax,60
  575.     add  eax,[posx]
  576.  
  577.     mov  [input_text+eax],bl
  578.  
  579.     jmp  new_data
  580.  
  581.   _ret:
  582.  
  583. ;    call draw_window
  584.  
  585.   _ret_now:
  586.  
  587.     popa
  588.     ret
  589.  
  590.  
  591.  
  592. check_connection_status:
  593.  
  594.     pusha
  595.  
  596.     mov  eax, 53
  597.     mov  ebx, 6
  598.     mov  ecx, [socket]
  599.     int  0x40
  600.  
  601.     cmp  eax,[status]
  602.     je   .ccs_ret
  603.     mov  [status],eax
  604.     add  eax,48
  605.     mov  [text+20],al
  606.     call draw_window
  607.    .ccs_ret:
  608.  
  609.     popa
  610.     ret
  611.  
  612.  
  613.  
  614.  
  615. ;   *********************************************
  616. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  617. ;   *********************************************
  618.  
  619.  
  620. draw_window:
  621.  
  622.     pusha
  623.  
  624.     mov  eax,12                    ; function 12:tell os about windowdraw
  625.     mov  ebx,1                     ; 1, start of draw
  626.     int  0x40
  627.  
  628.     mov  eax,0                     ; Draw Window
  629.     mov  ebx,50*65536+410
  630.     mov  ecx,100*65536+141
  631.     mov  edx,0x03ffffff
  632.     mov  esi,0x00405080
  633.     mov  edi,0x00405080
  634.     int  0x40
  635.  
  636.     mov  eax,4                     ; Window label
  637.     mov  ebx,8*65536+8
  638.     mov  ecx,0x10ffffff
  639.     mov  edx,wls
  640.     mov  esi,wle-wls
  641.     int  0x40
  642.  
  643.     mov  eax,8                     ; Start server
  644.     mov  ebx,(25)*65536+21
  645.     mov  ecx,57*65536+10
  646.     mov  edx,2
  647.     mov  esi,0x409040
  648.     int  0x40                      ; Stop server
  649.     mov  eax,8
  650.     mov  ebx,(25)*65536+21
  651.     mov  ecx,69*65536+10
  652.     mov  edx,4
  653.     mov  esi,0x904040
  654.     int  0x40
  655.  
  656.     mov  esi,0x3366d0
  657.  
  658.     mov  eax,8                     ; Enter filename
  659.     mov  ebx,(25)*65536+21
  660.     mov  ecx,93*65536+10
  661.     mov  edx,6
  662.     int  0x40
  663.     mov  eax,8                     ; Decrease transfer rate
  664.     mov  ebx,(25)*65536+10
  665.     mov  ecx,105*65536+10
  666.     mov  edx,8
  667.     int  0x40
  668.     mov  eax,8                     ; Increase transfer rate
  669.     mov  ebx,(36)*65536+10
  670.     mov  ecx,105*65536+10
  671.     mov  edx,9
  672.     int  0x40
  673.  
  674.     mov  ebx,10*65536+35           ; draw info text
  675.     mov  ecx,0x00000000
  676.     mov  edx,text
  677.     mov  esi,40
  678.   newline:
  679.     mov  eax,4
  680.     int  0x40
  681.     add  ebx,12
  682.     add  edx,40
  683.     cmp  [edx],byte 'x'
  684.     jnz  newline
  685.  
  686.     mov  eax,4                     ; Filename
  687.     mov  ebx,56*65536+95
  688.     mov  ecx,0x000000
  689.     mov  edx,filename
  690.     mov  esi,30
  691.     int  0x40
  692.  
  693.     mov  eax,[bps]
  694.     xor  edx,edx
  695.     mov  ebx,1000
  696.     div  ebx
  697.     mov  ecx,eax
  698.  
  699.     mov  eax,47
  700.     mov  ebx,3*65536
  701.     mov  edx,58*65536+107
  702.     mov  esi,0x00000000
  703.     int  0x40
  704.  
  705.     mov  [input_text+0],dword 'RECE'
  706.     mov  [input_text+4],dword 'IVED'
  707.     mov  [input_text+8],dword ':   '
  708.  
  709.     mov  ebx,230*65536+35           ; draw info text
  710.     mov  ecx,0x00000000
  711.     mov  edx,input_text
  712.     mov  esi,28
  713.     mov  edi,7
  714.    newline2:
  715.     mov  eax,4
  716.     int  0x40
  717.     add  ebx,10
  718.     add  edx,60
  719.     dec  edi
  720.     jnz  newline2
  721.  
  722.     mov  eax,38
  723.     mov  ebx,210*65536+210
  724.     mov  ecx,22*65536+136
  725.     mov  edx,0x6699cc ; 002288
  726.     int  0x40
  727.  
  728.     mov  eax,38
  729.     mov  ebx,211*65536+211
  730.     mov  ecx,22*65536+136
  731.     mov  edx,0x336699 ; 002288
  732.     int  0x40
  733.  
  734.     mov  eax,12                    ; function 12:tell os about windowdraw
  735.     mov  ebx,2                     ; 2, end of draw
  736.     int  0x40
  737.  
  738.     popa
  739.  
  740.     ret
  741.  
  742.  
  743.  
  744. ; DATA AREA
  745.  
  746. text:
  747.    db '        TCB status: 0                   '
  748.    db '                                        '
  749.    db '        Activate - port 8008            '
  750.    db '        Stop server                     '
  751.    db '                                        '
  752.    db '    >                                   '
  753.    db '   < >      Kbps                        '
  754.    db 'x';  <- END MARKER, DONT DELETE
  755.  
  756. headers:
  757.  
  758.   db   'ICY 200 OK',13,10
  759.   db   'icy-notice1:This stream requires Winamp or xmms',13,10
  760.   db   'icy-url:http://www.menuetos.org',13,10
  761.   db   'icy-pub: 1',13,10
  762.   db   'icy-name: Menuet Mp3 Shoutcast Radio ',version,' - '
  763.  transname:
  764.   db   '                              ',13,10,13,10
  765.  
  766. headere:
  767.  
  768. wls:  db   'MP3 shoutcast server ',version
  769. wle:
  770.  
  771. socket   dd  0
  772. status   dd  0
  773.  
  774. posy     dd  1
  775. posx     dd  0
  776.  
  777. read_on  db  1
  778. read_to  dd  0
  779.  
  780. addr     dd  0
  781. ya       dd  0
  782.  
  783. bps      dd  128*1000
  784.  
  785. fileinfo:  dd  0,0,0,0,0x20000
  786. filename:  db  '/RD/1/MENUET.MP3',0
  787. times 50   db  0
  788.  
  789. input_text:
  790.  
  791. I_END:
  792.