| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/renderer/media/cast_rtp_stream.h" | 5 #include "chrome/renderer/media/cast_rtp_stream.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 // This class receives MediaStreamTrack events and video frames from a | 229 // This class receives MediaStreamTrack events and video frames from a |
| 230 // MediaStreamTrack. | 230 // MediaStreamTrack. |
| 231 // | 231 // |
| 232 // Threading: Video frames are received on the IO thread and then | 232 // Threading: Video frames are received on the IO thread and then |
| 233 // forwarded to media::cast::VideoFrameInput through a static method. | 233 // forwarded to media::cast::VideoFrameInput through a static method. |
| 234 // Member variables of this class are only accessed on the render thread. | 234 // Member variables of this class are only accessed on the render thread. |
| 235 class CastVideoSink : public base::SupportsWeakPtr<CastVideoSink>, | 235 class CastVideoSink : public base::SupportsWeakPtr<CastVideoSink>, |
| 236 public content::MediaStreamVideoSink { | 236 public content::MediaStreamVideoSink { |
| 237 public: | 237 public: |
| 238 // |track| provides data for this sink. | 238 // |track| provides data for this sink. |
| 239 // |expected_coded_size| is the expected dimension of the video frame. | 239 // |expected_natural_size| is the expected dimension of the video frame. |
| 240 // |error_callback| is called if video formats don't match. | 240 // |error_callback| is called if video formats don't match. |
| 241 CastVideoSink(const blink::WebMediaStreamTrack& track, | 241 CastVideoSink(const blink::WebMediaStreamTrack& track, |
| 242 const gfx::Size& expected_coded_size, | 242 const gfx::Size& expected_natural_size, |
| 243 const CastRtpStream::ErrorCallback& error_callback) | 243 const CastRtpStream::ErrorCallback& error_callback) |
| 244 : track_(track), | 244 : track_(track), |
| 245 sink_added_(false), | 245 sink_added_(false), |
| 246 expected_coded_size_(expected_coded_size), | 246 expected_natural_size_(expected_natural_size), |
| 247 error_callback_(error_callback) {} | 247 error_callback_(error_callback) {} |
| 248 | 248 |
| 249 virtual ~CastVideoSink() { | 249 virtual ~CastVideoSink() { |
| 250 if (sink_added_) | 250 if (sink_added_) |
| 251 RemoveFromVideoTrack(this, track_); | 251 RemoveFromVideoTrack(this, track_); |
| 252 } | 252 } |
| 253 | 253 |
| 254 // This static method is used to forward video frames to |frame_input|. | 254 // This static method is used to forward video frames to |frame_input|. |
| 255 static void OnVideoFrame( | 255 static void OnVideoFrame( |
| 256 // These parameters are already bound when callback is created. | 256 // These parameters are already bound when callback is created. |
| 257 const gfx::Size& expected_coded_size, | 257 const gfx::Size& expected_natural_size, |
| 258 const CastRtpStream::ErrorCallback& error_callback, | 258 const CastRtpStream::ErrorCallback& error_callback, |
| 259 const scoped_refptr<media::cast::VideoFrameInput> frame_input, | 259 const scoped_refptr<media::cast::VideoFrameInput> frame_input, |
| 260 // These parameters are passed for each frame. | 260 // These parameters are passed for each frame. |
| 261 const scoped_refptr<media::VideoFrame>& frame, | 261 const scoped_refptr<media::VideoFrame>& frame, |
| 262 const media::VideoCaptureFormat& format, | 262 const media::VideoCaptureFormat& format, |
| 263 const base::TimeTicks& estimated_capture_time) { | 263 const base::TimeTicks& estimated_capture_time) { |
| 264 if (frame->coded_size() != expected_coded_size) { | 264 if (frame->natural_size() != expected_natural_size) { |
| 265 error_callback.Run( | 265 error_callback.Run( |
| 266 base::StringPrintf("Video frame resolution does not match config." | 266 base::StringPrintf("Video frame resolution does not match config." |
| 267 " Expected %dx%d. Got %dx%d.", | 267 " Expected %dx%d. Got %dx%d.", |
| 268 expected_coded_size.width(), | 268 expected_natural_size.width(), |
| 269 expected_coded_size.height(), | 269 expected_natural_size.height(), |
| 270 frame->coded_size().width(), | 270 frame->natural_size().width(), |
| 271 frame->coded_size().height())); | 271 frame->natural_size().height())); |
| 272 return; | 272 return; |
| 273 } | 273 } |
| 274 | 274 |
| 275 base::TimeTicks timestamp; | 275 base::TimeTicks timestamp; |
| 276 if (estimated_capture_time.is_null()) | 276 if (estimated_capture_time.is_null()) |
| 277 timestamp = base::TimeTicks::Now(); | 277 timestamp = base::TimeTicks::Now(); |
| 278 else | 278 else |
| 279 timestamp = estimated_capture_time; | 279 timestamp = estimated_capture_time; |
| 280 | 280 |
| 281 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc | 281 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc |
| 282 TRACE_EVENT_INSTANT2( | 282 TRACE_EVENT_INSTANT2( |
| 283 "cast_perf_test", "MediaStreamVideoSink::OnVideoFrame", | 283 "cast_perf_test", "MediaStreamVideoSink::OnVideoFrame", |
| 284 TRACE_EVENT_SCOPE_THREAD, | 284 TRACE_EVENT_SCOPE_THREAD, |
| 285 "timestamp", timestamp.ToInternalValue(), | 285 "timestamp", timestamp.ToInternalValue(), |
| 286 "time_delta", frame->timestamp().ToInternalValue()); | 286 "time_delta", frame->timestamp().ToInternalValue()); |
| 287 frame_input->InsertRawVideoFrame(frame, timestamp); | 287 frame_input->InsertRawVideoFrame(frame, timestamp); |
| 288 } | 288 } |
| 289 | 289 |
| 290 // Attach this sink to a video track represented by |track_|. | 290 // Attach this sink to a video track represented by |track_|. |
| 291 // Data received from the track will be submitted to |frame_input|. | 291 // Data received from the track will be submitted to |frame_input|. |
| 292 void AddToTrack( | 292 void AddToTrack( |
| 293 const scoped_refptr<media::cast::VideoFrameInput>& frame_input) { | 293 const scoped_refptr<media::cast::VideoFrameInput>& frame_input) { |
| 294 DCHECK(!sink_added_); | 294 DCHECK(!sink_added_); |
| 295 sink_added_ = true; | 295 sink_added_ = true; |
| 296 AddToVideoTrack( | 296 AddToVideoTrack( |
| 297 this, | 297 this, |
| 298 base::Bind( | 298 base::Bind( |
| 299 &CastVideoSink::OnVideoFrame, | 299 &CastVideoSink::OnVideoFrame, |
| 300 expected_coded_size_, | 300 expected_natural_size_, |
| 301 error_callback_, | 301 error_callback_, |
| 302 frame_input), | 302 frame_input), |
| 303 track_); | 303 track_); |
| 304 } | 304 } |
| 305 | 305 |
| 306 private: | 306 private: |
| 307 blink::WebMediaStreamTrack track_; | 307 blink::WebMediaStreamTrack track_; |
| 308 bool sink_added_; | 308 bool sink_added_; |
| 309 gfx::Size expected_coded_size_; | 309 gfx::Size expected_natural_size_; |
| 310 CastRtpStream::ErrorCallback error_callback_; | 310 CastRtpStream::ErrorCallback error_callback_; |
| 311 | 311 |
| 312 DISALLOW_COPY_AND_ASSIGN(CastVideoSink); | 312 DISALLOW_COPY_AND_ASSIGN(CastVideoSink); |
| 313 }; | 313 }; |
| 314 | 314 |
| 315 // Receives audio data from a MediaStreamTrack. Data is submitted to | 315 // Receives audio data from a MediaStreamTrack. Data is submitted to |
| 316 // media::cast::FrameInput. | 316 // media::cast::FrameInput. |
| 317 // | 317 // |
| 318 // Threading: Audio frames are received on the real-time audio thread. | 318 // Threading: Audio frames are received on the real-time audio thread. |
| 319 // Note that RemoveFromAudioTrack() is synchronous and we have | 319 // Note that RemoveFromAudioTrack() is synchronous and we have |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 void CastRtpStream::DidEncounterError(const std::string& message) { | 581 void CastRtpStream::DidEncounterError(const std::string& message) { |
| 582 VLOG(1) << "CastRtpStream::DidEncounterError(" << message << ") = " | 582 VLOG(1) << "CastRtpStream::DidEncounterError(" << message << ") = " |
| 583 << (IsAudio() ? "audio" : "video"); | 583 << (IsAudio() ? "audio" : "video"); |
| 584 // Save the WeakPtr first because the error callback might delete this object. | 584 // Save the WeakPtr first because the error callback might delete this object. |
| 585 base::WeakPtr<CastRtpStream> ptr = weak_factory_.GetWeakPtr(); | 585 base::WeakPtr<CastRtpStream> ptr = weak_factory_.GetWeakPtr(); |
| 586 error_callback_.Run(message); | 586 error_callback_.Run(message); |
| 587 content::RenderThread::Get()->GetMessageLoop()->PostTask( | 587 content::RenderThread::Get()->GetMessageLoop()->PostTask( |
| 588 FROM_HERE, | 588 FROM_HERE, |
| 589 base::Bind(&CastRtpStream::Stop, ptr)); | 589 base::Bind(&CastRtpStream::Stop, ptr)); |
| 590 } | 590 } |
| OLD | NEW |