1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.display;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.IndentingPrintWriter;
import android.content.Context;
import android.media.RemoteDisplay;
import android.os.Handler;
import android.os.IBinder;
import android.util.Slog;
import android.view.Surface;
import java.io.PrintWriter;
/**
* Connects to Wifi displays that implement the Miracast protocol.
* <p>
* The Wifi display protocol relies on Wifi direct for discovering and pairing
* with the display. Once connected, the Media Server opens an RTSP socket and accepts
* a connection from the display. After session negotiation, the Media Server
* streams encoded buffers to the display.
* </p><p>
* This class is responsible for connecting to Wifi displays and mediating
* the interactions between Media Server, Surface Flinger and the Display Manager Service.
* </p><p>
* Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock.
* </p>
*/
final class WifiDisplayAdapter extends DisplayAdapter {
private static final String TAG = "WifiDisplayAdapter";
private WifiDisplayHandle mDisplayHandle;
private WifiDisplayController mDisplayController;
public WifiDisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
Context context, Handler handler, Listener listener) {
super(syncRoot, context, handler, listener, TAG);
}
@Override
public void dumpLocked(PrintWriter pw) {
super.dumpLocked(pw);
if (mDisplayHandle == null) {
pw.println("mDisplayHandle=null");
} else {
pw.println("mDisplayHandle:");
mDisplayHandle.dumpLocked(pw);
}
// Try to dump the controller state.
if (mDisplayController == null) {
pw.println("mDisplayController=null");
} else {
pw.println("mDisplayController:");
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
ipw.increaseIndent();
DumpUtils.dumpAsync(getHandler(), mDisplayController, ipw, 200);
}
}
@Override
public void registerLocked() {
super.registerLocked();
getHandler().post(new Runnable() {
@Override
public void run() {
mDisplayController = new WifiDisplayController(
getContext(), getHandler(), mWifiDisplayListener);
}
});
}
private void connectLocked(String deviceName, String iface) {
disconnectLocked();
String name = getContext().getResources().getString(
com.android.internal.R.string.display_manager_wifi_display_name,
deviceName);
mDisplayHandle = new WifiDisplayHandle(name, iface);
}
private void disconnectLocked() {
if (mDisplayHandle != null) {
mDisplayHandle.disposeLocked();
mDisplayHandle = null;
}
}
private final WifiDisplayController.Listener mWifiDisplayListener =
new WifiDisplayController.Listener() {
@Override
public void onDisplayConnected(String deviceName, String iface) {
synchronized (getSyncRoot()) {
connectLocked(deviceName, iface);
}
}
@Override
public void onDisplayDisconnected() {
// Stop listening.
synchronized (getSyncRoot()) {
disconnectLocked();
}
}
};
private final class WifiDisplayDevice extends DisplayDevice {
private final String mName;
private final int mWidth;
private final int mHeight;
private final float mRefreshRate;
private final int mFlags;
private Surface mSurface;
private DisplayDeviceInfo mInfo;
public WifiDisplayDevice(IBinder displayToken, String name,
int width, int height, float refreshRate, int flags,
Surface surface) {
super(WifiDisplayAdapter.this, displayToken);
mName = name;
mWidth = width;
mHeight = height;
mRefreshRate = refreshRate;
mFlags = flags;
mSurface = surface;
}
public void clearSurfaceLocked() {
mSurface = null;
sendTraversalRequestLocked();
}
@Override
public void performTraversalInTransactionLocked() {
setSurfaceInTransactionLocked(mSurface);
}
@Override
public DisplayDeviceInfo getDisplayDeviceInfoLocked() {
if (mInfo == null) {
mInfo = new DisplayDeviceInfo();
mInfo.name = mName;
mInfo.width = mWidth;
mInfo.height = mHeight;
mInfo.refreshRate = mRefreshRate;
mInfo.flags = mFlags;
mInfo.setAssumedDensityForExternalDisplay(mWidth, mHeight);
}
return mInfo;
}
}
private final class WifiDisplayHandle implements RemoteDisplay.Listener {
private final String mName;
private final String mIface;
private final RemoteDisplay mRemoteDisplay;
private WifiDisplayDevice mDevice;
private int mLastError;
public WifiDisplayHandle(String name, String iface) {
mName = name;
mIface = iface;
mRemoteDisplay = RemoteDisplay.listen(iface, this, getHandler());
Slog.i(TAG, "Listening for Wifi display connections on " + iface
+ " from " + mName);
}
public void disposeLocked() {
Slog.i(TAG, "Stopped listening for Wifi display connections on " + mIface
+ " from " + mName);
removeDisplayLocked();
mRemoteDisplay.dispose();
}
public void dumpLocked(PrintWriter pw) {
pw.println(" " + mName + ": " + (mDevice != null ? "connected" : "disconnected"));
pw.println(" mIface=" + mIface);
pw.println(" mLastError=" + mLastError);
}
// Called on the handler thread.
@Override
public void onDisplayConnected(Surface surface, int width, int height, int flags) {
synchronized (getSyncRoot()) {
mLastError = 0;
removeDisplayLocked();
addDisplayLocked(surface, width, height, flags);
Slog.i(TAG, "Wifi display connected: " + mName);
}
}
// Called on the handler thread.
@Override
public void onDisplayDisconnected() {
synchronized (getSyncRoot()) {
mLastError = 0;
removeDisplayLocked();
Slog.i(TAG, "Wifi display disconnected: " + mName);
}
}
// Called on the handler thread.
@Override
public void onDisplayError(int error) {
synchronized (getSyncRoot()) {
mLastError = error;
removeDisplayLocked();
Slog.i(TAG, "Wifi display disconnected due to error " + error + ": " + mName);
}
}
private void addDisplayLocked(Surface surface, int width, int height, int flags) {
int deviceFlags = 0;
if ((flags & RemoteDisplay.DISPLAY_FLAG_SECURE) != 0) {
deviceFlags |= DisplayDeviceInfo.FLAG_SECURE;
}
float refreshRate = 60.0f; // TODO: get this for real
IBinder displayToken = Surface.createDisplay(mName);
mDevice = new WifiDisplayDevice(displayToken, mName, width, height,
refreshRate, deviceFlags, surface);
sendDisplayDeviceEventLocked(mDevice, DISPLAY_DEVICE_EVENT_ADDED);
}
private void removeDisplayLocked() {
if (mDevice != null) {
mDevice.clearSurfaceLocked();
sendDisplayDeviceEventLocked(mDevice, DISPLAY_DEVICE_EVENT_REMOVED);
mDevice = null;
}
}
}
}
|