├── .gitignore
├── .vscode
├── launch.json
└── settings.json
├── CONTRIBUTING.md
├── LICENSE
├── Makefile
├── README.md
├── android-emulator-webrtc.code-workspace
├── babel.config.js
├── cloudbuild.yaml
├── cloudbuilders
├── .gcloudignore
├── Dockerfile
├── README.md
└── cloudbuild.yaml
├── emulator
└── index.js
├── eslint_prefix.py
├── gen_md_doc.js
├── npmrc.enc
├── package-lock.json
├── package.json
├── proto
├── README.MD
├── emulator_controller.proto
└── rtc_service.proto
├── protoc-plugin
├── Makefile
└── grpc_generator.cc
├── src
├── components
│ └── emulator
│ │ ├── emulator.js
│ │ ├── net
│ │ ├── emulator_status.js
│ │ ├── jsep_protocol_driver.js
│ │ └── logcat.js
│ │ └── views
│ │ ├── event_handler.js
│ │ ├── simple_png_view.js
│ │ └── webrtc_view.js
├── index.js
└── proto
│ └── emulator_web_client.js
└── test
├── cloudbuild.yaml
├── emulator.test.js
├── event_handler.test.js
├── fake_events.js
├── jsep_protocol_driver.test.js
├── simple_png_view.test.js
└── touch_event_handler.test.js
/.gitignore:
--------------------------------------------------------------------------------
1 | src/proto/*pb.js
2 |
3 | # distribution
4 | # dist
5 |
6 | # dependencies
7 | node_modules
8 | .pnp
9 | .pnp.js
10 | *.o
11 |
12 | # testing
13 | coverage
14 |
15 | # production
16 | build
17 |
18 | # yalc
19 | .yalc
20 | yalc.lock
21 |
22 | # misc
23 | .DS_Store
24 | .env.local
25 | .env.development.local
26 | .env.test.local
27 | .env.production.local
28 |
29 | npm-debug.log*
30 | yarn-debug.log*
31 | yarn-error.log*
32 | protoc-gen-grpc-web
33 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "type": "node",
9 | "name": "vscode-jest-tests",
10 | "request": "launch",
11 | "args": [
12 | "--runInBand"
13 | ],
14 | "cwd": "${workspaceFolder}",
15 | "console": "integratedTerminal",
16 | "internalConsoleOptions": "neverOpen",
17 | "disableOptimisticBPs": true,
18 | "program": "${workspaceFolder}/node_modules/jest/bin/jest"
19 | }
20 | ]
21 | }
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools"
3 | }
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to Contribute
2 |
3 | We'd love to accept your patches and contributions to this project. There are
4 | just a few small guidelines you need to follow.
5 |
6 | ## Contributor License Agreement
7 |
8 | Contributions to this project must be accompanied by a Contributor License
9 | Agreement. You (or your employer) retain the copyright to your contribution;
10 | this simply gives us permission to use and redistribute your contributions as
11 | part of the project. Head over to to see
12 | your current agreements on file or to sign a new one.
13 |
14 | You generally only need to submit a CLA once, so if you've already submitted one
15 | (even if it was for a different project), you probably don't need to do it
16 | again.
17 |
18 | ## Code reviews
19 |
20 | All submissions, including submissions by project members, require review. We
21 | use GitHub pull requests for this purpose. Consult
22 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
23 | information on using pull requests.
24 |
25 | ## Community Guidelines
26 |
27 | This project follows
28 | [Google's Open Source Community Guidelines](https://opensource.google.com/conduct/).
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # Copyright (C) 2019 The Android Open Source Project
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | #
15 | PROTOC = protoc
16 | PROTOC_CMD = which $(PROTOC)
17 | PYTHON = python
18 |
19 | # Check that the minimum protocol version >3.6
20 | PROTOHEADER = $(shell pkg-config --variable prefix protobuf)/include
21 | PROTOLIB = $(shell pkg-config --variable prefix protobuf)/lib
22 |
23 | HAS_PROTOC = $(shell $(PROTOC_CMD) > /dev/null && echo true || echo false)
24 | HAS_DEP = $(shell which $(DEP) > /dev/null && echo true || echo false)
25 |
26 | # Prefixing works differently on mac vs linux, so let us account for that.
27 |
28 | UNAME_S := $(shell uname -s)
29 | PREFIX_ESLINT = $(PYTHON) eslint_prefix.py
30 |
31 | ifeq ($(HAS_PROTOC),true)
32 | ifneq (,$(wildcard $(PROTOHEADER)/google/protobuf/compiler/code_generator.h))
33 | HAS_VALID_PROTOC := true
34 | else
35 | HAS_VALID_PROTOC := false
36 | endif
37 | endif
38 |
39 | SYSTEM_OK = false
40 | ifeq ($(HAS_VALID_PROTOC),true)
41 | SYSTEM_OK = true
42 | endif
43 |
44 | MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
45 | CURRENT_DIR := $(abspath $(MAKEFILE_PATH)/..)
46 | # Protobuf settings. If you are running this in the AOSP tree you will want to run ninja install first.
47 | PROTODIR := $(CURRENT_DIR)/src/proto/
48 | PROTOSRCDIR := $(CURRENT_DIR)/proto
49 | PROTO_SRC := $(wildcard $(PROTOSRCDIR)/*.proto)
50 | PROTO_OBJS := $(addprefix $(PROTODIR)/, $(notdir $(PROTO_SRC:.proto=_pb.js)))
51 | PROXY_OBJS := $(addprefix $(PROTODIR)/, $(notdir $(PROTO_SRC:.proto=_grpc_web_pb.js)))
52 |
53 |
54 | CXX = g++
55 | CPPFLAGS += -I$(PROTOHEADER) -pthread
56 | CXXFLAGS += -std=c++11
57 | LDFLAGS += -L$(PROTOLIB) -lprotoc -lprotobuf -lpthread -ldl
58 |
59 | .PHONY: build-release run-release develop stop
60 |
61 | all: check
62 |
63 | clean:
64 | rm -rf $(PROTODIR)/*pb.js
65 |
66 | $(PROTODIR):
67 | @mkdir -p $(PROTODIR)
68 |
69 | # Protobuf --> js (note technically this produces 2 files.) which is not
70 | # the way you are supposed to do things in gnumake
71 | # https://www.gnu.org/software/automake/manual/html_node/Multiple-Outputs.html
72 | # Use sed to insert the /* eslint-disable */ at the start of the file
73 | # \`$$`\n` forces a real newline char, because make.
74 | $(PROTODIR)/%_pb.js : $(PROTOSRCDIR)/%.proto $(PROTODIR) protoc-gen-grpc-web
75 | $(PROTOC) \
76 | -I/usr/local/include -I$(PROTODIR) -I$(PROTOSRCDIR) \
77 | --plugin=protoc-gen-grpc-web=$(CURRENT_DIR)/protoc-gen-grpc-web \
78 | --js_out=import_style=commonjs,binary:$(PROTODIR) \
79 | --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$(PROTODIR) \
80 | $<
81 | $(PREFIX_ESLINT) $@
82 |
83 | # Fix up the proxies _grpc_web files by prefixing /* eslint-disable */
84 | # Not evey source produces a _grpc_web, so we onle do the replace if them file exists, otherwise it is a nop.
85 | $(PROTODIR)/%_grpc_web_pb.js : $(PROTODIR)/%_pb.js
86 | @test -f $@ && $(PREFIX_ESLINT) $@ || true
87 |
88 | protoc: $(PROTO_OBJS) $(PROXY_OBJS)
89 |
90 | protoc-gen-grpc-web: protoc-plugin/grpc_generator.o
91 | $(CXX) $^ $(LDFLAGS) -o $@
92 |
93 | deps: system-check protoc
94 | @npm install
95 |
96 | build: deps
97 | @npm run build
98 |
99 | check: build
100 | @npm run test
101 |
102 | system-check:
103 | ifneq ($(HAS_VALID_PROTOC),true)
104 | @echo " DEPENDENCY ERROR"
105 | @echo
106 | @echo "You don't have protoc 3.6.0 > installed in your path."
107 | @echo "Please install Google protocol buffers 3.6.0> and its compiler."
108 | @echo "You can find it here:"
109 | @echo
110 | @echo " https://github.com/google/protobuf/releases/tag/v3.6.0"
111 | @echo " or try $ brew install protobuf"
112 | @echo " or sudo apt-get install libprotoc-dev protobuf-compiler"
113 | @echo
114 | @echo "Here is what I get when trying to evaluate your version of protoc:"
115 | @echo
116 | -$(PROTOC) --version
117 | @echo
118 | @echo "And here is where I am looking for the headers: $(PROTOHEADER)/google/protobuf"
119 | @echo
120 | endif
121 | ifneq ($(SYSTEM_OK),true)
122 | @false
123 | endif
124 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # android-emulator-webrtc
2 |
3 | This contains a set of React components that can be used to interact with the android emulator from the browser. It is
4 | intended to be used with an [envoy proxy](https://blog.envoyproxy.io/envoy-and-grpc-web-a-fresh-new-alternative-to-rest-6504ce7eb880)
5 | that is connected to a running emulator.
6 |
7 | See the [android container](https://github.com/google/android-emulator-container-scripts) scripts for an example on how to run
8 | an emulator that is accessible via the web.
9 |
10 | ```
11 | npm install --save android-emulator-webrtc
12 | ```
13 |
14 | [Full reference](#full-reference)
15 |
16 | ## Features
17 |
18 | - Display and interact with android emulator over the web, including audio if available.
19 | - Retrieve logcat from remote emulator.
20 | - Retrieve emulator status
21 |
22 | ---
23 |
24 | ## Usage
25 |
26 | You can connect to remote unsecured emulator as follows:
27 |
28 | ```js
29 | import { Emulator } from "android-emulator-webrtc/emulator";
30 |
31 | class EmulatorScreen extends React.Component {
32 | render() {
33 | return ;
34 | }
35 | }
36 | ```
37 |
38 | In order to connect to a secure endpoint you will have to provide an authorization service that provides the following functions:
39 |
40 | - `authHeader()` which must return a set of headers that should be send along with a request. For example:
41 |
42 | ```js
43 | authHeader = () => {
44 | return { Authorization: 'Some Token' };
45 | };
46 | }
47 | ```
48 |
49 | - `unauthorized()` a function that gets called when a 401 was received. Here you can provide logic to handle token refresh, re-login etc.
50 |
51 | For example:
52 |
53 | ```js
54 | import { Emulator } from "android-emulator-webrtc/emulator";
55 |
56 | class EmulatorScreen extends React.Component {
57 | render() {
58 | return ;
59 | }
60 | }
61 | ```
62 |
63 | ---
64 |
65 | ## Full Reference
66 |
67 | ## Emulator
68 |
69 | A React component that displays a remote android emulator.
70 |
71 | The emulator will mount a png or webrtc view component to display the current state
72 | of the emulator. It will translate mouse events on this component and send them
73 | to the actual emulator.
74 |
75 | #### Authentication Service
76 |
77 | The authentication service should implement the following methods:
78 |
79 | - `authHeader()` which must return a set of headers that should be send along with a request.
80 | - `unauthorized()` a function that gets called when a 401 was received.
81 |
82 | #### Type of view
83 |
84 | You usually want this to be webrtc as this will make use of the efficient
85 | webrtc implementation. The png view will request screenshots, which are
86 | very slow, and require the envoy proxy. You should not use this for remote emulators.
87 |
88 | #### Pressing hardware buttons
89 |
90 | This component has a method `sendKey` to sends a key to the emulator.
91 | You can use this to send physical hardwar events to the emulator for example:
92 |
93 | "AudioVolumeDown" - Decreases the audio volume.
94 | "AudioVolumeUp" - Increases the audio volume.
95 | "Power" - The Power button or key, turn off the device.
96 | "AppSwitch" - Should bring up the application switcher dialog.
97 | "GoHome" - Go to the home screen.
98 | "GoBack" - Open the previous screen you were looking at.
99 |
100 | | prop | type | default | required | description |
101 | | ---------------------- | :---------------------: | :-----------------------------------------------: | :----------------: | ----------------------------------------------------------------------------------------------------------------------------------------------- |
102 | | **auth** | `Object` | `null` | :x: | The authentication service to use, or null for no authentication. |
103 | | **height** | `Number` | | :x: | The height of the component |
104 | | **muted** | `Boolean` | `true` | :x: | True if the audio should be disabled. This is only relevant when using the webrtc engine. |
105 | | **onAudioStateChange** | `Function` | `(s) => { console.log("emulator audio: " + s); }` | :x: | Called when the audio becomes (un)available. True if audio is available, false otherwise. |
106 | | **onError** | `Function` | `(e) => { console.error(e); }` | :x: | Callback that will be invoked in case of gRPC errors. |
107 | | **onStateChange** | `Function` | `(s) => { console.log("emulator state: " + s); }` | :x: | Called upon state change, one of ["connecting", "connected", "disconnected"] |
108 | | **gps** | | | :x: | A [GeolocationCoordinates](https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates) like object indicating where the device is. |
109 | | **poll** | `Boolean` | `false` | :x: | True if polling should be used, only set this to true if you are using the go webgrpc proxy. |
110 | | **uri** | `String` | | :white_check_mark: | gRPC Endpoint where we can reach the emulator. |
111 | | **view** | `Enum("webrtc", "png")` | `"webrtc"` | :x: | The underlying view used to display the emulator, one of ["webrtc", "png"] |
112 | | **volume** | `Number` | `1.0` | :x: | Volume between [0, 1] when audio is enabled. 0 is muted, 1.0 is 100% |
113 | | **width** | `Number` | | :x: | The width of the component |
114 |
115 | **Note**: The user must have interacted with the page before you can set the volume to "unmuted" (muted = false). Otherwise the video
116 | will not play and will throw an error, which is currently not handled.
117 |
118 | **Note**: The volume is the volume of the video element that is displayed, this is not the actual volume used inside the emulator. You can change the audio inside the emulator by sending the proper keys as documented
119 | above, or follow the steps in the [support](https://support.google.com/android/answer/9082609?hl=en) document
120 | on how to change the audio volume.
121 |
122 | ---
123 |
124 |
125 |
126 | ## EmulatorStatus
127 |
128 | **Kind**: global class
129 |
130 | - [EmulatorStatus](#EmulatorStatus)
131 | - [new EmulatorStatus()](#new_EmulatorStatus_new)
132 | - [.getStatus](#EmulatorStatus.getStatus)
133 | - [.updateStatus](#EmulatorStatus.updateStatus)
134 |
135 |
136 |
137 | ### new EmulatorStatus()
138 |
139 | Gets the status of the emulator, parsing the hardware config into something
140 | easy to digest.
141 |
142 | | Param | Type | Description |
143 | | ------------- | --------------------------------------------- | --------------------- |
144 | | uriOrEmulator | string/EmulatorControllerService
| uri to gRPC endpoint. |
145 | | auth | object
| authorization class. |
146 |
147 |
148 |
149 | ### EmulatorStatus.getStatus
150 |
151 | Gets the cached status.
152 |
153 | **Kind**: static property of [EmulatorStatus
](#EmulatorStatus)
154 |
155 |
156 | ### EmulatorStatus.updateStatus
157 |
158 | Retrieves the current status from the emulator.
159 |
160 | **Kind**: static property of [EmulatorStatus
](#EmulatorStatus)
161 |
162 | | Param | Type | Description |
163 | | -------- | --------------------- | ----------------------------------------------------------- |
164 | | fnNotify | Callback
| when the status is available, returns the retrieved status. |
165 | | cache | boolean
| True if the cache can be used. |
166 |
167 |
168 |
169 | ## Logcat
170 |
171 | Observe the logcat stream from the emulator.
172 |
173 | Streaming is done by either polling the emulator endpoint or making a streaming call.
174 |
175 | It will send out the following events:
176 |
177 | - `start` whenever the start method was called.
178 | - `data` whenever new data became available.
179 | - `end` whenever the stream is finished, either because it was stopped, or due to an error.
180 |
181 | **Kind**: global class
182 |
183 | - [Logcat](#Logcat)
184 | - [new Logcat(uriOrEmulator, auth)](#new_Logcat_new)
185 | - [.on](#Logcat.on)
186 | - [.off](#Logcat.off)
187 | - [.stop](#Logcat.stop)
188 | - [.start](#Logcat.start)
189 |
190 |
191 |
192 | ### new Logcat(uriOrEmulator, auth)
193 |
194 | Creates a logcat stream.
195 |
196 | The authentication service should implement the following methods:
197 |
198 | - `authHeader()` which must return a set of headers that should be send along with a request.
199 | - `unauthorized()` a function that gets called when a 401 was received.
200 |
201 | | Param | Type |
202 | | ------------- | ------------------- |
203 | | uriOrEmulator | object
|
204 | | auth | object
|
205 |
206 |
207 |
208 | ### Logcat.on
209 |
210 | Register a listener.
211 |
212 | **Kind**: static property of [Logcat
](#Logcat)
213 |
214 | | Param | Type | Description |
215 | | ----- | --------------------- | -------------------------------------- |
216 | | name | string
| Name of the event. |
217 | | fn | Callback
| Function to notify on the given event. |
218 |
219 |
220 |
221 | ### Logcat.off
222 |
223 | Removes a listener.
224 |
225 | **Kind**: static property of [Logcat
](#Logcat)
226 |
227 | | Param | Type | Description |
228 | | ----- | --------------------- | -------------------------------------- |
229 | | name | string
| Name of the event. |
230 | | fn | Callback
| Function to notify on the given event. |
231 |
232 |
233 |
234 | ### Logcat.stop
235 |
236 | Cancel the currently active logcat stream.
237 |
238 | **Kind**: static property of [Logcat
](#Logcat)
239 |
240 |
241 | ### Logcat.start
242 |
243 | Requests the logcat stream, invoking the callback when a log line arrives.
244 |
245 | _Note:_ Streaming can cause serious UI delays, so best not to use it.
246 |
247 | **Kind**: static property of [Logcat
](#Logcat)
248 |
249 | | Param | Type | Description |
250 | | ----------- | --------------------- | ---------------------------------------------------- |
251 | | fnNotify | Callback
| when a new log line arrives. |
252 | | refreshRate | number
| polling interval, or 0 if you wish to use streaming. |
253 |
--------------------------------------------------------------------------------
/android-emulator-webrtc.code-workspace:
--------------------------------------------------------------------------------
1 | {
2 | "folders": [
3 | {
4 | "path": "."
5 | }
6 | ]
7 | }
--------------------------------------------------------------------------------
/babel.config.js:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | module.exports = {
4 | presets: [
5 | [
6 | "@babel/preset-env",
7 | {
8 | targets: {
9 | node: "current",
10 | },
11 | },
12 | ],
13 | "@babel/preset-react",
14 | ],
15 | plugins: [["@babel/transform-runtime"]],
16 | };
17 |
--------------------------------------------------------------------------------
/cloudbuild.yaml:
--------------------------------------------------------------------------------
1 | steps:
2 | - name: "gcr.io/cloud-builders/gcloud"
3 | args:
4 | - kms
5 | - decrypt
6 | - --ciphertext-file=npmrc.enc
7 | - --plaintext-file=/root/.npmrc
8 | - --location=global
9 | - --keyring=my-keyring
10 | - --key=npm-key
11 | volumes:
12 | - name: "home"
13 | path: /root/
14 | - name: gcr.io/emu-dev-cts/protoc
15 | args:
16 | [
17 | "--include_imports",
18 | "--include_source_info",
19 | "--proto_path",
20 | "./proto",
21 | "--descriptor_set_out",
22 | "api_descriptor.pb",
23 | "--plugin=protoc-gen-grpc-web=/protoc/bin/protoc-gen-grpc-web",
24 | "--js_out=import_style=commonjs:src/proto",
25 | "--grpc-web_out=import_style=commonjs,mode=grpcwebtext:src/proto",
26 | "./proto/emulator_controller.proto",
27 | "./proto/rtc_service.proto",
28 | ]
29 | - name: "gcr.io/cloud-builders/npm"
30 | args: ["install"]
31 | - name: "gcr.io/cloud-builders/npm"
32 | args: ["run", "build"]
33 | - name: "gcr.io/cloud-builders/npm"
34 | args: ["test"]
35 | - name: "gcr.io/cloud-builders/npm"
36 | args: ["publish"]
37 | env:
38 | - HOME=/root/
39 | volumes:
40 | - name: "home"
41 | path: /root/
42 |
--------------------------------------------------------------------------------
/cloudbuilders/.gcloudignore:
--------------------------------------------------------------------------------
1 | # Empty
2 |
--------------------------------------------------------------------------------
/cloudbuilders/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM debian:buster-slim
2 |
3 | ARG VERS=23.1
4 | ARG ARCH=linux-x86_64
5 | ARG GRPC_WEB=1.4.2
6 |
7 | RUN echo "Building protoc Cloud Builder ${VERS}-${ARCH}" && \
8 | apt-get update -y && apt-get upgrade -y && \
9 | apt-get install wget unzip -y && \
10 | apt-get clean -y && \
11 | rm -rf /var/lib/apt/lists/* && \
12 | wget "https://github.com/protocolbuffers/protobuf/releases/download/v${VERS}/protoc-${VERS}-${ARCH}.zip" && \
13 | unzip "protoc-${VERS}-${ARCH}.zip" -d protoc && \
14 | wget "https://github.com/grpc/grpc-web/releases/download/${GRPC_WEB}/protoc-gen-grpc-web-${GRPC_WEB}-${ARCH}" -O /protoc/bin/protoc-gen-grpc-web && \
15 | chmod a+x /protoc/bin/protoc-gen-grpc-web && \
16 | rm "protoc-${VERS}-${ARCH}.zip"
17 |
18 | ENV PATH=$PATH:/protoc/bin/
19 | ENTRYPOINT ["protoc"]
20 | CMD ["--help"]
21 |
--------------------------------------------------------------------------------
/cloudbuilders/README.md:
--------------------------------------------------------------------------------
1 | # protoc with grpc-web
2 |
3 | This tool defines a custom build step that allows the Cloud Build worker to run the
4 | [protocol buffer compiler](https://github.com/protocolbuffers/protobuf), `protoc`.
5 | with the protoc-gen-grpc-web plugin. You will need this builder to successfully compile
6 | the distribution in the cloud.
7 |
8 | ## Building this builder
9 |
10 | You will need to build this Builder and push it to a container registry before you may use it.
11 | To build and push to Google Container Registry, run the following command in this directory:
12 |
13 | ```bash
14 | gcloud builds submit . --config=cloudbuild.yaml
15 | ```
16 |
17 | If you wish to specify a different version or architecture for the build, run the following:
18 |
19 | ```bash
20 | gcloud builds submit . --config=cloudbuild.yaml --substitutions=_VERS=${VERS},_ARCH=${ARCH}
21 | ```
22 |
23 | Where `${VERS}` and `${ARCH}` are defined to contain values for the release and architecture as listed on:
24 |
25 | https://github.com/protocolbuffers/protobuf/releases
26 |
27 | **NB** Due to inconsistent handling of URLs for release candidates, the build will fail when
28 | referencing these ([issue](https://github.com/protocolbuffers/protobuf/issues/6522)).
29 |
30 |
--------------------------------------------------------------------------------
/cloudbuilders/cloudbuild.yaml:
--------------------------------------------------------------------------------
1 | substitutions:
2 | _VERS: "3.12.4"
3 | _ARCH: linux-x86_64
4 |
5 | steps:
6 | - name: 'gcr.io/cloud-builders/docker'
7 | args:
8 | - build
9 | - "--build-arg=VERS=${_VERS}"
10 | - "--build-arg=ARCH=${_ARCH}"
11 | - --tag=gcr.io/${PROJECT_ID}/protoc:${_VERS}-${_ARCH}
12 | - --tag=gcr.io/${PROJECT_ID}/protoc:latest
13 | - "."
14 |
15 | images:
16 | - "gcr.io/${PROJECT_ID}/protoc:${_VERS}-${_ARCH}"
17 | - "gcr.io/${PROJECT_ID}/protoc:latest"
18 |
19 | tags: ['cloud-builders-community']
20 |
--------------------------------------------------------------------------------
/emulator/index.js:
--------------------------------------------------------------------------------
1 | module.exports = require('../dist')
--------------------------------------------------------------------------------
/eslint_prefix.py:
--------------------------------------------------------------------------------
1 | # Copyright 2019 The Android Open Source Project
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | import sys
15 | """This script prefixes the /* eslint-disable */ to the given file.
16 |
17 | For example:
18 |
19 | python eslint_prefix.py foo.js
20 |
21 | Will prefix /* eslint-disable */ to the file foo.js.
22 | """
23 |
24 | def main(argv):
25 | prefix = '/* eslint-disable */'
26 | fname = argv[1]
27 | text = ''
28 | with open(fname, 'r') as fn:
29 | text = fn.read()
30 |
31 | with open(fname, 'w') as fn:
32 | fn.write(prefix)
33 | fn.write('\n')
34 | fn.write(text)
35 |
36 | if __name__ == '__main__':
37 | main(sys.argv)
--------------------------------------------------------------------------------
/gen_md_doc.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 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 | const path = require("path");
17 | const fs = require("fs");
18 | const reactDocgen = require("react-docgen");
19 | const ReactDocGenMarkdownRenderer = require("react-docgen-markdown-renderer");
20 | const componentPath = path.join(
21 | __dirname,
22 | "src/components/emulator/emulator.js"
23 | );
24 | const renderer = new ReactDocGenMarkdownRenderer();
25 |
26 | fs.readFile(componentPath, (error, content) => {
27 | const documentationPath =
28 | path.basename(componentPath, path.extname(componentPath)) +
29 | renderer.extension;
30 | console.log(documentationPath);
31 |
32 | const doc = reactDocgen.parse(content);
33 | const md = renderer.render(componentPath, doc, []);
34 | fs.writeFile(documentationPath, md, (err) => {
35 | if (err) {
36 | console.error(err);
37 | return;
38 | }
39 | });
40 | });
41 |
--------------------------------------------------------------------------------
/npmrc.enc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google/android-emulator-webrtc/d55ff0e6eb7444f3696a04436e8e3cc9b845db27/npmrc.enc
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "android-emulator-webrtc",
3 | "version": "1.0.18",
4 | "description": "Android Emulator WebRTC module",
5 | "scripts": {
6 | "build": "babel src -d dist",
7 | "test": "jest"
8 | },
9 | "babel": {
10 | "presets": [
11 | "@babel/preset-env",
12 | "@babel/preset-react"
13 | ],
14 | "plugins": [
15 | "@babel/plugin-proposal-class-properties"
16 | ]
17 | },
18 | "jest": {
19 | "verbose": true,
20 | "transformIgnorePatterns": [
21 | "/node_modules/@juggle/resize-observer/"
22 | ]
23 | },
24 | "dependencies": {
25 | "google-protobuf": "^3.21.2",
26 | "grpc-web": "^1.4.2",
27 | "prop-types": "^15.8.1"
28 | },
29 | "peerDependencies": {
30 | "react": ">=16.8.0",
31 | "react-dom": ">=16.8.0"
32 | },
33 | "devDependencies": {
34 | "@babel/cli": "^7.21.5",
35 | "@babel/core": "^7.21.8",
36 | "@babel/plugin-proposal-class-properties": "^7.18.6",
37 | "@babel/plugin-transform-runtime": "^7.21.4",
38 | "@babel/preset-env": "^7.21.5",
39 | "@babel/preset-react": "^7.18.6",
40 | "@testing-library/jest-dom": "^5.16.5",
41 | "@testing-library/react": "^14.0.0",
42 | "babel-core": "7.0.0-bridge.0",
43 | "babel-jest": "^29.5.0",
44 | "babel-loader": "^9.1.2",
45 | "babel-polyfill": "^6.26.0",
46 | "jest": "^29.5.0",
47 | "jest-environment-jsdom": "^29.5.0",
48 | "react": "^18.2.0",
49 | "react-docgen-markdown-renderer": "^2.1.3",
50 | "react-dom": "^18.2.0",
51 | "resize-observer-polyfill": "^1.5.1",
52 | "yalc": "^1.0.0-pre.53"
53 | },
54 | "repository": {
55 | "type": "git",
56 | "url": "git+https://github.com/google/android-emulator-webrtc.git"
57 | },
58 | "keywords": [
59 | "webrtc",
60 | "android",
61 | "emulator"
62 | ],
63 | "author": "Erwin Jansen (https://github.com/pokowaka/)",
64 | "license": "Apache-2.0",
65 | "bugs": {
66 | "url": "https://github.com/google/android-emulator-container-scripts/issues"
67 | },
68 | "homepage": "https://github.com/google/android-emulator-container-scripts#js/android-emulator-webrtc/README.MD"
69 | }
70 |
--------------------------------------------------------------------------------
/proto/README.MD:
--------------------------------------------------------------------------------
1 | This contains all the protobuf files that are shipped with the emulator.
2 | These were taken at sha b5705ca57ad61b81413aca46d82baf5ceeaeed8c
3 |
--------------------------------------------------------------------------------
/proto/emulator_controller.proto:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2018 The Android Open Source Project
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | // Note that if you add/remove methods in this file you must update
16 | // the metrics sql as well ./android/scripts/gen-grpc-sql.py
17 | //
18 | // Please group deleted methods in a block including the date (MM/DD/YY)
19 | // it was removed. This enables us to easily keep metrics around after removal
20 | //
21 | // List of deleted methods
22 | // rpc iWasDeleted (03/12/12)
23 | // ...
24 | syntax = "proto3";
25 |
26 | option java_multiple_files = true;
27 | option java_package = "com.android.emulator.control";
28 | option objc_class_prefix = "AEC";
29 |
30 | package android.emulation.control;
31 | import "google/protobuf/empty.proto";
32 |
33 | // An EmulatorController service lets you control the emulator.
34 | // Note that this is currently an experimental feature, and that the
35 | // service definition might change without notice. Use at your own risk!
36 | //
37 | // We use the following rough conventions:
38 | //
39 | // streamXXX --> streams values XXX (usually for emulator lifetime). Values
40 | // are updated as soon as they become available.
41 | // getXXX --> gets a single value XXX
42 | // setXXX --> sets a single value XXX, does not returning state, these
43 | // usually have an observable lasting side effect.
44 | // sendXXX --> send a single event XXX, possibly returning state information.
45 | // android usually responds to these events.
46 | service EmulatorController {
47 | // set/get/stream the sensor data
48 | rpc streamSensor(SensorValue) returns (stream SensorValue) {}
49 | rpc getSensor(SensorValue) returns (SensorValue) {}
50 | rpc setSensor(SensorValue) returns (google.protobuf.Empty) {}
51 |
52 | // set/get/stream the physical model, this is likely the one you are
53 | // looking for when you wish to modify the device state.
54 | rpc setPhysicalModel(PhysicalModelValue) returns (google.protobuf.Empty) {}
55 | rpc getPhysicalModel(PhysicalModelValue) returns (PhysicalModelValue) {}
56 | rpc streamPhysicalModel(PhysicalModelValue)
57 | returns (stream PhysicalModelValue) {}
58 |
59 | // Atomically set/get the current primary clipboard data.
60 | rpc setClipboard(ClipData) returns (google.protobuf.Empty) {}
61 | rpc getClipboard(google.protobuf.Empty) returns (ClipData) {}
62 |
63 | // Streams the current data on the clipboard. This will immediately produce
64 | // a result with the current state of the clipboard after which the stream
65 | // will block and wait until a new clip event is available from the guest.
66 | // Calling the setClipboard method above will not result in generating a
67 | // clip event. It is possible to lose clipboard events if the clipboard
68 | // updates very rapidly.
69 | rpc streamClipboard(google.protobuf.Empty) returns (stream ClipData) {}
70 |
71 | // Set/get the battery to the given state.
72 | rpc setBattery(BatteryState) returns (google.protobuf.Empty) {}
73 | rpc getBattery(google.protobuf.Empty) returns (BatteryState) {}
74 |
75 | // Set the state of the gps.
76 | // Note: Setting the gps position will not be reflected in the user
77 | // interface. Keep in mind that android usually only samples the gps at 1
78 | // hz.
79 | rpc setGps(GpsState) returns (google.protobuf.Empty) {}
80 |
81 | // Gets the latest gps state as delivered by the setGps call, or location ui
82 | // if active.
83 | //
84 | // Note: this is not necessarily the actual gps coordinate visible at the
85 | // time, due to gps sample frequency (usually 1hz).
86 | rpc getGps(google.protobuf.Empty) returns (GpsState) {}
87 |
88 | // Simulate a touch event on the finger print sensor.
89 | rpc sendFingerprint(Fingerprint) returns (google.protobuf.Empty) {}
90 |
91 | // Send a keyboard event. Translating the event.
92 | rpc sendKey(KeyboardEvent) returns (google.protobuf.Empty) {}
93 |
94 | // Send touch/mouse events. Note that mouse events can be simulated
95 | // by touch events.
96 | rpc sendTouch(TouchEvent) returns (google.protobuf.Empty) {}
97 | rpc sendMouse(MouseEvent) returns (google.protobuf.Empty) {}
98 | rpc injectWheel(stream WheelEvent) returns (google.protobuf.Empty) {}
99 |
100 | // Make a phone call.
101 | rpc sendPhone(PhoneCall) returns (PhoneResponse) {}
102 |
103 | // Sends an sms message to the emulator.
104 | rpc sendSms(SmsMessage) returns (PhoneResponse) {}
105 |
106 | // Sends an sms message to the emulator.
107 | rpc setPhoneNumber(PhoneNumber) returns (PhoneResponse) {}
108 |
109 | // Retrieve the status of the emulator. This will contain general
110 | // hardware information, and whether the device has booted or not.
111 | rpc getStatus(google.protobuf.Empty) returns (EmulatorStatus) {}
112 |
113 | // Gets an individual screenshot in the desired format.
114 | //
115 | // The image will be scaled to the desired ImageFormat, while maintaining
116 | // the aspect ratio. The returned image will never exceed resolution of the
117 | // device display. Not setting the width or height (i.e. they are 0) will
118 | // result in using the display width and height.
119 | //
120 | // The resulting image will be properly oriented and can be displayed
121 | // directly without post processing. For example, if the device has a
122 | // 1080x1920 screen and is in landscape mode and called with no width or
123 | // height parameter, it will return a 1920x1080 image.
124 | //
125 | // The dimensions of the returned image will never exceed the corresponding
126 | // display dimensions. For example, this method will return a 1920x1080
127 | // screenshot, if the display resolution is 1080x1920 and a screenshot of
128 | // 2048x2048 is requested when the device is in landscape mode.
129 | //
130 | // This method will return an empty image if the display is not visible.
131 | rpc getScreenshot(ImageFormat) returns (Image) {}
132 |
133 | // Streams a series of screenshots in the desired format.
134 | //
135 | // A new frame will be delivered whenever the device produces a new frame.
136 | // Beware that this can produce a significant amount of data and that
137 | // certain translations can be very costly. For example, streaming a series
138 | // of png images is very cpu intensive.
139 | //
140 | // Images are produced according to the getScreenshot API described above.
141 | //
142 | // If the display is inactive, or becomes inactive, an empty image will be
143 | // delivered. Images will be delived again if the display becomes active and
144 | // new frames are produced.
145 | rpc streamScreenshot(ImageFormat) returns (stream Image) {}
146 |
147 | // Streams a series of audio packets in the desired format.
148 | // A new frame will be delivered whenever the emulated device
149 | // produces a new audio frame. You can expect packets to be
150 | // delivered in intervals of 20-30ms.
151 | //
152 | // Be aware that this can block when the emulator does not
153 | // produce any audio whatsoever!
154 | rpc streamAudio(AudioFormat) returns (stream AudioPacket) {}
155 |
156 | // Injects a series of audio packets to the android microphone.
157 | // A new frame will be delivered whenever the emulated device
158 | // requests a new audio frame. Audio is usually delivered at a rate
159 | // that the emulator is requesting frames. Audio will be stored in a
160 | // temporary buffer that can hold 300ms of audio.
161 | //
162 | // Notes:
163 | // - Only the first audio format packet that is delivered will be
164 | // honored. There is no need to send the audio format multiple times.
165 | // - Real time audio currently immediately overrides the buffer. This
166 | // means you must provide a constant rate of audio packets. The real
167 | // time mode is experimental. Timestamps of audio packets might be
168 | // used in the future to improve synchronization.
169 | //
170 | // - INVALID_ARGUMENT (code 3) The sampling rate was too high/low
171 | // - INVALID_ARGUMENT (code 3) The audio packet was too large to handle.
172 | // - FAILED_PRECONDITION (code 9) If there was a microphone registered
173 | // already.
174 | rpc injectAudio(stream AudioPacket) returns (google.protobuf.Empty) {}
175 |
176 | // Returns the last 128Kb of logcat output from the emulator
177 | // Note that parsed logcat messages are only available after L (Api >23).
178 | // it is possible that the logcat buffer gets overwritten, or falls behind.
179 | rpc getLogcat(LogMessage) returns (LogMessage) {}
180 |
181 | // Streams the logcat output from the emulator. The first call
182 | // can retrieve up to 128Kb. This call will not return.
183 | // Note that parsed logcat messages are only available after L (Api >23)
184 | // it is possible that the logcat buffer gets overwritten, or falls behind.
185 | rpc streamLogcat(LogMessage) returns (stream LogMessage) {}
186 |
187 | // Transition the virtual machine to the desired state. Note that
188 | // some states are only observable. For example you cannot transition
189 | // to the error state.
190 | rpc setVmState(VmRunState) returns (google.protobuf.Empty) {}
191 |
192 | // Gets the state of the virtual machine.
193 | rpc getVmState(google.protobuf.Empty) returns (VmRunState) {}
194 |
195 | // Atomically changes the current multi-display configuration.
196 | // After this call the given display configurations will be activated. You
197 | // can only update secondary displays. Displays with id 0 will be ignored.
198 | //
199 | // This call can result in the removal or addition of secondary displays,
200 | // the final display state can be observed by the returned configuration.
201 | //
202 | // The following gRPC error codes can be returned:
203 | // - FAILED_PRECONDITION (code 9) if the AVD does not support a
204 | // configurable
205 | // secondary display.
206 | // - INVALID_ARGUMENT (code 3) if:
207 | // - The same display id is defined multiple times.
208 | // - The display configurations are outside valid ranges.
209 | // See DisplayConfiguration for details on valid ranges.
210 | // - INTERNAL (code 13) if there was an internal emulator failure.
211 | rpc setDisplayConfigurations(DisplayConfigurations)
212 | returns (DisplayConfigurations) {}
213 |
214 | // Returns all currently valid logical displays.
215 | //
216 | // The gRPC error code FAILED_PRECONDITION (code 9) is returned if the AVD
217 | // does not support a configurable secondary display.
218 | rpc getDisplayConfigurations(google.protobuf.Empty)
219 | returns (DisplayConfigurations) {}
220 |
221 | // Notifies client of the following changes:
222 | //
223 | // - Virtual scene camera status change.
224 | // - Display configuration changes from extended ui. This will only be fired
225 | // if the user makes modifications the extended displays through the
226 | // extended control tab.
227 | //
228 | // Note that this method will send the initial virtual scene state
229 | // immediately.
230 | rpc streamNotification(google.protobuf.Empty)
231 | returns (stream Notification) {}
232 |
233 | // RotationRadian is relative to the camera's current orientation.
234 | rpc rotateVirtualSceneCamera(RotationRadian)
235 | returns (google.protobuf.Empty) {}
236 | // Velocity is absolute
237 | rpc setVirtualSceneCameraVelocity(Velocity)
238 | returns (google.protobuf.Empty) {}
239 | // Set foldable posture
240 | rpc setPosture(Posture) returns (google.protobuf.Empty) {}
241 |
242 | // Get the backlight brightness.
243 | // The following gRPC error codes can be returned:
244 | // - FAILED_PRECONDITION (code 9) if the AVD does not support hw-control.
245 | rpc getBrightness(BrightnessValue) returns (BrightnessValue) {}
246 |
247 | // Set the backlight brightness.
248 | // The following gRPC error codes can be returned:
249 | // - FAILED_PRECONDITION (code 9) if the AVD does not support hw-control.
250 | // - INVALID_ARGUMENT (code 3) The brightness exceeds the valid range.
251 | rpc setBrightness(BrightnessValue) returns (google.protobuf.Empty) {}
252 |
253 | // Returns the current mode of the primary display of a resizable AVD.
254 | // The following gRPC error codes can be returned:
255 | // - FAILED_PRECONDITION (code 9) if the AVD is not resizable.
256 | rpc getDisplayMode(google.protobuf.Empty) returns (DisplayMode) {}
257 |
258 | // Sets the size of the primary display of a resizable AVD. Fails if the AVD
259 | // is not resizable. The following gRPC error codes can be returned:
260 | // - FAILED_PRECONDITION (code 9) if the AVD is not resizable.
261 | rpc setDisplayMode(DisplayMode) returns (google.protobuf.Empty) {}
262 | }
263 |
264 | // A Run State that describes the state of the Virtual Machine.
265 | message VmRunState {
266 | enum RunState {
267 | // The emulator is in an unknown state. You cannot transition to this
268 | // state.
269 | UNKNOWN = 0;
270 | // Guest is actively running. You can transition to this state from the
271 | // paused state.
272 | RUNNING = 1;
273 | // Guest is paused to load a snapshot. You cannot transition to this
274 | // state.
275 | RESTORE_VM = 2;
276 | // Guest has been paused. Transitioning to this state will pause the
277 | // emulator the guest will not be consuming any cpu cycles.
278 | PAUSED = 3;
279 | // Guest is paused to take or export a snapshot. You cannot
280 | // transition to this state.
281 | SAVE_VM = 4;
282 | // System shutdown, note that it is similar to power off. It tries to
283 | // set the system status and notify guest. The system is likely going to
284 | // disappear soon and do proper cleanup of resources, possibly taking
285 | // a snapshot. This is the same behavior as closing the emulator by
286 | // clicking the X (close) in the user interface.
287 | SHUTDOWN = 5;
288 | // Immediately terminate the emulator. No resource cleanup will take
289 | // place. There is a good change to corrupt the system.
290 | TERMINATE = 7;
291 | // Will cause the emulator to reset. This is not a state you can
292 | // observe.
293 | RESET = 9;
294 | // Guest experienced some error state, you cannot transition to this
295 | // state.
296 | INTERNAL_ERROR = 10;
297 | }
298 |
299 | RunState state = 1;
300 | }
301 |
302 | message ParameterValue {
303 | repeated float data = 1 [packed = true];
304 | }
305 |
306 | message PhysicalModelValue {
307 | enum State {
308 | OK = 0;
309 | NO_SERVICE = -3; // qemud service is not available/initiated.
310 | DISABLED = -2; // Sensor is disabled.
311 | UNKNOWN = -1; // Unknown sensor (should not happen)
312 | }
313 |
314 | // Details on the sensors documentation can be found here:
315 | // https://developer.android.com/reference/android/hardware/Sensor.html#TYPE_
316 | // The types must follow the order defined in
317 | // "external/qemu/android/hw-sensors.h"
318 | enum PhysicalType {
319 | POSITION = 0;
320 |
321 | // All values are angles in degrees.
322 | // values = [x,y,z]
323 | ROTATION = 1;
324 |
325 | MAGNETIC_FIELD = 2;
326 |
327 | // Temperature in °C
328 | TEMPERATURE = 3;
329 |
330 | // Proximity sensor distance measured in centimeters
331 | PROXIMITY = 4;
332 |
333 | // Ambient light level in SI lux units
334 | LIGHT = 5;
335 |
336 | // Atmospheric pressure in hPa (millibar)
337 | PRESSURE = 6;
338 |
339 | // Relative ambient air humidity in percent
340 | HUMIDITY = 7;
341 |
342 | VELOCITY = 8;
343 | AMBIENT_MOTION = 9;
344 |
345 | // Describing a hinge angle sensor in degrees.
346 | HINGE_ANGLE0 = 10;
347 | HINGE_ANGLE1 = 11;
348 | HINGE_ANGLE2 = 12;
349 |
350 | ROLLABLE0 = 13;
351 | ROLLABLE1 = 14;
352 | ROLLABLE2 = 15;
353 |
354 | // Describing the device posture; the value should be an enum defined
355 | // in Posture::PostureValue.
356 | POSTURE = 16;
357 |
358 | // Heart rate in bpm
359 | HEART_RATE = 17;
360 |
361 | // Ambient RGBC light intensity. Values are in order (Red, Green, Blue,
362 | // Clear).
363 | RGBC_LIGHT = 18;
364 |
365 | // Wrist tilt gesture (1 = gaze, 0 = ungaze)
366 | WRIST_TILT = 19;
367 | }
368 | PhysicalType target = 1;
369 |
370 | // [Output Only]
371 | State status = 2;
372 |
373 | // Value interpretation depends on sensor.
374 | ParameterValue value = 3;
375 | }
376 |
377 | // A single sensor value.
378 | message SensorValue {
379 | enum State {
380 | OK = 0;
381 | NO_SERVICE = -3; // qemud service is not available/initiated.
382 | DISABLED = -2; // Sensor is disabled.
383 | UNKNOWN = -1; // Unknown sensor (should not happen)
384 | }
385 |
386 | // These are the various sensors that can be available in an emulated
387 | // devices.
388 | enum SensorType {
389 | // Measures the acceleration force in m/s2 that is applied to a device
390 | // on all three physical axes (x, y, and z), including the force of
391 | // gravity.
392 | ACCELERATION = 0;
393 | // Measures a device's rate of rotation in rad/s around each of the
394 | // three physical axes (x, y, and z).
395 | GYROSCOPE = 1;
396 | // Measures the ambient geomagnetic field for all three physical axes
397 | // (x, y, z) in μT.
398 | MAGNETIC_FIELD = 2;
399 | // Measures degrees of rotation that a device makes around all three
400 | // physical axes (x, y, z)
401 | ORIENTATION = 3;
402 | // Measures the temperature of the device in degrees Celsius (°C).
403 | TEMPERATURE = 4;
404 | // Measures the proximity of an object in cm relative to the view screen
405 | // of a device. This sensor is typically used to determine whether a
406 | // handset is being held up to a person's ear.
407 | PROXIMITY = 5;
408 | // Measures the ambient light level (illumination) in lx.
409 | LIGHT = 6;
410 | // Measures the ambient air pressure in hPa or mbar.
411 | PRESSURE = 7;
412 | // Measures the relative ambient humidity in percent (%).
413 | HUMIDITY = 8;
414 | MAGNETIC_FIELD_UNCALIBRATED = 9;
415 | GYROSCOPE_UNCALIBRATED = 10;
416 |
417 | // HINGE_ANGLE0 (11), HINGE_ANGLE1 (12), HINGE_ANGLE2 (13) are
418 | // skipped; clients should use get/setPhysicalModel() instead for these
419 | // "sensors".
420 |
421 | // Measures the heart rate in bpm.
422 | HEART_RATE = 14;
423 | // Measures the ambient RGBC light intensity.
424 | // Values are in order (Red, Green, Blue, Clear).
425 | RGBC_LIGHT = 15;
426 | // WIRST_TILT (16) is skipped; clients should use get/setPhysicalModel()
427 | // instead.
428 | // Measures acceleration force and provides bias data.
429 | ACCELERATION_UNCALIBRATED = 17;
430 | }
431 |
432 | // Type of sensor
433 | SensorType target = 1;
434 |
435 | // [Output Only]
436 | State status = 2;
437 |
438 | // Value interpretation depends on sensor enum.
439 | ParameterValue value = 3;
440 | }
441 |
442 | // A single backlight brightness value.
443 | message BrightnessValue {
444 | enum LightType {
445 | // Display backlight. This will affect all displays.
446 | LCD = 0;
447 | KEYBOARD = 1;
448 | BUTTON = 2;
449 | }
450 |
451 | // Type of light
452 | LightType target = 1;
453 |
454 | // Light intensity, ranges from 0-255.
455 | uint32 value = 2;
456 | }
457 |
458 | // in line with android/emulation/resizable_display_config.h
459 | enum DisplayModeValue {
460 | PHONE = 0;
461 | FOLDABLE = 1;
462 | TABLET = 2;
463 | DESKTOP = 3;
464 | }
465 |
466 | message DisplayMode {
467 | DisplayModeValue value = 1;
468 | }
469 |
470 | message LogMessage {
471 | // [Output Only] The contents of the log output.
472 | string contents = 1;
473 | // The starting byte position of the output that was returned. This
474 | // should match the start parameter sent with the request. If the serial
475 | // console output exceeds the size of the buffer, older output will be
476 | // overwritten by newer content and the start values will be mismatched.
477 | int64 start = 2;
478 | //[Output Only] The position of the next byte of content from the serial
479 | // console output. Use this value in the next request as the start
480 | // parameter.
481 | int64 next = 3;
482 |
483 | // Set the sort of response you are interested it in.
484 | // It the type is "Parsed" the entries field will contain the parsed
485 | // results. otherwise the contents field will be set.
486 | LogType sort = 4;
487 |
488 | // [Output Only] The parsed logcat entries so far. Only set if sort is
489 | // set to Parsed
490 | repeated LogcatEntry entries = 5;
491 |
492 | enum LogType {
493 | Text = 0;
494 | Parsed = 1;
495 | }
496 | }
497 |
498 | // A parsed logcat entry.
499 | message LogcatEntry {
500 | // The possible log levels.
501 | enum LogLevel {
502 | UNKNOWN = 0;
503 | DEFAULT = 1;
504 | VERBOSE = 2;
505 | DEBUG = 3;
506 | INFO = 4;
507 | WARN = 5;
508 | ERR = 6;
509 | FATAL = 7;
510 | SILENT = 8;
511 | }
512 |
513 | // A Unix timestamps in milliseconds (The number of milliseconds that
514 | // have elapsed since January 1, 1970 (midnight UTC/GMT), not counting
515 | // leap seconds)
516 | uint64 timestamp = 1;
517 |
518 | // Process id.
519 | uint32 pid = 2;
520 |
521 | // Thread id.
522 | uint32 tid = 3;
523 | LogLevel level = 4;
524 | string tag = 5;
525 | string msg = 6;
526 | }
527 |
528 | // Information about the hypervisor that is currently in use.
529 | message VmConfiguration {
530 | enum VmHypervisorType {
531 | // An unknown hypervisor
532 | UNKNOWN = 0;
533 |
534 | // No hypervisor is in use. This usually means that the guest is
535 | // running on a different CPU than the host, or you are using a
536 | // platform where no hypervisor is available.
537 | NONE = 1;
538 |
539 | // The Kernel based Virtual Machine
540 | // (https://www.linux-kvm.org/page/Main_Page)
541 | KVM = 2;
542 |
543 | // Intel® Hardware Accelerated Execution Manager (Intel® HAXM)
544 | // https://github.com/intel/haxm
545 | HAXM = 3;
546 |
547 | // Hypervisor Framework.
548 | // https://developer.apple.com/documentation/hypervisor
549 | HVF = 4;
550 |
551 | // Window Hypervisor Platform
552 | // https://docs.microsoft.com/en-us/virtualization/api/
553 | WHPX = 5;
554 |
555 | AEHD = 6;
556 | }
557 |
558 | VmHypervisorType hypervisorType = 1;
559 | int32 numberOfCpuCores = 2;
560 | int64 ramSizeBytes = 3;
561 | }
562 |
563 | // Representation of a clipped data object on the clipboard.
564 | message ClipData {
565 | // UTF-8 Encoded text.
566 | string text = 1;
567 | }
568 |
569 | // The Touch interface represents a single contact point on a
570 | // touch-sensitive device. The contact point is commonly a finger or stylus
571 | // and the device may be a touchscreen or trackpad.
572 | message Touch {
573 | // The horizontal coordinate. This is the physical location on the
574 | // screen For example 0 indicates the leftmost coordinate.
575 | int32 x = 1;
576 |
577 | // The vertical coordinate. This is the physical location on the screen
578 | // For example 0 indicates the top left coordinate.
579 | int32 y = 2;
580 |
581 | // The identifier is an arbitrary non-negative integer that is used to
582 | // identify and track each tool independently when multiple tools are
583 | // active. For example, when multiple fingers are touching the device,
584 | // each finger should be assigned a distinct tracking id that is used as
585 | // long as the finger remains in contact. Tracking ids may be reused
586 | // when their associated tools move out of range.
587 | //
588 | // The emulator currently supports up to 10 concurrent touch events. The
589 | // identifier can be any uninque value and will be mapped to the next
590 | // available internal identifier.
591 | int32 identifier = 3;
592 |
593 | // Reports the physical pressure applied to the tip of the tool or the
594 | // signal strength of the touch contact.
595 | //
596 | // The values reported must be non-zero when the tool is touching the
597 | // device and zero otherwise to indicate that the touch event is
598 | // completed.
599 | //
600 | // Make sure to deliver a pressure of 0 for the given identifier when
601 | // the touch event is completed, otherwise the touch identifier will not
602 | // be unregistered!
603 | int32 pressure = 4;
604 |
605 | // Optionally reports the cross-sectional area of the touch contact, or
606 | // the length of the longer dimension of the touch contact.
607 | int32 touch_major = 5;
608 |
609 | // Optionally reports the length of the shorter dimension of the touch
610 | // contact. This axis will be ignored if touch_major is reporting an
611 | // area measurement greater than 0.
612 | int32 touch_minor = 6;
613 |
614 | enum EventExpiration {
615 | // The system will use the default time of 120s to track
616 | // the touch event with the given identifier. If no update happens
617 | // within this timeframe the identifier is considered expired
618 | // and can be made available for re-use. This means that a touch event
619 | // with pressure 0 for this identifier will be send to the emulator.
620 | EVENT_EXPIRATION_UNSPECIFIED = 0;
621 |
622 | // Never expire the given slot. You must *ALWAYS* close the identifier
623 | // by sending a touch event with 0 pressure.
624 | NEVER_EXPIRE = 1;
625 | }
626 |
627 | EventExpiration expiration = 7;
628 | }
629 |
630 | // A TouchEvent contains a list of Touch objects that are in contact with
631 | // the touch surface.
632 | //
633 | // Touch events are delivered in sequence as specified in the touchList.
634 | //
635 | // TouchEvents are delivered to the emulated devices using ["Protocol
636 | // B"](https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt)
637 | message TouchEvent {
638 | // The list of Touch objects, note that these do not need to be unique
639 | repeated Touch touches = 1;
640 |
641 | // The display device where the touch event occurred.
642 | // Omitting or using the value 0 indicates the main display.
643 | //
644 | // Touch events cannot be send to displays other than 0, due to
645 | // https://issuetracker.google.com/issues/150699691
646 | int32 display = 2;
647 | }
648 |
649 | // The MouseEvent interface represents events that occur due to the user
650 | // interacting with a pointing device (such as a mouse).
651 | message MouseEvent {
652 | // The horizontal coordinate. This is the physical location on the
653 | // screen For example 0 indicates the leftmost coordinate.
654 | int32 x = 1;
655 |
656 | // The vertical coordinate. This is the physical location on the screen
657 | // For example 0 indicates the top left coordinate.
658 | int32 y = 2;
659 |
660 | // Indicates which buttons are pressed.
661 | // 0: No button was pressed
662 | // 1: Primary button (left)
663 | // 2: Secondary button (right)
664 | int32 buttons = 3;
665 |
666 | // The display device where the mouse event occurred.
667 | // Omitting or using the value 0 indicates the main display.
668 | int32 display = 4;
669 | }
670 |
671 | message WheelEvent {
672 | // The value indicating how much the mouse wheel is rotated. Scaled so that
673 | // 120 equals to 1 wheel click. (120 is chosen as a multiplier often used to
674 | // represent wheel movements less than 1 wheel click. e.g.
675 | // https://doc.qt.io/qt-5/qwheelevent.html#angleDelta) Positive delta value
676 | // is assigned to dx when the top of wheel is moved to left. Similarly
677 | // positive delta value is assigned to dy when the top of wheel is moved
678 | // away from the user.
679 | int32 dx = 1;
680 | int32 dy = 2;
681 |
682 | // The display device where the mouse event occurred.
683 | // Omitting or using the value 0 indicates the main display.
684 | int32 display = 3;
685 | }
686 |
687 | // KeyboardEvent objects describe a user interaction with the keyboard; each
688 | // event describes a single interaction between the user and a key (or
689 | // combination of a key with modifier keys) on the keyboard.
690 | // This follows the pattern as set by
691 | // (javascript)[https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent]
692 | //
693 | // Note: that only keyCode, key, or text can be set and that the semantics
694 | // will slightly vary.
695 | message KeyboardEvent {
696 | // Code types that the emulator can receive. Note that the emulator
697 | // will do its best to translate the code to an evdev value that
698 | // will be send to the emulator. This translation is based on
699 | // the chromium translation tables. See
700 | // (this)[https://android.googlesource.com/platform/external/qemu/+/refs/heads/emu-master-dev/android/android-grpc/android/emulation/control/keyboard/keycode_converter_data.inc]
701 | // for details on the translation.
702 | enum KeyCodeType {
703 | Usb = 0;
704 | Evdev = 1;
705 | XKB = 2;
706 | Win = 3;
707 | Mac = 4;
708 | }
709 |
710 | enum KeyEventType {
711 | // Indicates that this keyevent should be send to the emulator
712 | // as a key down event. Meaning that the key event will be
713 | // translated to an EvDev event type and bit 11 (0x400) will be
714 | // set before it is sent to the emulator.
715 | keydown = 0;
716 |
717 | // Indicates that the keyevent should be send to the emulator
718 | // as a key up event. Meaning that the key event will be
719 | // translated to an EvDev event type and
720 | // sent to the emulator.
721 | keyup = 1;
722 |
723 | // Indicates that the keyevent will be send to the emulator
724 | // as e key down event and immediately followed by a keyup event.
725 | keypress = 2;
726 | }
727 |
728 | // Type of keycode contained in the keyCode field.
729 | KeyCodeType codeType = 1;
730 |
731 | // The type of keyboard event that should be sent to the emulator
732 | KeyEventType eventType = 2;
733 |
734 | // This property represents a physical key on the keyboard (as opposed
735 | // to the character generated by pressing the key). In other words, this
736 | // property is a value which isn't altered by keyboard layout or the
737 | // state of the modifier keys. This value will be interpreted by the
738 | // emulator depending on the KeyCodeType. The incoming key code will be
739 | // translated to an evdev code type and send to the emulator.
740 | // The values in key and text will be ignored.
741 | int32 keyCode = 3;
742 |
743 | // The value of the key pressed by the user, taking into consideration
744 | // the state of modifier keys such as Shift as well as the keyboard
745 | // locale and layout. This follows the w3c standard used in browsers.
746 | // You can find an accurate description of valid values
747 | // [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)
748 | //
749 | // Note that some keys can result in multiple evdev events that are
750 | // delivered to the emulator. for example the Key "A" will result in a
751 | // sequence:
752 | // ["Shift", "a"] -> [0x2a, 0x1e] whereas "a" results in ["a"] -> [0x1e].
753 | //
754 | // Not all documented keys are understood by android, and only printable
755 | // ASCII [32-127) characters are properly translated.
756 | //
757 | // Keep in mind that there are a set of key values that result in android
758 | // specific behavior
759 | // [see](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values#Phone_keys):
760 | //
761 | // - "AppSwitch": Behaves as the "Overview" button in android.
762 | // - "GoBack": The Back button.
763 | // - "GoHome": The Home button, which takes the user to the phone's main
764 | // screen (usually an application launcher).
765 | // - "Power": The Power button.
766 | string key = 4;
767 |
768 | // Series of utf8 encoded characters to send to the emulator. An attempt
769 | // will be made to translate every character will an EvDev event type and
770 | // send to the emulator as a keypress event. The values in keyCode,
771 | // eventType, codeType and key will be ignored.
772 | //
773 | // Note that most printable ASCII characters (range [32-127) can be send
774 | // individually with the "key" param. Do not expect arbitrary UTF symbols to
775 | // arrive in the emulator (most will be ignored).
776 | //
777 | // Note that it is possible to overrun the keyboard buffer by slamming this
778 | // endpoint with large quantities of text (>1kb). The clipboard api is
779 | // better suited for transferring large quantities of text.
780 | string text = 5;
781 | }
782 |
783 | message Fingerprint {
784 | // True when the fingprint is touched.
785 | bool isTouching = 1;
786 |
787 | // The identifier of the registered fingerprint.
788 | int32 touchId = 2;
789 | }
790 |
791 | message GpsState {
792 | // Setting this to false will disable auto updating from the LocationUI,
793 | // otherwise the location UI will override the location at a frequency of
794 | // 1hz.
795 | //
796 | // - This is unused if the emulator is launched with -no-window, or when he
797 | // location ui is disabled.
798 | // - This will BREAK the location ui experience if it is set to false. For
799 | // example routing will no longer function.
800 | bool passiveUpdate = 1;
801 |
802 | // The latitude, in degrees.
803 | double latitude = 2;
804 |
805 | // The longitude, in degrees.
806 | double longitude = 3;
807 |
808 | // The speed if it is available, in meters/second over ground
809 | double speed = 4;
810 |
811 | // gets the horizontal direction of travel of this device, and is not
812 | // related to the device orientation. It is guaranteed to be in the
813 | // range [0.0, 360.0] if the device has a bearing. 0=North, 90=East,
814 | // 180=South, etc..
815 | double bearing = 5;
816 |
817 | // The altitude if available, in meters above the WGS 84 reference
818 | // ellipsoid.
819 | double altitude = 6;
820 |
821 | // The number of satellites used to derive the fix
822 | int32 satellites = 7;
823 | }
824 |
825 | message BatteryState {
826 | enum BatteryStatus {
827 | UNKNOWN = 0;
828 | CHARGING = 1;
829 | DISCHARGING = 2;
830 | NOT_CHARGING = 3;
831 | FULL = 4;
832 | }
833 |
834 | enum BatteryCharger {
835 | NONE = 0;
836 | AC = 1;
837 | USB = 2;
838 | WIRELESS = 3;
839 | }
840 |
841 | enum BatteryHealth {
842 | GOOD = 0;
843 | FAILED = 1;
844 | DEAD = 2;
845 | OVERVOLTAGE = 3;
846 | OVERHEATED = 4;
847 | }
848 |
849 | bool hasBattery = 1;
850 | bool isPresent = 2;
851 | BatteryCharger charger = 3;
852 | int32 chargeLevel = 4;
853 | BatteryHealth health = 5;
854 | BatteryStatus status = 6;
855 | }
856 |
857 | // An ImageTransport allows for specifying a side channel for
858 | // delivering image frames versus using the standard bytes array that is
859 | // returned with the gRPC request.
860 | message ImageTransport {
861 | enum TransportChannel {
862 | // Return full frames over the gRPC transport
863 | TRANSPORT_CHANNEL_UNSPECIFIED = 0;
864 |
865 | // Write images to the a file/shared memory handle.
866 | MMAP = 1;
867 | }
868 |
869 | // The desired transport channel used for delivering image frames. Only
870 | // relevant when streaming screenshots.
871 | TransportChannel channel = 1;
872 |
873 | // Handle used for writing image frames if transport is mmap. The client
874 | // sets and owns this handle. It can be either a shm region, or a mmap. A
875 | // mmap should be a url that starts with `file:///` Note: the mmap can
876 | // result in tearing.
877 | string handle = 2;
878 | }
879 |
880 | // The aspect ratio (width/height) will be different from the one
881 | // where the device is unfolded.
882 | message FoldedDisplay {
883 | uint32 width = 1;
884 | uint32 height = 2;
885 | // It is possible for the screen to be folded in different ways depending
886 | // on which surface is shown to the user. So xOffset and yOffset indicate
887 | // the top left corner of the folded screen within the original unfolded
888 | // screen.
889 | uint32 xOffset = 3;
890 | uint32 yOffset = 4;
891 | }
892 |
893 | message ImageFormat {
894 | enum ImgFormat {
895 | // Portable Network Graphics format
896 | // (https://en.wikipedia.org/wiki/Portable_Network_Graphics)
897 | PNG = 0;
898 |
899 | // Three-channel RGB color model supplemented with a fourth alpha
900 | // channel. https://en.wikipedia.org/wiki/RGBA_color_model
901 | // Each pixel consists of 4 bytes.
902 | RGBA8888 = 1;
903 |
904 | // Three-channel RGB color model, each pixel consists of 3 bytes
905 | RGB888 = 2;
906 | }
907 |
908 | // The (desired) format of the resulting bytes.
909 | ImgFormat format = 1;
910 |
911 | // [Output Only] The rotation of the image. The image will be rotated
912 | // based upon the coarse grained orientation of the device.
913 | Rotation rotation = 2;
914 |
915 | // The (desired) width of the image. When passed as input
916 | // the image will be scaled to match the given
917 | // width, while maintaining the aspect ratio of the device.
918 | // The returned image will never exceed the given width, but can be less.
919 | // Omitting this value (or passing in 0) will result in no scaling,
920 | // and the width of the actual device will be used.
921 | uint32 width = 3;
922 |
923 | // The (desired) height of the image. When passed as input
924 | // the image will be scaled to match the given
925 | // height, while maintaining the aspect ratio of the device.
926 | // The returned image will never exceed the given height, but can be less.
927 | // Omitting this value (or passing in 0) will result in no scaling,
928 | // and the height of the actual device will be used.
929 | uint32 height = 4;
930 |
931 | // The (desired) display id of the device. Setting this to 0 (or omitting)
932 | // indicates the main display.
933 | uint32 display = 5;
934 |
935 | // Set this if you wish to use a different transport channel to deliver
936 | // image frames.
937 | ImageTransport transport = 6;
938 |
939 | // [Output Only] Display configuration when screen is folded. The value is
940 | // the original configuration before scaling.
941 | FoldedDisplay foldedDisplay = 7;
942 |
943 | // [Output Only] Display mode when AVD is resizable.
944 | DisplayModeValue displayMode = 8;
945 | }
946 |
947 | message Image {
948 | ImageFormat format = 1;
949 |
950 | uint32 width = 2 [deprecated = true]; // width is contained in format.
951 | uint32 height = 3 [deprecated = true]; // height is contained in format.
952 |
953 | // The organization of the pixels in the image buffer is from left to
954 | // right and bottom up. This will be empty if an alternative image transport
955 | // is requested in the image format. In that case the side channel should
956 | // be used to obtain the image data.
957 | bytes image = 4;
958 |
959 | // [Output Only] Monotonically increasing sequence number in a stream of
960 | // screenshots. The first screenshot will have a sequence of 0. A single
961 | // screenshot will always have a sequence number of 0. The sequence is not
962 | // necessarily contiguous, and can be used to detect how many frames were
963 | // dropped. An example sequence could be: [0, 3, 5, 7, 9, 11].
964 | uint32 seq = 5;
965 |
966 | // [Output Only] Unix timestamp in microseconds when the emulator estimates
967 | // the frame was generated. The timestamp is before the actual frame is
968 | // copied and transformed. This can be used to calculate variance between
969 | // frame production time, and frame depiction time.
970 | uint64 timestampUs = 6;
971 | }
972 |
973 | message Rotation {
974 | enum SkinRotation {
975 | PORTRAIT = 0; // 0 degrees
976 | LANDSCAPE = 1; // 90 degrees
977 | REVERSE_PORTRAIT = 2; // -180 degrees
978 | REVERSE_LANDSCAPE = 3; // -90 degrees
979 | }
980 |
981 | // The rotation of the device, derived from the sensor state
982 | // of the emulator. The derivation reflects how android observes
983 | // the rotation state.
984 | SkinRotation rotation = 1;
985 |
986 | // Specifies the angle of rotation, in degrees [-180, 180]
987 | double xAxis = 2;
988 | double yAxis = 3;
989 | double zAxis = 4;
990 | }
991 |
992 | message PhoneCall {
993 | enum Operation {
994 | InitCall = 0;
995 | AcceptCall = 1;
996 | RejectCallExplicit = 2;
997 | RejectCallBusy = 3;
998 | DisconnectCall = 4;
999 | PlaceCallOnHold = 5;
1000 | TakeCallOffHold = 6;
1001 | }
1002 | Operation operation = 1;
1003 | string number = 2;
1004 | }
1005 |
1006 | message PhoneResponse {
1007 | enum Response {
1008 | OK = 0;
1009 | BadOperation = 1; // Enum out of range
1010 | BadNumber = 2; // Mal-formed telephone number
1011 | InvalidAction = 3; // E.g., disconnect when no call is in progress
1012 | ActionFailed = 4; // Internal error
1013 | RadioOff = 5; // Radio power off
1014 | }
1015 | Response response = 1;
1016 | }
1017 |
1018 | message Entry {
1019 | string key = 1;
1020 | string value = 2;
1021 | }
1022 |
1023 | message EntryList {
1024 | repeated Entry entry = 1;
1025 | }
1026 |
1027 | message EmulatorStatus {
1028 | // The emulator version string.
1029 | string version = 1;
1030 |
1031 | // The time the emulator has been active in .ms
1032 | uint64 uptime = 2;
1033 |
1034 | // True if the device has completed booting.
1035 | // For P and later this information will accurate,
1036 | // for older images we rely on adb.
1037 | bool booted = 3;
1038 |
1039 | // The current vm configuration
1040 | VmConfiguration vmConfig = 4;
1041 |
1042 | // The hardware configuration of the running emulator as
1043 | // key valure pairs.
1044 | EntryList hardwareConfig = 5;
1045 | }
1046 |
1047 | message AudioFormat {
1048 | enum SampleFormat {
1049 | AUD_FMT_U8 = 0; // Unsigned 8 bit
1050 | AUD_FMT_S16 = 1; // Signed 16 bit (little endian)
1051 | }
1052 |
1053 | enum Channels {
1054 | Mono = 0;
1055 | Stereo = 1;
1056 | }
1057 |
1058 | enum DeliveryMode {
1059 | // The audio queue will block and wait until the emulator requests
1060 | // packets. The client does not have to throttle and can push packets at
1061 | // will. This can result in the client falling behind.
1062 | MODE_UNSPECIFIED = 0;
1063 | // Audio packets will be delivered in real time (when possible). The
1064 | // audio queue will be overwritten with incoming data if data is made
1065 | // available. This means the client needs to control timing properly, or
1066 | // packets will get overwritten.
1067 | MODE_REAL_TIME = 1; //
1068 | }
1069 | // Sampling rate to use, defaulting to 44100 if this is not set.
1070 | // Note, that android devices typically will not use a sampling
1071 | // rate higher than 48kHz. See
1072 | // https://developer.android.com/ndk/guides/audio.
1073 | uint64 samplingRate = 1;
1074 | Channels channels = 2;
1075 | SampleFormat format = 3;
1076 |
1077 | // [Input Only]
1078 | // The mode used when delivering audio packets.
1079 | DeliveryMode mode = 4;
1080 | }
1081 |
1082 | message AudioPacket {
1083 | AudioFormat format = 1;
1084 |
1085 | // Unix epoch in us when this frame was captured.
1086 | uint64 timestamp = 2;
1087 |
1088 | // Contains a sample in the given audio format.
1089 | bytes audio = 3;
1090 | }
1091 |
1092 | message SmsMessage {
1093 | // The source address where this message came from.
1094 | //
1095 | // The address should be a valid GSM-formatted address as specified by
1096 | // 3GPP 23.040 Sec 9.1.2.5.
1097 | //
1098 | // For example: +3106225412 or (650) 555-1221
1099 | string srcAddress = 1;
1100 |
1101 | // A utf8 encoded text message that should be delivered.
1102 | string text = 2;
1103 | }
1104 |
1105 | // A DisplayConfiguration describes a primary or secondary
1106 | // display available to the emulator. The screen aspect ratio
1107 | // cannot be longer (or wider) than 21:9 (or 9:21). Screen sizes
1108 | // larger than 4k will be rejected.
1109 | //
1110 | // Common configurations (w x h) are:
1111 | // - 480p (480x720) 142 dpi
1112 | // - 720p (720x1280) 213 dpi
1113 | // - 1080p (1080x1920) 320 dpi
1114 | // - 4K (2160x3840) 320 dpi
1115 | // - 4K (2160x3840) 640 dpi (upscaled)
1116 | //
1117 | // The behavior of the virtual display depends on the flags that are provided to
1118 | // this method. By default, virtual displays are created to be private,
1119 | // non-presentation and unsecure.
1120 | message DisplayConfiguration {
1121 | // These are the set of known android flags and their respective values.
1122 | // you can combine the int values to (de)construct the flags field below.
1123 | enum DisplayFlags {
1124 | DISPLAYFLAGS_UNSPECIFIED = 0;
1125 |
1126 | // When this flag is set, the virtual display is public.
1127 | // A public virtual display behaves just like most any other display
1128 | // that is connected to the system such as an external or wireless
1129 | // display. Applications can open windows on the display and the system
1130 | // may mirror the contents of other displays onto it. see:
1131 | // https://developer.android.com/reference/android/hardware/display/DisplayManager#VIRTUAL_DISPLAY_FLAG_PUBLIC
1132 | VIRTUAL_DISPLAY_FLAG_PUBLIC = 1;
1133 |
1134 | // When this flag is set, the virtual display is registered as a
1135 | // presentation display in the presentation display category.
1136 | // Applications may automatically project their content to presentation
1137 | // displays to provide richer second screen experiences.
1138 | // https://developer.android.com/reference/android/hardware/display/DisplayManager#VIRTUAL_DISPLAY_FLAG_PRESENTATION
1139 | VIRTUAL_DISPLAY_FLAG_PRESENTATION = 2;
1140 |
1141 | // When this flag is set, the virtual display is considered secure as
1142 | // defined by the Display#FLAG_SECURE display flag. The caller promises
1143 | // to take reasonable measures, such as over-the-air encryption, to
1144 | // prevent the contents of the display from being intercepted or
1145 | // recorded on a persistent medium.
1146 | // see:
1147 | // https://developer.android.com/reference/android/hardware/display/DisplayManager#VIRTUAL_DISPLAY_FLAG_SECURE
1148 | VIRTUAL_DISPLAY_FLAG_SECURE = 4;
1149 |
1150 | // This flag is used in conjunction with VIRTUAL_DISPLAY_FLAG_PUBLIC.
1151 | // Ordinarily public virtual displays will automatically mirror the
1152 | // content of the default display if they have no windows of their own.
1153 | // When this flag is specified, the virtual display will only ever show
1154 | // its own content and will be blanked instead if it has no windows. See
1155 | // https://developer.android.com/reference/android/hardware/display/DisplayManager#VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
1156 | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY = 8;
1157 |
1158 | // Allows content to be mirrored on private displays when no content is
1159 | // being shown.
1160 | // This flag is mutually exclusive with
1161 | // VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY. If both flags are specified
1162 | // then the own-content only behavior will be applied.
1163 | // see:
1164 | // https://developer.android.com/reference/android/hardware/display/DisplayManager#VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR)
1165 | VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR = 16;
1166 | }
1167 |
1168 | // The width of the display, restricted to:
1169 | // 320 * (dpi / 160) <= width
1170 | uint32 width = 1;
1171 |
1172 | // The heigh of the display, restricted to:
1173 | // * 320 * (dpi / 160) <= height
1174 | uint32 height = 2;
1175 |
1176 | // The pixel density (dpi).
1177 | // See https://developer.android.com/training/multiscreen/screendensities
1178 | // for details. This value should be in the range [120, ..., 640]
1179 | uint32 dpi = 3;
1180 |
1181 | // A combination of virtual display flags. These flags can be constructed
1182 | // by combining the DisplayFlags enum described above.
1183 | //
1184 | // The behavior of the virtual display depends on the flags. By default
1185 | // virtual displays are created to be private, non-presentation and
1186 | // unsecure.
1187 | uint32 flags = 4;
1188 |
1189 | // The id of the display.
1190 | // The primary (default) display has the display ID of 0.
1191 | // A secondary display has a display ID not 0.
1192 | //
1193 | // A display with the id in the range [1, userConfigurable]
1194 | // can be modified. See DisplayConfigurations below for details.
1195 | //
1196 | // The id can be used to get or stream a screenshot.
1197 | uint32 display = 5;
1198 | }
1199 | // Provides information about all the displays that can be attached
1200 | // to the emulator. The emulator will always have at least one display.
1201 | //
1202 | // The emulator usually has the following display configurations:
1203 | // 0: The default display.
1204 | // 1 - 3: User configurable displays. These can be added/removed.
1205 | // For example the standalone emulator allows you to modify these
1206 | // in the extended controls.
1207 | // 6 - 11: Fixed external displays. For example Android Auto uses fixed
1208 | // displays in this range.
1209 | message DisplayConfigurations {
1210 | repeated DisplayConfiguration displays = 1;
1211 |
1212 | // Display configurations with id [1, userConfigurable] are
1213 | // user configurable, that is they can be added, removed or
1214 | // updated.
1215 | uint32 userConfigurable = 2;
1216 |
1217 | // The maximum number of attached displays this emulator supports.
1218 | // This is the total number of displays that can be attached to
1219 | // the emulator.
1220 | //
1221 | // Note: A display with an id that is larger than userConfigurable cannot
1222 | // be modified.
1223 | uint32 maxDisplays = 3;
1224 | }
1225 |
1226 | message Notification {
1227 | enum EventType {
1228 | VIRTUAL_SCENE_CAMERA_INACTIVE = 0;
1229 | VIRTUAL_SCENE_CAMERA_ACTIVE = 1;
1230 |
1231 | // Fired when an update to a display event has been fired through
1232 | // the extended ui. This does not fire events when the display
1233 | // is changed through the console or gRPC endpoint.
1234 | DISPLAY_CONFIGURATIONS_CHANGED_UI = 2;
1235 | // Keep adding more for other event types
1236 | }
1237 |
1238 | // Deprecated, use the type below to get detailed information
1239 | // regarding the event.
1240 | EventType event = 1 [deprecated = true];
1241 |
1242 | // Detailed notification information.
1243 | oneof type {
1244 | CameraNotification cameraNotification = 2;
1245 | DisplayConfigurationsChangedNotification
1246 | displayConfigurationsChangedNotification = 3;
1247 | }
1248 | }
1249 |
1250 | // Fired when the virtual scene camera is activated or deactivated and also in
1251 | // response to the streamNotification call.
1252 | message CameraNotification {
1253 | // Indicates whether the camera app was activated or deactivated.
1254 | bool active = 1;
1255 | // The display the camera app is associated with.
1256 | int32 display = 2;
1257 | }
1258 |
1259 | // Fired when an update to a display event has been fired through the extended
1260 | // ui. This does not fire events when the display is changed through the console
1261 | // or the gRPC endpoint.
1262 | message DisplayConfigurationsChangedNotification {
1263 | DisplayConfigurations displayConfigurations = 1;
1264 | }
1265 |
1266 | message RotationRadian {
1267 | float x = 1; // x axis is horizontal and orthogonal to the view direction.
1268 | float y = 2; // y axis points up and is perpendicular to the floor.
1269 | float z = 3; // z axis is the view direction and is set to 0.0 in
1270 | // rotateVirtualSceneCamera call.
1271 | }
1272 |
1273 | message Velocity {
1274 | float x = 1; // x axis is horizontal and orthogonal to the view direction.
1275 | float y = 2; // y axis points up and is perpendicular to the floor.
1276 | float z = 3; // z axis is the view direction
1277 | }
1278 |
1279 | // must follow the definition in "external/qemu/android/hw-sensors.h"
1280 | message Posture {
1281 | enum PostureValue {
1282 | POSTURE_UNKNOWN = 0;
1283 | POSTURE_CLOSED = 1;
1284 | POSTURE_HALF_OPENED = 2;
1285 | POSTURE_OPENED = 3;
1286 | POSTURE_FLIPPED = 4;
1287 | POSTURE_TENT = 5;
1288 | POSTURE_MAX = 6;
1289 | }
1290 | PostureValue value = 3;
1291 | }
1292 |
1293 | message PhoneNumber {
1294 | //
1295 | // The phone number should be a valid GSM-formatted number as specified by
1296 | // 3GPP 23.040 Sec 9.1.2.5.
1297 | //
1298 | // For example: +3106225412 or (650) 555-1221
1299 | string number = 1;
1300 | }
1301 |
--------------------------------------------------------------------------------
/proto/rtc_service.proto:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2018 The Android Open Source Project
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | // Note that if you add/remove methods in this file you must update
16 | // the metrics sql as well by running ./android/scripts/gen-grpc-sql.py
17 | //
18 | // Please group deleted methods in a block including the date (MM/DD/YY)
19 | // it was removed. This enables us to easily keep metrics around after removal
20 | //
21 | // List of deleted methods
22 | // rpc iWasDeleted (03/12/12)
23 | // ...
24 | syntax = "proto3";
25 |
26 | option java_multiple_files = true;
27 | option java_package = "com.android.emulator.control";
28 | option objc_class_prefix = "AEC";
29 |
30 | package android.emulation.control;
31 | import "google/protobuf/empty.proto";
32 |
33 | // An RTC service lets you interact with the emulator through WebRTC
34 | // Note that this is currently an experimental feature, and that the
35 | // service definition might change without notice. Use at your own risk!
36 | //
37 | // The following endpoints are needed to establish the webrtc protocol
38 | // Due to limitiations in Javascript we cannot make use of bidirectional
39 | // endpoints See this [blog](https://grpc.io/blog/state-of-grpc-web) for
40 | // details.
41 | service Rtc {
42 | // This function will generate a new identifier that the client
43 | // should use for further interaction. It will initiate the
44 | // JSEP protocol on the server side.
45 | rpc requestRtcStream(google.protobuf.Empty) returns (RtcId) {}
46 |
47 | // Sends the given JsepMsg to the server. The RtcId in the
48 | // message should point to an active stream negotiation in
49 | // progress, otherwise the message will be ignored.
50 | rpc sendJsepMessage(JsepMsg) returns (google.protobuf.Empty) {}
51 |
52 | // Reads an available jsep messages for the given client id,
53 | // blocking until one becomes available. Do not use the polling version
54 | // above if you opt for this one.
55 | //
56 | // The ice candidates for example will trickle in on this callback,
57 | // as will the SDP negotation.
58 | rpc receiveJsepMessages(RtcId) returns (stream JsepMsg) {}
59 |
60 |
61 | // [DEPRECATED] This is only here as the go grpc webproxy used
62 | // by fuchsia does not support server side streaming. This method
63 | // will be removed in the future and should not be relied upon.
64 | //
65 | // Reads an available jsep messages for the given client id,
66 | // blocking until one becomes available. Do not use the polling version
67 | // above if you opt for this one.
68 | //
69 | // The ice candidates for example will trickle in on this callback,
70 | // as will the SDP negotation.
71 | rpc receiveJsepMessage(RtcId) returns (JsepMsg) {}
72 | }
73 |
74 | message RtcId {
75 | // The unique identifier of this connection. You will have to use the
76 | // same identifier when sending/receiving messages. The server will
77 | // generate a guid when receiving the start message.
78 | string guid = 1;
79 | }
80 |
81 | message JsepMsg {
82 | // The unique identifier of this connection. You will have to use the
83 | // same identifier when sending/receiving messages. The server will
84 | // generate a guid when receiving the start message.
85 | RtcId id = 1;
86 | // The JSON payload. This usually can be directly handled by the
87 | // Javascript library.
88 | //
89 | // The dictionary can contain the following properties
90 | //
91 | // - bye:
92 | // You can hang up now. No new message expected for you.
93 | // The server has stopped the RTC stream.
94 | //
95 | // - start:
96 | // An RTCConfiguration dictionary providing options to
97 | // configure the new connection. This can include the
98 | // turn configuration the serve is using. This dictionary can be
99 | // passed in directly to the
100 | // [RTCPeerConnection](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection)
101 | // object.
102 | //
103 | // - candidate:
104 | // The WebRTC API's RTCIceCandidateInit dictionary, which
105 | // contains the information needed to fundamentally describe an
106 | // RTCIceCandidate. See
107 | // [RTCIceCandidate](https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidate)
108 | // and [Session
109 | // Lifetime](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Session_lifetime)
110 | // for more details.
111 | //
112 | // - sdp:
113 | // RTCSessionDescriptionInit dictionary containing the values
114 | // to that can be assigned to a
115 | // [RTCSessionDescription](https://developer.mozilla.org/en-US/docs/Web/API/RTCSessionDescription)
116 | string message = 2;
117 | }
118 |
--------------------------------------------------------------------------------
/protoc-plugin/Makefile:
--------------------------------------------------------------------------------
1 | # Copyright 2018 Google LLC
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # https://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | CXX = g++
16 | PROTOHEADER=$(shell pkg-config --variable prefix protobuf)/include
17 | PROTOLIB=$(shell pkg-config --variable prefix protobuf)/lib
18 | CPPFLAGS += -I/usr/local/include -pthread -I$(PROTOHEADER)
19 | CXXFLAGS += -std=c++11
20 | LDFLAGS += -L/usr/local/lib -L$(PROTOLIB) -lprotoc -lprotobuf -lpthread -ldl
21 |
22 | all: protoc-gen-grpc-web
23 |
24 | protoc-gen-grpc-web: grpc_generator.o
25 | $(CXX) $^ $(LDFLAGS) -o $@
26 |
27 | install: protoc-gen-grpc-web
28 | install protoc-gen-grpc-web /usr/local/bin/protoc-gen-grpc-web
29 |
30 | clean:
31 | rm -f *.o protoc-gen-grpc-web
32 |
--------------------------------------------------------------------------------
/src/components/emulator/emulator.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 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 | import PropTypes from "prop-types";
17 | import React, { Component } from "react";
18 | import EmulatorPngView from "./views/simple_png_view.js";
19 | import EmulatorWebrtcView from "./views/webrtc_view.js";
20 | import withMouseKeyHandler from "./views/event_handler";
21 | import JsepProtocol from "./net/jsep_protocol_driver.js";
22 | import * as Proto from "../../proto/emulator_controller_pb";
23 | import {
24 | RtcService,
25 | EmulatorControllerService,
26 | } from "../../proto/emulator_web_client";
27 |
28 | const PngView = withMouseKeyHandler(EmulatorPngView);
29 | const RtcView = withMouseKeyHandler(EmulatorWebrtcView);
30 |
31 | /**
32 | * A React component that displays a remote android emulator.
33 | *
34 | * The emulator will mount a png or webrtc view component to display the current state
35 | * of the emulator. It will translate mouse events on this component and send them
36 | * to the actual emulator.
37 | *
38 | * #### Authentication Service
39 | *
40 | * The authentication service should implement the following methods:
41 | *
42 | * - `authHeader()` which must return a set of headers that should be send along with a request.
43 | * - `unauthorized()` a function that gets called when a 401 was received.
44 | *
45 | * #### Type of view
46 | *
47 | * You usually want this to be webrtc as this will make use of the efficient
48 | * webrtc implementation. The png view will request screenshots, which are
49 | * very slow, and require the envoy proxy. You should not use this for remote emulators.
50 | *
51 | * Note that chrome will not autoplay the video if it is not muted and no interaction
52 | * with the page has taken place. See https://developers.google.com/web/updates/2017/09/autoplay-policy-changes.
53 | *
54 | * #### Pressing hardware buttons
55 | *
56 | * This component has a method `sendKey` that sends a key to the emulator.
57 | * You can use this to send physical button events to the emulator for example:
58 | *
59 | * "AudioVolumeDown" - Decreases the audio volume.
60 | * "AudioVolumeUp" - Increases the audio volume.
61 | * "Power" - The Power button or key, turn off the device.
62 | * "AppSwitch" - Should bring up the application switcher dialog.
63 | * "GoHome" - Go to the home screen.
64 | * "GoBack" - Open the previous screen you were looking at.
65 | *
66 | */
67 | class Emulator extends Component {
68 | static propTypes = {
69 | /** gRPC Endpoint where we can reach the emulator. */
70 | uri: PropTypes.string.isRequired,
71 | /** The authentication service to use, or null for no authentication. */
72 | auth: PropTypes.object,
73 | /** True if the audio should be disabled. This is only relevant when using the webrtc engine. */
74 | muted: PropTypes.bool,
75 | /** Volume between [0, 1] when audio is enabled. 0 is muted, 1.0 is 100% */
76 | volume: PropTypes.number,
77 | /** Called upon state change, one of ["connecting", "connected", "disconnected"] */
78 | onStateChange: PropTypes.func,
79 | /** Called when the audio becomes (un)available. True if audio is available, false otherwise. */
80 | onAudioStateChange: PropTypes.func,
81 | /** The width of the component */
82 | width: PropTypes.number,
83 | /** The height of the component */
84 | height: PropTypes.number,
85 | /** The underlying view used to display the emulator, one of ["webrtc", "png"] */
86 | view: PropTypes.oneOf(["webrtc", "png"]).isRequired,
87 | /** A [GeolocationCoordinates](https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates) like object indicating where the device is. */
88 | gps: PropTypes.object,
89 | /** True if polling should be used, only set this to true if you are using the go webgrpc proxy. */
90 | poll: PropTypes.bool,
91 | /** Callback that will be invoked in case of gRPC errors. */
92 | onError: PropTypes.func,
93 | };
94 |
95 | static defaultProps = {
96 | view: "webrtc",
97 | auth: null,
98 | poll: false,
99 | muted: true,
100 | volume: 1.0,
101 | onError: (e) => {
102 | console.error(e);
103 | },
104 | onAudioStateChange: (s) => {
105 | console.debug("emulator audio: " + s);
106 | },
107 | onStateChange: (s) => {
108 | console.debug("emulator state: " + s);
109 | },
110 | };
111 |
112 | components = {
113 | webrtc: RtcView,
114 | png: PngView,
115 | };
116 |
117 | state = {
118 | audio: false,
119 | };
120 |
121 | constructor(props) {
122 | super(props);
123 | const { uri, auth, poll, onError } = props;
124 | this.emulator = new EmulatorControllerService(uri, auth, onError);
125 | this.rtc = new RtcService(uri, auth, onError);
126 | this.jsep = new JsepProtocol(this.emulator, this.rtc, poll);
127 | this.view = React.createRef();
128 | }
129 |
130 | static getDerivedStateFromProps(nextProps, prevState) {
131 | if (nextProps.view === "png")
132 | return {
133 | audio: false,
134 | };
135 |
136 | return prevState;
137 | }
138 |
139 | componentDidMount = () => {
140 | this.updateLocation();
141 | };
142 |
143 | componentDidUpdate = (prevProps) => {
144 | if (prevProps.gps !== this.props.gps) {
145 | this.updateLocation();
146 | }
147 | };
148 |
149 | updateLocation = () => {
150 | const { gps } = this.props;
151 | if (typeof gps === "undefined") {
152 | return;
153 | }
154 |
155 | const state = new Proto.GpsState();
156 | state.setLatitude(gps.latitude);
157 | state.setLongitude(gps.longitude);
158 | state.setAltitude(gps.altitude);
159 | state.setBearing(gps.heading);
160 | state.setSpeed(gps.speed);
161 | this.emulator.setGps(state);
162 | };
163 |
164 | /**
165 | * Sends the given key to the emulator.
166 | *
167 | * You can use this to send physical hardware events to the emulator for example:
168 | *
169 | * "AudioVolumeDown" - Decreases the audio volume.
170 | * "AudioVolumeUp" - Increases the audio volume.
171 | * "Power" - The Power button or key, turn off the device.
172 | * "AppSwitch" - Should bring up the application switcher dialog.
173 | * "GoHome" - Go to the home screen.
174 | * "GoBack" - Open the previous screen you were looking at.
175 | *
176 | * See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values for
177 | * a list of valid values you can send as well.
178 | */
179 | sendKey = (key) => {
180 | var request = new Proto.KeyboardEvent();
181 | request.setEventtype(Proto.KeyboardEvent.KeyEventType.KEYPRESS);
182 | request.setKey(key);
183 | this.jsep.send("keyboard", request);
184 | };
185 |
186 | _onAudioStateChange = (s) => {
187 | const { onAudioStateChange } = this.props;
188 | this.setState({ audio: s }, () => {
189 | onAudioStateChange(s);
190 | });
191 | };
192 |
193 | render() {
194 | const { width, height, view, poll, muted, onStateChange, onError, volume } =
195 | this.props;
196 | const SpecificView = this.components[view] || RtcView;
197 |
198 | console.log(`render ${width}x${height}`);
199 | return (
200 |
213 | );
214 | }
215 | }
216 |
217 | export default Emulator;
218 |
--------------------------------------------------------------------------------
/src/components/emulator/net/emulator_status.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 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 | import { Empty } from "google-protobuf/google/protobuf/empty_pb";
17 | import { EmulatorControllerService } from "../../../proto/emulator_web_client";
18 |
19 | /**
20 | * Gets the status of the emulator, parsing the hardware config into something
21 | * easy to digest.
22 | *
23 | * @export
24 | * @class EmulatorStatus
25 | */
26 | class EmulatorStatus {
27 | /**
28 | * Creates an EmulatorStatus object that can retrieve the status of the running emulator.
29 | *
30 | * @param {object} uriOrEmulator An emulator controller service, or a URI to a gRPC endpoint.
31 | * @param {object} auth The authentication service to use, or null for no authentication.
32 | *
33 | * The authentication service should implement the following methods:
34 | * - `authHeader()` which must return a set of headers that should be send along with a request.
35 | * - `unauthorized()` a function that gets called when a 401 was received.
36 | */
37 | constructor(uriOrEmulator, auth) {
38 | if (uriOrEmulator instanceof EmulatorControllerService) {
39 | this.emulator = uriOrEmulator;
40 | } else {
41 | this.emulator = new EmulatorControllerService(uriOrEmulator, auth);
42 | }
43 | this.status = null;
44 | }
45 |
46 | /**
47 | * Gets the cached status.
48 | *
49 | * @memberof EmulatorStatus
50 | */
51 | getStatus = () => {
52 | return this.status;
53 | };
54 |
55 | /**
56 | * Retrieves the current status from the emulator.
57 | *
58 | * @param {Callback} fnNotify when the status is available, returns the retrieved status.
59 | * @param {boolean} cache True if the cache can be used.
60 | * @memberof EmulatorStatus
61 | */
62 | updateStatus = (fnNotify, cache) => {
63 | const request = new Empty();
64 | if (cache && this.status) {
65 | fnNotify(this.status);
66 | return this.status;
67 | }
68 | this.emulator.getStatus(request, {}, (err, response) => {
69 | var hwConfig = {};
70 | const entryList = response.getHardwareconfig().getEntryList();
71 | for (var i = 0; i < entryList.length; i++) {
72 | const key = entryList[i].getKey();
73 | const val = entryList[i].getValue();
74 | hwConfig[key] = val;
75 | }
76 |
77 | const vmConfig = response.getVmconfig();
78 | this.status = {
79 | version: response.getVersion(),
80 | uptime: response.getUptime(),
81 | booted: response.getBooted(),
82 | hardwareConfig: hwConfig,
83 | vmConfig: {
84 | hypervisorType: vmConfig.getHypervisortype(),
85 | numberOfCpuCores: vmConfig.getNumberofcpucores(),
86 | ramSizeBytes: vmConfig.getRamsizebytes()
87 | }
88 | };
89 | fnNotify(this.status);
90 | });
91 | };
92 | }
93 |
94 | export default EmulatorStatus;
--------------------------------------------------------------------------------
/src/components/emulator/net/jsep_protocol_driver.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 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 | import { EventEmitter } from "events";
17 | import { Empty } from "google-protobuf/google/protobuf/empty_pb";
18 | /**
19 | * This drives the jsep protocol with the emulator, and can be used to
20 | * send key/mouse/touch events to the emulator. Events will be send
21 | * over the data channel if open, otherwise they will be send via the
22 | * grpc endpoint.
23 | *
24 | * The jsep protocol is described here:
25 | * https://rtcweb-wg.github.io/jsep/.
26 | *
27 | * This class can fire two events:
28 | *
29 | * - `connected` when the stream has become available.
30 | * - `disconnected` when the stream broke down, or when we failed to establish a connection
31 | *
32 | * You usually want to start the stream after instantiating this object. Do not forget to
33 | * disconnect once you are finished to terminate the message pump.
34 | *
35 | *
36 | * @example
37 | * jsep = new JsepProtocolDriver(emulator, s => { video.srcObject = s; video.play() });
38 | * jsep.startStream();
39 | *
40 | * @export
41 | * @class JsepProtocol
42 | */
43 | export default class JsepProtocol {
44 | /**
45 | * Creates an instance of JsepProtocol.
46 | * @param {EmulatorService} emulator Service used to make the gRPC calls
47 | * @param {RtcService} rtc Service used to open up the rtc calls.
48 | * @param {boolean} poll True if we should use polling
49 | * @param {callback} onConnect optional callback that is invoked when a stream is available
50 | * @param {callback} onDisconnect optional callback that is invoked when the stream is closed.
51 | * @memberof JsepProtocol
52 | */
53 | constructor(emulator, rtc, poll, onConnect, onDisconnect) {
54 | this.emulator = emulator;
55 | this.rtc = rtc;
56 | this.events = new EventEmitter();
57 |
58 | // Workaround for older emulators that send messages out of order
59 | // and do not handle all answers properly.
60 | this.old_emu_patch = {
61 | candidates: [],
62 | sdp: null,
63 | haveOffer: false,
64 | answer: false,
65 | };
66 |
67 | this.poll = poll;
68 | this.guid = null;
69 | this.stream = null;
70 | this.event_forwarders = {};
71 | if (onConnect) this.events.on("connected", onConnect);
72 | if (onDisconnect) this.events.on("disconnected", onDisconnect);
73 | }
74 |
75 | on = (name, fn) => {
76 | this.events.on(name, fn);
77 | };
78 |
79 | /**
80 | * Disconnects the stream. This will stop the message pump as well.
81 | *
82 | * @memberof JsepProtocol
83 | */
84 | disconnect = () => {
85 | this.connected = false;
86 | if (this.peerConnection) this.peerConnection.close();
87 | if (this.stream) {
88 | this.stream.cancel();
89 | this.stream = null;
90 | }
91 | this.active = false;
92 | this.old_emu_patch = {
93 | candidates: [],
94 | sdp: null,
95 | haveOffer: false,
96 | answer: false,
97 | };
98 | this.events.emit("disconnected", this);
99 | };
100 |
101 | /**
102 | * Initiates the JSEP protocol.
103 | *
104 | * @memberof JsepProtocol
105 | */
106 | startStream = () => {
107 | const self = this;
108 | this.connected = false;
109 | this.peerConnection = null;
110 | this.active = true;
111 | this.old_emu_patch = {
112 | candidates: [],
113 | sdp: null,
114 | haveOffer: false,
115 | answer: false,
116 | };
117 | var request = new Empty();
118 | this.rtc.requestRtcStream(request, {}, (err, response) => {
119 | if (err) {
120 | console.error("Failed to configure rtc stream: " + JSON.stringify(err));
121 | this.disconnect();
122 | return;
123 | }
124 |
125 | // Configure
126 | self.guid = response;
127 | self.connected = true;
128 |
129 | if (!this.poll) {
130 | // Streaming envoy based.
131 | self._streamJsepMessage();
132 | } else {
133 | // Poll pump messages, go/envoy based proxy.
134 | console.debug("Polling jsep messages.");
135 | self._receiveJsepMessage();
136 | }
137 | });
138 | };
139 |
140 | cleanup = () => {
141 | this.disconnect();
142 | if (this.peerConnection) {
143 | this.peerConnection.removeEventListener(
144 | "track",
145 | this._handlePeerConnectionTrack
146 | );
147 | this.peerConnection.removeEventListener(
148 | "icecandidate",
149 | this._handlePeerIceCandidate
150 | );
151 | this.peerConnection = null;
152 | }
153 | this.event_forwarders = {};
154 | };
155 |
156 | _handlePeerConnectionTrack = (e) => {
157 | this.events.emit("connected", e.track);
158 | };
159 |
160 | _handlePeerConnectionStateChange = (e) => {
161 | switch (this.peerConnection.connectionState) {
162 | case "disconnected":
163 | // At least one of the ICE transports for the connection is in the "disconnected" state
164 | // and none of the other transports are in the state "failed", "connecting",
165 | // or "checking".
166 | case "failed":
167 | // One or more of the ICE transports on the connection is in the "failed" state.
168 | case "closed":
169 | //The RTCPeerConnection is closed.
170 | this.disconnect();
171 | }
172 | };
173 |
174 | send(label, msg) {
175 | let bytes = msg.serializeBinary();
176 | let forwarder = this.event_forwarders[label];
177 | console.log("Send " + label + " " + JSON.stringify(msg.toObject()));
178 | // Send via data channel/gRPC bridge.
179 | if (this.connected && forwarder && forwarder.readyState == "open") {
180 | this.event_forwarders[label].send(bytes);
181 | } else {
182 | // Fallback to using the gRPC protocol
183 | switch (label) {
184 | case "mouse":
185 | this.emulator.sendMouse(msg);
186 | break;
187 | case "keyboard":
188 | this.emulator.sendKey(msg);
189 | break;
190 | case "touch":
191 | this.emulator.sendTouch(msg);
192 | break;
193 | }
194 | }
195 | }
196 |
197 | _handlePeerIceCandidate = (e) => {
198 | if (e.candidate === null) return;
199 | this._sendJsep({ candidate: e.candidate });
200 | };
201 |
202 | _handleDataChannel = (e) => {
203 | let channel = e.channel;
204 | this.event_forwarders[channel.label] = channel;
205 | };
206 |
207 | _handleStart = (signal) => {
208 | this.peerConnection = new RTCPeerConnection(signal.start);
209 | this.peerConnection.ontrack = this._handlePeerConnectionTrack;
210 | this.peerConnection.onicecandidate = this._handlePeerIceCandidate;
211 | this.peerConnection.onconnectionstatechange = this._handlePeerConnectionStateChange;
212 | this.peerConnection.ondatachannel = this._handleDataChannel;
213 | this.peerConnection.onsignalingstatechange = this._handlePeerState;
214 | };
215 |
216 | _handlePeerState = (event) => {
217 | if (!this.peerConnection) {
218 | console.log("Peerconnection no longer available, ignoring signal state.");
219 | }
220 | switch (this.peerConnection.signalingState) {
221 | case "have-remote-offer":
222 | this.old_emu_patch.haveOffer = true;
223 | while (this.old_emu_patch.candidates.length > 0) {
224 | this._handleCandidate(this.old_emu_patch.candidates.shift());
225 | }
226 | break;
227 | }
228 | };
229 |
230 | _handleSDP = async (signal) => {
231 | // We should not call this more than once..
232 | this.old_emu_patch.sdp = null;
233 | this.peerConnection.setRemoteDescription(new RTCSessionDescription(signal));
234 | const answer = await this.peerConnection.createAnswer();
235 | if (answer) {
236 | // Older emulators cannot handle multiple answers, so make sure we do not send one
237 | // again.
238 | if (!this.old_emu_patch.answer) {
239 | this.old_emu_patch.answer = true;
240 | this.peerConnection.setLocalDescription(answer);
241 | this._sendJsep({ sdp: answer });
242 | }
243 | } else {
244 | this.disconnect();
245 | }
246 | };
247 |
248 | _handleCandidate = (signal) => {
249 | this.peerConnection.addIceCandidate(new RTCIceCandidate(signal));
250 | };
251 |
252 | _handleSignal = (signal) => {
253 | if (signal.start) {
254 | this._handleStart(signal);
255 | }
256 | if (signal.bye) {
257 | this._handleBye();
258 | }
259 | if (signal.sdp && !this.old_emu_patch.sdp) {
260 | this.old_emu_patch.sdp = signal;
261 | }
262 | if (signal.candidate) {
263 | this.old_emu_patch.candidates.push(signal);
264 | }
265 |
266 | if (!!this.peerConnection) {
267 | // We have created a peer connection..
268 | if (this.old_emu_patch.sdp) {
269 | this._handleSDP(this.old_emu_patch.sdp);
270 | }
271 |
272 | if (this.old_emu_patch.haveOffer) {
273 | // We have a remote peer, add the candidates in.
274 | while (this.old_emu_patch.candidates.length > 0) {
275 | this._handleCandidate(this.old_emu_patch.candidates.shift());
276 | }
277 | }
278 | }
279 | };
280 |
281 | _handleJsepMessage = (message) => {
282 | try {
283 | const signal = JSON.parse(message);
284 | this._handleSignal(signal);
285 | } catch (e) {
286 | console.error(
287 | "Failed to handle message: [" +
288 | message +
289 | "], due to: " +
290 | JSON.stringify(e)
291 | );
292 | }
293 | };
294 |
295 | _handleBye = () => {
296 | if (this.connected) {
297 | this.disconnect();
298 | }
299 | };
300 |
301 | _sendJsep = (jsonObject) => {
302 | /* eslint-disable */
303 | var request = new proto.android.emulation.control.JsepMsg();
304 | request.setId(this.guid);
305 | request.setMessage(JSON.stringify(jsonObject));
306 | this.rtc.sendJsepMessage(request);
307 | };
308 |
309 | _streamJsepMessage = () => {
310 | if (!this.connected) return;
311 | var self = this;
312 |
313 | this.stream = this.rtc.receiveJsepMessages(this.guid, {});
314 | this.stream.on("data", (response) => {
315 | const msg = response.getMessage();
316 | self._handleJsepMessage(msg);
317 | });
318 | this.stream.on("error", (e) => {
319 | console.log("Jsep message stream error:", e);
320 | if (self.connected) {
321 | console.log("Attempting to reconnect to jsep message stream.");
322 | self._streamJsepMessage();
323 | }
324 | });
325 | this.stream.on("end", (e) => {
326 | if (self.connected) {
327 | console.log("Stream end while still connected.. Reconnecting");
328 | self._streamJsepMessage();
329 | }
330 | });
331 | };
332 |
333 | // This function is a fallback for v1 (go based proxy), that does not support streaming.
334 | _receiveJsepMessage = () => {
335 | if (!this.connected) return;
336 |
337 | var self = this;
338 |
339 | // This is a blocking call, that will return as soon as a series
340 | // of messages have been made available, or if we reach a timeout
341 | this.rtc.receiveJsepMessage(this.guid, {}, (err, response) => {
342 | if (err) {
343 | console.error(
344 | "Failed to receive jsep message, disconnecting: " +
345 | JSON.stringify(err)
346 | );
347 | this.disconnect();
348 | }
349 | const msg = response.getMessage();
350 | // Handle only if we received a useful message.
351 | // it is possible to get nothing if the server decides
352 | // to kick us out.
353 | if (msg) {
354 | self._handleJsepMessage(response.getMessage());
355 | }
356 |
357 | // And pump messages. Note we must continue the message pump as we
358 | // can receive new ICE candidates at any point in time.
359 | if (self.active) {
360 | self._receiveJsepMessage();
361 | }
362 | });
363 | };
364 | }
365 |
--------------------------------------------------------------------------------
/src/components/emulator/net/logcat.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 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 | import { EventEmitter } from "events";
17 | import "../../../proto/emulator_controller_pb";
18 | import { EmulatorControllerService } from "../../../proto/emulator_web_client";
19 |
20 | /**
21 | * Observe the logcat stream from the emulator.
22 | *
23 | * Streaming is done by either polling the emulator endpoint or making a streaming call.
24 | *
25 | * It will send out the following events:
26 | *
27 | * - `start` whenever the start method was called.
28 | * - `data` whenever new data became available.
29 | * - `end` whenever the stream is finished, either because it was stopped, or due to an error.
30 | */
31 | class Logcat {
32 | /**
33 | * Creates a logcat stream.
34 | *
35 | * The authentication service should implement the following methods:
36 | * - `authHeader()` which must return a set of headers that should be send along with a request.
37 | * - `unauthorized()` a function that gets called when a 401 was received.
38 | *
39 | * @constructor
40 | * @param {object} uriOrEmulator
41 | * @param {object} auth
42 | */
43 | constructor(uriOrEmulator, auth) {
44 | if (uriOrEmulator instanceof EmulatorControllerService) {
45 | this.emulator = uriOrEmulator;
46 | } else {
47 | this.emulator = new EmulatorControllerService(uriOrEmulator, auth);
48 | }
49 | this.offset = 0;
50 | this.lastline = "";
51 | this.stream = null;
52 | this.events = new EventEmitter();
53 | this.refreshRate = 1000;
54 | this.timerID = null;
55 | }
56 |
57 | /**
58 | * Register a listener.
59 | *
60 | * @param {string} name Name of the event.
61 | * @param {Callback} fn Function to notify on the given event.
62 | * @memberof Logcat
63 | */
64 | on = (name, fn) => {
65 | this.events.on(name, fn);
66 | };
67 |
68 | /**
69 | * Removes a listener.
70 | *
71 | * @param {string} name Name of the event.
72 | * @param {Callback} fn Function to notify on the given event.
73 | * @memberof Logcat
74 | */
75 | off = (name, fn) => {
76 | this.events.off(name, fn);
77 | };
78 |
79 | /**
80 | * Cancel the currently active logcat stream.
81 | *
82 | * @memberof Logcat
83 | */
84 | stop = () => {
85 | if (this.stream) {
86 | this.stream.cancel();
87 | }
88 | if (this.timerID) {
89 | clearInterval(this.timerID);
90 | this.timerID = null;
91 | }
92 | this.events.emit("end");
93 | };
94 |
95 | pollStream = () => {
96 | const self = this;
97 | /* eslint-disable */
98 | const request = new proto.android.emulation.control.LogMessage();
99 | request.setStart(this.offset);
100 | this.emulator.getLogcat(request, {}, (err, response) => {
101 | if (err) {
102 | this.stop();
103 | }
104 | if (response) {
105 | const nextOffset = response.getNext();
106 | if (nextOffset > self.offset) {
107 | self.offset = response.getNext();
108 | self.events.emit("data", response.getContents());
109 | }
110 | }
111 | });
112 | };
113 |
114 | // Uses streaming, this really locks up the ui, so best not to use for now.
115 | stream = () => {
116 | const self = this;
117 | /* eslint-disable */
118 | const request = new proto.android.emulation.control.LogMessage();
119 | request.setStart(this.offset);
120 | this.stream = this.emulator.streamLogcat(request);
121 | this.stream.on("data", (response) => {
122 | self.offset = response.getNext();
123 | const contents = response.getContents();
124 | self.events.emit("data", contents);
125 | });
126 | this.stream.on("error", (error) => {
127 | if ((error.code = 1)) {
128 | // Ignore we got cancelled.
129 | }
130 | });
131 | };
132 |
133 | /**
134 | * Requests the logcat stream, invoking the callback when a log line arrives.
135 | *
136 | * *Note:* Streaming can cause serious UI delays, so best not to use it.
137 | *
138 | * @param {Callback} fnNotify when a new log line arrives.
139 | * @param {number} refreshRate polling interval, or 0 if you wish to use streaming.
140 | * @memberof Logcat
141 | */
142 | start = (fnNotify, refreshRate = 1000) => {
143 | if (fnNotify) this.on("data", fnNotify);
144 |
145 | this.refreshRate = refreshRate;
146 | if (this.refreshRate > 0) {
147 | this.timerID = setInterval(() => this.pollStream(), this.refreshRate);
148 | } else {
149 | this.stream();
150 | }
151 | this.events.emit("start");
152 | };
153 | }
154 |
155 | export default Logcat;
156 |
--------------------------------------------------------------------------------
/src/components/emulator/views/event_handler.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 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 | import PropTypes from "prop-types";
17 | import React from "react";
18 | import * as Proto from "../../../proto/emulator_controller_pb";
19 | import EmulatorStatus from "../net/emulator_status";
20 |
21 | /**
22 | * A handler that extends a view to send key/mouse events to the emulator.
23 | * It wraps the inner component in a div, and will use the jsep handler
24 | * to send key/mouse/touch events over the proper channel.
25 | *
26 | * It will translate the mouse events based upon the returned display size of
27 | * the emulator.
28 | *
29 | * You usually want to wrap a EmulatorRtcview, or EmulatorPngView in it.
30 | */
31 | export default function withMouseKeyHandler(WrappedComponent) {
32 | return class extends React.Component {
33 | constructor(props) {
34 | super(props);
35 | this.state = {
36 | deviceHeight: 1920,
37 | deviceWidth: 1080,
38 | mouse: {
39 | xp: 0,
40 | yp: 0,
41 | mouseDown: false, // Current state of mouse
42 | // Current button pressed.
43 | // In proto, 0 is "no button", 1 is left, and 2 is right.
44 | mouseButton: 0,
45 | },
46 | };
47 | this.handler = React.createRef();
48 | const { emulator } = this.props;
49 | this.status = new EmulatorStatus(emulator);
50 | }
51 |
52 | static propTypes = {
53 | /** The emulator object */
54 | emulator: PropTypes.object.isRequired,
55 | /** Jsep protocol driver, used to send mouse & touch events. */
56 | jsep: PropTypes.object.isRequired,
57 | };
58 |
59 | componentDidMount() {
60 | this.getScreenSize();
61 | }
62 |
63 | getScreenSize() {
64 | this.status.updateStatus((state) => {
65 | this.setState({
66 | deviceWidth: parseInt(state.hardwareConfig["hw.lcd.width"]) || 1080,
67 | deviceHeight: parseInt(state.hardwareConfig["hw.lcd.height"]) || 1920,
68 | });
69 | });
70 | }
71 |
72 | onContextMenu = (e) => {
73 | e.preventDefault();
74 | };
75 |
76 | scaleCoordinates = (xp, yp) => {
77 | // It is totally possible that we send clicks that are offscreen..
78 | const { deviceWidth, deviceHeight } = this.state;
79 | const { clientHeight, clientWidth } = this.handler.current;
80 | const scaleX = deviceWidth / clientWidth;
81 | const scaleY = deviceHeight / clientHeight;
82 | const x = Math.round(xp * scaleX);
83 | const y = Math.round(yp * scaleY);
84 |
85 | if (isNaN(x) || isNaN(y)) {
86 | console.log("Ignoring: x: " + x + ", y:" + y);
87 | return { x: -1, y: -1 };
88 | }
89 | return { x: x, y: y, scaleX: scaleX, scaleY: scaleY };
90 | };
91 |
92 | setMouseCoordinates = () => {
93 | // Forward the request to the jsep engine.
94 | const { mouseDown, mouseButton, xp, yp } = this.state.mouse;
95 | var request = new Proto.MouseEvent();
96 | const { x, y } = this.scaleCoordinates(xp, yp);
97 | request.setX(x);
98 | request.setY(y);
99 | request.setButtons(mouseDown ? mouseButton : 0);
100 | const { jsep } = this.props;
101 | jsep.send("mouse", request);
102 | };
103 |
104 | handleKey = (eventType) => {
105 | return (e) => {
106 | // Disable jumping to next control when pressing the space bar.
107 | if (e.keyCode === 32) {
108 | e.preventDefault();
109 | }
110 | var request = new Proto.KeyboardEvent();
111 | request.setEventtype(
112 | eventType === "KEYDOWN"
113 | ? Proto.KeyboardEvent.KeyEventType.KEYDOWN
114 | : eventType === "KEYUP"
115 | ? Proto.KeyboardEvent.KeyEventType.KEYUP
116 | : Proto.KeyboardEvent.KeyEventType.KEYPRESS
117 | );
118 | request.setKey(e.key);
119 | const { jsep } = this.props;
120 | jsep.send("keyboard", request);
121 | };
122 | };
123 |
124 | // Properly handle the mouse events.
125 | handleMouseDown = (e) => {
126 | const { offsetX, offsetY } = e.nativeEvent;
127 | this.setState(
128 | {
129 | mouse: {
130 | xp: offsetX,
131 | yp: offsetY,
132 | mouseDown: true,
133 | // In browser's MouseEvent.button property,
134 | // 0 stands for left button and 2 stands for right button.
135 | mouseButton: e.button === 0 ? 1 : e.button === 2 ? 2 : 0,
136 | },
137 | },
138 | this.setMouseCoordinates
139 | );
140 | };
141 |
142 | handleMouseUp = (e) => {
143 | const { offsetX, offsetY } = e.nativeEvent;
144 | this.setState(
145 | {
146 | mouse: { xp: offsetX, yp: offsetY, mouseDown: false, mouseButton: 0 },
147 | },
148 | this.setMouseCoordinates
149 | );
150 | };
151 |
152 | handleMouseMove = (e) => {
153 | // Let's not overload the endpoint with useless events.
154 | if (!this.state.mouse.mouseDown) return;
155 |
156 | const { offsetX, offsetY } = e.nativeEvent;
157 | var mouse = this.state.mouse;
158 | mouse.xp = offsetX;
159 | mouse.yp = offsetY;
160 | this.setState({ mouse: mouse }, this.setMouseCoordinates);
161 | };
162 |
163 | /**
164 | * Scales an axis to linux input codes that the emulator understands.
165 | *
166 | * @param {*} value The value to transform.
167 | * @param {*} minIn The minimum value, the lower bound of the value param.
168 | * @param {*} maxIn The maximum value, the upper bound of the value param.
169 | */
170 | scaleAxis = (value, minIn, maxIn) => {
171 | const minOut = 0x0; // EV_ABS_MIN
172 | const maxOut = 0x7fff; // EV_ABS_MAX
173 | const rangeOut = maxOut - minOut;
174 | const rangeIn = maxIn - minIn;
175 | if (rangeIn < 1) {
176 | return minOut + rangeOut / 2;
177 | }
178 | return (((value - minIn) * rangeOut) / rangeIn + minOut) | 0;
179 | };
180 |
181 | setTouchCoordinates = (type, touches, minForce, maxForce) => {
182 | // We need to calculate the offset of the touch events.
183 | const rect = this.handler.current.getBoundingClientRect();
184 | const scaleCoordinates = this.scaleCoordinates;
185 | const touchesToSend = Object.keys(touches).map((index) => {
186 | const touch = touches[index];
187 | const { clientX, clientY, identifier, force, radiusX, radiusY } = touch;
188 | const offsetX = clientX - rect.left;
189 | const offsetY = clientY - rect.top;
190 | const { x, y, scaleX, scaleY } = scaleCoordinates(offsetX, offsetY);
191 | const scaledRadiusX = 2 * radiusX * scaleX;
192 | const scaledRadiusY = 2 * radiusY * scaleY;
193 |
194 | const protoTouch = new Proto.Touch();
195 | protoTouch.setX(x | 0);
196 | protoTouch.setY(y | 0);
197 | protoTouch.setIdentifier(identifier);
198 |
199 | // Normalize the force
200 | const MT_PRESSURE = this.scaleAxis(
201 | Math.max(minForce, Math.min(maxForce, force)),
202 | 0,
203 | 1
204 | );
205 | protoTouch.setPressure(MT_PRESSURE);
206 | protoTouch.setTouchMajor(Math.max(scaledRadiusX, scaledRadiusY) | 0);
207 | protoTouch.setTouchMinor(Math.min(scaledRadiusX, scaledRadiusY) | 0);
208 |
209 | return protoTouch;
210 | });
211 |
212 | // Make the grpc call.
213 | const requestTouchEvent = new Proto.TouchEvent();
214 | requestTouchEvent.setTouchesList(touchesToSend);
215 | const { jsep } = this.props;
216 | jsep.send("touch", requestTouchEvent);
217 | };
218 |
219 | handleTouch = (minForce, maxForce) => {
220 | return (e) => {
221 | // Make sure they are not processed as mouse events later on.
222 | // See https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
223 | if (e.cancelable) {
224 | e.preventDefault();
225 | }
226 | // Some browsers do net have a force sensor, so we have to "fake" values
227 | // for start/move/end events.
228 | this.setTouchCoordinates(
229 | e.nativeEvent.type,
230 | e.nativeEvent.changedTouches,
231 | minForce,
232 | maxForce
233 | );
234 | };
235 | };
236 |
237 | render() {
238 | return (
239 |
263 |
264 |
265 | );
266 | }
267 | };
268 | }
269 |
--------------------------------------------------------------------------------
/src/components/emulator/views/simple_png_view.js:
--------------------------------------------------------------------------------
1 | import PropTypes from "prop-types";
2 | import React, { useState, useEffect } from "react";
3 | import * as Proto from "../../../proto/emulator_controller_pb";
4 |
5 | const EmulatorPngView = ({
6 | emulator,
7 | onStateChange,
8 | poll,
9 | width,
10 | height,
11 | "data-testid": dataTestId,
12 | }) => {
13 | const [png, setPng] = useState("");
14 | const [connect, setConnect] = useState("connecting");
15 | var screenShot = null;
16 |
17 | useEffect(() => {
18 | startStream();
19 | return () => {
20 | setConnect("disconnected");
21 | if (screenShot) {
22 | screenShot.cancel();
23 | }
24 | };
25 | }, [width, height]);
26 |
27 | useEffect(() => {
28 | if (onStateChange) {
29 | onStateChange(connect);
30 | }
31 | }, [connect]);
32 |
33 | const startStream = () => {
34 | setConnect("connecting");
35 | const request = new Proto.ImageFormat();
36 | if (!isNaN(width)) {
37 | request.setWidth(Math.floor(width));
38 | }
39 | if (!isNaN(height)) {
40 | request.setWidth(Math.floor(height));
41 | }
42 |
43 | if (poll && connect !== "disconnected") {
44 | emulator.getScreenshot(request, {}, (err, response) => {
45 | setConnect("connected");
46 | setPng("data:image/jpeg;base64," + response.getImage_asB64());
47 | startStream();
48 | });
49 | } else {
50 | var receivedImage = false;
51 | screenShot = emulator.streamScreenshot(request);
52 | screenShot.on("data", (response) => {
53 | receivedImage = true;
54 | setConnect("connected");
55 | setPng("data:image/jpeg;base64," + response.getImage_asB64());
56 | });
57 | screenShot.on("error", (e) => {
58 | console.warn("Screenshot stream broken", e);
59 | if (receivedImage) {
60 | setConnect("connecting");
61 | startStream();
62 | } else {
63 | setConnect("disconnected");
64 | }
65 | });
66 | }
67 | };
68 |
69 | const preventDragHandler = (e) => {
70 | e.preventDefault();
71 | };
72 |
73 |
74 | return (
75 |
86 |

92 |
93 | );
94 | };
95 |
96 | EmulatorPngView.propTypes = {
97 | emulator: PropTypes.object,
98 | onStateChange: PropTypes.func,
99 | poll: PropTypes.bool,
100 | width: PropTypes.number,
101 | height: PropTypes.number,
102 | "data-testid": PropTypes.string,
103 | };
104 |
105 | export default EmulatorPngView;
106 |
--------------------------------------------------------------------------------
/src/components/emulator/views/webrtc_view.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 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 | import PropTypes from "prop-types";
17 | import React, { useEffect, useRef, useState } from "react";
18 |
19 | const EmulatorWebrtcView = ({
20 | jsep,
21 | onStateChange,
22 | onAudioStateChange,
23 | muted,
24 | volume,
25 | onError,
26 | }) => {
27 | const [audio, setAudio] = useState(false);
28 | const videoRef = useRef(null);
29 | const [connect, setConnect] = useState("connecting");
30 |
31 |
32 | useEffect(() => {
33 | if (onStateChange) {
34 | onStateChange(connect);
35 | }
36 | }, [connect]);
37 |
38 |
39 | useEffect(() => {
40 | if (onAudioStateChange) {
41 | onAudioStateChange(audio);
42 | }
43 | }, [audio]);
44 |
45 | const onDisconnect = () => {
46 | setConnect("disconnected");
47 | setAudio(false);
48 | };
49 |
50 | const onConnect = (track) => {
51 | setConnect("connected");
52 | const video = videoRef.current;
53 | if (!video) {
54 | // Component was unmounted.
55 | return;
56 | }
57 |
58 | if (!video.srcObject) {
59 | video.srcObject = new MediaStream();
60 | }
61 | video.srcObject.addTrack(track);
62 | if (track.kind === "audio") {
63 | setAudio(true);
64 | }
65 | };
66 |
67 | const safePlay = () => {
68 | const video = videoRef.current;
69 | if (!video) {
70 | // Component was unmounted.
71 | return;
72 | }
73 |
74 | const possiblePromise = video.play();
75 | if (possiblePromise) {
76 | possiblePromise
77 | .then((_) => {
78 | console.debug("Automatic playback started!");
79 | })
80 | .catch((error) => {
81 | // Notify listeners that we cannot start.
82 | onError(error);
83 | });
84 | }
85 | };
86 |
87 | const onCanPlay = () => {
88 | safePlay();
89 | };
90 |
91 | const onContextMenu = (e) => {
92 | e.preventDefault();
93 | };
94 |
95 | useEffect(() => {
96 | jsep.on("connected", onConnect);
97 | jsep.on("disconnected", onDisconnect);
98 | jsep.startStream()
99 |
100 | setConnect("connecting");
101 |
102 | return () => {
103 | jsep.disconnect();
104 | };
105 | }, []);
106 |
107 | return (
108 |
123 | );
124 | };
125 |
126 | EmulatorWebrtcView.propTypes = {
127 | jsep: PropTypes.object,
128 | onStateChange: PropTypes.func,
129 | onAudioStateChange: PropTypes.func,
130 | muted: PropTypes.bool,
131 | volume: PropTypes.number,
132 | onError: PropTypes.func,
133 | };
134 |
135 | EmulatorWebrtcView.defaultProps = {
136 | muted: true,
137 | volume: 1.0,
138 | onError: (e) => console.error("WebRTC error: " + e),
139 | };
140 |
141 | export default EmulatorWebrtcView;
142 |
143 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 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 | import Emulator from "./components/emulator/emulator";
17 | import Logcat from "./components/emulator/net/logcat";
18 | import EmulatorStatus from "./components/emulator/net/emulator_status";
19 |
20 | export { Emulator, Logcat, EmulatorStatus };
21 |
--------------------------------------------------------------------------------
/src/proto/emulator_web_client.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 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 | import { EmulatorControllerClient } from "../proto/emulator_controller_grpc_web_pb";
17 | import { RtcClient } from "../proto/rtc_service_grpc_web_pb";
18 | import { GrpcWebClientBase } from "grpc-web";
19 | import { EventEmitter } from "events";
20 |
21 | export class NopAuthenticator {
22 | authHeader = () => {
23 | return {};
24 | };
25 |
26 | unauthorized = () => { };
27 | }
28 |
29 | /**
30 | * A GrcpWebClientBase that inject authentication headers and intercepts
31 | * errors. If the errors are 401, the unauthorized method of the authenticator will be invoked.
32 | *
33 | * @export
34 | * @class EmulatorWebClient
35 | * @extends {GrpcWebClientBase}
36 | */
37 | class EmulatorWebClient extends GrpcWebClientBase {
38 | constructor(options, auth) {
39 | super(options);
40 | this.auth = auth;
41 | this.events = new EventEmitter();
42 | this.events.on("error", e => {console.log("low level gRPC error: " + JSON.stringify(e));})
43 | }
44 |
45 | on = (name, fn) => {
46 | this.events.on(name, fn);
47 | };
48 |
49 | rpcCall = (method, request, metadata, methodinfo, callback) => {
50 | const authHeader = this.auth.authHeader();
51 | const meta = { ...metadata, ...authHeader };
52 | const self = this;
53 | return super.rpcCall(method, request, meta, methodinfo, (err, res) => {
54 | if (err) {
55 | if (err.code === 401) self.auth.unauthorized();
56 | if (self.events)
57 | self.events.emit("error", err);
58 | }
59 | if (callback) callback(err, res);
60 | });
61 | };
62 |
63 | serverStreaming = (method, request, metadata, methodInfo) => {
64 | const authHeader = this.auth.authHeader();
65 | const meta = { ...metadata, ...authHeader };
66 | const stream = super.serverStreaming(method, request, meta, methodInfo);
67 | const self = this;
68 |
69 | // Intercept errors.
70 | stream.on("error", e => {
71 | if (e.code === 401) {
72 | self.auth.unauthorized();
73 | }
74 | self.events.emit("error", e);
75 | });
76 | return stream;
77 | };
78 | }
79 |
80 | /**
81 | * An EmulatorControllerService is an EmulatorControllerClient that inject authentication headers.
82 | * You can provide your own authenticator service that must implement the following mehtods:
83 | *
84 | * - `authHeader()` which must return a set of headers that should be send along with a request.
85 | * - `unauthorized()` a function that gets called when a 401 was received.
86 | *
87 | * You can use this to simplify handling authentication failures.
88 | *
89 | * TODO(jansene): Maybe expose error handling? That way it does
90 | * not have to be repeated at every function call.
91 | *
92 | * @export
93 | * @class EmulatorControllerService
94 | * @extends {EmulatorControllerClient}
95 | */
96 | export class EmulatorControllerService extends EmulatorControllerClient {
97 | /**
98 | *Creates an instance of EmulatorControllerService.
99 | * @param {string} uri of the emulator controller endpoint.
100 | * @param {Authenticator} authenticator used to authenticate with the emulator endpoint.
101 | * @param onError callback that will be invoked when a low level gRPC error arises.
102 | * @memberof EmulatorControllerService
103 | */
104 | constructor(uri, authenticator, onError) {
105 | super(uri);
106 | if (!authenticator) authenticator = new NopAuthenticator();
107 | this.client_ = new EmulatorWebClient({}, authenticator);
108 | if (onError) this.client_.on('error', e => { onError(e); });
109 | }
110 | }
111 |
112 |
113 | /**
114 | * An RtcService is an RtcClient that inject authentication headers.
115 | * You can provide your own authenticator service that must implement the following mehtods:
116 | *
117 | * - `authHeader()` which must return a set of headers that should be send along with a request.
118 | * - `unauthorized()` a function that gets called when a 401 was received.
119 | *
120 | * You can use this to simplify handling authentication failures.
121 | *
122 | * @export
123 | * @class EmulatorControllerService
124 | * @extends {RtcClient}
125 | */
126 | export class RtcService extends RtcClient {
127 | /**
128 | *Creates an instance of RtcService.
129 | * @param {string} uri of the emulator controller endpoint.
130 | * @param {Authenticator} authenticator used to authenticate with the emulator endpoint.
131 | * @param onError callback that will be invoked when a low level gRPC error arises.
132 | * @memberof RtcService
133 | */
134 | constructor(uri, authenticator, onError) {
135 | super(uri);
136 | if (!authenticator) authenticator = new NopAuthenticator();
137 | this.client_ = new EmulatorWebClient({}, authenticator);
138 | if (onError) this.client_.on('error', e => { onError(e); });
139 | }
140 | }
--------------------------------------------------------------------------------
/test/cloudbuild.yaml:
--------------------------------------------------------------------------------
1 | steps:
2 | - name: "gcr.io/cloud-builders/gcloud"
3 | args:
4 | - kms
5 | - decrypt
6 | - --ciphertext-file=npmrc.enc
7 | - --plaintext-file=/root/.npmrc
8 | - --location=global
9 | - --keyring=my-keyring
10 | - --key=npm-key
11 | volumes:
12 | - name: "home"
13 | path: /root/
14 | - name: gcr.io/emu-dev-cts/protoc
15 | args:
16 | [
17 | "--include_imports",
18 | "--include_source_info",
19 | "--proto_path",
20 | "./proto",
21 | "--descriptor_set_out",
22 | "api_descriptor.pb",
23 | "--plugin=protoc-gen-grpc-web=/protoc/bin/protoc-gen-grpc-web",
24 | "--js_out=import_style=commonjs:src/proto",
25 | "--grpc-web_out=import_style=commonjs,mode=grpcwebtext:src/proto",
26 | "./proto/emulator_controller.proto",
27 | "./proto/rtc_service.proto",
28 | ]
29 | - name: "gcr.io/cloud-builders/npm"
30 | args: ["install"]
31 | - name: "gcr.io/cloud-builders/npm"
32 | args: ["run", "build"]
33 | - name: "gcr.io/cloud-builders/npm"
34 | args: ["test"]
35 | - name: "gcr.io/cloud-builders/npm"
36 | args: ["publish", "/workspace", "--dry-run"]
37 | env:
38 | - HOME=/root/
39 | volumes:
40 | - name: "home"
41 | path: /root/
42 |
--------------------------------------------------------------------------------
/test/emulator.test.js:
--------------------------------------------------------------------------------
1 |
2 | /**
3 | * @jest-environment jsdom
4 | */
5 | /*
6 | * Copyright 2020 The Android Open Source Project
7 | *
8 | * Licensed under the Apache License, Version 2.0 (the "License")
9 | * you may not use this file except in compliance with the License.
10 | * You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing, software
15 | * distributed under the License is distributed on an "AS IS" BASIS,
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 | * See the License for the specific language governing permissions and
18 | * limitations under the License.
19 | */
20 | import "@testing-library/jest-dom";
21 | import "babel-polyfill";
22 | import { EventEmitter } from "events";
23 | import React from "react";
24 | import { render, fireEvent, screen, waitFor } from "@testing-library/react";
25 | import Emulator from "../src/components/emulator/emulator";
26 | import * as Proto from "../src/proto/emulator_controller_pb";
27 | import * as Rtc from "../src/proto/rtc_service_pb";
28 | import {
29 | RtcService,
30 | EmulatorControllerService,
31 | } from "../src/proto/emulator_web_client";
32 |
33 | jest.mock("../src/proto/emulator_web_client");
34 |
35 | // See https://github.com/testing-library/react-testing-library/issues/470
36 | // As well as https://github.com/facebook/react/issues/10389
37 | // All because of the "muted" tag on our video element inside webrtc_view
38 | const renderIgnoringUnstableFlushDiscreteUpdates = (component) => {
39 | // tslint:disable: no-console
40 | const originalError = console.error;
41 | const error = jest.fn();
42 | console.error = error;
43 | const result = render(component);
44 | expect(error).toHaveBeenCalledTimes(1);
45 | expect(error).toHaveBeenCalledWith(
46 | "Warning: unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering.%s",
47 | expect.any(String)
48 | );
49 | console.error = originalError;
50 | // tslint:enable: no-console
51 | return result;
52 | };
53 |
54 | describe("The emulator", () => {
55 | beforeEach(() => {
56 | // Clear all instances and calls to constructor and all methods:
57 | RtcService.mockClear();
58 | EmulatorControllerService.mockClear();
59 | });
60 |
61 | test("Creates gRPC services", async () => {
62 | render(
63 |
64 | );
65 |
66 | expect(EmulatorControllerService).toHaveBeenCalled();
67 | expect(RtcService).toHaveBeenCalled();
68 | // Shipped out a gps call
69 | });
70 | test("Tries to establish a webrtc connection", async () => {
71 | let state;
72 | render(
73 | {
78 | state = e;
79 | }}
80 | />
81 | );
82 |
83 | await waitFor(() => state === "connecting");
84 | expect(RtcService).toHaveBeenCalled();
85 | });
86 |
87 | test("Sends a gps location to the emulator", async () => {
88 | // Let's go to Seattle!
89 | render(
90 |
96 | );
97 |
98 | const setGps = EmulatorControllerService.mock.instances[0].setGps;
99 | expect(setGps).toHaveBeenCalled();
100 |
101 | const location = new Proto.GpsState();
102 | location.setLatitude(47.6062);
103 | location.setLongitude(122.3321);
104 | location.setAltitude(undefined);
105 | location.setBearing(undefined);
106 | location.setSpeed(undefined);
107 | expect(setGps).toHaveBeenCalledWith(location);
108 | });
109 |
110 | test("The png view requests images", async () => {
111 | let pngCall = false
112 | EmulatorControllerService.mockImplementation(() => {
113 | return {
114 | streamScreenshot: jest.fn((request) => {
115 | pngCall = true
116 | return { on: jest.fn(), cancel: jest.fn() };
117 | }),
118 | getStatus: jest.fn(() => {}),
119 | };
120 | });
121 |
122 | render();
123 | expect(pngCall).toBeTruthy()
124 | });
125 | });
126 |
--------------------------------------------------------------------------------
/test/event_handler.test.js:
--------------------------------------------------------------------------------
1 | /**
2 | * @jest-environment jsdom
3 | */
4 | /*
5 | * Copyright 2020 The Android Open Source Project
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License")
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | import "@testing-library/jest-dom";
20 |
21 | import React from "react";
22 | import { render, fireEvent, screen } from "@testing-library/react";
23 | import { fakeMouseEvent, fakeTouchEvent} from "./fake_events";
24 | import withMouseKeyHandler from "../src/components/emulator/views/event_handler";
25 |
26 | import JsepProtocol from "../src/components/emulator/net/jsep_protocol_driver";
27 | import {
28 | RtcService,
29 | EmulatorControllerService,
30 | } from "../src/proto/emulator_web_client";
31 |
32 | jest.mock("../src/proto/emulator_web_client");
33 | jest.mock("../src/proto/rtc_service_pb");
34 | jest.mock("../src/proto/emulator_controller_pb");
35 |
36 |
37 | class FakeEmulator extends React.Component {
38 | render() {
39 | return (
40 |
44 | );
45 | }
46 | }
47 |
48 | const TestView = withMouseKeyHandler(FakeEmulator);
49 | describe("The event handler using a real jsep serializer", () => {
50 |
51 | const rtcServiceInstance = new RtcService("http://foo");
52 | const emulatorServiceInstance = new EmulatorControllerService("http://foo");
53 | let jsep, fakeScreen;
54 |
55 | beforeEach(() => {
56 | jest.clearAllMocks();
57 | jsep = new JsepProtocol(emulatorServiceInstance, rtcServiceInstance, true);
58 |
59 | render();
60 | fakeScreen = screen.getByTestId("fake").parentElement;
61 | Object.defineProperty(fakeScreen, "clientWidth", { get: () => 200 });
62 | Object.defineProperty(fakeScreen, "clientHeight", { get: () => 200 });
63 |
64 | expect(fakeScreen).toBeInTheDocument();
65 | });
66 |
67 | test("Forwards mouse events", () => {
68 | fireEvent(fakeScreen, fakeMouseEvent("mousedown", 10, 10));
69 | fireEvent(fakeScreen, fakeMouseEvent("mouseup", 20, 20));
70 |
71 | // Shipped out a mouse event
72 | expect(emulatorServiceInstance.sendMouse).toHaveBeenCalledTimes(2);
73 | });
74 |
75 | test("Forwards keyboard events", () => {
76 | fireEvent.keyDown(fakeScreen, { key: "Enter", code: "Enter" });
77 | fireEvent.keyUp(fakeScreen, { key: "Enter", code: "Enter" });
78 |
79 | // Shipped out a keyboard event
80 | expect(emulatorServiceInstance.sendKey).toHaveBeenCalledTimes(2);
81 | });
82 |
83 | test("Forwards touch events", () => {
84 | fireEvent(fakeScreen, fakeTouchEvent("touchstart", 10, 10, 1));
85 | fireEvent(fakeScreen, fakeTouchEvent("touchmove", 20, 20, 2));
86 | fireEvent(fakeScreen, fakeTouchEvent("touchend", 30, 30, 0));
87 |
88 | // Shipped out a touch event
89 | expect(emulatorServiceInstance.sendTouch).toHaveBeenCalledTimes(3);
90 | });
91 | });
92 |
--------------------------------------------------------------------------------
/test/fake_events.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 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 | export function fakeMouseEvent(tp, x, y, props = {}) {
17 | const event = new MouseEvent(tp, {
18 | bubbles: true,
19 | cancelable: true,
20 | ...props,
21 | });
22 |
23 | Object.defineProperty(event, "offsetX", { get: () => x });
24 | Object.defineProperty(event, "offsetY", { get: () => y });
25 | return event;
26 | }
27 |
28 | export function fakeTouchEvent(tp, x, y, force, props = {}) {
29 | const event = new TouchEvent(tp, {
30 | bubbles: true,
31 | cancelable: true,
32 | ...props,
33 | });
34 |
35 | Object.defineProperty(event, "changedTouches", {
36 | get: () => [
37 | { clientX: x, clientY: y, radiusX: 4, radiusY: 4, force: force },
38 | ],
39 | });
40 | return event;
41 | }
42 |
43 | export function resize(width, height) {
44 | const resizeEvent = document.createEvent("Event");
45 | resizeEvent.initEvent("resize", true, true);
46 |
47 | global.window.innerWidth = width || global.window.innerWidth;
48 | global.window.innerHeight = height || global.window.innerHeight;
49 | global.window.dispatchEvent(resizeEvent);
50 | }
51 |
--------------------------------------------------------------------------------
/test/jsep_protocol_driver.test.js:
--------------------------------------------------------------------------------
1 | /**
2 | * @jest-environment jsdom
3 | */
4 | /*
5 | * Copyright 2020 The Android Open Source Project
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License")
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | import { jest } from "@jest/globals";
20 | import "regenerator-runtime/runtime"; // Somehow needed for jest to be happy with ES6
21 |
22 | import * as Proto from "../src/proto/emulator_controller_pb";
23 | import * as Rtc from "../src/proto/rtc_service_pb";
24 | import JsepProtocol from "../src/components/emulator/net/jsep_protocol_driver.js";
25 | import {
26 | RtcService,
27 | EmulatorControllerService,
28 | } from "../src/proto/emulator_web_client";
29 |
30 | jest.mock("../src/proto/emulator_web_client");
31 |
32 | const jsepMessage = (object, uid) => {
33 | const guid = new Rtc.RtcId();
34 | guid.setGuid(uid);
35 |
36 | const jsepMsg = new Rtc.JsepMsg();
37 | jsepMsg.setId(guid);
38 | jsepMsg.setMessage(JSON.stringify(object));
39 | return jsepMsg;
40 | };
41 |
42 | const requestRtcStream = jest.fn((request, metadata, callback) => {
43 | const guid = new Rtc.RtcId();
44 | guid.setGuid("abcde");
45 |
46 | callback(null, guid);
47 | });
48 |
49 | const receiveJsepMessage = (message) => {
50 | const jsepMsg = jsepMessage(message);
51 | return jest.fn((request, metadata, callback) => {
52 | callback(null, jsepMsg);
53 | });
54 | };
55 |
56 | const RTCPeerConnectionMock = jest.fn().mockImplementation((cfg) => ({
57 | ondatachannel: jest.fn(),
58 | createAnswer: jest.fn(),
59 | addEventListener: jest.fn(),
60 | addIceCandidate: jest.fn(),
61 | dispatchEvent: jest.fn(),
62 | setRemoteDescription: jest.fn(),
63 | setLocalDescription: jest.fn(),
64 | close: jest.fn(),
65 | }));
66 |
67 | Object.defineProperty(window, "RTCPeerConnection", {
68 | writable: true,
69 | value: jest.fn().mockImplementation((cfg) => {
70 | let sdp = null;
71 | let signal = null;
72 | let state = null;
73 | return {
74 | ondatachannel: jest.fn(),
75 | createAnswer: jest.fn(() => {
76 | return new Promise((resolve) => {
77 | return { answer: "fake-answer" };
78 | });
79 | }),
80 | addEventListener: jest.fn(),
81 | addIceCandidate: jest.fn(),
82 | dispatchEvent: jest.fn(),
83 | setRemoteDescription: (desc) => {
84 | sdp = desc;
85 | state = "have-remote-offer";
86 | if (signal) signal();
87 | },
88 | setLocalDescription: jest.fn(),
89 | get currentRemoteDescription() {
90 | return sdp;
91 | },
92 | set onsignalingstatechange(fn) {
93 | signal = fn;
94 | },
95 | get signalingState() {
96 | return state;
97 | },
98 | close: jest.fn(),
99 | };
100 | }),
101 | });
102 |
103 | Object.defineProperty(window, "RTCIceCandidate", {
104 | writable: true,
105 | value: jest.fn().mockImplementation((cfg) => ({})),
106 | });
107 |
108 | Object.defineProperty(window, "RTCSessionDescription", {
109 | writable: true,
110 | value: jest.fn().mockImplementation((desc) => {
111 | return { sdp: desc };
112 | }),
113 | });
114 |
115 | const sdp = {
116 | sdp: "fakesdp1",
117 | type: "offer",
118 | };
119 |
120 | const sdp2 = {
121 | sdp: "fakesdp2",
122 | type: "offer",
123 | };
124 | const candidates_and_sdp = [
125 | {
126 | candidate:
127 | "candidate:4205781435 1 udp 2122260223 10.146.0.6 37608 typ host generation 0 ufrag Er9W network-id 1 network-cost 50",
128 | sdpMLineIndex: 1,
129 | sdpMid: "1",
130 | },
131 | {
132 | candidate:
133 | "candidate:3022839115 1 tcp 1518280447 10.146.0.6 36959 typ host tcptype passive generation 0 ufrag Er9W network-id 1 network-cost 50",
134 | sdpMLineIndex: 0,
135 | sdpMid: "0",
136 | },
137 | sdp,
138 | ];
139 |
140 | const jsepProtocol = (messages) => {
141 | RtcService.mockClear();
142 | EmulatorControllerService.mockClear();
143 |
144 | let receive = jest.fn();
145 | for (var i = 0; i < messages.length; i++) {
146 | receive.mockImplementationOnce(receiveJsepMessage(messages[i]));
147 | }
148 | RtcService.mockImplementation(() => {
149 | return {
150 | requestRtcStream: requestRtcStream,
151 | receiveJsepMessage: receive,
152 | sendJsepMessage: jest.fn((msg) => {
153 | console.log("Sending " + msg);
154 | }),
155 | };
156 | });
157 | const rtcServiceInstance = new RtcService("http://foo");
158 | const emulatorServiceInstance = new EmulatorControllerService("http://foo");
159 | return {
160 | rtc: rtcServiceInstance,
161 | jsep: new JsepProtocol(emulatorServiceInstance, rtcServiceInstance, true),
162 | };
163 | };
164 |
165 | describe("Basic jsep protocol with polling.", () => {
166 | it("calls request rtc stream", () => {
167 | const { rtc, jsep } = jsepProtocol([{ bye: "we're done" }]);
168 | jsep.startStream();
169 | expect(rtc.requestRtcStream.mock.calls.length).toBe(1);
170 | expect(rtc.receiveJsepMessage.mock.calls.length).toBe(1);
171 | });
172 |
173 | it("Notifies listeners of a disconnect", () => {
174 | const { jsep } = jsepProtocol([{ bye: "we're done" }]);
175 |
176 | const disconnect = jest.fn();
177 | jsep.on("disconnected", disconnect);
178 | jsep.startStream();
179 | expect(disconnect).toHaveBeenCalledTimes(1);
180 | });
181 |
182 | it("Does not process messages before start", () => {
183 | const { jsep } = jsepProtocol(candidates_and_sdp);
184 |
185 | jsep.startStream();
186 | // All candidates are queued.
187 | expect(jsep.old_emu_patch.candidates.length).toBe(2);
188 | expect(jsep.old_emu_patch.sdp).not.toBeNull();
189 | });
190 |
191 | it("Flush message queue after bye", () => {
192 | let msg = Array.from(candidates_and_sdp);
193 | msg.push({ bye: "we're done" });
194 | const { jsep } = jsepProtocol(msg);
195 |
196 | jsep.startStream();
197 | expect(jsep.old_emu_patch.candidates.length).toBe(0);
198 | expect(jsep.old_emu_patch.sdp).toBe(null);
199 | expect(jsep.peerConnection).toBe(null);
200 | });
201 |
202 | it("Out of order messages handled after start", () => {
203 | let msg = Array.from(candidates_and_sdp);
204 | msg.push({ start: { foo: "bar" } });
205 | const { jsep } = jsepProtocol(msg);
206 |
207 | jsep.startStream();
208 | expect(jsep.old_emu_patch.candidates.length).toBe(0);
209 | expect(jsep.peerConnection).not.toBeNull();
210 | expect(jsep.peerConnection.currentRemoteDescription).not.toBeNull();
211 | // Peer connection was initialized with rtc config
212 | expect(RTCPeerConnection.mock.calls[0][0]).toStrictEqual({ foo: "bar" });
213 | });
214 |
215 | it.skip("Never handles sdp twice / (async problem)", async () => {
216 | // 2nd jsep gets dropped.
217 | let { rtc, jsep } = jsepProtocol([sdp, sdp2, { start: { foo: "bar" } }]);
218 | jsep.startStream();
219 | expect(jsep.old_emu_patch.candidates.length).toBe(0);
220 | expect(jsep.old_emu_patch.sdp).toBeNull();
221 | expect(jsep.old_emu_patch.answer).toBe(true);
222 | expect(jsep.peerConnection).not.toBeNull();
223 | expect(jsep.peerConnection.currentRemoteDescription.sdp.sdp).toBe(sdp.sdp);
224 | // Peer connection was initialized with rtc config
225 | expect(RTCPeerConnection.mock.calls[0][0]).toStrictEqual({ foo: "bar" });
226 | expect(rtc.sendJsepMessage.mock.calls[0]).toBe(1);
227 | });
228 | });
229 |
--------------------------------------------------------------------------------
/test/simple_png_view.test.js:
--------------------------------------------------------------------------------
1 | /**
2 | * @jest-environment jsdom
3 | */
4 | /*
5 | * Copyright 2021 The Android Open Source Project
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License")
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | import { jest } from "@jest/globals";
20 | import "regenerator-runtime/runtime"; // Somehow needed for jest to be happy with ES6
21 |
22 | import * as Proto from "../src/proto/emulator_controller_pb";
23 | import React from "react";
24 | import EmulatorPngView from "../src/components/emulator/views/simple_png_view";
25 | import { resize } from "./fake_events";
26 | import {
27 | act,
28 | fireEvent,
29 | getByTestId,
30 | render,
31 | screen,
32 | waitFor,
33 | } from "@testing-library/react";
34 | import { EmulatorControllerService } from "../src/proto/emulator_web_client";
35 | import { EventEmitter } from "events";
36 | import { flushSync } from "react-dom";
37 |
38 | jest.mock("../src/proto/emulator_web_client");
39 |
40 | const googleLogo =
41 | "iVBORw0KGgoAAAANSUhEUgAAAJwAAACcCAYAAACKuMJNAAAQG0lEQVR42uydfWydVR3HD/Tert2YvOiGwQHTAAMyB905py/Lwu3znNsyZEiGqxGViUowwOJLMiIakym9ZQP+IAgxAUEmCGunZLjJ5npbmxBCpm4zghhfcEOg63Oe51K63tv1bfb4/MpissfLVtZ7n+e55/y+yTdpuv7R5Xx6zvm9nN8lKEJUW1uVZ7ElUvBVrs1vc222UQr2U0fwXb73+/6btPi//X9zXcHzjkWPORb/j2vxMUfQYdeiOf/7b/rfO+DatMexaZcU/CFX0O/INLvJFayufzWdS1Dm6a3GxlpP0GYfqm/7kPzMEWyfD8ZR/2tVTvvQTvlgHpQW2+nDnPFsdsPhVVcvICi99I9Vl8xx0ywlBf+hD9dL0uLjAEBsbPF/+QA+4drsC/0p+jGCqjwNpunZ0mK3+JD9upS7Vxi7oLT4H2Sa3+OmGy4lqPjKW7FivhT16+DICuxilWuLvyoFu1um+McJKh6SVv1V0uaPwWXet9LR00GKzX4D9z61kZxJUGFHlVdWw5HpWvQVWBDD/AZEv7lV9R8hqPIHANKm6x1B3zYNtKAdwQYhEBpaufJcgip9KsO12bcci/ebDlrQkAeUNrsX7rAENTvBfWU6ESvogOlgndIWk1KwO1UqlSCo0wkG2ApIyhoP0oe0FPR1yDsS1MzkpesucAV/BnJSpsMzK9vs51jJOIWkzW91LD5kPCylss1dadO1BHWiILEpBd9hPCBlO2b5VoxmjwtqiNKi75oORbkN3S6O4E1G59SgBch0EEJOoUxCqUwRcoZpxfWLHJv+0XQAIvQvjenPcwRLu4J7Li56tLbpn+APn+gsKEtBp6zxix0bU8ezGqiuwcEmXOBYOi+t+laii6DU4lpsi4sLG1vDmw1FaVKLojs8RDF9QeNsaHGHu5webwkE7zZ+QRG2cBokHcFfNH5BEbaQ7myCbTd+QRG28gsy2NLincYvaJxt0YPa5N9cwTYbv6AIW2iwfc34BUXYwpFn11uO4BPGLyrCFkYvW90l8ILI+EVF2EJLf+w3flERtnDk2OxhTRfrn9AlK23W7tr1X3fSXOTSDVfA4r0j+EehggJ/bAMty+bBuwGYAwLNjf7PtkG/mWPzJ13B97qCjiJsJZIj6I06vXSCCBvmww21Np5XypwkdGNIi21wBdsDM0/Cgu2wVXcx0UXvtrILK78tnP0ZRiY4gn2KhCQYz+AJ/kWoL8O8EIRthvL/U7srNMM+AoNvPME4iVi5VP0iOLKhGRVhO4k8wb9UieMQoB8vju80odX7/Wmc1EHYAoILM7x1rKCXSuP+7/tAJTyRgwAEZgvDAEWE7bhcwZ+uFNiOH/uXkQrTQKph8YfrtKGHtIQNBjFXAmjTwUyaf16PKQT0iJGwQRdIZTzrY3tgNgkB6VLFsfgBE2ALRqU3xxk0eAkGg5h1fOB7KJWqkYI/ZwxskFGHS2mMYRvyf7/PEM0FAYX2sIG81uUbYry7vem10suJIZKCf0Vr2FQfOevos/Pfy7VdFcfJ3n+HigdB6aOJbOLuyZ6kmnixRr13x5JYfX6BYzecT1D6SG0j1eM9ycMA3LSzSZXPLFJumkXeyTHQ0rSQoPSSD9utAFrQo0+cp3I3XB3ZLIzpYjtKP032VB0AwIp5fPtcNbjuytAL7/AxkASl5d2tCcA6qfdUqyPf/WRowEHTAEHpqcls1VMA1Uw88uOFyruWlhu4RwhKT029TOb797cCwDRTj3XOV7m2ZeWa7LN/erIPyoRgYeae2FWjhu64rOTtRQMtbClBaQ1cFgA6Xecznyhl6uQHBKWvpnaRBT5wxwCc2Xj0qXNV7rOzTJ3Y/DX83Cj9d7fbAJhSePyF2lmlTqbHgKK0B24nwFIyd/upk+8tPq1uXYLSW6qP1PjAjQAopfbIIwuUdx2daVQ6hYGCAZrsrWoFOMrl8a4Zpk5s/gJBmZDsTW4uPWjB1MkcNbT+0lPtcE0EZQBw3cnfAxRhuLDpAuW2sCKwsZcISn+pHWTueHdyEmAIy6NbzoHUCdZLTdREdyIVDmjB1InfdfLVK94HrmX5UZhKRFDmdPZGYkidfP9ilbt5aS9BGbPDbYXFj9Kjv5pnkQqS3VFQ6JO7OZP/5gcEDFWvRwmbn/+TaiM5E4HTzJn84yQotY8k/YBhIkrgJnoSW4gvBE4ztxdeJkGN/bb68qiP02M9yS8jcFraK3acXh81cFN9tYsQOD2d3jx49okBQ09ifZSw+QHLO8QXAqenxaZCXbDCcH+kwGUTOxA4nSPVkRuDO9wvIj1Ss8kOBE5n5+8q2lIelY/1Jm9B4PS11ZHPFHnwHOmRuhKBMygXN5GtOhRphaFvzmIETmfgCs+feKR2J70ogYNOFQROXze3F34XBO5ohCWtMeILgdPYmeF9QeAmIwMumxxE4HQPGgp/DQI3FWXRHoHTPko9iMAhcGEeqW/jkYrAhQncAAYNCFyIpa3hfkyLIHAh3uGG3wpWGg5i4heBK6PfCPbD7cfSFgJXxq7f14JHanekxftsch0Cp3UtdW+c2pPA9yFwWkepe2LVgAkjwhA4rb0tXi3mPYl+BE7rbpGfxO4RjcrWXITA6Wkrk//Ric8Ee6uXwKJj4IDAlccj3whOvkxE/hA6m3gagdPT4r7C6mJvU/8SceDg4qgHPZ3qyC8tNj3puaiP1Vd3L0yTClJz+/BYJThq4FoenJr3/8D1JjZEt7tVq4d3LFN0602PE1RJRR9TyWihGz5MQEWAuyYK2GT3Wer251OKdX5OLe9cc6Sxay0OJCyhxL35T0fc7dtHimnqFVIbduBwYPf56rptqwG2/5l3rsWRqyWUlRm5PdqUSOHRk00x3xsWbM/uXKIau9YAZAGvwaHSJZTVXngmNimRoHwQNpUbtCPZWnXP9iaA6wPNu9bi2PxSSKkzmjsKMtoqw3/buxLYqIowPIJYBIkSAghqiIqJSqx2jy4upQeHgIG2+3Y3oNxEMEQTokQ0RChJtSBCyqVVECmUvt2W20ppOZRIMEI0BA/AyI0HQa7sUiRod/y/BIl5lO71dnfevvmSL02bttnd/3sz85/TlN3K2Py2AxMptqONnbln3RCIKgwVeTGIDsgva3KktiwpcH3o4lBGazcJZmgu5tWNDfW9eG5NEQQVnqo7RFurvPooTtDqVp7i0vL9kUwz36JrNe+ODP7+liwIKTr63A1MImbklfA70bySSsFB8OFvE9zVbqJeYvudQh6T1hdAQDHR4leGMImYQPEvjwA51GIWDqEdrIsebYP7tvXgz2lCHtELzvVj3pfygt7YBBfcm9rVLdCcM/dy50jvTG2IR2yVdU/wbL8C0cRPnzKbSUQZCgnkwujCnt+0wIDAWIR2eUcHPn1TPwhFR7quZ6nuTCYRMTCqXoA+hlIWKUKNrCNtq4FoxHaksQsvXjcUItGfqvu7J2u9dzGJsMC5SWN8oeJvrWQd2q6IVGx1Wx/mOf5iiCOR/JBJtIq+C0N3k7NwUoROewSdo73szR5OaFd3ZvCyzVaIITn0KWOZxG1BectFgvQwLGGxgES1/3Zi+3V7Jz5+wwAIIWm0qK6rdtVtYxItBHmDAymyHxJAcPBQ+7FYQH0Go1sS2976nnyQJuSRNKquc45q5TEmcRP937nSg8aanhVjvGrwWFTbqbbXgW6JOXOz74C4vK5PmJBHUla645YqVw8mgYxCe3S2CzR8cA6LA+hZnQaxXdjekU/bmAODC0Gr6vopSy3syUyMkhLeBk3GoogNwd6C0qu9WDxAYeaBhq7nRtQOg6FF49GstUovs5Ye4f4DsfpPA9uYHqBeg9dhYBFp8bvPkCPRx3wrW3ClcA3PZVeeZ3qgd/3QDKvffUJY0anugNXnLjJLrA0XbQg4kuuwxlmIDxaf8qJoQtPW0Fl87lmMsztYmoKi9w/AQRB0fshLTE/AkBR4/RbGFZy7bL6ih9Ku+6o0OAjl4qKOVPWWcP1Tj7Rt5RtAcIjVXbbVuMewNADaJp3LfMsQ1DXE9ZT6i85VBaMagVa/a6eRHQqqeh4GT5zIncuX0KH8koBiC5zUrG76wr6xuAui/QYRHRyKv6kpZ/EzNZ6uzCCw1HieIrFt1b4Xx+rpvOC9E6Kd3SawRAPNygYQm9apaKIjQbnIwWJLrddKjs96OEC3baGsHsdzy78WZXU7gBBNcj4cv1KPD8CAwrtm87kqrbXeHCFSU6vGt8cDTA7Znsjfh4f3q6hCGimlgkNlMUsW4AlSTvMCPgAD8zCtem9aq5XHky0yWsmGU2xzFeapxPr6+346hxfMO5uquFs1SzboLFdoYLFpPdtfsOUSi+y13vuZjshcM6YjCaw/ZWzeopL5Rmzver3u7LUv8/wFPyRbbBfJM+3OUgF6Sheliei0KbPTVr+ygd7ffAp6T4XHiN4Ki6/4UQjSubmwk/Wz4R1oa74X32fVeHqTg+IgB2UEfp/+x1z6m020CxwhkTUn8rXafaN4ztK6ZA6omcRSBfQa/BcQlkwtnSvK+YCyi4l2FFLfoI6nHuc5sxtcBGaveY3nzz+eqLvrz6PYk4kAZCHQ0md2g4tAe/VYnrtoTwKqQZoKmUiw+5QJZje2OPRwZ0UltkC9JlkuZCICh2VpbHHoWDWLQid/xOuV7sZwHCYiUFViURWf2Q0t1hY7mectOBjjuS1wanBJsBsTGRhAQ+GEjWY3tEi0+0ZS6GRLtIILUrwt0yDj2qe0o5hUndkNLRqfXbGAF8y9ENH0ShLbYGYkoDQdgwXNbmTxQifTKHRyrNXuKxLbKGZEIG8oVzrxaFdHU+hkd0srW4g80snMyPDWetvSm/zE7EYWkc6PViJ08j+xBaaydAFtr6VmN7CYoZOZPG/e6WZNjjQ9gMQ2pcH+MbuRRSJlia45KmdMZGkKFG8WoEzd7IYWg8qftPM4Wboju8r1IOVev5EGTykPmWESlba0qcImDZ90otYPNX3MjLD6PV4s7WYXQXLouk5im4EUJDMzHOqI7rhjSwoioVXMP8sJoreuduPoEHvJ7OLQe+4K9VN8gN4KJnErMN3Spiqr8UGZXixxEj0VaIlkEuGBphRyKvaZXTQxdqEFaaeYiXw2k4iuvu5GJfFvphdRRHQ1Y3eQM5D1qDxR3a9g4qUUVctEDaK8ZzYBsTukx2jLOGV2gYE30oTrqIPfwiQSW+BprXG/QE/0V2Y9o9HXpbZq5REmkVxgG6Fg5jLNnI60JJrOiVNopb+HSaQWMAImESGATOL7K42EdpQeqHcxN45JiAnkCTEIGwdpDI8xWqAW4SDibHk2M6ijgeAnppyTMb/A6ieawHDdOrIBJLKRmWtc3ZhE+gB9FhAgGfhV4nKUSRGvJOtGRBLV9+jdpa9v0PcDnt5UdB+TMN3FVW0wcsuqugbTmISJWA1pO/6YRPL5jelQh3A5CsUBz8I5wexgBFmxWuJiEvr98wjV0M8O0la+G6O6UHpFZ6+38f/oXDkI1zSZvkqD8C+ip2n51/dBhAAAAABJRU5ErkJggg==";
42 | const googleImage = new Proto.Image();
43 | googleImage.setImage(Uint8Array.from(atob(googleLogo), (c) => c.charCodeAt(0)));
44 |
45 | const stream = new EventEmitter();
46 | describe("A simple png view", () => {
47 | EmulatorControllerService.mockImplementation(() => {
48 | let count = 0;
49 | return {
50 | getScreenshot: jest.fn((a, b, response) => {
51 | count++;
52 | if (count < 2) response(null, googleImage);
53 | }),
54 | streamScreenshot: jest.fn((request) => {
55 | stream.removeAllListeners();
56 | return {
57 | on: (name, fn) => {
58 | stream.on(name, fn);
59 | },
60 | cancel: jest.fn(),
61 | };
62 | }),
63 | getStatus: jest.fn(() => {}),
64 | };
65 | });
66 |
67 | beforeEach(() => {
68 | stream.removeAllListeners();
69 | });
70 |
71 | it("stream actually starts", () => {
72 | const emulatorServiceInstance = new EmulatorControllerService("http://foo");
73 | const { container } = render(
74 |
75 |
76 |
77 | );
78 |
79 | expect(emulatorServiceInstance.streamScreenshot).toHaveBeenCalledTimes(1);
80 | });
81 |
82 | it("Get screenshot renders an image.", () => {
83 | const emulatorServiceInstance = new EmulatorControllerService("http://foo");
84 | const { container } = render(
85 |
86 | );
87 | const pngView = container.querySelector("img");
88 | expect(pngView.src).toBe("data:image/jpeg;base64," + googleLogo);
89 | });
90 |
91 | it("Has a connected state after the first image arrives", () => {
92 | const emulatorServiceInstance = new EmulatorControllerService("http://foo");
93 | const changeState = jest.fn();
94 | render(
95 |
99 | );
100 | expect(changeState).toHaveBeenCalledWith("connecting");
101 | act(() => stream.emit("data", googleImage));
102 | expect(changeState).toHaveBeenCalledWith("connected");
103 | });
104 |
105 | it("Attempts to reconnect if the server disconnects", async () => {
106 | const emulatorServiceInstance = new EmulatorControllerService("http://foo");
107 | const changeState = jest.fn();
108 | render(
109 |
113 | );
114 | expect(emulatorServiceInstance.streamScreenshot).toHaveBeenCalledTimes(1);
115 | stream.emit("data", googleImage);
116 | waitFor(() => expect(changeState).toHaveBeenCalledWith("connected"));
117 | stream.emit("error", "fda");
118 | expect(emulatorServiceInstance.streamScreenshot).toHaveBeenCalledTimes(2);
119 | });
120 |
121 | it("Gives up on the second failure.", () => {
122 | const emulatorServiceInstance = new EmulatorControllerService("http://foo");
123 | const changeState = jest.fn();
124 | render(
125 |
129 | );
130 | expect(emulatorServiceInstance.streamScreenshot).toHaveBeenCalledTimes(1);
131 |
132 | // Connect
133 | stream.emit("data", googleImage);
134 | waitFor(() => expect(changeState).toHaveBeenCalledWith("connected"));
135 |
136 | // First break, we attempt a reconnect.
137 | stream.emit("error", "fda");
138 | expect(emulatorServiceInstance.streamScreenshot).toHaveBeenCalledTimes(2);
139 | expect(changeState).not.toHaveBeenCalledWith("disconnected");
140 |
141 | // We could not reconnect, so we fail.
142 | stream.emit("error", "fda");
143 | waitFor(() => expect(changeState).toHaveBeenCalledWith("disconnected"));
144 | });
145 | });
146 |
--------------------------------------------------------------------------------
/test/touch_event_handler.test.js:
--------------------------------------------------------------------------------
1 | /**
2 | * @jest-environment jsdom
3 | */
4 | /*
5 | * Copyright 2021 The Android Open Source Project
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License")
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | import "@testing-library/jest-dom";
20 |
21 | import React from "react";
22 | import { render, fireEvent, screen } from "@testing-library/react";
23 | import withMouseKeyHandler from "../src/components/emulator/views/event_handler";
24 | import * as Proto from "../src/proto/emulator_controller_pb";
25 | import * as Rtc from "../src/proto/rtc_service_pb";
26 |
27 | import JsepProtocol from "../src/components/emulator/net/jsep_protocol_driver";
28 | import {
29 | RtcService,
30 | EmulatorControllerService,
31 | } from "../src/proto/emulator_web_client";
32 |
33 | jest.mock("../src/proto/emulator_web_client");
34 | jest.mock("../src/components/emulator/net/jsep_protocol_driver");
35 |
36 | class FakeEmulator extends React.Component {
37 | render() {
38 | return (
39 |
43 | );
44 | }
45 | }
46 |
47 |
48 |
49 | const fakeTouchEvent = (tp, x, y, force, props = {}) => {
50 | const event = new TouchEvent(tp, {
51 | bubbles: true,
52 | cancelable: true,
53 | ...props,
54 | });
55 |
56 | Object.defineProperty(event, "changedTouches", {
57 | get: () => [
58 | { clientX: x, clientY: y, radiusX: 4, radiusY: 4, force: force },
59 | ],
60 | });
61 | return event;
62 | };
63 |
64 | const TestView = withMouseKeyHandler(FakeEmulator);
65 | describe("The event handler", () => {
66 | const rtcServiceInstance = new RtcService("http://foo");
67 | const emulatorServiceInstance = new EmulatorControllerService("http://foo");
68 | let jsep, fakeScreen;
69 |
70 | beforeEach(() => {
71 | jsep = new JsepProtocol(emulatorServiceInstance, rtcServiceInstance, true);
72 |
73 | render();
74 | fakeScreen = screen.getByTestId("fake").parentElement;
75 | Object.defineProperty(fakeScreen, "clientWidth", { get: () => 200 });
76 | Object.defineProperty(fakeScreen, "clientHeight", { get: () => 200 });
77 |
78 | expect(fakeScreen).toBeInTheDocument();
79 | });
80 |
81 | test("Normalizes touch pressure of 1.0 to EV_MAX", () => {
82 | fireEvent(fakeScreen, fakeTouchEvent("touchstart", 10, 10, 1.0));
83 |
84 | // EV_MAX = 0x7fff
85 | expect(jsep.send.mock.calls[0][1]["array"].flat(3)[3]).toBe(0x7fff);
86 | });
87 |
88 |
89 | test("Normalizes touch pressure >1.0 to EV_MAX", () => {
90 | fireEvent(fakeScreen, fakeTouchEvent("touchstart", 10, 10, 10.0));
91 |
92 | // EV_MAX = 0x7fff
93 | expect(jsep.send.mock.calls[0][1]["array"].flat(3)[3]).toBe(0x7fff);
94 | });
95 |
96 | test("A touch start event has a minimum value >0.01", () => {
97 | fireEvent(fakeScreen, fakeTouchEvent("touchstart", 10, 10, 0.0));
98 |
99 | // Some browsers do no set the force property, which could be mistaken for
100 | // lift event in the emulator. We now make sure we always have a minimum
101 | // value.
102 | expect(jsep.send.mock.calls[0][1]["array"].flat(3)[3]).toBeGreaterThanOrEqual(327);
103 | });
104 |
105 | test("Normalizes touch end event to a pressure of 0.0 to EV_MIN", () => {
106 | fireEvent(fakeScreen, fakeTouchEvent("touchend", 10, 10, 0.0));
107 |
108 | // So the result we test against is a protobuf message. Protobuf
109 | // is optimized to not ship the value 0 and will set it to "null".
110 | expect(jsep.send.mock.calls[0][1]["array"].flat(3)[3]).toBe(null);
111 | });
112 |
113 | test("Normalizes touch pressure of 0.5 to an integer of of +/- EV_MAX", () => {
114 | fireEvent(fakeScreen, fakeTouchEvent("touchstart", 10, 10, 0.5));
115 | expect(jsep.send.mock.calls[0][1]["array"].flat(3)[3]).toBeGreaterThan(16380);
116 | expect(jsep.send.mock.calls[0][1]["array"].flat(3)[3]).toBeLessThan(16387);
117 | });
118 | });
119 |
--------------------------------------------------------------------------------