OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/gpu/android_video_surface_chooser_impl.h" | 5 #include "media/gpu/android_video_surface_chooser_impl.h" |
6 | 6 |
7 namespace media { | 7 namespace media { |
8 | 8 |
9 AndroidVideoSurfaceChooserImpl::AndroidVideoSurfaceChooserImpl() | 9 AndroidVideoSurfaceChooserImpl::AndroidVideoSurfaceChooserImpl() |
10 : weak_factory_(this) {} | 10 : weak_factory_(this) {} |
11 | 11 |
12 AndroidVideoSurfaceChooserImpl::~AndroidVideoSurfaceChooserImpl() {} | 12 AndroidVideoSurfaceChooserImpl::~AndroidVideoSurfaceChooserImpl() {} |
13 | 13 |
14 void AndroidVideoSurfaceChooserImpl::Initialize( | 14 void AndroidVideoSurfaceChooserImpl::Initialize( |
15 UseOverlayCB use_overlay_cb, | 15 UseOverlayCB use_overlay_cb, |
16 UseSurfaceTextureCB use_surface_texture_cb, | 16 UseSurfaceTextureCB use_surface_texture_cb, |
17 StopUsingOverlayImmediatelyCB stop_immediately_cb, | |
18 AndroidOverlayFactoryCB initial_factory) { | 17 AndroidOverlayFactoryCB initial_factory) { |
19 use_overlay_cb_ = std::move(use_overlay_cb); | 18 use_overlay_cb_ = std::move(use_overlay_cb); |
20 use_surface_texture_cb_ = std::move(use_surface_texture_cb); | 19 use_surface_texture_cb_ = std::move(use_surface_texture_cb); |
21 stop_immediately_cb_ = std::move(stop_immediately_cb); | |
22 | 20 |
23 if (initial_factory) { | 21 if (initial_factory) { |
24 // We requested an overlay. Wait to see if it succeeds or fails, since | 22 // We requested an overlay. Wait to see if it succeeds or fails, since |
25 // hopefully this will be fast. On M+, we could ask it to start with a | 23 // hopefully this will be fast. On M+, we could ask it to start with a |
26 // SurfaceTexture either way. Before M, we can't switch surfaces. In that | 24 // SurfaceTexture either way. Before M, we can't switch surfaces. In that |
27 // case, it's important not to request a SurfaceTexture if we have an | 25 // case, it's important not to request a SurfaceTexture if we have an |
28 // overlay pending, so that we know not to transition to SurfaceTexture. | 26 // overlay pending, so that we know not to transition to SurfaceTexture. |
29 client_notification_pending_ = true; | 27 client_notification_pending_ = true; |
30 ReplaceOverlayFactory(std::move(initial_factory)); | 28 ReplaceOverlayFactory(std::move(initial_factory)); |
31 } | 29 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // between the two. | 69 // between the two. |
72 AndroidOverlayConfig config; | 70 AndroidOverlayConfig config; |
73 // We bind all of our callbacks with weak ptrs, since we don't know how long | 71 // We bind all of our callbacks with weak ptrs, since we don't know how long |
74 // the client will hold on to overlays. They could, in principle, show up | 72 // the client will hold on to overlays. They could, in principle, show up |
75 // long after the client is destroyed too, if codec destruction hangs. | 73 // long after the client is destroyed too, if codec destruction hangs. |
76 config.ready_cb = base::Bind(&AndroidVideoSurfaceChooserImpl::OnOverlayReady, | 74 config.ready_cb = base::Bind(&AndroidVideoSurfaceChooserImpl::OnOverlayReady, |
77 weak_factory_.GetWeakPtr()); | 75 weak_factory_.GetWeakPtr()); |
78 config.failed_cb = | 76 config.failed_cb = |
79 base::Bind(&AndroidVideoSurfaceChooserImpl::OnOverlayFailed, | 77 base::Bind(&AndroidVideoSurfaceChooserImpl::OnOverlayFailed, |
80 weak_factory_.GetWeakPtr()); | 78 weak_factory_.GetWeakPtr()); |
81 config.destroyed_cb = | |
82 base::Bind(&AndroidVideoSurfaceChooserImpl::OnSurfaceDestroyed, | |
83 weak_factory_.GetWeakPtr()); | |
84 // TODO(liberato): where do we get the initial size from? For CVV, it's | 79 // TODO(liberato): where do we get the initial size from? For CVV, it's |
85 // set via the natural size, and this is ignored anyway. The client should | 80 // set via the natural size, and this is ignored anyway. The client should |
86 // provide this. | 81 // provide this. |
87 config.rect = gfx::Rect(0, 0, 1, 1); | 82 config.rect = gfx::Rect(0, 0, 1, 1); |
88 overlay_ = overlay_factory_.Run(std::move(config)); | 83 overlay_ = overlay_factory_.Run(std::move(config)); |
| 84 // We could add a destruction callback here, if we need to find out when the |
| 85 // surface has been destroyed. It might also be good to have a 'overlay has |
| 86 // been destroyed' callback from ~AndroidOverlay, since we don't really know |
| 87 // how long that will take after SurfaceDestroyed. |
89 } | 88 } |
90 | 89 |
91 void AndroidVideoSurfaceChooserImpl::OnOverlayReady(AndroidOverlay* overlay) { | 90 void AndroidVideoSurfaceChooserImpl::OnOverlayReady(AndroidOverlay* overlay) { |
92 // |overlay_| is the only overlay for which we haven't gotten a ready callback | 91 // |overlay_| is the only overlay for which we haven't gotten a ready callback |
93 // back yet. | 92 // back yet. |
94 DCHECK_EQ(overlay, overlay_.get()); | 93 DCHECK_EQ(overlay, overlay_.get()); |
95 | 94 |
96 // If we haven't sent the client notification yet, we're doing so now. | 95 // If we haven't sent the client notification yet, we're doing so now. |
97 client_notification_pending_ = false; | 96 client_notification_pending_ = false; |
98 | 97 |
99 use_overlay_cb_.Run(std::move(overlay_)); | 98 use_overlay_cb_.Run(std::move(overlay_)); |
100 } | 99 } |
101 | 100 |
102 void AndroidVideoSurfaceChooserImpl::OnOverlayFailed(AndroidOverlay* overlay) { | 101 void AndroidVideoSurfaceChooserImpl::OnOverlayFailed(AndroidOverlay* overlay) { |
103 // We shouldn't get a failure for any overlay except the incoming one. | 102 // We shouldn't get a failure for any overlay except the incoming one. |
104 DCHECK_EQ(overlay, overlay_.get()); | 103 DCHECK_EQ(overlay, overlay_.get()); |
105 | 104 |
106 // If we owe the client a notification callback, then send it. | 105 // If we owe the client a notification callback, then send it. |
107 if (client_notification_pending_) { | 106 if (client_notification_pending_) { |
108 // The overlay that we requested failed, so notify the client to try | 107 // The overlay that we requested failed, so notify the client to try |
109 // using SurfaceTexture instead. | 108 // using SurfaceTexture instead. |
110 client_notification_pending_ = false; | 109 client_notification_pending_ = false; |
111 use_surface_texture_cb_.Run(); | 110 use_surface_texture_cb_.Run(); |
112 } | 111 } |
113 | 112 |
114 overlay_ = nullptr; | 113 overlay_ = nullptr; |
115 } | 114 } |
116 | 115 |
117 void AndroidVideoSurfaceChooserImpl::OnSurfaceDestroyed( | |
118 AndroidOverlay* overlay) { | |
119 // We shouldn't get OnSurfaceDestroyed unless we previously got Ready. In | |
120 // that case, we should have notified the client then. | |
121 DCHECK(!client_notification_pending_); | |
122 | |
123 // We should not get OnSurfaceDestroyed for the incoming overlay, since we | |
124 // should't get OnSurfaceDestroyed before OnSurfaceReady. OnSurfaceReady | |
125 // should imply that this isn't the incoming overlay. | |
126 DCHECK_NE(overlay_.get(), overlay); | |
127 | |
128 // We unconditionally send 'stop immediately', since we don't know what | |
129 // overlay this is. Even if we revoked the overlay before, we may have done | |
130 // so with the slow transition, which isn't good enough now. The client has | |
131 // to be smart enoug to understand if it's currently using |overlay| or not. | |
132 | |
133 // Also remember that we don't know when the client drops the overlay, after | |
134 // we revoke it. We can get callbacks until that happens, even if (for | |
135 // example), the overlay is waiting for MediaCodec destruction. So, it's | |
136 // likely that we'll send callbacks for overlays that the client is already | |
137 // not using. | |
138 stop_immediately_cb_.Run(overlay); | |
139 } | |
140 | |
141 } // namespace media | 116 } // namespace media |
OLD | NEW |