OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/webrtc/media_stream_remote_video_source.h" | 5 #include "content/renderer/media/webrtc/media_stream_remote_video_source.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "content/renderer/media/webrtc/track_observer.h" | 12 #include "content/renderer/media/webrtc/track_observer.h" |
13 #include "media/base/bind_to_current_loop.h" | 13 #include "media/base/bind_to_current_loop.h" |
14 #include "media/base/video_frame.h" | 14 #include "media/base/video_frame.h" |
15 #include "media/base/video_util.h" | 15 #include "media/base/video_util.h" |
16 #include "third_party/libjingle/source/talk/media/base/videoframe.h" | 16 #include "third_party/libjingle/source/talk/media/base/videoframe.h" |
| 17 #include "third_party/webrtc/system_wrappers/interface/tick_util.h" |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 | 20 |
20 // Internal class used for receiving frames from the webrtc track on a | 21 // Internal class used for receiving frames from the webrtc track on a |
21 // libjingle thread and forward it to the IO-thread. | 22 // libjingle thread and forward it to the IO-thread. |
22 class MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate | 23 class MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate |
23 : public base::RefCountedThreadSafe<RemoteVideoSourceDelegate>, | 24 : public base::RefCountedThreadSafe<RemoteVideoSourceDelegate>, |
24 public webrtc::VideoRendererInterface { | 25 public webrtc::VideoRendererInterface { |
25 public: | 26 public: |
26 RemoteVideoSourceDelegate( | 27 RemoteVideoSourceDelegate( |
(...skipping 13 matching lines...) Expand all Loading... |
40 const scoped_refptr<media::VideoFrame>& video_frame); | 41 const scoped_refptr<media::VideoFrame>& video_frame); |
41 | 42 |
42 private: | 43 private: |
43 // Bound to the render thread. | 44 // Bound to the render thread. |
44 base::ThreadChecker thread_checker_; | 45 base::ThreadChecker thread_checker_; |
45 | 46 |
46 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 47 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
47 | 48 |
48 // |frame_callback_| is accessed on the IO thread. | 49 // |frame_callback_| is accessed on the IO thread. |
49 VideoCaptureDeliverFrameCB frame_callback_; | 50 VideoCaptureDeliverFrameCB frame_callback_; |
| 51 |
| 52 // WebRTC Chromium timestamp diff |
| 53 int64_t time_diff_us_; |
50 }; | 54 }; |
51 | 55 |
52 MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate:: | 56 MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate:: |
53 RemoteVideoSourceDelegate( | 57 RemoteVideoSourceDelegate( |
54 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 58 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
55 const VideoCaptureDeliverFrameCB& new_frame_callback) | 59 const VideoCaptureDeliverFrameCB& new_frame_callback) |
56 : io_task_runner_(io_task_runner), frame_callback_(new_frame_callback) { | 60 : io_task_runner_(io_task_runner), frame_callback_(new_frame_callback) { |
| 61 // TODO(qiangchen): There can be two differences between clocks: 1) |
| 62 // the offset, 2) the rate (i.e., one clock runs faster than the other). |
| 63 // See http://crbug/516700 |
| 64 time_diff_us_ = |
| 65 (base::TimeTicks::Now() - base::TimeTicks()).InMicroseconds() - |
| 66 webrtc::TickTime::MicrosecondTimestamp(); |
57 } | 67 } |
58 | 68 |
59 MediaStreamRemoteVideoSource:: | 69 MediaStreamRemoteVideoSource:: |
60 RemoteVideoSourceDelegate::~RemoteVideoSourceDelegate() { | 70 RemoteVideoSourceDelegate::~RemoteVideoSourceDelegate() { |
61 } | 71 } |
62 | 72 |
63 void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::RenderFrame( | 73 void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::RenderFrame( |
64 const cricket::VideoFrame* incoming_frame) { | 74 const cricket::VideoFrame* incoming_frame) { |
65 TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::RenderFrame"); | 75 base::TimeTicks render_time = |
| 76 base::TimeTicks() + |
| 77 base::TimeDelta::FromMicroseconds(incoming_frame->GetTimeStamp() / 1000 + |
| 78 time_diff_us_); |
| 79 |
| 80 TRACE_EVENT1("webrtc", "RemoteVideoSourceDelegate::RenderFrame", |
| 81 "Ideal Render Instant", render_time.ToInternalValue()); |
| 82 |
66 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( | 83 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
67 incoming_frame->GetElapsedTime() / rtc::kNumNanosecsPerMicrosec); | 84 incoming_frame->GetElapsedTime() / rtc::kNumNanosecsPerMicrosec); |
68 | 85 |
69 scoped_refptr<media::VideoFrame> video_frame; | 86 scoped_refptr<media::VideoFrame> video_frame; |
70 if (incoming_frame->GetNativeHandle() != NULL) { | 87 if (incoming_frame->GetNativeHandle() != NULL) { |
71 video_frame = | 88 video_frame = |
72 static_cast<media::VideoFrame*>(incoming_frame->GetNativeHandle()); | 89 static_cast<media::VideoFrame*>(incoming_frame->GetNativeHandle()); |
73 video_frame->set_timestamp(timestamp); | 90 video_frame->set_timestamp(timestamp); |
74 } else { | 91 } else { |
75 const cricket::VideoFrame* frame = | 92 const cricket::VideoFrame* frame = |
(...skipping 13 matching lines...) Expand all Loading... |
89 video_frame = media::VideoFrame::WrapExternalYuvData( | 106 video_frame = media::VideoFrame::WrapExternalYuvData( |
90 media::PIXEL_FORMAT_YV12, size, gfx::Rect(size), size, | 107 media::PIXEL_FORMAT_YV12, size, gfx::Rect(size), size, |
91 frame->GetYPitch(), frame->GetUPitch(), frame->GetVPitch(), | 108 frame->GetYPitch(), frame->GetUPitch(), frame->GetVPitch(), |
92 const_cast<uint8_t*>(frame->GetYPlane()), | 109 const_cast<uint8_t*>(frame->GetYPlane()), |
93 const_cast<uint8_t*>(frame->GetUPlane()), | 110 const_cast<uint8_t*>(frame->GetUPlane()), |
94 const_cast<uint8_t*>(frame->GetVPlane()), timestamp); | 111 const_cast<uint8_t*>(frame->GetVPlane()), timestamp); |
95 video_frame->AddDestructionObserver( | 112 video_frame->AddDestructionObserver( |
96 base::Bind(&base::DeletePointer<cricket::VideoFrame>, frame->Copy())); | 113 base::Bind(&base::DeletePointer<cricket::VideoFrame>, frame->Copy())); |
97 } | 114 } |
98 | 115 |
| 116 video_frame->metadata()->SetTimeTicks( |
| 117 media::VideoFrameMetadata::REFERENCE_TIME, render_time); |
| 118 |
99 io_task_runner_->PostTask( | 119 io_task_runner_->PostTask( |
100 FROM_HERE, base::Bind(&RemoteVideoSourceDelegate::DoRenderFrameOnIOThread, | 120 FROM_HERE, base::Bind(&RemoteVideoSourceDelegate::DoRenderFrameOnIOThread, |
101 this, video_frame)); | 121 this, video_frame)); |
102 } | 122 } |
103 | 123 |
104 void MediaStreamRemoteVideoSource:: | 124 void MediaStreamRemoteVideoSource:: |
105 RemoteVideoSourceDelegate::DoRenderFrameOnIOThread( | 125 RemoteVideoSourceDelegate::DoRenderFrameOnIOThread( |
106 const scoped_refptr<media::VideoFrame>& video_frame) { | 126 const scoped_refptr<media::VideoFrame>& video_frame) { |
107 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 127 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
108 TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::DoRenderFrameOnIOThread"); | 128 TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::DoRenderFrameOnIOThread"); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 case webrtc::MediaStreamTrackInterface::kEnded: | 195 case webrtc::MediaStreamTrackInterface::kEnded: |
176 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded); | 196 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded); |
177 break; | 197 break; |
178 default: | 198 default: |
179 NOTREACHED(); | 199 NOTREACHED(); |
180 break; | 200 break; |
181 } | 201 } |
182 } | 202 } |
183 | 203 |
184 } // namespace content | 204 } // namespace content |
OLD | NEW |