File: overlay_window_android.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (468 lines) | stat: -rw-r--r-- 16,148 bytes parent folder | download | duplicates (6)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/android/overlay/overlay_window_android.h"

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/unguessable_token_android.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "cc/slim/surface_layer.h"
#include "chrome/android/chrome_jni_headers/PictureInPictureActivity_jni.h"
#include "chrome/browser/android/tab_android.h"
#include "components/thin_webview/compositor_view.h"
#include "content/public/browser/overlay_window.h"
#include "content/public/browser/video_picture_in_picture_window_controller.h"
#include "content/public/browser/web_contents.h"
#include "ui/android/window_android_compositor.h"

using WindowMap = base::flat_map<base::UnguessableToken, OverlayWindowAndroid*>;

namespace {
WindowMap& GetWindowMap() {
  static base::NoDestructor<WindowMap> instance;
  return *instance;
}

}  // namespace

// static
std::unique_ptr<content::VideoOverlayWindow>
content::VideoOverlayWindow::Create(
    VideoPictureInPictureWindowController* controller) {
  return std::make_unique<OverlayWindowAndroid>(controller);
}

OverlayWindowAndroid::OverlayWindowAndroid(
    content::VideoPictureInPictureWindowController* controller)
    : window_android_(nullptr),
      compositor_view_(nullptr),
      surface_layer_(cc::slim::SurfaceLayer::Create()),
      bounds_(gfx::Rect(0, 0)),
      update_action_timer_(std::make_unique<base::OneShotTimer>()),
      controller_(controller) {
  GetWindowMap().emplace(token_, this);
  surface_layer_->SetIsDrawable(true);
  surface_layer_->SetStretchContentToFillBounds(true);
  surface_layer_->SetBackgroundColor(SkColors::kBlack);

  auto* web_contents = controller_->GetWebContents();

  // Compute the screen position of the video, and see if it fits inside the
  // WebContents or if it's clipped / off-screen.  If it's onscreen, then T and
  // later, Android can do a nicer animated transition to PiP with a screen
  // capture of the video.  However, if the video is clipped / offscreen, then
  // it'll look nicer to use the default light grey transition.

  // We provide a small buffer for what "clipped" means, rather than enforcing
  // it strictly.  It'll still look fine while allowing small positioning errors
  // that sites sometimes make.  See https://crbug.com/1411517 for an example.

  // The java side will ignore any source bounds that are not on the screen for
  // the source rect hint. It will use the aspect ratio only in that case.  We
  // set the x position to be <0 to ensure this, to skip the transition.

  // Get the size of the video, and inset it to provide some slack.
  gfx::Rect source_bounds = controller_->GetSourceBounds();
  gfx::Rect smaller_source_bounds = source_bounds;
  constexpr int inset_size = 4;  // pixels on each side
  smaller_source_bounds.Inset(inset_size);

  // Get the size of the WebContents, and convert to pixels.
  gfx::Rect unscaled_content_bounds = web_contents->GetContainerBounds();
  auto* native_view = web_contents->GetNativeView();
  const float dip_scale = native_view->GetDipScale();
  gfx::Rect content_bounds(unscaled_content_bounds.x() * dip_scale,
                           unscaled_content_bounds.y() * dip_scale,
                           unscaled_content_bounds.width() * dip_scale,
                           unscaled_content_bounds.height() * dip_scale);
  const bool out_of_bounds = !content_bounds.Contains(smaller_source_bounds);

  if (!out_of_bounds) {
    // Use the newer transition, if available.
    // Convert to screen space.  Since the comparison was with the inset source
    // bounds, clamp the real source bounds to the container.
    source_bounds.Intersect(content_bounds);
    gfx::PointF offset = native_view->GetLocationOnScreen(0, 0);
    source_bounds.Offset(
        static_cast<int>(offset.x()),
        static_cast<int>(offset.y()) +
            native_view->content_offset() * native_view->GetDipScale());
  } else {
    // Use the old transition.
    // Slide this offscreen, while keeping the aspect ratio the same.
    source_bounds.set_x(-1);
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  auto j_token = base::android::UnguessableTokenAndroid::Create(env, token_);
  Java_PictureInPictureActivity_createActivity(
      env, j_token, TabAndroid::FromWebContents(web_contents)->GetJavaObject(),
      source_bounds.x(), source_bounds.y(), source_bounds.width(),
      source_bounds.height());
}

OverlayWindowAndroid::~OverlayWindowAndroid() {
  // Any future use of our token will fail.
  GetWindowMap().erase(token_);
  if (java_ref_.is_uninitialized()) {
    return;
  }
  // Notify the java side that the native side is gone.
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_PictureInPictureActivity_onWindowDestroyed(env, java_ref_.get(env));
}

static jlong JNI_PictureInPictureActivity_OnActivityStart(
    JNIEnv* env,
    const jni_zero::JavaParamRef<jobject>& j_token,
    const jni_zero::JavaParamRef<jobject>& self,
    const jni_zero::JavaParamRef<jobject>& window) {
  auto token = base::android::UnguessableTokenAndroid::FromJavaUnguessableToken(
      env, j_token);
  auto iter = GetWindowMap().find(token);
  if (iter == GetWindowMap().end()) {
    return 0;
  }
  OverlayWindowAndroid* thiz = iter->second;
  thiz->Initialize(env, self, window);

  return reinterpret_cast<jlong>(thiz);
}

void OverlayWindowAndroid::Initialize(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& obj,
    const base::android::JavaParamRef<jobject>& jwindow_android) {
  java_ref_ = JavaObjectWeakGlobalRef(env, obj);
  window_android_ = ui::WindowAndroid::FromJavaWindowAndroid(jwindow_android);
  window_android_->AddObserver(this);

  Java_PictureInPictureActivity_setPlaybackState(env, java_ref_.get(env),
                                                 playback_state_);
  Java_PictureInPictureActivity_setMicrophoneMuted(env, java_ref_.get(env),
                                                   microphone_muted_);
  Java_PictureInPictureActivity_setCameraState(env, java_ref_.get(env),
                                               camera_on_);

  if (!update_action_timer_->IsRunning()) {
    MaybeNotifyVisibleActionsChanged();
  }

  if (video_size_.IsEmpty()) {
    return;
  }

  Java_PictureInPictureActivity_updateVideoSize(
      env, java_ref_.get(env), video_size_.width(), video_size_.height());
}

void OverlayWindowAndroid::OnAttachCompositor() {
  window_android_->GetCompositor()->AddChildFrameSink(
      surface_layer_->surface_id().frame_sink_id());
}

void OverlayWindowAndroid::OnDetachCompositor() {
  window_android_->GetCompositor()->RemoveChildFrameSink(
      surface_layer_->surface_id().frame_sink_id());
}

void OverlayWindowAndroid::OnActivityStopped() {
  if (java_ref_.is_uninitialized()) {
    return;
  }

  // If the activity stops, pretend that somebody pressed the close button.
  // This will notify the java side to forget about us, and clean up.
  Close();
  // `this` may be destroyed.
}

void OverlayWindowAndroid::DestroyStartedByJava(JNIEnv* env) {
  // Note that the java side also clears its native ptr when calling us, so it's
  // okay that we don't notify it in the dtor.
  // ** IMPORTANT ** Do not add calls here unless the above statement continues
  // to be true.  It's unlikely that anything on the native side should call
  // this method directly.
  java_ref_.reset();

  // Stop the timer for completeness, though resetting `java_ref_` will make it
  // a no-op.
  update_action_timer_->Stop();

  if (window_android_) {
    window_android_->RemoveObserver(this);
    window_android_ = nullptr;
  }

  // Only pause the video when play/pause button is visible.
  controller_->OnWindowDestroyed(
      /*should_pause_video=*/visible_actions_.find(
          static_cast<int>(media_session::mojom::MediaSessionAction::kPlay)) !=
      visible_actions_.end());
  // `this` may be destroyed.
}

void OverlayWindowAndroid::TogglePlayPause(JNIEnv* env, bool toggleOn) {
  DCHECK(!controller_->IsPlayerActive());
  if (toggleOn == (playback_state_ == PlaybackState::kPaused)) {
    controller_->TogglePlayPause();
  }
}

void OverlayWindowAndroid::NextTrack(JNIEnv* env) {
  controller_->NextTrack();
}

void OverlayWindowAndroid::PreviousTrack(JNIEnv* env) {
  controller_->PreviousTrack();
}

void OverlayWindowAndroid::NextSlide(JNIEnv* env) {
  controller_->NextSlide();
}

void OverlayWindowAndroid::PreviousSlide(JNIEnv* env) {
  controller_->PreviousSlide();
}

void OverlayWindowAndroid::ToggleMicrophone(JNIEnv* env, bool toggleOn) {
  if (microphone_muted_ == toggleOn) {
    controller_->ToggleMicrophone();
  }
}

void OverlayWindowAndroid::ToggleCamera(JNIEnv* env, bool toggleOn) {
  if (!camera_on_ == toggleOn) {
    controller_->ToggleCamera();
  }
}

void OverlayWindowAndroid::HangUp(JNIEnv* env) {
  controller_->HangUp();
}

void OverlayWindowAndroid::CompositorViewCreated(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& compositor_view) {
  compositor_view_ =
      thin_webview::android::CompositorView::FromJavaObject(compositor_view);
  DCHECK(compositor_view_);
  compositor_view_->SetRootLayer(surface_layer_);
}

void OverlayWindowAndroid::OnViewSizeChanged(JNIEnv* env,
                                             jint width,
                                             jint height) {
  gfx::Size content_size(width, height);
  if (bounds_.size() == content_size) {
    return;
  }

  bounds_.set_size(content_size);
  surface_layer_->SetBounds(content_size);
  controller_->UpdateLayerBounds();
}

void OverlayWindowAndroid::OnBackToTab(JNIEnv* env) {
  controller_->FocusInitiator();
  Hide();
}

void OverlayWindowAndroid::Close() {
  CloseInternal();
  controller_->OnWindowDestroyed(/*should_pause_video=*/true);
  // `this` may be destroyed.
}

void OverlayWindowAndroid::Hide() {
  CloseInternal();
  controller_->OnWindowDestroyed(/*should_pause_video=*/false);
  // `this` may be destroyed.
}

void OverlayWindowAndroid::CloseInternal() {
  if (java_ref_.is_uninitialized()) {
    return;
  }

  DCHECK(window_android_);
  window_android_->RemoveObserver(this);
  window_android_ = nullptr;
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_PictureInPictureActivity_close(env, java_ref_.get(env));
  // The java side forgets about us on close, so don't call back.
  java_ref_.reset();

  // Stop any in-flight action button updates.  We won't find out if the Android
  // window is destroyed since that comes from `WindowAndroidObserver` but we
  // just unregistered from that.
  update_action_timer_->Stop();
}

bool OverlayWindowAndroid::IsActive() const {
  return true;
}

bool OverlayWindowAndroid::IsVisible() const {
  return true;
}

gfx::Rect OverlayWindowAndroid::GetBounds() {
  return bounds_;
}

void OverlayWindowAndroid::UpdateNaturalSize(const gfx::Size& natural_size) {
  if (java_ref_.is_uninitialized()) {
    video_size_ = natural_size;
    // This isn't guaranteed to be right, but it's better than (0,0).
    bounds_.set_size(natural_size);
    return;
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  Java_PictureInPictureActivity_updateVideoSize(
      env, java_ref_.get(env), natural_size.width(), natural_size.height());
}

void OverlayWindowAndroid::SetPlaybackState(PlaybackState playback_state) {
  if (playback_state_ == playback_state) {
    return;
  }

  playback_state_ = playback_state;
  if (java_ref_.is_uninitialized()) {
    return;
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  Java_PictureInPictureActivity_setPlaybackState(env, java_ref_.get(env),
                                                 playback_state);
}

void OverlayWindowAndroid::SetMicrophoneMuted(bool muted) {
  if (microphone_muted_ == muted) {
    return;
  }

  microphone_muted_ = muted;
  if (java_ref_.is_uninitialized()) {
    return;
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  Java_PictureInPictureActivity_setMicrophoneMuted(env, java_ref_.get(env),
                                                   microphone_muted_);
}

void OverlayWindowAndroid::SetCameraState(bool turned_on) {
  if (camera_on_ == turned_on) {
    return;
  }

  camera_on_ = turned_on;
  if (java_ref_.is_uninitialized()) {
    return;
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  Java_PictureInPictureActivity_setCameraState(env, java_ref_.get(env),
                                               camera_on_);
}

void OverlayWindowAndroid::SetPlayPauseButtonVisibility(bool is_visible) {
  MaybeUpdateVisibleAction(media_session::mojom::MediaSessionAction::kPlay,
                           is_visible);
}

void OverlayWindowAndroid::SetNextTrackButtonVisibility(bool is_visible) {
  MaybeUpdateVisibleAction(media_session::mojom::MediaSessionAction::kNextTrack,
                           is_visible);
}

void OverlayWindowAndroid::SetPreviousTrackButtonVisibility(bool is_visible) {
  MaybeUpdateVisibleAction(
      media_session::mojom::MediaSessionAction::kPreviousTrack, is_visible);
}

void OverlayWindowAndroid::SetToggleMicrophoneButtonVisibility(
    bool is_visible) {
  MaybeUpdateVisibleAction(
      media_session::mojom::MediaSessionAction::kToggleMicrophone, is_visible);
}

void OverlayWindowAndroid::SetToggleCameraButtonVisibility(bool is_visible) {
  MaybeUpdateVisibleAction(
      media_session::mojom::MediaSessionAction::kToggleCamera, is_visible);
}

void OverlayWindowAndroid::SetHangUpButtonVisibility(bool is_visible) {
  MaybeUpdateVisibleAction(media_session::mojom::MediaSessionAction::kHangUp,
                           is_visible);
}

void OverlayWindowAndroid::SetNextSlideButtonVisibility(bool is_visible) {
  MaybeUpdateVisibleAction(media_session::mojom::MediaSessionAction::kNextSlide,
                           is_visible);
}

void OverlayWindowAndroid::SetPreviousSlideButtonVisibility(bool is_visible) {
  MaybeUpdateVisibleAction(
      media_session::mojom::MediaSessionAction::kPreviousSlide, is_visible);
}

void OverlayWindowAndroid::SetSurfaceId(const viz::SurfaceId& surface_id) {
  const viz::SurfaceId& old_surface_id = surface_layer_->surface_id().is_valid()
                                             ? surface_layer_->surface_id()
                                             : surface_id;
  if (window_android_ && window_android_->GetCompositor() &&
      old_surface_id.frame_sink_id() != surface_id.frame_sink_id()) {
    // On Android, the new frame sink needs to be added before
    // removing the previous surface sink.
    window_android_->GetCompositor()->AddChildFrameSink(
        surface_id.frame_sink_id());
    window_android_->GetCompositor()->RemoveChildFrameSink(
        old_surface_id.frame_sink_id());
  }
  // Set the surface after frame sink hierarchy update.
  surface_layer_->SetSurfaceId(surface_id,
                               cc::DeadlinePolicy::UseDefaultDeadline());
}

void OverlayWindowAndroid::MaybeNotifyVisibleActionsChanged() {
  if (java_ref_.is_uninitialized()) {
    return;
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  Java_PictureInPictureActivity_updateVisibleActions(
      env, java_ref_.get(env),
      base::android::ToJavaIntArray(
          env,
          std::vector<int>(visible_actions_.begin(), visible_actions_.end())));
}

void OverlayWindowAndroid::MaybeUpdateVisibleAction(
    const media_session::mojom::MediaSessionAction& action,
    bool is_visible) {
  int action_code = static_cast<int>(action);
  if ((visible_actions_.find(action_code) != visible_actions_.end()) ==
      is_visible) {
    return;
  }

  if (is_visible) {
    visible_actions_.insert(action_code);
  } else {
    visible_actions_.erase(action_code);
  }

  if (!update_action_timer_->IsRunning()) {
    update_action_timer_->Start(
        FROM_HERE, base::Seconds(1),
        base::BindOnce(&OverlayWindowAndroid::MaybeNotifyVisibleActionsChanged,
                       base::Unretained(this)));
  }
}