pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | #include <stdlib.h> |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 15 | |
| 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 | |
| 26 | namespace webrtc { |
| 27 | namespace internal { |
| 28 | |
| 29 | VideoReceiveStream::VideoReceiveStream( |
| 30 | webrtc::VideoEngine* video_engine, |
pbos@webrtc.org | 025f4f1 | 2013-06-05 11:33:21 +0000 | [diff] [blame] | 31 | const newapi::VideoReceiveStream::Config& config, |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 32 | newapi::Transport* transport) |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 33 | : transport_(transport), config_(config), channel_(-1) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 34 | 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.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 42 | // TODO(pbos): This is not fine grained enough... |
| 43 | rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0); |
| 44 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 45 | assert(config_.rtp.ssrc != 0); |
| 46 | |
| 47 | network_ = ViENetwork::GetInterface(video_engine); |
| 48 | assert(network_ != NULL); |
| 49 | |
| 50 | network_->RegisterSendTransport(channel_, *this); |
| 51 | |
| 52 | codec_ = ViECodec::GetInterface(video_engine); |
| 53 | |
| 54 | for (size_t i = 0; i < config_.codecs.size(); ++i) { |
| 55 | if (codec_->SetReceiveCodec(channel_, config_.codecs[i]) != 0) { |
| 56 | // TODO(pbos): Abort gracefully, this can be a runtime error. |
| 57 | // Factor out to an Init() method. |
| 58 | abort(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | render_ = webrtc::ViERender::GetInterface(video_engine); |
| 63 | assert(render_ != NULL); |
| 64 | |
| 65 | if (render_->AddRenderer(channel_, kVideoI420, this) != 0) { |
| 66 | abort(); |
| 67 | } |
| 68 | |
| 69 | clock_ = Clock::GetRealTimeClock(); |
| 70 | } |
| 71 | |
| 72 | VideoReceiveStream::~VideoReceiveStream() { |
| 73 | network_->DeregisterSendTransport(channel_); |
| 74 | |
| 75 | video_engine_base_->Release(); |
| 76 | codec_->Release(); |
| 77 | network_->Release(); |
| 78 | render_->Release(); |
| 79 | rtp_rtcp_->Release(); |
| 80 | } |
| 81 | |
| 82 | void VideoReceiveStream::StartReceive() { |
| 83 | if (render_->StartRender(channel_)) { |
| 84 | abort(); |
| 85 | } |
| 86 | if (video_engine_base_->StartReceive(channel_) != 0) { |
| 87 | abort(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void VideoReceiveStream::StopReceive() { |
| 92 | if (render_->StopRender(channel_)) { |
| 93 | abort(); |
| 94 | } |
| 95 | if (video_engine_base_->StopReceive(channel_) != 0) { |
| 96 | abort(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void VideoReceiveStream::GetCurrentReceiveCodec(VideoCodec* receive_codec) { |
| 101 | // TODO(pbos): Implement |
| 102 | } |
| 103 | |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 104 | bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 105 | return network_->ReceivedRTCPPacket(channel_, packet, length) == 0; |
| 106 | } |
| 107 | |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 108 | bool VideoReceiveStream::DeliverRtp(const uint8_t* packet, size_t length) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 109 | return network_->ReceivedRTPPacket(channel_, packet, length) == 0; |
| 110 | } |
| 111 | |
| 112 | int VideoReceiveStream::FrameSizeChange(unsigned int width, unsigned int height, |
| 113 | unsigned int /*number_of_streams*/) { |
| 114 | width_ = width; |
| 115 | height_ = height; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int VideoReceiveStream::DeliverFrame(uint8_t* frame, int buffer_size, |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame^] | 120 | uint32_t timestamp, int64_t render_time, |
| 121 | void* /*handle*/) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 122 | if (config_.renderer == NULL) { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | I420VideoFrame video_frame; |
| 127 | video_frame.CreateEmptyFrame(width_, height_, width_, height_, height_); |
| 128 | ConvertToI420(kI420, frame, 0, 0, width_, height_, buffer_size, |
| 129 | webrtc::kRotateNone, &video_frame); |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 130 | video_frame.set_timestamp(timestamp); |
| 131 | video_frame.set_render_time_ms(render_time); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 132 | |
| 133 | if (config_.post_decode_callback != NULL) { |
| 134 | config_.post_decode_callback->FrameCallback(&video_frame); |
| 135 | } |
| 136 | |
| 137 | if (config_.renderer != NULL) { |
| 138 | // TODO(pbos): Add timing to RenderFrame call |
| 139 | config_.renderer |
| 140 | ->RenderFrame(video_frame, render_time - clock_->TimeInMilliseconds()); |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame^] | 146 | bool VideoReceiveStream::IsTextureSupported() { return false; } |
| 147 | |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 148 | int VideoReceiveStream::SendPacket(int /*channel*/, |
| 149 | const void* packet, |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 150 | int length) { |
| 151 | assert(length >= 0); |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 152 | bool success = transport_->SendRTP(static_cast<const uint8_t*>(packet), |
| 153 | static_cast<size_t>(length)); |
| 154 | return success ? 0 : -1; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 155 | } |
| 156 | |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 157 | int VideoReceiveStream::SendRTCPPacket(int /*channel*/, |
| 158 | const void* packet, |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 159 | int length) { |
| 160 | assert(length >= 0); |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 161 | bool success = transport_->SendRTCP(static_cast<const uint8_t*>(packet), |
| 162 | static_cast<size_t>(length)); |
| 163 | return success ? 0 : -1; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 164 | } |
| 165 | } // internal |
| 166 | } // webrtc |