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_factory_tv.h" | |
6 | |
7 #include "base/callback_helpers.h" | |
8 #include "content/renderer/media/rtc_video_decoder_bridge_tv.h" | |
9 #include "media/base/bind_to_loop.h" | |
10 #include "media/base/decoder_buffer.h" | |
11 #include "third_party/libjingle/source/talk/base/ratetracker.h" | |
12 | |
13 using media::DemuxerStream; | |
14 | |
15 namespace content { | |
16 | |
17 // RTCDemuxerStream ------------------------------------------------------------ | |
18 | |
19 class RTCDemuxerStream : public DemuxerStream { | |
20 public: | |
21 explicit RTCDemuxerStream(const gfx::Size& size); | |
22 virtual ~RTCDemuxerStream(); | |
23 | |
24 // DemuxerStream implementation. | |
25 virtual void Read(const ReadCB& read_cb) OVERRIDE; | |
26 virtual const media::AudioDecoderConfig& audio_decoder_config() OVERRIDE; | |
27 virtual const media::VideoDecoderConfig& video_decoder_config() OVERRIDE; | |
28 virtual Type type() OVERRIDE; | |
29 virtual void EnableBitstreamConverter() OVERRIDE; | |
30 | |
31 void QueueBuffer(scoped_refptr<media::DecoderBuffer> buffer, | |
32 const base::Closure& done_cb, | |
33 const gfx::Size& new_size); | |
34 void Destroy(); | |
35 | |
36 private: | |
37 struct BufferEntry { | |
38 BufferEntry(const scoped_refptr<media::DecoderBuffer>& decoder_buffer_param, | |
39 const base::Closure& done_cb_param, | |
40 const gfx::Size& new_size_param) | |
41 : decoder_buffer(decoder_buffer_param), | |
42 done_cb(done_cb_param), | |
43 new_size(new_size_param) {} | |
44 | |
45 scoped_refptr<media::DecoderBuffer> decoder_buffer; | |
46 base::Closure done_cb; | |
47 // When |!new_size.isEmpty()|, it means that config change with new size | |
48 // |new_size| happened. | |
49 gfx::Size new_size; | |
50 }; | |
51 | |
52 void RunReadCallback_Locked(); | |
53 | |
54 base::Lock lock_; | |
55 bool is_destroyed_; | |
56 std::queue<BufferEntry> buffer_queue_; | |
57 ReadCB read_cb_; | |
58 base::Closure pending_done_cb_; | |
59 | |
60 media::AudioDecoderConfig dummy_audio_decoder_config_; | |
61 media::VideoDecoderConfig video_decoder_config_; | |
62 talk_base::RateTracker frame_rate_tracker_; | |
63 }; | |
64 | |
65 RTCDemuxerStream::RTCDemuxerStream(const gfx::Size& size) | |
66 : is_destroyed_(false), | |
67 video_decoder_config_(media::kCodecVP8, | |
68 media::VP8PROFILE_MAIN, | |
69 media::VideoFrame::NATIVE_TEXTURE, | |
70 size, | |
71 gfx::Rect(size), | |
72 size, | |
73 NULL, | |
74 0, | |
75 false) {} | |
76 | |
77 RTCDemuxerStream::~RTCDemuxerStream() { CHECK(is_destroyed_); } | |
78 | |
79 const media::AudioDecoderConfig& RTCDemuxerStream::audio_decoder_config() { | |
80 LOG(FATAL) << "Does not support audio."; | |
81 return dummy_audio_decoder_config_; | |
82 } | |
83 | |
84 const media::VideoDecoderConfig& RTCDemuxerStream::video_decoder_config() { | |
85 base::AutoLock lock(lock_); | |
ycheo (away)
2013/05/14 13:28:09
Do you need this?
wonsik
2013/05/14 14:18:55
I think so, since video_decoder_config_ may change
| |
86 return video_decoder_config_; | |
87 } | |
88 | |
89 DemuxerStream::Type RTCDemuxerStream::type() { return DemuxerStream::VIDEO; } | |
90 | |
91 void RTCDemuxerStream::EnableBitstreamConverter() { | |
92 LOG(FATAL) << "Not reachable."; | |
93 } | |
94 | |
95 void RTCDemuxerStream::QueueBuffer(scoped_refptr<media::DecoderBuffer> buffer, | |
96 const base::Closure& done_cb, | |
97 const gfx::Size& new_size) { | |
98 base::AutoLock lock(lock_); | |
99 if (is_destroyed_) | |
100 return; | |
101 buffer_queue_.push(BufferEntry(buffer, done_cb, new_size)); | |
102 if (buffer) | |
103 frame_rate_tracker_.Update(1); | |
104 DLOG(INFO) << "frame rate received : " << frame_rate_tracker_.units_second(); | |
105 RunReadCallback_Locked(); | |
106 } | |
107 | |
108 void RTCDemuxerStream::Read(const ReadCB& read_cb) { | |
109 base::AutoLock lock(lock_); | |
110 CHECK(read_cb_.is_null()); | |
111 // A call to |Read| operation means that |MediaSourceDelegate| is done with | |
112 // the previous buffer. | |
113 if (!pending_done_cb_.is_null()) | |
114 base::ResetAndReturn(&pending_done_cb_).Run(); | |
115 read_cb_ = media::BindToLoop(base::MessageLoopProxy::current(), read_cb); | |
116 RunReadCallback_Locked(); | |
117 } | |
118 | |
119 void RTCDemuxerStream::Destroy() { | |
120 base::AutoLock lock(lock_); | |
121 CHECK(!is_destroyed_); | |
122 is_destroyed_ = true; | |
123 if (!read_cb_.is_null()) | |
124 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kAborted, NULL); | |
125 } | |
126 | |
127 void RTCDemuxerStream::RunReadCallback_Locked() { | |
128 if (read_cb_.is_null() || buffer_queue_.empty()) | |
129 return; | |
130 | |
131 BufferEntry& front = buffer_queue_.front(); | |
132 if (!front.new_size.IsEmpty()) { | |
133 // No VideoFrame actually reaches cc in Google TV case. We just make | |
134 // coded_size == visible_rect == natural_size here. | |
135 video_decoder_config_.Initialize(media::kCodecVP8, | |
136 media::VP8PROFILE_MAIN, | |
137 media::VideoFrame::NATIVE_TEXTURE, | |
138 front.new_size, | |
139 gfx::Rect(front.new_size), | |
140 front.new_size, | |
141 NULL, | |
142 0, | |
143 false, | |
144 false); | |
145 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kConfigChanged, NULL); | |
146 front.new_size.SetSize(0, 0); | |
147 return; | |
148 } | |
149 DCHECK(pending_done_cb_.is_null()); | |
150 pending_done_cb_ = front.done_cb; | |
151 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk, front.decoder_buffer); | |
152 buffer_queue_.pop(); | |
153 } | |
154 | |
155 // RTCVideoDecoderFactoryTv ---------------------------------------------------- | |
156 | |
157 RTCVideoDecoderFactoryTv::RTCVideoDecoderFactoryTv() {} | |
158 RTCVideoDecoderFactoryTv::~RTCVideoDecoderFactoryTv() {} | |
159 | |
160 webrtc::VideoDecoder* RTCVideoDecoderFactoryTv::CreateVideoDecoder( | |
161 webrtc::VideoCodecType type) { | |
162 base::AutoLock lock(lock_); | |
163 // One decoder at a time! | |
164 if (decoder_) | |
165 return NULL; | |
166 if (type == webrtc::kVideoCodecVP8) { | |
167 decoder_.reset(new RTCVideoDecoderBridgeTv(this)); | |
168 return decoder_.get(); | |
169 } | |
170 // returning NULL will make WebRTC fall back to SW decoder. | |
171 return NULL; | |
172 } | |
173 | |
174 void RTCVideoDecoderFactoryTv::DestroyVideoDecoder( | |
175 webrtc::VideoDecoder* decoder) { | |
176 base::AutoLock lock(lock_); | |
177 CHECK(decoder_.get() == decoder); | |
178 decoder_.reset(); | |
179 } | |
180 | |
181 bool RTCVideoDecoderFactoryTv::AcquireDemuxer() { | |
182 base::AutoLock lock(lock_); | |
183 if (is_acquired_) | |
184 return false; | |
185 is_acquired_ = true; | |
186 return true; | |
187 } | |
188 | |
189 void RTCVideoDecoderFactoryTv::ReleaseDemuxer() { | |
190 base::AutoLock lock(lock_); | |
191 CHECK(is_acquired_); | |
192 is_acquired_ = false; | |
193 // Clean up internal state as a demuxer. | |
194 init_cb_.Reset(); | |
195 if (stream_) { | |
196 stream_->Destroy(); | |
197 stream_.reset(); | |
198 } | |
199 } | |
200 | |
201 void RTCVideoDecoderFactoryTv::Initialize(media::DemuxerHost*, | |
202 const media::PipelineStatusCB& cb) { | |
203 base::AutoLock lock(lock_); | |
204 init_cb_ = cb; | |
205 if (!stream_) | |
206 base::ResetAndReturn(&init_cb_).Run(media::PIPELINE_OK); | |
207 } | |
208 | |
209 DemuxerStream* RTCVideoDecoderFactoryTv::GetStream(DemuxerStream::Type type) { | |
dwkang1
2013/05/14 13:24:07
Same question for this. Can we say this will be ca
wonsik
2013/05/14 14:18:55
Yes. GetStream will only be called after init_cb_
| |
210 base::AutoLock lock(lock_); | |
211 if (type == DemuxerStream::VIDEO) | |
212 return stream_.get(); | |
213 return NULL; | |
214 } | |
215 | |
216 base::TimeDelta RTCVideoDecoderFactoryTv::GetStartTime() const { | |
217 return base::TimeDelta(); | |
218 } | |
219 | |
220 void RTCVideoDecoderFactoryTv::InitializeStream(const gfx::Size& size) { | |
221 base::AutoLock lock(lock_); | |
222 CHECK(!stream_); | |
223 stream_.reset(new RTCDemuxerStream(size)); | |
224 if (!init_cb_.is_null()) | |
225 base::ResetAndReturn(&init_cb_).Run(media::PIPELINE_OK); | |
226 } | |
227 | |
228 void RTCVideoDecoderFactoryTv::QueueBuffer( | |
229 scoped_refptr<media::DecoderBuffer> buffer, | |
230 const base::Closure& done_cb, | |
231 const gfx::Size& new_size) { | |
232 base::AutoLock lock(lock_); | |
233 CHECK(stream_); | |
234 stream_->QueueBuffer(buffer, done_cb, new_size); | |
235 } | |
236 | |
237 } // namespace content | |
OLD | NEW |