OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "content/renderer/media/rtc_video_decoder_bridge_tv.h" | |
6 | |
7 #include <queue> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/callback_helpers.h" | |
11 #include "base/location.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/singleton.h" | |
15 #include "base/message_loop_proxy.h" | |
16 #include "base/time.h" | |
17 #include "content/renderer/media/rtc_video_decoder_factory_tv.h" | |
18 #include "media/base/bind_to_loop.h" | |
19 #include "media/base/decoder_buffer.h" | |
20 #include "third_party/libjingle/source/talk/base/ratetracker.h" | |
21 | |
22 namespace content { | |
23 | |
24 RTCVideoDecoderBridgeTv::RTCVideoDecoderBridgeTv( | |
25 RTCVideoDecoderFactoryTv* factory) | |
26 : factory_(factory), | |
27 is_initialized_(false), | |
28 first_frame_(true), | |
29 decode_complete_callback_(NULL) {} | |
30 | |
31 RTCVideoDecoderBridgeTv::~RTCVideoDecoderBridgeTv() {} | |
32 | |
33 WebRtc_Word32 RTCVideoDecoderBridgeTv::InitDecode( | |
34 const webrtc::VideoCodec* codecSettings, | |
35 WebRtc_Word32 numberOfCores) { | |
36 if (codecSettings->codecType != webrtc::kVideoCodecVP8) | |
37 return WEBRTC_VIDEO_CODEC_ERROR; | |
38 // We don't support feedback mode. | |
39 if (codecSettings->codecSpecific.VP8.feedbackModeOn) | |
40 return WEBRTC_VIDEO_CODEC_ERROR; | |
41 | |
42 if (is_initialized_) | |
43 return WEBRTC_VIDEO_CODEC_ERROR; | |
44 size_ = gfx::Size(codecSettings->width, codecSettings->height); | |
45 is_initialized_ = true; | |
46 first_frame_ = true; | |
47 factory_->UpdateSize(size_); | |
48 | |
49 return WEBRTC_VIDEO_CODEC_OK; | |
50 } | |
51 | |
52 WebRtc_Word32 RTCVideoDecoderBridgeTv::Decode( | |
53 const webrtc::EncodedImage& inputImage, | |
54 bool missingFrames, | |
55 const webrtc::RTPFragmentationHeader* /* fragmentation */, | |
56 const webrtc::CodecSpecificInfo* /* codecSpecificInfo */, | |
57 WebRtc_Word64 renderTimeMs) { | |
58 // Unlike the SW decoder in libvpx, hw decoder can not handle broken frames. | |
59 // Here, we return an error in order to request a key frame. | |
60 if (missingFrames || !inputImage._completeFrame) | |
61 return WEBRTC_VIDEO_CODEC_ERROR; | |
62 | |
63 if (!is_initialized_ || decode_complete_callback_ == NULL) | |
64 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | |
65 | |
66 if (first_frame_) { | |
67 // If the first frame is not a key frame, return an error to request a key | |
68 // frame. | |
69 if (inputImage._frameType != webrtc::kKeyFrame) | |
70 return WEBRTC_VIDEO_CODEC_ERROR; | |
71 | |
72 // Google TV expects timestamp from 0, so we store the initial timestamp as | |
73 // an offset and subtract the value from every timestamps to meet the | |
74 // expectation. | |
75 timestamp_offset_millis_ = renderTimeMs; | |
76 } | |
77 first_frame_ = false; | |
78 gfx::Size new_size; | |
79 if (inputImage._frameType == webrtc::kKeyFrame && | |
80 inputImage._encodedWidth != 0 && inputImage._encodedHeight != 0) { | |
81 // Only a key frame has a meaningful size. | |
82 new_size.SetSize(inputImage._encodedWidth, inputImage._encodedHeight); | |
83 if (size_ == new_size) | |
84 new_size = gfx::Size(); | |
85 else | |
86 size_ = new_size; | |
87 } | |
88 // |inputImage| may be destroyed after this call, so we make a copy of the | |
dwkang1
2013/05/14 07:57:48
s/inputImage/inputImage_/
wonsik
2013/05/14 12:53:57
Done.
| |
89 // buffer so that we can queue the buffer asynchronously. | |
90 scoped_refptr<media::DecoderBuffer> buffer = | |
91 media::DecoderBuffer::CopyFrom(inputImage._buffer, inputImage._length); | |
92 if (renderTimeMs != -1) { | |
93 buffer->SetTimestamp(base::TimeDelta::FromMilliseconds( | |
94 renderTimeMs - timestamp_offset_millis_)); | |
95 } | |
96 | |
97 factory_->QueueBuffer( | |
98 buffer, | |
99 base::Bind(&RTCVideoDecoderBridgeTv::RunDecodeCompleteCallback, | |
100 decode_complete_callback_, | |
101 renderTimeMs), | |
dwkang1
2013/05/14 07:57:48
As discussed, webrtc expects inputImage._timeStamp
wonsik
2013/05/14 12:53:57
Done.
| |
102 new_size); | |
103 | |
104 return WEBRTC_VIDEO_CODEC_OK; | |
105 } | |
106 | |
107 WebRtc_Word32 RTCVideoDecoderBridgeTv::RegisterDecodeCompleteCallback( | |
108 webrtc::DecodedImageCallback* callback) { | |
109 decode_complete_callback_ = callback; | |
110 return WEBRTC_VIDEO_CODEC_OK; | |
111 } | |
112 | |
113 WebRtc_Word32 RTCVideoDecoderBridgeTv::Release() { | |
114 is_initialized_ = false; | |
115 return WEBRTC_VIDEO_CODEC_OK; | |
116 } | |
117 | |
118 WebRtc_Word32 RTCVideoDecoderBridgeTv::Reset() { | |
119 first_frame_ = true; | |
120 return WEBRTC_VIDEO_CODEC_OK; | |
121 } | |
122 | |
123 // static | |
124 void RTCVideoDecoderBridgeTv::RunDecodeCompleteCallback( | |
125 webrtc::DecodedImageCallback* callback, | |
126 WebRtc_Word64 timestamp) { | |
127 // We call the decode complete callback function to notify libjingle that | |
128 // decoding is finished. | |
129 webrtc::I420VideoFrame dummy_video_frame; | |
130 // Smallest possible non-zero I420 image is 2x2 square, with stride_y being 2 | |
131 // and stride_u & stride_v being 1. In other words, this dummy frame contains | |
132 // 2x2 y values and 1x1 u & v values each. | |
133 dummy_video_frame.CreateEmptyFrame(2, 2, 2, 1, 1); | |
134 dummy_video_frame.set_timestamp(timestamp); | |
135 callback->Decoded(dummy_video_frame); | |
dwkang1
2013/05/14 07:57:48
Could you add a doc which says this is needed for
wonsik
2013/05/14 12:53:57
Done.
| |
136 } | |
137 | |
138 } // namespace content | |
OLD | NEW |