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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/android/video_decoder_job.h"
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/threading/thread.h"
#include "media/base/android/media_codec_bridge.h"
#include "media/base/android/media_drm_bridge.h"
namespace media {
class VideoDecoderThread : public base::Thread {
public:
VideoDecoderThread() : base::Thread("MediaSource_VideoDecoderThread") {
Start();
}
};
// TODO(qinmin): Check if it is tolerable to use worker pool to handle all the
// decoding tasks so that we don't need a global thread here.
// http://crbug.com/245750
base::LazyInstance<VideoDecoderThread>::Leaky
g_video_decoder_thread = LAZY_INSTANCE_INITIALIZER;
VideoDecoderJob::VideoDecoderJob(
const base::Closure& request_data_cb,
const base::Closure& request_resources_cb,
const base::Closure& on_demuxer_config_changed_cb)
: MediaDecoderJob(g_video_decoder_thread.Pointer()->task_runner(),
request_data_cb,
on_demuxer_config_changed_cb),
video_codec_(kUnknownVideoCodec),
config_width_(0),
config_height_(0),
output_width_(0),
output_height_(0),
request_resources_cb_(request_resources_cb) {
}
VideoDecoderJob::~VideoDecoderJob() {}
bool VideoDecoderJob::SetVideoSurface(gfx::ScopedJavaSurface surface) {
// For an empty surface, always pass it to the |media_codec_bridge_| job so
// that it can detach from the current one. Otherwise, don't pass an
// unprotected surface if the video content requires a protected one.
if (!surface.IsEmpty() && IsProtectedSurfaceRequired() &&
!surface.is_protected()) {
return false;
}
surface_ = surface.Pass();
need_to_reconfig_decoder_job_ = true;
return true;
}
bool VideoDecoderJob::HasStream() const {
return video_codec_ != kUnknownVideoCodec;
}
void VideoDecoderJob::ReleaseDecoderResources() {
MediaDecoderJob::ReleaseDecoderResources();
surface_ = gfx::ScopedJavaSurface();
}
void VideoDecoderJob::SetDemuxerConfigs(const DemuxerConfigs& configs) {
video_codec_ = configs.video_codec;
config_width_ = configs.video_size.width();
config_height_ = configs.video_size.height();
set_is_content_encrypted(configs.is_video_encrypted);
if (!media_codec_bridge_) {
output_width_ = config_width_;
output_height_ = config_height_;
}
}
void VideoDecoderJob::ReleaseOutputBuffer(
int output_buffer_index,
size_t offset,
size_t size,
bool render_output,
base::TimeDelta current_presentation_timestamp,
const ReleaseOutputCompletionCallback& callback) {
media_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, render_output);
callback.Run(current_presentation_timestamp, current_presentation_timestamp);
}
bool VideoDecoderJob::ComputeTimeToRender() const {
return true;
}
bool VideoDecoderJob::IsCodecReconfigureNeeded(
const DemuxerConfigs& configs) const {
if (!media_codec_bridge_)
return true;
if (!AreDemuxerConfigsChanged(configs))
return false;
bool only_size_changed = false;
if (video_codec_ == configs.video_codec &&
is_content_encrypted() == configs.is_video_encrypted) {
only_size_changed = true;
}
return !only_size_changed ||
!static_cast<VideoCodecBridge*>(media_codec_bridge_.get())->
IsAdaptivePlaybackSupported(configs.video_size.width(),
configs.video_size.height());
}
bool VideoDecoderJob::AreDemuxerConfigsChanged(
const DemuxerConfigs& configs) const {
return video_codec_ != configs.video_codec ||
is_content_encrypted() != configs.is_video_encrypted ||
config_width_ != configs.video_size.width() ||
config_height_ != configs.video_size.height();
}
MediaDecoderJob::MediaDecoderJobStatus
VideoDecoderJob::CreateMediaCodecBridgeInternal() {
if (surface_.IsEmpty()) {
ReleaseMediaCodecBridge();
return STATUS_FAILURE;
}
// If we cannot find a key frame in cache, browser seek is needed.
bool next_video_data_is_iframe = SetCurrentFrameToPreviouslyCachedKeyFrame();
if (!next_video_data_is_iframe)
return STATUS_KEY_FRAME_REQUIRED;
bool is_secure = is_content_encrypted() && drm_bridge() &&
drm_bridge()->IsProtectedSurfaceRequired();
media_codec_bridge_.reset(VideoCodecBridge::CreateDecoder(
video_codec_, is_secure, gfx::Size(config_width_, config_height_),
surface_.j_surface().obj(), GetMediaCrypto().obj()));
if (!media_codec_bridge_)
return STATUS_FAILURE;
request_resources_cb_.Run();
return STATUS_SUCCESS;
}
bool VideoDecoderJob::UpdateOutputFormat() {
if (!media_codec_bridge_)
return false;
int prev_output_width = output_width_;
int prev_output_height = output_height_;
// See b/18224769. The values reported from MediaCodecBridge::GetOutputFormat
// correspond to the actual video frame size, but this is not necessarily the
// size that should be output.
output_width_ = config_width_;
output_height_ = config_height_;
return (output_width_ != prev_output_width) ||
(output_height_ != prev_output_height);
}
bool VideoDecoderJob::IsProtectedSurfaceRequired() {
return is_content_encrypted() && drm_bridge() &&
drm_bridge()->IsProtectedSurfaceRequired();
}
} // namespace media
|