blob: ebd94a7b180dfb66790b8a97f286efcd027cd075 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
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.
9 */
10
11#include "webrtc/video_engine/internal/video_receive_stream.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14#include <stdlib.h>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000015
16#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
17#include "webrtc/system_wrappers/interface/clock.h"
18#include "webrtc/video_engine/include/vie_base.h"
19#include "webrtc/video_engine/include/vie_capture.h"
20#include "webrtc/video_engine/include/vie_codec.h"
21#include "webrtc/video_engine/include/vie_network.h"
22#include "webrtc/video_engine/include/vie_render.h"
23#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
24#include "webrtc/video_engine/new_include/video_receive_stream.h"
25
26namespace webrtc {
27namespace internal {
28
29VideoReceiveStream::VideoReceiveStream(
30 webrtc::VideoEngine* video_engine,
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000031 const newapi::VideoReceiveStream::Config& config,
pbos@webrtc.org29d58392013-05-16 12:08:03 +000032 newapi::Transport* transport)
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +000033 : transport_(transport), config_(config), channel_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000034 video_engine_base_ = ViEBase::GetInterface(video_engine);
35 // TODO(mflodman): Use the other CreateChannel method.
36 video_engine_base_->CreateChannel(channel_);
37 assert(channel_ != -1);
38
39 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
40 assert(rtp_rtcp_ != NULL);
41
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +000042 // TODO(pbos): This is not fine grained enough...
43 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +000044 rtp_rtcp_->SetKeyFrameRequestMethod(channel_,
45 kViEKeyFrameRequestPliRtcp);
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +000046
pbos@webrtc.org29d58392013-05-16 12:08:03 +000047 assert(config_.rtp.ssrc != 0);
48
49 network_ = ViENetwork::GetInterface(video_engine);
50 assert(network_ != NULL);
51
52 network_->RegisterSendTransport(channel_, *this);
53
54 codec_ = ViECodec::GetInterface(video_engine);
55
56 for (size_t i = 0; i < config_.codecs.size(); ++i) {
57 if (codec_->SetReceiveCodec(channel_, config_.codecs[i]) != 0) {
58 // TODO(pbos): Abort gracefully, this can be a runtime error.
59 // Factor out to an Init() method.
60 abort();
61 }
62 }
63
64 render_ = webrtc::ViERender::GetInterface(video_engine);
65 assert(render_ != NULL);
66
67 if (render_->AddRenderer(channel_, kVideoI420, this) != 0) {
68 abort();
69 }
70
71 clock_ = Clock::GetRealTimeClock();
72}
73
74VideoReceiveStream::~VideoReceiveStream() {
75 network_->DeregisterSendTransport(channel_);
76
77 video_engine_base_->Release();
78 codec_->Release();
79 network_->Release();
80 render_->Release();
81 rtp_rtcp_->Release();
82}
83
84void VideoReceiveStream::StartReceive() {
85 if (render_->StartRender(channel_)) {
86 abort();
87 }
88 if (video_engine_base_->StartReceive(channel_) != 0) {
89 abort();
90 }
91}
92
93void VideoReceiveStream::StopReceive() {
94 if (render_->StopRender(channel_)) {
95 abort();
96 }
97 if (video_engine_base_->StopReceive(channel_) != 0) {
98 abort();
99 }
100}
101
102void VideoReceiveStream::GetCurrentReceiveCodec(VideoCodec* receive_codec) {
103 // TODO(pbos): Implement
104}
105
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000106bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000107 return network_->ReceivedRTCPPacket(channel_, packet, length) == 0;
108}
109
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000110bool VideoReceiveStream::DeliverRtp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000111 return network_->ReceivedRTPPacket(channel_, packet, length) == 0;
112}
113
114int VideoReceiveStream::FrameSizeChange(unsigned int width, unsigned int height,
115 unsigned int /*number_of_streams*/) {
116 width_ = width;
117 height_ = height;
118 return 0;
119}
120
121int VideoReceiveStream::DeliverFrame(uint8_t* frame, int buffer_size,
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000122 uint32_t timestamp, int64_t render_time,
123 void* /*handle*/) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000124 if (config_.renderer == NULL) {
125 return 0;
126 }
127
128 I420VideoFrame video_frame;
129 video_frame.CreateEmptyFrame(width_, height_, width_, height_, height_);
130 ConvertToI420(kI420, frame, 0, 0, width_, height_, buffer_size,
131 webrtc::kRotateNone, &video_frame);
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000132 video_frame.set_timestamp(timestamp);
133 video_frame.set_render_time_ms(render_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000134
135 if (config_.post_decode_callback != NULL) {
136 config_.post_decode_callback->FrameCallback(&video_frame);
137 }
138
139 if (config_.renderer != NULL) {
140 // TODO(pbos): Add timing to RenderFrame call
141 config_.renderer
142 ->RenderFrame(video_frame, render_time - clock_->TimeInMilliseconds());
143 }
144
145 return 0;
146}
147
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000148bool VideoReceiveStream::IsTextureSupported() { return false; }
149
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000150int VideoReceiveStream::SendPacket(int /*channel*/,
151 const void* packet,
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000152 int length) {
153 assert(length >= 0);
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000154 bool success = transport_->SendRTP(static_cast<const uint8_t*>(packet),
155 static_cast<size_t>(length));
156 return success ? 0 : -1;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000157}
158
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000159int VideoReceiveStream::SendRTCPPacket(int /*channel*/,
160 const void* packet,
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000161 int length) {
162 assert(length >= 0);
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000163 bool success = transport_->SendRTCP(static_cast<const uint8_t*>(packet),
164 static_cast<size_t>(length));
165 return success ? 0 : -1;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000166}
167} // internal
168} // webrtc