1 /*
2  * Copyright (C) 2023 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 
17 package com.example.android.vdmdemo.client;
18 
19 import android.view.Surface;
20 
21 import com.example.android.vdmdemo.common.RemoteEventProto.DisplayCapabilities;
22 import com.example.android.vdmdemo.common.RemoteEventProto.RemoteEvent;
23 import com.example.android.vdmdemo.common.RemoteEventProto.StopStreaming;
24 import com.example.android.vdmdemo.common.RemoteIo;
25 import com.example.android.vdmdemo.common.VideoManager;
26 
27 final class DisplayController {
28     private static final int DPI = 300;
29 
30     private final int mDisplayId;
31     private final RemoteIo mRemoteIo;
32 
33     private VideoManager mVideoManager = null;
34 
35     private int mDpi = DPI;
36     private RemoteEvent mDisplayCapabilities;
37 
DisplayController(int displayId, RemoteIo remoteIo)38     DisplayController(int displayId, RemoteIo remoteIo) {
39         mDisplayId = displayId;
40         mRemoteIo = remoteIo;
41     }
42 
setDpi(int dpi)43     void setDpi(int dpi) {
44         mDpi = dpi;
45     }
46 
close()47     void close() {
48         mRemoteIo.sendMessage(
49                 RemoteEvent.newBuilder()
50                         .setDisplayId(mDisplayId)
51                         .setStopStreaming(StopStreaming.newBuilder())
52                         .build());
53 
54         if (mVideoManager != null) {
55             mVideoManager.stop();
56         }
57     }
58 
pause()59     void pause() {
60         if (mVideoManager == null) {
61             return;
62         }
63         mVideoManager.stop();
64         mVideoManager = null;
65 
66         mRemoteIo.sendMessage(
67                 RemoteEvent.newBuilder()
68                         .setDisplayId(mDisplayId)
69                         .setStopStreaming(StopStreaming.newBuilder().setPause(true))
70                         .build());
71     }
72 
sendDisplayCapabilities()73     void sendDisplayCapabilities() {
74         mRemoteIo.sendMessage(mDisplayCapabilities);
75     }
76 
setSurface(Surface surface, int width, int height)77     void setSurface(Surface surface, int width, int height) {
78         if (mVideoManager != null) {
79             mVideoManager.stop();
80         }
81         mVideoManager = VideoManager.createDisplayDecoder(mDisplayId, mRemoteIo);
82         mVideoManager.startDecoding(surface, width, height);
83         mDisplayCapabilities =
84                 RemoteEvent.newBuilder()
85                         .setDisplayId(mDisplayId)
86                         .setDisplayCapabilities(
87                                 DisplayCapabilities.newBuilder()
88                                         .setViewportWidth(width)
89                                         .setViewportHeight(height)
90                                         .setDensityDpi(mDpi))
91                         .build();
92         sendDisplayCapabilities();
93     }
94 }
95