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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
media / base / android / media_player_bridge.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/android/media_player_bridge.h"
#include <algorithm>
#include <utility>
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "media/base/android/media_common_android.h"
#include "media/base/android/media_resource_getter.h"
#include "media/base/android/media_url_interceptor.h"
#include "media/base/timestamp_constants.h"
#include "net/storage_access_api/status.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "media/base/android/media_jni_headers/MediaPlayerBridge_jni.h"
using base::android::ConvertUTF8ToJavaString;
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
namespace media {
namespace {
enum UMAExitStatus {
UMA_EXIT_SUCCESS = 0,
UMA_EXIT_ERROR,
UMA_EXIT_STATUS_MAX = UMA_EXIT_ERROR,
};
const double kDefaultVolume = 1.0;
const char kWatchTimeHistogram[] = "Media.Android.MediaPlayerWatchTime";
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class WatchTimeType {
kNonHls = 0,
kHlsAudioOnly = 1,
kHlsVideo = 2,
kMaxValue = kHlsVideo,
};
void RecordWatchTimeUMA(bool is_hls, bool has_video) {
WatchTimeType type = WatchTimeType::kNonHls;
if (is_hls) {
if (!has_video) {
type = WatchTimeType::kHlsAudioOnly;
} else {
type = WatchTimeType::kHlsVideo;
}
}
UMA_HISTOGRAM_ENUMERATION(kWatchTimeHistogram, type);
}
} // namespace
MediaPlayerBridge::MediaPlayerBridge(
const GURL& url,
const net::SiteForCookies& site_for_cookies,
const url::Origin& top_frame_origin,
net::StorageAccessApiStatus storage_access_api_status,
const std::string& user_agent,
bool hide_url_log,
Client* client,
bool allow_credentials,
bool is_hls,
const base::flat_map<std::string, std::string> headers)
: prepared_(false),
playback_completed_(false),
pending_play_(false),
should_seek_on_prepare_(false),
url_(url),
site_for_cookies_(site_for_cookies),
top_frame_origin_(top_frame_origin),
storage_access_api_status_(storage_access_api_status),
pending_retrieve_cookies_(false),
should_prepare_on_retrieved_cookies_(false),
user_agent_(user_agent),
hide_url_log_(hide_url_log),
width_(0),
height_(0),
can_seek_forward_(true),
can_seek_backward_(true),
volume_(kDefaultVolume),
allow_credentials_(allow_credentials),
is_active_(false),
has_error_(false),
has_ever_started_(false),
is_hls_(is_hls),
watch_timer_(base::BindRepeating(&MediaPlayerBridge::OnWatchTimerTick,
base::Unretained(this)),
base::BindRepeating(&MediaPlayerBridge::GetCurrentTime,
base::Unretained(this))),
headers_(std::move(headers)),
client_(client) {
listener_ = std::make_unique<MediaPlayerListener>(
base::SingleThreadTaskRunner::GetCurrentDefault(),
weak_factory_.GetWeakPtr());
}
MediaPlayerBridge::~MediaPlayerBridge() {
if (!j_media_player_bridge_.is_null()) {
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
Java_MediaPlayerBridge_destroy(env, j_media_player_bridge_);
}
Release();
if (has_ever_started_) {
UMA_HISTOGRAM_ENUMERATION("Media.Android.MediaPlayerSuccess",
has_error_ ? UMA_EXIT_ERROR : UMA_EXIT_SUCCESS,
UMA_EXIT_STATUS_MAX + 1);
}
}
void MediaPlayerBridge::Initialize() {
cookies_.clear();
CHECK(!url_.SchemeIsBlob());
CHECK(!url_.SchemeIsFileSystem());
if (allow_credentials_ && !url_.SchemeIsFile()) {
media::MediaResourceGetter* resource_getter =
client_->GetMediaResourceGetter();
pending_retrieve_cookies_ = true;
resource_getter->GetCookies(
url_, site_for_cookies_, top_frame_origin_, storage_access_api_status_,
base::BindOnce(&MediaPlayerBridge::OnCookiesRetrieved,
weak_factory_.GetWeakPtr()));
}
}
void MediaPlayerBridge::CreateJavaMediaPlayerBridge() {
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
j_media_player_bridge_.Reset(Java_MediaPlayerBridge_create(
env, reinterpret_cast<intptr_t>(this)));
UpdateVolumeInternal();
AttachListener(j_media_player_bridge_);
}
void MediaPlayerBridge::PropagateDuration(base::TimeDelta duration) {
duration_ = duration;
client_->OnMediaDurationChanged(duration_);
}
void MediaPlayerBridge::SetVideoSurface(gl::ScopedJavaSurface surface) {
surface_ = std::move(surface);
if (j_media_player_bridge_.is_null())
return;
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
Java_MediaPlayerBridge_setSurface(env, j_media_player_bridge_,
surface_.j_surface());
}
void MediaPlayerBridge::SetPlaybackRate(double playback_rate) {
if (!prepared_) {
pending_playback_rate_ = playback_rate;
return;
}
if (j_media_player_bridge_.is_null())
return;
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
Java_MediaPlayerBridge_setPlaybackRate(env, j_media_player_bridge_,
playback_rate);
}
void MediaPlayerBridge::Prepare() {
DCHECK(j_media_player_bridge_.is_null());
CHECK(!url_.SchemeIsBlob());
CHECK(!url_.SchemeIsFileSystem());
CreateJavaMediaPlayerBridge();
SetDataSource(url_.spec());
}
void MediaPlayerBridge::SetDataSource(const std::string& url) {
if (j_media_player_bridge_.is_null())
return;
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
int fd;
int64_t offset;
int64_t size;
if (InterceptMediaUrl(url, &fd, &offset, &size)) {
if (!Java_MediaPlayerBridge_setDataSourceFromFd(env, j_media_player_bridge_,
fd, offset, size)) {
OnMediaError(MEDIA_ERROR_FORMAT);
return;
}
if (!Java_MediaPlayerBridge_prepareAsync(env, j_media_player_bridge_))
OnMediaError(MEDIA_ERROR_FORMAT);
return;
}
// Create a Java String for the URL.
ScopedJavaLocalRef<jstring> j_url_string = ConvertUTF8ToJavaString(env, url);
const std::string data_uri_prefix("data:");
if (base::StartsWith(url, data_uri_prefix, base::CompareCase::SENSITIVE)) {
if (!Java_MediaPlayerBridge_setDataUriDataSource(
env, j_media_player_bridge_, j_url_string)) {
OnMediaError(MEDIA_ERROR_FORMAT);
}
return;
}
// Cookies may not have been retrieved yet, delay prepare until they are
// retrieved.
if (pending_retrieve_cookies_) {
should_prepare_on_retrieved_cookies_ = true;
return;
}
SetDataSourceInternal();
}
void MediaPlayerBridge::SetDataSourceInternal() {
DCHECK(!pending_retrieve_cookies_);
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
ScopedJavaLocalRef<jstring> j_cookies =
ConvertUTF8ToJavaString(env, cookies_);
ScopedJavaLocalRef<jstring> j_user_agent =
ConvertUTF8ToJavaString(env, user_agent_);
ScopedJavaLocalRef<jstring> j_url_string =
ConvertUTF8ToJavaString(env, url_.spec());
jclass hashMapClass = env->FindClass("java/util/HashMap");
jmethodID hashMapConstructor =
env->GetMethodID(hashMapClass, "<init>", "()V");
jobject javaHashMap = env->NewObject(hashMapClass, hashMapConstructor);
for (const auto& entry : headers_) {
jstring key = env->NewStringUTF(entry.first.c_str());
jstring value = env->NewStringUTF(entry.second.c_str());
jmethodID putMethod = env->GetMethodID(
hashMapClass, "put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
env->CallObjectMethod(javaHashMap, putMethod, key, value);
env->DeleteLocalRef(key);
env->DeleteLocalRef(value);
}
base::android::ScopedJavaLocalRef<jobject> scoped_hash_map(env, javaHashMap);
if (!Java_MediaPlayerBridge_setDataSource(
env, j_media_player_bridge_, j_url_string, j_cookies, j_user_agent,
hide_url_log_, scoped_hash_map)) {
OnMediaError(MEDIA_ERROR_FORMAT);
return;
}
if (!Java_MediaPlayerBridge_prepareAsync(env, j_media_player_bridge_))
OnMediaError(MEDIA_ERROR_FORMAT);
}
bool MediaPlayerBridge::InterceptMediaUrl(const std::string& url,
int* fd,
int64_t* offset,
int64_t* size) {
// Sentinel value to check whether the output arguments have been set.
const int kUnsetValue = -1;
*fd = kUnsetValue;
*offset = kUnsetValue;
*size = kUnsetValue;
media::MediaUrlInterceptor* url_interceptor =
client_->GetMediaUrlInterceptor();
if (url_interceptor && url_interceptor->Intercept(url, fd, offset, size)) {
DCHECK_NE(kUnsetValue, *fd);
DCHECK_NE(kUnsetValue, *offset);
DCHECK_NE(kUnsetValue, *size);
return true;
}
return false;
}
void MediaPlayerBridge::OnDidSetDataUriDataSource(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jboolean success) {
if (!success) {
OnMediaError(MEDIA_ERROR_FORMAT);
return;
}
if (!Java_MediaPlayerBridge_prepareAsync(env, j_media_player_bridge_))
OnMediaError(MEDIA_ERROR_FORMAT);
}
void MediaPlayerBridge::OnCookiesRetrieved(const std::string& cookies) {
cookies_ = cookies;
pending_retrieve_cookies_ = false;
client_->GetMediaResourceGetter()->GetAuthCredentials(
url_, base::BindOnce(&MediaPlayerBridge::OnAuthCredentialsRetrieved,
weak_factory_.GetWeakPtr()));
if (should_prepare_on_retrieved_cookies_) {
SetDataSourceInternal();
should_prepare_on_retrieved_cookies_ = false;
}
}
void MediaPlayerBridge::OnAuthCredentialsRetrieved(
const std::u16string& username,
const std::u16string& password) {
GURL::ReplacementsW replacements;
if (!username.empty()) {
replacements.SetUsernameStr(username);
if (!password.empty())
replacements.SetPasswordStr(password);
url_ = url_.ReplaceComponents(replacements);
}
}
void MediaPlayerBridge::Start() {
// A second Start() call after an error is considered another attempt for UMA
// and causes UMA reporting.
if (has_ever_started_ && has_error_) {
UMA_HISTOGRAM_ENUMERATION("Media.Android.MediaPlayerSuccess",
UMA_EXIT_ERROR, UMA_EXIT_STATUS_MAX + 1);
}
has_ever_started_ = true;
has_error_ = false;
is_active_ = true;
if (j_media_player_bridge_.is_null()) {
pending_play_ = true;
Prepare();
} else {
if (prepared_)
StartInternal();
else
pending_play_ = true;
}
}
void MediaPlayerBridge::Pause() {
if (j_media_player_bridge_.is_null()) {
pending_play_ = false;
} else {
if (prepared_ && IsPlaying())
PauseInternal();
else
pending_play_ = false;
}
is_active_ = false;
}
bool MediaPlayerBridge::IsPlaying() {
DCHECK(prepared_);
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
jboolean result =
Java_MediaPlayerBridge_isPlaying(env, j_media_player_bridge_);
return result;
}
void MediaPlayerBridge::SeekTo(base::TimeDelta timestamp) {
// Record the time to seek when OnMediaPrepared() is called.
pending_seek_ = timestamp;
should_seek_on_prepare_ = true;
if (prepared_)
SeekInternal(timestamp);
}
base::TimeDelta MediaPlayerBridge::GetCurrentTime() {
if (!prepared_)
return pending_seek_;
JNIEnv* env = base::android::AttachCurrentThread();
return base::Milliseconds(
Java_MediaPlayerBridge_getCurrentPosition(env, j_media_player_bridge_));
}
base::TimeDelta MediaPlayerBridge::GetDuration() {
DCHECK(prepared_);
JNIEnv* env = base::android::AttachCurrentThread();
const int duration_ms =
Java_MediaPlayerBridge_getDuration(env, j_media_player_bridge_);
return duration_ms < 0 ? media::kInfiniteDuration
: base::Milliseconds(duration_ms);
}
void MediaPlayerBridge::Release() {
watch_timer_.Stop();
is_active_ = false;
if (j_media_player_bridge_.is_null())
return;
if (prepared_) {
pending_seek_ = GetCurrentTime();
should_seek_on_prepare_ = true;
}
prepared_ = false;
pending_play_ = false;
SetVideoSurface(gl::ScopedJavaSurface());
JNIEnv* env = base::android::AttachCurrentThread();
Java_MediaPlayerBridge_release(env, j_media_player_bridge_);
j_media_player_bridge_.Reset();
DetachListener();
}
void MediaPlayerBridge::SetVolume(double volume) {
volume_ = std::clamp(volume, 0.0, 1.0);
UpdateVolumeInternal();
}
void MediaPlayerBridge::UpdateVolumeInternal() {
if (j_media_player_bridge_.is_null()) {
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
Java_MediaPlayerBridge_setVolume(env, j_media_player_bridge_, volume_);
}
void MediaPlayerBridge::OnVideoSizeChanged(int width, int height) {
width_ = width;
height_ = height;
client_->OnVideoSizeChanged(width, height);
}
void MediaPlayerBridge::OnMediaError(int error_type) {
// Gather errors for UMA only in the active state.
// The MEDIA_ERROR_INVALID_CODE is reported by MediaPlayerListener.java in
// the situations that are considered normal, and is ignored by upper level.
if (is_active_ && error_type != MEDIA_ERROR_INVALID_CODE)
has_error_ = true;
// Do not propagate MEDIA_ERROR_SERVER_DIED. If it happens in the active state
// we want the playback to stall. It can be recovered by pressing the Play
// button again.
if (error_type == MEDIA_ERROR_SERVER_DIED)
error_type = MEDIA_ERROR_INVALID_CODE;
client_->OnError(error_type);
}
void MediaPlayerBridge::OnPlaybackComplete() {
if (!playback_completed_) {
playback_completed_ = true;
client_->OnPlaybackComplete();
}
}
void MediaPlayerBridge::OnMediaPrepared() {
if (j_media_player_bridge_.is_null())
return;
prepared_ = true;
PropagateDuration(GetDuration());
UpdateAllowedOperations();
// If media player was recovered from a saved state, consume all the pending
// events.
if (should_seek_on_prepare_) {
SeekInternal(pending_seek_);
pending_seek_ = base::Milliseconds(0);
should_seek_on_prepare_ = false;
}
if (!surface_.IsEmpty())
SetVideoSurface(std::move(surface_));
if (pending_play_) {
StartInternal();
pending_play_ = false;
}
if (pending_playback_rate_) {
SetPlaybackRate(pending_playback_rate_.value());
pending_playback_rate_.reset();
}
}
ScopedJavaLocalRef<jobject> MediaPlayerBridge::GetAllowedOperations() {
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
return Java_MediaPlayerBridge_getAllowedOperations(env,
j_media_player_bridge_);
}
void MediaPlayerBridge::AttachListener(const JavaRef<jobject>& j_media_player) {
listener_->CreateMediaPlayerListener(j_media_player);
}
void MediaPlayerBridge::DetachListener() {
listener_->ReleaseMediaPlayerListenerResources();
}
base::WeakPtr<MediaPlayerBridge> MediaPlayerBridge::WeakPtrForUIThread() {
return weak_factory_.GetWeakPtr();
}
void MediaPlayerBridge::UpdateAllowedOperations() {
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
ScopedJavaLocalRef<jobject> allowedOperations = GetAllowedOperations();
can_seek_forward_ =
Java_AllowedOperations_canSeekForward(env, allowedOperations);
can_seek_backward_ =
Java_AllowedOperations_canSeekBackward(env, allowedOperations);
}
void MediaPlayerBridge::StartInternal() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_MediaPlayerBridge_start(env, j_media_player_bridge_);
watch_timer_.Start();
}
void MediaPlayerBridge::PauseInternal() {
watch_timer_.Stop();
JNIEnv* env = base::android::AttachCurrentThread();
Java_MediaPlayerBridge_pause(env, j_media_player_bridge_);
}
void MediaPlayerBridge::SeekInternal(base::TimeDelta time) {
base::TimeDelta current_time = GetCurrentTime();
// Seeking on content like live streams may cause the media player to
// get stuck in an error state.
if (time < current_time && !can_seek_backward_)
return;
if (time >= current_time && !can_seek_forward_)
return;
if (time > duration_)
time = duration_;
// Seeking to an invalid position may cause media player to stuck in an
// error state.
if (time.is_negative()) {
DCHECK_EQ(-1.0, time.InMillisecondsF());
return;
}
playback_completed_ = false;
// Note: we do not want to count changes in media time due to seeks as watch
// time, but tracking pending seeks is not completely trivial. Instead seeks
// larger than kWatchTimeReportingInterval * 2 will be discarded by the sanity
// checks and shorter seeks will be counted.
JNIEnv* env = base::android::AttachCurrentThread();
CHECK(env);
int time_msec = static_cast<int>(time.InMilliseconds());
Java_MediaPlayerBridge_seekTo(env, j_media_player_bridge_, time_msec);
}
GURL MediaPlayerBridge::GetUrl() {
return url_;
}
const net::SiteForCookies& MediaPlayerBridge::GetSiteForCookies() {
return site_for_cookies_;
}
void MediaPlayerBridge::OnWatchTimerTick() {
RecordWatchTimeUMA(is_hls_, height_ > 0);
}
} // namespace media