Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: content/renderer/media/webrtc_videosink_adapter.cc

Issue 83023005: Add VideoTrackSink interface to content/public (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/webrtc_videosink_adapter.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "content/renderer/media/native_handle_impl.h"
12 #include "media/base/video_frame.h"
13 #include "media/base/video_util.h"
14 #include "third_party/libjingle/source/talk/media/base/videoframe.h"
15 #include "ui/gfx/size.h"
16
17 using media::CopyYPlane;
18 using media::CopyUPlane;
19 using media::CopyVPlane;
20
21 namespace content {
22
23 WebRtcVideoSinkAdapter::WebRtcVideoSinkAdapter(
24 webrtc::VideoTrackInterface* video_track,
25 VideoTrackSink* sink)
26 : message_loop_proxy_(base::MessageLoopProxy::current()),
27 sink_(sink),
28 video_track_(video_track) {
29 DCHECK(sink);
30 video_track_->AddRenderer(this);
Alpha Left Google 2013/11/26 04:59:32 Oh boy. Please tell me that this is temporary. The
perkj_chrome 2013/11/26 09:16:38 Yes- this is the first step. We will need this for
Alpha Left Google 2013/11/26 10:00:47 Sounds good for this being the first step. As long
31 video_track_->RegisterObserver(this);
32 state_ = video_track_->state();
33 DVLOG(1) << "WebRtcVideoSinkAdapter";
34 }
35
36 WebRtcVideoSinkAdapter::~WebRtcVideoSinkAdapter() {
37 video_track_->RemoveRenderer(this);
38 video_track_->UnregisterObserver(this);
39 DVLOG(1) << "~WebRtcVideoSinkAdapter";
40 }
41
42 void WebRtcVideoSinkAdapter::SetSize(int width, int height) {
43 }
44
45 void WebRtcVideoSinkAdapter::RenderFrame(const cricket::VideoFrame* frame) {
46 base::TimeDelta timestamp = base::TimeDelta::FromMilliseconds(
47 frame->GetTimeStamp() / talk_base::kNumNanosecsPerMillisec);
48
49 scoped_refptr<media::VideoFrame> video_frame;
50 if (frame->GetNativeHandle() != NULL) {
51 NativeHandleImpl* handle =
52 static_cast<NativeHandleImpl*>(frame->GetNativeHandle());
53 video_frame = static_cast<media::VideoFrame*>(handle->GetHandle());
54 video_frame->SetTimestamp(timestamp);
55 } else {
56 gfx::Size size(frame->GetWidth(), frame->GetHeight());
57 video_frame = media::VideoFrame::CreateFrame(
58 media::VideoFrame::YV12, size, gfx::Rect(size), size, timestamp);
59
60 // Aspect ratio unsupported; DCHECK when there are non-square pixels.
61 DCHECK_EQ(frame->GetPixelWidth(), 1u);
62 DCHECK_EQ(frame->GetPixelHeight(), 1u);
63
64 int y_rows = frame->GetHeight();
65 int uv_rows = frame->GetHeight() / 2; // YV12 format.
66 CopyYPlane(
67 frame->GetYPlane(), frame->GetYPitch(), y_rows, video_frame.get());
68 CopyUPlane(
69 frame->GetUPlane(), frame->GetUPitch(), uv_rows, video_frame.get());
70 CopyVPlane(
71 frame->GetVPlane(), frame->GetVPitch(), uv_rows, video_frame.get());
72 }
73
74 message_loop_proxy_->PostTask(
75 FROM_HERE, base::Bind(&WebRtcVideoSinkAdapter::DoRenderFrameOnMainThread,
76 AsWeakPtr(), video_frame));
77 }
78
79 void WebRtcVideoSinkAdapter::OnChanged() {
80 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
81
82 // TODO(perkj) OnChanged belong to base class of WebRtcVideoSinkAdapter
83 // common for both webrtc audio and video.
84 webrtc::MediaStreamTrackInterface::TrackState state = video_track_->state();
85 if (state == state_)
86 return;
87 state_ = state;
88 switch (state) {
89 case webrtc::MediaStreamTrackInterface::kInitializing:
90 // Ignore the kInitializing state since there is no match in
91 // WebMediaStreamSource::ReadyState.
92 break;
93 case webrtc::MediaStreamTrackInterface::kLive:
94 sink_->OnSourceChangedState(MediaTrackSink::kLive);
95 break;
96 case webrtc::MediaStreamTrackInterface::kEnded:
97 sink_->OnSourceChangedState(MediaTrackSink::kEnded);
98 break;
99 default:
100 NOTREACHED();
101 break;
102 }
103 }
104
105 void WebRtcVideoSinkAdapter::DoRenderFrameOnMainThread(
106 scoped_refptr<media::VideoFrame> video_frame) {
107 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
108
109 if (!sink_)
110 return;
111
112 sink_->OnVideoFrame(video_frame);
113 }
114
115 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698