OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/renderer/media/video_destination_handler.h" | 5 #include "content/renderer/media/video_destination_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "content/renderer/media/media_stream_dependency_factory.h" | 10 #include "content/renderer/media/media_stream_dependency_factory.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // This assumes the handler of the SignalFrameCaptured won't call Start/Stop. | 87 // This assumes the handler of the SignalFrameCaptured won't call Start/Stop. |
88 // TODO(ronghuawu): Avoid the using of lock. One way is to post this call to | 88 // TODO(ronghuawu): Avoid the using of lock. One way is to post this call to |
89 // libjingle worker thread, which will require an extra copy of |image_data|. | 89 // libjingle worker thread, which will require an extra copy of |image_data|. |
90 // However if pepper host can hand over the ownership of |image_data| | 90 // However if pepper host can hand over the ownership of |image_data| |
91 // then we can avoid this extra copy. | 91 // then we can avoid this extra copy. |
92 if (!started_) { | 92 if (!started_) { |
93 LOG(ERROR) << "PpFrameWriter::PutFrame - " | 93 LOG(ERROR) << "PpFrameWriter::PutFrame - " |
94 << "Called when capturer is not started."; | 94 << "Called when capturer is not started."; |
95 return; | 95 return; |
96 } | 96 } |
97 ASSERT(image_data != NULL); | 97 if (!image_data) { |
| 98 LOG(ERROR) << "PpFrameWriter::PutFrame - Called with NULL image_data."; |
| 99 return; |
| 100 } |
98 webkit::ppapi::ImageDataAutoMapper mapper(image_data); | 101 webkit::ppapi::ImageDataAutoMapper mapper(image_data); |
99 if (!mapper.is_valid()) { | 102 if (!mapper.is_valid()) { |
100 LOG(ERROR) << "PpFrameWriter::PutFrame - " | 103 LOG(ERROR) << "PpFrameWriter::PutFrame - " |
101 << "The image could not be mapped and is unusable."; | 104 << "The image could not be mapped and is unusable."; |
102 return; | 105 return; |
103 } | 106 } |
104 const SkBitmap* bitmap = image_data->GetMappedBitmap(); | 107 const SkBitmap* bitmap = image_data->GetMappedBitmap(); |
105 ASSERT(bitmap != NULL); | 108 if (!bitmap) { |
| 109 LOG(ERROR) << "PpFrameWriter::PutFrame - " |
| 110 << "The image_data's mapped bitmap is NULL."; |
| 111 return; |
| 112 } |
106 | 113 |
107 cricket::CapturedFrame frame; | 114 cricket::CapturedFrame frame; |
108 frame.elapsed_time = 0; | 115 frame.elapsed_time = 0; |
109 frame.time_stamp = time_stamp_ns; | 116 frame.time_stamp = time_stamp_ns; |
110 frame.pixel_height = 1; | 117 frame.pixel_height = 1; |
111 frame.pixel_width = 1; | 118 frame.pixel_width = 1; |
112 frame.width = bitmap->width(); | 119 frame.width = bitmap->width(); |
113 frame.height = bitmap->height(); | 120 frame.height = bitmap->height(); |
114 if (image_data->format() == PP_IMAGEDATAFORMAT_BGRA_PREMUL) { | 121 if (image_data->format() == PP_IMAGEDATAFORMAT_BGRA_PREMUL) { |
115 frame.fourcc = cricket::FOURCC_BGRA; | 122 frame.fourcc = cricket::FOURCC_BGRA; |
116 } else { | 123 } else { |
117 LOG(ERROR) << "PpFrameWriter::PutFrame - Got RGBA which is not supported."; | 124 LOG(ERROR) << "PpFrameWriter::PutFrame - Got RGBA which is not supported."; |
118 ASSERT(false); | |
119 return; | 125 return; |
120 } | 126 } |
121 frame.data_size = bitmap->getSize(); | 127 frame.data_size = bitmap->getSize(); |
122 frame.data = bitmap->getPixels(); | 128 frame.data = bitmap->getPixels(); |
123 | 129 |
124 // This signals to libJingle that a new VideoFrame is available. | 130 // This signals to libJingle that a new VideoFrame is available. |
125 // libJingle have no assumptions on what thread this signal come from. | 131 // libJingle have no assumptions on what thread this signal come from. |
126 SignalFrameCaptured(this, &frame); | 132 SignalFrameCaptured(this, &frame); |
127 } | 133 } |
128 | 134 |
129 // PpFrameWriterProxy is a helper class to make sure the user won't use | 135 // PpFrameWriterProxy is a helper class to make sure the user won't use |
130 // PpFrameWriter after it is released (IOW its owner - WebMediaStreamTrack - | 136 // PpFrameWriter after it is released (IOW its owner - WebMediaStreamTrack - |
131 // is released). | 137 // is released). |
132 class PpFrameWriterProxy : public FrameWriterInterface { | 138 class PpFrameWriterProxy : public FrameWriterInterface { |
133 public: | 139 public: |
134 PpFrameWriterProxy(VideoTrackInterface* track, | 140 PpFrameWriterProxy(VideoTrackInterface* track, |
135 PpFrameWriter* writer) | 141 PpFrameWriter* writer) |
136 : track_(track), | 142 : track_(track), |
137 writer_(writer) { | 143 writer_(writer) { |
138 ASSERT(writer_ != NULL); | 144 DCHECK(writer_ != NULL); |
139 } | 145 } |
140 | 146 |
141 virtual ~PpFrameWriterProxy() {} | 147 virtual ~PpFrameWriterProxy() {} |
142 | 148 |
143 virtual void PutFrame(webkit::ppapi::PPB_ImageData_Impl* image_data, | 149 virtual void PutFrame(webkit::ppapi::PPB_ImageData_Impl* image_data, |
144 int64 time_stamp_ns) OVERRIDE { | 150 int64 time_stamp_ns) OVERRIDE { |
145 writer_->PutFrame(image_data, time_stamp_ns); | 151 writer_->PutFrame(image_data, time_stamp_ns); |
146 } | 152 } |
147 | 153 |
148 private: | 154 private: |
149 scoped_refptr<VideoTrackInterface> track_; | 155 scoped_refptr<VideoTrackInterface> track_; |
150 PpFrameWriter* writer_; | 156 PpFrameWriter* writer_; |
151 | 157 |
152 DISALLOW_COPY_AND_ASSIGN(PpFrameWriterProxy); | 158 DISALLOW_COPY_AND_ASSIGN(PpFrameWriterProxy); |
153 }; | 159 }; |
154 | 160 |
155 bool VideoDestinationHandler::Open( | 161 bool VideoDestinationHandler::Open( |
156 MediaStreamDependencyFactory* factory, | 162 MediaStreamDependencyFactory* factory, |
157 MediaStreamRegistryInterface* registry, | 163 MediaStreamRegistryInterface* registry, |
158 const std::string& url, | 164 const std::string& url, |
159 FrameWriterInterface** frame_writer) { | 165 FrameWriterInterface** frame_writer) { |
160 if (!factory) { | 166 if (!factory) { |
161 factory = RenderThreadImpl::current()->GetMediaStreamDependencyFactory(); | 167 factory = RenderThreadImpl::current()->GetMediaStreamDependencyFactory(); |
162 ASSERT(factory != NULL); | 168 DCHECK(factory != NULL); |
163 } | 169 } |
164 WebKit::WebMediaStream stream; | 170 WebKit::WebMediaStream stream; |
165 if (registry) { | 171 if (registry) { |
166 stream = registry->GetMediaStream(url); | 172 stream = registry->GetMediaStream(url); |
167 } else { | 173 } else { |
168 stream = | 174 stream = |
169 WebKit::WebMediaStreamRegistry::lookupMediaStreamDescriptor(GURL(url)); | 175 WebKit::WebMediaStreamRegistry::lookupMediaStreamDescriptor(GURL(url)); |
170 } | 176 } |
171 if (stream.isNull() || !stream.extraData()) { | 177 if (stream.isNull() || !stream.extraData()) { |
172 LOG(ERROR) << "VideoDestinationHandler::Open - invalid url: " << url; | 178 LOG(ERROR) << "VideoDestinationHandler::Open - invalid url: " << url; |
173 return false; | 179 return false; |
174 } | 180 } |
175 | 181 |
176 // Create a new native video track and add it to |stream|. | 182 // Create a new native video track and add it to |stream|. |
177 std::string track_id = talk_base::ToString(talk_base::CreateRandomId64()); | 183 std::string track_id = talk_base::ToString(talk_base::CreateRandomId64()); |
178 PpFrameWriter* writer = new PpFrameWriter(); | 184 PpFrameWriter* writer = new PpFrameWriter(); |
179 if (!factory->AddNativeVideoMediaTrack(track_id, &stream, writer)) { | 185 if (!factory->AddNativeVideoMediaTrack(track_id, &stream, writer)) { |
180 delete writer; | 186 delete writer; |
181 return false; | 187 return false; |
182 } | 188 } |
183 | 189 |
184 // Gets a handler to the native video track, which owns the |writer|. | 190 // Gets a handler to the native video track, which owns the |writer|. |
185 MediaStreamExtraData* extra_data = | 191 MediaStreamExtraData* extra_data = |
186 static_cast<MediaStreamExtraData*>(stream.extraData()); | 192 static_cast<MediaStreamExtraData*>(stream.extraData()); |
187 DCHECK(extra_data); | |
188 webrtc::MediaStreamInterface* native_stream = extra_data->stream(); | 193 webrtc::MediaStreamInterface* native_stream = extra_data->stream(); |
189 DCHECK(native_stream); | 194 DCHECK(native_stream); |
190 VideoTrackVector video_tracks = native_stream->GetVideoTracks(); | 195 VideoTrackVector video_tracks = native_stream->GetVideoTracks(); |
191 // Currently one supports one video track per media stream. | 196 // Currently one supports one video track per media stream. |
192 ASSERT(video_tracks.size() == 1); | 197 DCHECK(video_tracks.size() == 1); |
193 | 198 |
194 *frame_writer = new PpFrameWriterProxy(video_tracks[0].get(), writer); | 199 *frame_writer = new PpFrameWriterProxy(video_tracks[0].get(), writer); |
195 return true; | 200 return true; |
196 } | 201 } |
197 | 202 |
198 } // namespace content | 203 } // namespace content |
199 | 204 |
OLD | NEW |