@@ -3,29 +3,30 @@ import Struct, { placeholder } from "@yume-chan/struct";
3
3
import { AndroidCodecLevel , AndroidCodecProfile } from "../codec" ;
4
4
import { ScrcpyClientConnection , ScrcpyClientForwardConnection , ScrcpyClientReverseConnection } from "../connection" ;
5
5
import { AndroidKeyEventAction , ScrcpyControlMessageType } from "../message" ;
6
- import { ScrcpyLogLevel , ScrcpyOptions , ScrcpyScreenOrientation , toScrcpyOption , ToScrcpyOption } from "./common" ;
6
+ import { ScrcpyLogLevel , ScrcpyOptions , ScrcpyOptionValue , ScrcpyScreenOrientation , toScrcpyOptionValue } from "./common" ;
7
7
8
8
export interface CodecOptionsType {
9
9
profile : AndroidCodecProfile ;
10
10
11
11
level : AndroidCodecLevel ;
12
12
}
13
13
14
- export class CodecOptions implements ToScrcpyOption {
15
- public value : CodecOptionsType ;
16
-
17
- public constructor ( {
18
- profile = AndroidCodecProfile . Baseline ,
19
- level = AndroidCodecLevel . Level4 ,
20
- } : Partial < CodecOptionsType > ) {
21
- this . value = {
22
- profile,
23
- level,
24
- } ;
14
+ export class CodecOptions implements ScrcpyOptionValue {
15
+ public value : Partial < CodecOptionsType > ;
16
+
17
+ public constructor ( value : Partial < CodecOptionsType > ) {
18
+ this . value = value ;
25
19
}
26
20
27
- public toScrcpyOption ( ) : string {
28
- return Object . entries ( this . value )
21
+ public toOptionValue ( ) : string | undefined {
22
+ const entries = Object . entries ( this . value )
23
+ . filter ( ( [ key , value ] ) => value !== undefined ) ;
24
+
25
+ if ( entries . length === 0 ) {
26
+ return undefined ;
27
+ }
28
+
29
+ return entries
29
30
. map ( ( [ key , value ] ) => `${ key } =${ value } ` )
30
31
. join ( ',' ) ;
31
32
}
@@ -53,9 +54,7 @@ export interface ScrcpyOptions1_16Type {
53
54
54
55
tunnelForward : boolean ;
55
56
56
- // Because Scrcpy 1.21 changed the empty value from '-' to '',
57
- // We mark properties which can be empty with `| undefined`
58
- crop : string | undefined ;
57
+ crop : string ;
59
58
60
59
sendFrameMeta : boolean ;
61
60
@@ -67,60 +66,30 @@ export interface ScrcpyOptions1_16Type {
67
66
68
67
stayAwake : boolean ;
69
68
70
- codecOptions : CodecOptions | undefined ;
69
+ codecOptions : CodecOptions ;
71
70
72
- encoderName : string | undefined ;
71
+ encoderName : string ;
73
72
}
74
73
75
74
export const ScrcpyBackOrScreenOnEvent1_16 =
76
75
new Struct ( )
77
76
. uint8 ( 'type' , placeholder < ScrcpyControlMessageType . BackOrScreenOn > ( ) ) ;
78
77
79
- export class ScrcpyOptions1_16 < T extends ScrcpyOptions1_16Type = ScrcpyOptions1_16Type > implements ScrcpyOptions {
80
- public value : T ;
81
-
82
- public constructor ( {
83
- logLevel = ScrcpyLogLevel . Error ,
84
- maxSize = 0 ,
85
- bitRate = 8_000_000 ,
86
- maxFps = 0 ,
87
- lockVideoOrientation = ScrcpyScreenOrientation . Unlocked ,
88
- tunnelForward = false ,
89
- crop,
90
- sendFrameMeta = true ,
91
- control = true ,
92
- displayId = 0 ,
93
- showTouches = false ,
94
- stayAwake = true ,
95
- codecOptions,
96
- encoderName,
97
- } : Partial < ScrcpyOptions1_16Type > ) {
78
+ export class ScrcpyOptions1_16 < T extends ScrcpyOptions1_16Type = ScrcpyOptions1_16Type > implements ScrcpyOptions < T > {
79
+ public value : Partial < T > ;
80
+
81
+ public constructor ( value : Partial < ScrcpyOptions1_16Type > ) {
98
82
if ( new . target === ScrcpyOptions1_16 &&
99
- logLevel === ScrcpyLogLevel . Verbose ) {
100
- logLevel = ScrcpyLogLevel . Debug ;
83
+ value . logLevel === ScrcpyLogLevel . Verbose ) {
84
+ value . logLevel = ScrcpyLogLevel . Debug ;
101
85
}
102
86
103
87
if ( new . target === ScrcpyOptions1_16 &&
104
- lockVideoOrientation === ScrcpyScreenOrientation . Initial ) {
105
- lockVideoOrientation = ScrcpyScreenOrientation . Unlocked ;
88
+ value . lockVideoOrientation === ScrcpyScreenOrientation . Initial ) {
89
+ value . lockVideoOrientation = ScrcpyScreenOrientation . Unlocked ;
106
90
}
107
91
108
- this . value = {
109
- logLevel,
110
- maxSize,
111
- bitRate,
112
- maxFps,
113
- lockVideoOrientation,
114
- tunnelForward,
115
- crop,
116
- sendFrameMeta,
117
- control,
118
- displayId,
119
- showTouches,
120
- stayAwake,
121
- codecOptions,
122
- encoderName,
123
- } as T ;
92
+ this . value = value as Partial < T > ;
124
93
}
125
94
126
95
protected getArgumnetOrder ( ) : ( keyof T ) [ ] {
@@ -142,22 +111,29 @@ export class ScrcpyOptions1_16<T extends ScrcpyOptions1_16Type = ScrcpyOptions1_
142
111
] ;
143
112
}
144
113
145
- public formatServerArguments ( ) : string [ ] {
146
- return this . getArgumnetOrder ( ) . map ( key => {
147
- return toScrcpyOption ( this . value [ key ] , '-' ) ;
148
- } ) ;
114
+ protected getDefaultValue ( ) : T {
115
+ return {
116
+ logLevel : ScrcpyLogLevel . Error ,
117
+ maxSize : 0 ,
118
+ bitRate : 8_000_000 ,
119
+ maxFps : 0 ,
120
+ lockVideoOrientation : ScrcpyScreenOrientation . Unlocked ,
121
+ tunnelForward : false ,
122
+ crop : '-' ,
123
+ sendFrameMeta : true ,
124
+ control : true ,
125
+ displayId : 0 ,
126
+ showTouches : false ,
127
+ stayAwake : true ,
128
+ codecOptions : new CodecOptions ( { } ) ,
129
+ encoderName : '-' ,
130
+ } as T ;
149
131
}
150
132
151
- public formatGetEncoderListArguments ( ) : string [ ] {
152
- return this . getArgumnetOrder ( ) . map ( key => {
153
- if ( key === 'encoderName' ) {
154
- // Provide an invalid encoder name
155
- // So the server will return all available encoders
156
- return '_' ;
157
- }
158
-
159
- return toScrcpyOption ( this . value [ key ] , '-' ) ;
160
- } ) ;
133
+ public formatServerArguments ( ) : string [ ] {
134
+ const defaults = this . getDefaultValue ( ) ;
135
+ return this . getArgumnetOrder ( )
136
+ . map ( key => toScrcpyOptionValue ( this . value [ key ] || defaults [ key ] , '-' ) ) ;
161
137
}
162
138
163
139
public createConnection ( device : Adb ) : ScrcpyClientConnection {
0 commit comments