@@ -678,6 +678,17 @@ func sendBanner(w http.ResponseWriter, r *http.Request) {
678678 _ = json .NewEncoder (w ).Encode (okResponse )
679679}
680680
681+ // newJsonRpcNotification creates a JSON-RPC notification message
682+ func newJsonRpcNotification (message string ) map [string ]interface {} {
683+ return map [string ]interface {}{
684+ "jsonrpc" : "2.0" ,
685+ "method" : "notification/message" ,
686+ "params" : map [string ]string {
687+ "message" : message ,
688+ },
689+ }
690+ }
691+
681692func handleScreenCapture (w http.ResponseWriter , params json.RawMessage ) error {
682693
683694 _ = http .NewResponseController (w ).SetWriteDeadline (time .Now ().Add (10 * time .Minute ))
@@ -693,41 +704,59 @@ func handleScreenCapture(w http.ResponseWriter, params json.RawMessage) error {
693704 return fmt .Errorf ("error finding device: %w" , err )
694705 }
695706
696- if screenCaptureParams .Format == "" || screenCaptureParams .Format != "mjpeg" {
697- return fmt .Errorf ("format must be 'mjpeg' for screen capture" )
707+ // Set default format if not provided
708+ if screenCaptureParams .Format == "" {
709+ screenCaptureParams .Format = "mjpeg"
710+ }
711+
712+ // Validate format
713+ if screenCaptureParams .Format != "mjpeg" && screenCaptureParams .Format != "avc" {
714+ return fmt .Errorf ("format must be 'mjpeg' or 'avc' for screen capture" )
715+ }
716+
717+ // AVC format is only supported on Android
718+ if screenCaptureParams .Format == "avc" && targetDevice .Platform () != "android" {
719+ return fmt .Errorf ("avc format is only supported on Android devices" )
698720 }
699721
700722 // Set defaults if not provided
701723 quality := screenCaptureParams .Quality
702724 if quality == 0 {
703- quality = devices .DefaultMJPEGQuality
725+ quality = devices .DefaultQuality
704726 }
705727
706728 scale := screenCaptureParams .Scale
707729 if scale == 0.0 {
708- scale = devices .DefaultMJPEGScale
730+ scale = devices .DefaultScale
709731 }
710732
711- // Set headers for streaming response
712- w .Header ().Set ("Content-Type" , "multipart/x-mixed-replace; boundary=BoundaryString" )
733+ // Set headers for streaming response based on format
734+ if screenCaptureParams .Format == "mjpeg" {
735+ w .Header ().Set ("Content-Type" , "multipart/x-mixed-replace; boundary=BoundaryString" )
736+ } else {
737+ // avc format
738+ w .Header ().Set ("Content-Type" , "video/h264" )
739+ }
713740 w .Header ().Set ("Cache-Control" , "no-cache" )
714741 w .Header ().Set ("Connection" , "keep-alive" )
715742 w .Header ().Set ("Transfer-Encoding" , "chunked" )
716743
717744 // progress callback sends JSON-RPC notifications through the MJPEG stream
718- progressCallback := func (message string ) {
719- notification := map [string ]interface {}{
720- "jsonrpc" : "2.0" ,
721- "method" : "notification/message" ,
722- "params" : map [string ]string {
723- "message" : message ,
724- },
725- }
726- statusJSON , _ := json .Marshal (notification )
727- mimeMessage := fmt .Sprintf ("--BoundaryString\r \n Content-Type: application/json\r \n Content-Length: %d\r \n \r \n %s\r \n " , len (statusJSON ), statusJSON )
728- _ , _ = w .Write ([]byte (mimeMessage ))
729- if flusher , ok := w .(http.Flusher ); ok {
730- flusher .Flush ()
745+ // only used for MJPEG format, not for AVC
746+ var progressCallback func (string )
747+ if screenCaptureParams .Format == "mjpeg" {
748+ progressCallback = func (message string ) {
749+ notification := newJsonRpcNotification (message )
750+ statusJSON , err := json .Marshal (notification )
751+ if err != nil {
752+ log .Printf ("Failed to marshal progress message: %v" , err )
753+ return
754+ }
755+ mimeMessage := fmt .Sprintf ("--BoundaryString\r \n Content-Type: application/json\r \n Content-Length: %d\r \n \r \n %s\r \n " , len (statusJSON ), statusJSON )
756+ _ , _ = w .Write ([]byte (mimeMessage ))
757+ if flusher , ok := w .(http.Flusher ); ok {
758+ flusher .Flush ()
759+ }
731760 }
732761 }
733762
0 commit comments