2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/video/video_receive_stream.h"
18 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
19 #include "webrtc/system_wrappers/interface/clock.h"
20 #include "webrtc/video/receive_statistics_proxy.h"
21 #include "webrtc/video_engine/include/vie_base.h"
22 #include "webrtc/video_engine/include/vie_capture.h"
23 #include "webrtc/video_engine/include/vie_codec.h"
24 #include "webrtc/video_engine/include/vie_external_codec.h"
25 #include "webrtc/video_engine/include/vie_image_process.h"
26 #include "webrtc/video_engine/include/vie_network.h"
27 #include "webrtc/video_engine/include/vie_render.h"
28 #include "webrtc/video_engine/include/vie_rtp_rtcp.h"
29 #include "webrtc/video_receive_stream.h"
34 VideoReceiveStream::VideoReceiveStream(webrtc::VideoEngine* video_engine,
35 const VideoReceiveStream::Config& config,
36 newapi::Transport* transport,
37 webrtc::VoiceEngine* voice_engine,
39 : transport_adapter_(transport),
40 encoded_frame_proxy_(config.pre_decode_callback),
42 clock_(Clock::GetRealTimeClock()),
44 video_engine_base_ = ViEBase::GetInterface(video_engine);
45 video_engine_base_->CreateReceiveChannel(channel_, base_channel);
46 assert(channel_ != -1);
48 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
49 assert(rtp_rtcp_ != NULL);
51 // TODO(pbos): This is not fine grained enough...
52 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
53 rtp_rtcp_->SetKeyFrameRequestMethod(channel_, kViEKeyFrameRequestPliRtcp);
54 switch (config_.rtp.rtcp_mode) {
55 case newapi::kRtcpCompound:
56 rtp_rtcp_->SetRTCPStatus(channel_, kRtcpCompound_RFC4585);
58 case newapi::kRtcpReducedSize:
59 rtp_rtcp_->SetRTCPStatus(channel_, kRtcpNonCompound_RFC5506);
63 assert(config_.rtp.remote_ssrc != 0);
64 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
65 assert(config_.rtp.local_ssrc != 0);
66 assert(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
68 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.local_ssrc);
69 // TODO(pbos): Support multiple RTX, per video payload.
70 Config::Rtp::RtxMap::const_iterator it = config_.rtp.rtx.begin();
71 if (it != config_.rtp.rtx.end()) {
72 assert(it->second.ssrc != 0);
73 assert(it->second.payload_type != 0);
75 rtp_rtcp_->SetRemoteSSRCType(channel_, kViEStreamTypeRtx, it->second.ssrc);
76 rtp_rtcp_->SetRtxReceivePayloadType(channel_, it->second.payload_type);
79 rtp_rtcp_->SetRembStatus(channel_, false, config_.rtp.remb);
81 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
82 const std::string& extension = config_.rtp.extensions[i].name;
83 int id = config_.rtp.extensions[i].id;
84 if (extension == RtpExtension::kTOffset) {
85 if (rtp_rtcp_->SetReceiveTimestampOffsetStatus(channel_, true, id) != 0)
87 } else if (extension == RtpExtension::kAbsSendTime) {
88 if (rtp_rtcp_->SetReceiveAbsoluteSendTimeStatus(channel_, true, id) != 0)
91 abort(); // Unsupported extension.
95 network_ = ViENetwork::GetInterface(video_engine);
96 assert(network_ != NULL);
98 network_->RegisterSendTransport(channel_, transport_adapter_);
100 codec_ = ViECodec::GetInterface(video_engine);
102 for (size_t i = 0; i < config_.codecs.size(); ++i) {
103 if (codec_->SetReceiveCodec(channel_, config_.codecs[i]) != 0) {
104 // TODO(pbos): Abort gracefully, this can be a runtime error.
105 // Factor out to an Init() method.
110 stats_proxy_.reset(new ReceiveStatisticsProxy(
111 config_.rtp.local_ssrc, clock_, rtp_rtcp_, codec_, channel_));
113 if (rtp_rtcp_->RegisterReceiveChannelRtcpStatisticsCallback(
114 channel_, stats_proxy_.get()) != 0)
117 if (rtp_rtcp_->RegisterReceiveChannelRtpStatisticsCallback(
118 channel_, stats_proxy_.get()) != 0)
121 if (codec_->RegisterDecoderObserver(channel_, *stats_proxy_) != 0)
124 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
125 for (size_t i = 0; i < config_.external_decoders.size(); ++i) {
126 ExternalVideoDecoder* decoder = &config_.external_decoders[i];
127 if (external_codec_->RegisterExternalReceiveCodec(
129 decoder->payload_type,
132 decoder->expected_delay_ms) != 0) {
133 // TODO(pbos): Abort gracefully? Can this be a runtime error?
138 render_ = ViERender::GetInterface(video_engine);
139 assert(render_ != NULL);
141 render_->AddRenderCallback(channel_, this);
144 video_engine_base_->SetVoiceEngine(voice_engine);
145 video_engine_base_->ConnectAudioChannel(channel_, config_.audio_channel_id);
148 image_process_ = ViEImageProcess::GetInterface(video_engine);
149 if (config.pre_decode_callback) {
150 image_process_->RegisterPreDecodeImageCallback(channel_,
151 &encoded_frame_proxy_);
153 image_process_->RegisterPreRenderCallback(channel_, this);
155 if (config.rtp.rtcp_xr.receiver_reference_time_report) {
156 rtp_rtcp_->SetRtcpXrRrtrStatus(channel_, true);
160 VideoReceiveStream::~VideoReceiveStream() {
161 image_process_->DeRegisterPreRenderCallback(channel_);
162 image_process_->DeRegisterPreDecodeCallback(channel_);
164 render_->RemoveRenderer(channel_);
166 for (size_t i = 0; i < config_.external_decoders.size(); ++i) {
167 external_codec_->DeRegisterExternalReceiveCodec(
168 channel_, config_.external_decoders[i].payload_type);
171 network_->DeregisterSendTransport(channel_);
173 video_engine_base_->SetVoiceEngine(NULL);
174 image_process_->Release();
175 video_engine_base_->Release();
176 external_codec_->Release();
177 codec_->DeregisterDecoderObserver(channel_);
178 rtp_rtcp_->DeregisterReceiveChannelRtpStatisticsCallback(channel_,
180 rtp_rtcp_->DeregisterReceiveChannelRtcpStatisticsCallback(channel_,
185 rtp_rtcp_->Release();
188 void VideoReceiveStream::StartReceiving() {
189 transport_adapter_.Enable();
190 if (render_->StartRender(channel_) != 0)
192 if (video_engine_base_->StartReceive(channel_) != 0)
196 void VideoReceiveStream::StopReceiving() {
197 if (render_->StopRender(channel_) != 0)
199 if (video_engine_base_->StopReceive(channel_) != 0)
201 transport_adapter_.Disable();
204 VideoReceiveStream::Stats VideoReceiveStream::GetStats() const {
205 return stats_proxy_->GetStats();
208 void VideoReceiveStream::GetCurrentReceiveCodec(VideoCodec* receive_codec) {
209 // TODO(pbos): Implement
212 bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
213 return network_->ReceivedRTCPPacket(
214 channel_, packet, static_cast<int>(length)) == 0;
217 bool VideoReceiveStream::DeliverRtp(const uint8_t* packet, size_t length) {
218 return network_->ReceivedRTPPacket(
219 channel_, packet, static_cast<int>(length), PacketTime()) == 0;
222 void VideoReceiveStream::FrameCallback(I420VideoFrame* video_frame) {
223 stats_proxy_->OnDecodedFrame();
225 if (config_.pre_render_callback)
226 config_.pre_render_callback->FrameCallback(video_frame);
229 int32_t VideoReceiveStream::RenderFrame(const uint32_t stream_id,
230 I420VideoFrame& video_frame) {
231 if (config_.renderer != NULL)
232 config_.renderer->RenderFrame(
234 video_frame.render_time_ms() - clock_->TimeInMilliseconds());
236 stats_proxy_->OnRenderedFrame();
240 } // namespace internal
241 } // namespace webrtc