OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/cast/receiver/video_decoder.h" | 5 #include "media/cast/receiver/video_decoder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // Base class that handles the common problem of detecting dropped frames, and | 26 // Base class that handles the common problem of detecting dropped frames, and |
27 // then invoking the Decode() method implemented by the subclasses to convert | 27 // then invoking the Decode() method implemented by the subclasses to convert |
28 // the encoded payload data into a usable video frame. | 28 // the encoded payload data into a usable video frame. |
29 class VideoDecoder::ImplBase | 29 class VideoDecoder::ImplBase |
30 : public base::RefCountedThreadSafe<VideoDecoder::ImplBase> { | 30 : public base::RefCountedThreadSafe<VideoDecoder::ImplBase> { |
31 public: | 31 public: |
32 ImplBase(const scoped_refptr<CastEnvironment>& cast_environment, | 32 ImplBase(const scoped_refptr<CastEnvironment>& cast_environment, |
33 Codec codec) | 33 Codec codec) |
34 : cast_environment_(cast_environment), | 34 : cast_environment_(cast_environment), |
35 codec_(codec), | 35 codec_(codec), |
36 cast_initialization_status_(STATUS_VIDEO_UNINITIALIZED), | 36 operational_status_(STATUS_UNINITIALIZED), |
37 seen_first_frame_(false) {} | 37 seen_first_frame_(false) {} |
38 | 38 |
39 CastInitializationStatus InitializationResult() const { | 39 OperationalStatus InitializationResult() const { |
40 return cast_initialization_status_; | 40 return operational_status_; |
41 } | 41 } |
42 | 42 |
43 void DecodeFrame(scoped_ptr<EncodedFrame> encoded_frame, | 43 void DecodeFrame(scoped_ptr<EncodedFrame> encoded_frame, |
44 const DecodeFrameCallback& callback) { | 44 const DecodeFrameCallback& callback) { |
45 DCHECK_EQ(cast_initialization_status_, STATUS_VIDEO_INITIALIZED); | 45 DCHECK_EQ(operational_status_, STATUS_INITIALIZED); |
46 | 46 |
47 static_assert(sizeof(encoded_frame->frame_id) == sizeof(last_frame_id_), | 47 static_assert(sizeof(encoded_frame->frame_id) == sizeof(last_frame_id_), |
48 "size of frame_id types do not match"); | 48 "size of frame_id types do not match"); |
49 bool is_continuous = true; | 49 bool is_continuous = true; |
50 if (seen_first_frame_) { | 50 if (seen_first_frame_) { |
51 const uint32 frames_ahead = encoded_frame->frame_id - last_frame_id_; | 51 const uint32 frames_ahead = encoded_frame->frame_id - last_frame_id_; |
52 if (frames_ahead > 1) { | 52 if (frames_ahead > 1) { |
53 RecoverBecauseFramesWereDropped(); | 53 RecoverBecauseFramesWereDropped(); |
54 is_continuous = false; | 54 is_continuous = false; |
55 } | 55 } |
(...skipping 16 matching lines...) Expand all Loading... |
72 virtual ~ImplBase() {} | 72 virtual ~ImplBase() {} |
73 | 73 |
74 virtual void RecoverBecauseFramesWereDropped() {} | 74 virtual void RecoverBecauseFramesWereDropped() {} |
75 | 75 |
76 // Note: Implementation of Decode() is allowed to mutate |data|. | 76 // Note: Implementation of Decode() is allowed to mutate |data|. |
77 virtual scoped_refptr<VideoFrame> Decode(uint8* data, int len) = 0; | 77 virtual scoped_refptr<VideoFrame> Decode(uint8* data, int len) = 0; |
78 | 78 |
79 const scoped_refptr<CastEnvironment> cast_environment_; | 79 const scoped_refptr<CastEnvironment> cast_environment_; |
80 const Codec codec_; | 80 const Codec codec_; |
81 | 81 |
82 // Subclass' ctor is expected to set this to STATUS_VIDEO_INITIALIZED. | 82 // Subclass' ctor is expected to set this to STATUS_INITIALIZED. |
83 CastInitializationStatus cast_initialization_status_; | 83 OperationalStatus operational_status_; |
84 | 84 |
85 private: | 85 private: |
86 bool seen_first_frame_; | 86 bool seen_first_frame_; |
87 uint32 last_frame_id_; | 87 uint32 last_frame_id_; |
88 | 88 |
89 DISALLOW_COPY_AND_ASSIGN(ImplBase); | 89 DISALLOW_COPY_AND_ASSIGN(ImplBase); |
90 }; | 90 }; |
91 | 91 |
92 class VideoDecoder::Vp8Impl : public VideoDecoder::ImplBase { | 92 class VideoDecoder::Vp8Impl : public VideoDecoder::ImplBase { |
93 public: | 93 public: |
94 explicit Vp8Impl(const scoped_refptr<CastEnvironment>& cast_environment) | 94 explicit Vp8Impl(const scoped_refptr<CastEnvironment>& cast_environment) |
95 : ImplBase(cast_environment, CODEC_VIDEO_VP8) { | 95 : ImplBase(cast_environment, CODEC_VIDEO_VP8) { |
96 if (ImplBase::cast_initialization_status_ != STATUS_VIDEO_UNINITIALIZED) | 96 if (ImplBase::operational_status_ != STATUS_UNINITIALIZED) |
97 return; | 97 return; |
98 | 98 |
99 vpx_codec_dec_cfg_t cfg = {0}; | 99 vpx_codec_dec_cfg_t cfg = {0}; |
100 // TODO(miu): Revisit this for typical multi-core desktop use case. This | 100 // TODO(miu): Revisit this for typical multi-core desktop use case. This |
101 // feels like it should be 4 or 8. | 101 // feels like it should be 4 or 8. |
102 cfg.threads = 1; | 102 cfg.threads = 1; |
103 | 103 |
104 DCHECK(vpx_codec_get_caps(vpx_codec_vp8_dx()) & VPX_CODEC_CAP_POSTPROC); | 104 DCHECK(vpx_codec_get_caps(vpx_codec_vp8_dx()) & VPX_CODEC_CAP_POSTPROC); |
105 if (vpx_codec_dec_init(&context_, | 105 if (vpx_codec_dec_init(&context_, |
106 vpx_codec_vp8_dx(), | 106 vpx_codec_vp8_dx(), |
107 &cfg, | 107 &cfg, |
108 VPX_CODEC_USE_POSTPROC) != VPX_CODEC_OK) { | 108 VPX_CODEC_USE_POSTPROC) != VPX_CODEC_OK) { |
109 ImplBase::cast_initialization_status_ = | 109 ImplBase::operational_status_ = STATUS_INVALID_CONFIGURATION; |
110 STATUS_INVALID_VIDEO_CONFIGURATION; | |
111 return; | 110 return; |
112 } | 111 } |
113 ImplBase::cast_initialization_status_ = STATUS_VIDEO_INITIALIZED; | 112 ImplBase::operational_status_ = STATUS_INITIALIZED; |
114 } | 113 } |
115 | 114 |
116 private: | 115 private: |
117 ~Vp8Impl() override { | 116 ~Vp8Impl() override { |
118 if (ImplBase::cast_initialization_status_ == STATUS_VIDEO_INITIALIZED) | 117 if (ImplBase::operational_status_ == STATUS_INITIALIZED) |
119 CHECK_EQ(VPX_CODEC_OK, vpx_codec_destroy(&context_)); | 118 CHECK_EQ(VPX_CODEC_OK, vpx_codec_destroy(&context_)); |
120 } | 119 } |
121 | 120 |
122 scoped_refptr<VideoFrame> Decode(uint8* data, int len) override { | 121 scoped_refptr<VideoFrame> Decode(uint8* data, int len) override { |
123 if (len <= 0 || vpx_codec_decode(&context_, | 122 if (len <= 0 || vpx_codec_decode(&context_, |
124 data, | 123 data, |
125 static_cast<unsigned int>(len), | 124 static_cast<unsigned int>(len), |
126 NULL, | 125 NULL, |
127 0) != VPX_CODEC_OK) { | 126 0) != VPX_CODEC_OK) { |
128 return NULL; | 127 return NULL; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 DISALLOW_COPY_AND_ASSIGN(Vp8Impl); | 167 DISALLOW_COPY_AND_ASSIGN(Vp8Impl); |
169 }; | 168 }; |
170 | 169 |
171 #ifndef OFFICIAL_BUILD | 170 #ifndef OFFICIAL_BUILD |
172 // A fake video decoder that always output 2x2 black frames. | 171 // A fake video decoder that always output 2x2 black frames. |
173 class VideoDecoder::FakeImpl : public VideoDecoder::ImplBase { | 172 class VideoDecoder::FakeImpl : public VideoDecoder::ImplBase { |
174 public: | 173 public: |
175 explicit FakeImpl(const scoped_refptr<CastEnvironment>& cast_environment) | 174 explicit FakeImpl(const scoped_refptr<CastEnvironment>& cast_environment) |
176 : ImplBase(cast_environment, CODEC_VIDEO_FAKE), | 175 : ImplBase(cast_environment, CODEC_VIDEO_FAKE), |
177 last_decoded_id_(-1) { | 176 last_decoded_id_(-1) { |
178 if (ImplBase::cast_initialization_status_ != STATUS_VIDEO_UNINITIALIZED) | 177 if (ImplBase::operational_status_ != STATUS_UNINITIALIZED) |
179 return; | 178 return; |
180 ImplBase::cast_initialization_status_ = STATUS_VIDEO_INITIALIZED; | 179 ImplBase::operational_status_ = STATUS_INITIALIZED; |
181 } | 180 } |
182 | 181 |
183 private: | 182 private: |
184 ~FakeImpl() override {} | 183 ~FakeImpl() override {} |
185 | 184 |
186 scoped_refptr<VideoFrame> Decode(uint8* data, int len) override { | 185 scoped_refptr<VideoFrame> Decode(uint8* data, int len) override { |
187 // Make sure this is a JSON string. | 186 // Make sure this is a JSON string. |
188 if (!len || data[0] != '{') | 187 if (!len || data[0] != '{') |
189 return NULL; | 188 return NULL; |
190 base::JSONReader reader; | 189 base::JSONReader reader; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 NOTIMPLEMENTED(); | 229 NOTIMPLEMENTED(); |
231 break; | 230 break; |
232 default: | 231 default: |
233 NOTREACHED() << "Unknown or unspecified codec."; | 232 NOTREACHED() << "Unknown or unspecified codec."; |
234 break; | 233 break; |
235 } | 234 } |
236 } | 235 } |
237 | 236 |
238 VideoDecoder::~VideoDecoder() {} | 237 VideoDecoder::~VideoDecoder() {} |
239 | 238 |
240 CastInitializationStatus VideoDecoder::InitializationResult() const { | 239 OperationalStatus VideoDecoder::InitializationResult() const { |
241 if (impl_.get()) | 240 if (impl_.get()) |
242 return impl_->InitializationResult(); | 241 return impl_->InitializationResult(); |
243 return STATUS_UNSUPPORTED_VIDEO_CODEC; | 242 return STATUS_UNSUPPORTED_CODEC; |
244 } | 243 } |
245 | 244 |
246 void VideoDecoder::DecodeFrame( | 245 void VideoDecoder::DecodeFrame( |
247 scoped_ptr<EncodedFrame> encoded_frame, | 246 scoped_ptr<EncodedFrame> encoded_frame, |
248 const DecodeFrameCallback& callback) { | 247 const DecodeFrameCallback& callback) { |
249 DCHECK(encoded_frame.get()); | 248 DCHECK(encoded_frame.get()); |
250 DCHECK(!callback.is_null()); | 249 DCHECK(!callback.is_null()); |
251 if (!impl_.get() || | 250 if (!impl_.get() || impl_->InitializationResult() != STATUS_INITIALIZED) { |
252 impl_->InitializationResult() != STATUS_VIDEO_INITIALIZED) { | |
253 callback.Run(make_scoped_refptr<VideoFrame>(NULL), false); | 251 callback.Run(make_scoped_refptr<VideoFrame>(NULL), false); |
254 return; | 252 return; |
255 } | 253 } |
256 cast_environment_->PostTask(CastEnvironment::VIDEO, | 254 cast_environment_->PostTask(CastEnvironment::VIDEO, |
257 FROM_HERE, | 255 FROM_HERE, |
258 base::Bind(&VideoDecoder::ImplBase::DecodeFrame, | 256 base::Bind(&VideoDecoder::ImplBase::DecodeFrame, |
259 impl_, | 257 impl_, |
260 base::Passed(&encoded_frame), | 258 base::Passed(&encoded_frame), |
261 callback)); | 259 callback)); |
262 } | 260 } |
263 | 261 |
264 } // namespace cast | 262 } // namespace cast |
265 } // namespace media | 263 } // namespace media |
OLD | NEW |