OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "chrome/renderer/media/ipc_video_decoder.h" | |
6 | |
7 #include "base/task.h" | |
8 #include "chrome/renderer/ggl/ggl.h" | |
9 #include "content/common/child_process.h" | |
10 #include "media/base/callback.h" | |
11 #include "media/base/filters.h" | |
12 #include "media/base/filter_host.h" | |
13 #include "media/base/limits.h" | |
14 #include "media/base/media_format.h" | |
15 #include "media/base/video_frame.h" | |
16 #include "media/ffmpeg/ffmpeg_common.h" | |
17 #include "media/filters/ffmpeg_interfaces.h" | |
18 #include "media/video/video_decode_engine.h" | |
19 | |
20 IpcVideoDecoder::IpcVideoDecoder(MessageLoop* message_loop, | |
21 ggl::Context* ggl_context) | |
22 : decode_context_message_loop_(message_loop), | |
23 ggl_context_(ggl_context) { | |
24 } | |
25 | |
26 IpcVideoDecoder::~IpcVideoDecoder() { | |
27 } | |
28 | |
29 void IpcVideoDecoder::Initialize(media::DemuxerStream* demuxer_stream, | |
30 media::FilterCallback* callback, | |
31 media::StatisticsCallback* statsCallback) { | |
32 // It doesn't matter which thread we perform initialization because | |
33 // all this method does is create objects and delegate the initialize | |
34 // messsage. | |
35 | |
36 DCHECK(!demuxer_stream_); | |
37 demuxer_stream_ = demuxer_stream; | |
38 initialize_callback_.reset(callback); | |
39 statistics_callback_.reset(statsCallback); | |
40 | |
41 // We require bit stream converter for hardware decoder. | |
42 demuxer_stream->EnableBitstreamConverter(); | |
43 | |
44 // Get the AVStream by querying for the provider interface. | |
45 media::AVStreamProvider* av_stream_provider; | |
46 if (!demuxer_stream->QueryInterface(&av_stream_provider)) { | |
47 media::VideoCodecInfo info = {0}; | |
48 OnInitializeComplete(info); | |
49 return; | |
50 } | |
51 | |
52 AVStream* av_stream = av_stream_provider->GetAVStream(); | |
53 | |
54 int width = av_stream->codec->coded_width; | |
55 int height = av_stream->codec->coded_height; | |
56 if (width > media::Limits::kMaxDimension || | |
57 height > media::Limits::kMaxDimension || | |
58 (width * height) > media::Limits::kMaxCanvas) { | |
59 media::VideoCodecInfo info = {0}; | |
60 OnInitializeComplete(info); | |
61 return; | |
62 } | |
63 | |
64 // Create a video decode context that assocates with the graphics | |
65 // context. | |
66 decode_context_.reset( | |
67 ggl::CreateVideoDecodeContext( | |
68 ggl_context_, decode_context_message_loop_, true)); | |
69 | |
70 // Create a hardware video decoder handle. | |
71 decode_engine_.reset(ggl::CreateVideoDecodeEngine(ggl_context_)); | |
72 | |
73 media::VideoCodecConfig config( | |
74 media::CodecIDToVideoCodec(av_stream->codec->codec_id), | |
75 width, height, | |
76 av_stream->r_frame_rate.num, | |
77 av_stream->r_frame_rate.den, | |
78 av_stream->codec->extradata, | |
79 av_stream->codec->extradata_size); | |
80 | |
81 // VideoDecodeEngine will perform initialization on the message loop | |
82 // given to it so it doesn't matter on which thread we are calling this. | |
83 decode_engine_->Initialize(ChildProcess::current()->io_message_loop(), this, | |
84 decode_context_.get(), config); | |
85 } | |
86 | |
87 const media::MediaFormat& IpcVideoDecoder::media_format() { | |
88 return media_format_; | |
89 } | |
90 | |
91 void IpcVideoDecoder::Stop(media::FilterCallback* callback) { | |
92 stop_callback_.reset(callback); | |
93 decode_engine_->Uninitialize(); | |
94 } | |
95 | |
96 void IpcVideoDecoder::Pause(media::FilterCallback* callback) { | |
97 // TODO(hclam): It looks like that pause is not necessary so implement this | |
98 // later. | |
99 callback->Run(); | |
100 delete callback; | |
101 } | |
102 | |
103 void IpcVideoDecoder::Flush(media::FilterCallback* callback) { | |
104 flush_callback_.reset(callback); | |
105 decode_engine_->Flush(); | |
106 } | |
107 | |
108 void IpcVideoDecoder::Seek(base::TimeDelta time, | |
109 media::FilterCallback* callback) { | |
110 seek_callback_.reset(callback); | |
111 decode_engine_->Seek(); | |
112 } | |
113 | |
114 void IpcVideoDecoder::OnInitializeComplete(const media::VideoCodecInfo& info) { | |
115 DCHECK_EQ(ChildProcess::current()->io_message_loop(), MessageLoop::current()); | |
116 | |
117 if (info.success) { | |
118 media_format_.SetAsInteger(media::MediaFormat::kSurfaceType, | |
119 media::VideoFrame::TYPE_GL_TEXTURE); | |
120 media_format_.SetAsInteger(media::MediaFormat::kSurfaceFormat, | |
121 info.stream_info.surface_format); | |
122 media_format_.SetAsInteger(media::MediaFormat::kWidth, | |
123 info.stream_info.surface_width); | |
124 media_format_.SetAsInteger(media::MediaFormat::kHeight, | |
125 info.stream_info.surface_height); | |
126 media_format_.SetAsInteger( | |
127 media::MediaFormat::kSurfaceType, | |
128 static_cast<int>(media::VideoFrame::TYPE_GL_TEXTURE)); | |
129 } else { | |
130 LOG(ERROR) << "IpcVideoDecoder initialization failed!"; | |
131 host()->SetError(media::PIPELINE_ERROR_DECODE); | |
132 } | |
133 | |
134 initialize_callback_->Run(); | |
135 initialize_callback_.reset(); | |
136 } | |
137 | |
138 void IpcVideoDecoder::OnUninitializeComplete() { | |
139 DCHECK_EQ(ChildProcess::current()->io_message_loop(), MessageLoop::current()); | |
140 | |
141 // After the decode engine is uninitialized we are safe to destroy the decode | |
142 // context. The task will add a refcount to this object so don't need to worry | |
143 // about objects lifetime. | |
144 decode_context_->Destroy( | |
145 NewRunnableMethod(this, &IpcVideoDecoder::OnDestroyComplete)); | |
146 | |
147 // We don't need to wait for destruction of decode context to complete because | |
148 // it can happen asynchronously. This object and decode context will live | |
149 // until the destruction task is called. | |
150 stop_callback_->Run(); | |
151 stop_callback_.reset(); | |
152 } | |
153 | |
154 void IpcVideoDecoder::OnFlushComplete() { | |
155 DCHECK_EQ(ChildProcess::current()->io_message_loop(), MessageLoop::current()); | |
156 flush_callback_->Run(); | |
157 flush_callback_.reset(); | |
158 } | |
159 | |
160 void IpcVideoDecoder::OnSeekComplete() { | |
161 DCHECK_EQ(ChildProcess::current()->io_message_loop(), MessageLoop::current()); | |
162 seek_callback_->Run(); | |
163 seek_callback_.reset(); | |
164 } | |
165 | |
166 void IpcVideoDecoder::OnError() { | |
167 DCHECK_EQ(ChildProcess::current()->io_message_loop(), MessageLoop::current()); | |
168 host()->SetError(media::PIPELINE_ERROR_DECODE); | |
169 } | |
170 | |
171 // This methid is called by Demuxer after a demuxed packet is produced. | |
172 void IpcVideoDecoder::OnReadComplete(media::Buffer* buffer) { | |
173 decode_engine_->ConsumeVideoSample(buffer); | |
174 } | |
175 | |
176 void IpcVideoDecoder::OnDestroyComplete() { | |
177 // We don't need to do anything in this method. Destruction of objects will | |
178 // occur as soon as refcount goes to 0. | |
179 } | |
180 | |
181 // This method is called by VideoRenderer. We delegate the method call to | |
182 // VideoDecodeEngine. | |
183 void IpcVideoDecoder::ProduceVideoFrame( | |
184 scoped_refptr<media::VideoFrame> video_frame) { | |
185 decode_engine_->ProduceVideoFrame(video_frame); | |
186 } | |
187 | |
188 bool IpcVideoDecoder::ProvidesBuffer() { | |
189 return true; | |
190 } | |
191 | |
192 // This method is called by VideoDecodeEngine that a video frame is produced. | |
193 // This is then passed to VideoRenderer. | |
194 void IpcVideoDecoder::ConsumeVideoFrame( | |
195 scoped_refptr<media::VideoFrame> video_frame, | |
196 const media::PipelineStatistics& statistics) { | |
197 DCHECK(video_frame); | |
198 statistics_callback_->Run(statistics); | |
199 | |
200 VideoFrameReady(video_frame); | |
201 } | |
202 | |
203 // This method is called by VideoDecodeEngine to request a video frame. The | |
204 // request is passed to demuxer. | |
205 void IpcVideoDecoder::ProduceVideoSample(scoped_refptr<media::Buffer> buffer) { | |
206 demuxer_stream_->Read(NewCallback(this, &IpcVideoDecoder::OnReadComplete)); | |
207 } | |
OLD | NEW |