Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1590)

Side by Side Diff: media/gpu/avda_overlay_helper_impl.cc

Issue 2813303003: Add AndroidVideoSurfaceChooser to manage overlays. (Closed)
Patch Set: from ps11 Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/gpu/avda_overlay_helper_impl.h"
6
7 namespace media {
8
9 AVDAOverlayHelperImpl::AVDAOverlayHelperImpl() : weak_factory_(this) {}
10
11 AVDAOverlayHelperImpl::~AVDAOverlayHelperImpl() {}
12
13 void AVDAOverlayHelperImpl::Initialize(
14 const UseOverlayCB& use_overlay_cb,
DaleCurtis 2017/04/27 18:57:38 Callback are passed by value now and used with std
liberato (no reviews please) 2017/04/27 20:05:02 Done.
15 const UseSurfaceTextureCB& use_surface_texture_cb,
16 const StopUsingOverlayImmediatelyCB& stop_immediately_cb,
17 std::unique_ptr<AndroidOverlayFactory> initial_factory) {
18 use_overlay_cb_ = use_overlay_cb;
19 use_surface_texture_cb_ = use_surface_texture_cb;
20 stop_immediately_cb_ = stop_immediately_cb;
21
22 if (initial_factory) {
23 // We requested an overlay. Wait to see if it succeeds or fails, since
24 // hopefully this will be fast. On M+, we could ask it to start with a
25 // SurfaceTexture either way. Before M, we can't switch surfaces. In that
26 // case, it's important not to request a SurfaceTexture if we have an
27 // overlay pending, so that we know not to transition to SurfaceTexture.
28 client_notification_pending_ = true;
29 OnOverlayFactory(std::move(initial_factory));
30 }
31
32 if (!overlay_) {
33 // We haven't requested an overlay yet. Just ask the client to start with
34 // SurfaceTexture now.
35 use_surface_texture_cb_.Run();
36 return;
37 }
38 }
39
40 void AVDAOverlayHelperImpl::OnOverlayFactory(
41 std::unique_ptr<AndroidOverlayFactory> factory) {
42 // If we have an overlay, then we should transition away from it. It
43 // doesn't matter if we have a new factory or no factory; the old overlay goes
44 // with the old factory.
45
46 // Notify the client to transition to SurfaceTexture, which it might already
47 // be using. If |!client_notification_pending_|, then we're still during
48 // initial startup, so we don't switch the client. Otherwise, we'd cause
49 // pre-M to break, since we'd start with a SurfaceTexture in all cases.
50 if (!client_notification_pending_)
51 use_surface_texture_cb_.Run();
52
53 // If we started construction of an overlay, but it's not ready yet, then
54 // just drop it.
55 if (overlay_)
56 overlay_ = nullptr;
57
58 overlay_factory_ = std::move(factory);
59
60 // If we don't have a new factory, then just stop here.
61 if (!overlay_factory_)
62 return;
63
64 // We just got an overlay factory. Get an overlay immediately. This is
65 // the right behavior to match what AVDA does with CVV. For DS, we should
66 // probably check to see if it's a good idea, at least on M+.
67 // Also note that for pre-M, AVDA depends on this behavior, else it will get
68 // a SurfaceTexture then be unable to switch. Perhaps there should be a
69 // pre-M implementation of this class that guarantees that it won't change
70 // between the two.
71 AndroidOverlay::Config config;
72 // We bind all of our callbacks with weak ptrs, since we don't know how long
73 // the client will hold on to overlays. They could, in principle, show up
74 // long after the client is destroyed too, if codec destruction hangs.
75 config.ready_cb = base::Bind(&AVDAOverlayHelperImpl::OnOverlayReady,
76 weak_factory_.GetWeakPtr());
77 config.failed_cb = base::Bind(&AVDAOverlayHelperImpl::OnOverlayFailed,
78 weak_factory_.GetWeakPtr());
79 config.destroyed_cb = base::Bind(&AVDAOverlayHelperImpl::OnSurfaceDestroyed,
80 weak_factory_.GetWeakPtr());
81 // TODO(liberato): where do we get the initial size from? For CVV, it's
82 // set via the natural size, and this is ignored anyway. The client should
83 // provide this.
84 config.rect = gfx::Rect(0, 0, 1, 1);
85 overlay_ = overlay_factory_->CreateOverlay(config);
86 }
87
88 void AVDAOverlayHelperImpl::OnOverlayReady(AndroidOverlay* overlay) {
89 // We |overlay_| is the only overlay for which we haven't gotten a ready
90 // callback yet.
91 DCHECK_EQ(overlay, overlay_.get());
92
93 // If we haven't sent the client notification yet, we're doing so now.
94 client_notification_pending_ = false;
95
96 use_overlay_cb_.Run(std::move(overlay_));
97 }
98
99 void AVDAOverlayHelperImpl::OnOverlayFailed(AndroidOverlay* overlay) {
100 // We shouldn't get a failure for any overlay except the incoming one.
101 DCHECK_EQ(overlay, overlay_.get());
102
103 // If we owe the client a notification callback, then send it.
104 if (client_notification_pending_) {
105 // The overlay that we requested failed, so notify the client to try
106 // using SurfaceTexture instead.
107 client_notification_pending_ = false;
108 use_surface_texture_cb_.Run();
109 }
110
111 overlay_ = nullptr;
112 }
113
114 void AVDAOverlayHelperImpl::OnSurfaceDestroyed(AndroidOverlay* overlay) {
115 // We shouldn't get OnSurfaceDestroyed unless we previously got Ready. In
116 // that case, we should have notified the client then.
117 DCHECK(!client_notification_pending_);
118
119 // We should not get OnSurfaceDestroyed for the incoming overlay, since we
120 // should't get OnSurfaceDestroyed before OnSurfaceReady. OnSurfaceReady
121 // should imply that this isn't the incoming overlay.
122 DCHECK_NE(overlay_.get(), overlay);
123
124 // We unconditionally send 'stop immediately', since we don't know what
125 // overlay this is. Even if we revoked the overlay before, we may have done
126 // so with the slow transition, which isn't good enough now. The client has
127 // to be smart enoug to understand if it's currently using |overlay| or not.
128
129 // Also remember that we don't know when the client drops the overlay, after
130 // we revoke it. We can get callbacks until that happens, even if (for
131 // example), the overlay is waiting for MediaCodec destruction. So, it's
132 // likely that we'll send callbacks for overlays that the client is already
133 // not using.
134 stop_immediately_cb_.Run(overlay);
135 }
136
137 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698