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 | |
| 13 | #include <cassert> |
| 14 | #include <cstdlib> |
| 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, |
| 31 | const newapi::VideoReceiveStreamConfig& config, |
| 32 | newapi::Transport* transport) |
| 33 | : transport_(transport), config_(config) { |
| 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 | |
| 42 | assert(config_.rtp.ssrc != 0); |
| 43 | |
| 44 | network_ = ViENetwork::GetInterface(video_engine); |
| 45 | assert(network_ != NULL); |
| 46 | |
| 47 | network_->RegisterSendTransport(channel_, *this); |
| 48 | |
| 49 | codec_ = ViECodec::GetInterface(video_engine); |
| 50 | |
| 51 | for (size_t i = 0; i < config_.codecs.size(); ++i) { |
| 52 | if (codec_->SetReceiveCodec(channel_, config_.codecs[i]) != 0) { |
| 53 | // TODO(pbos): Abort gracefully, this can be a runtime error. |
| 54 | // Factor out to an Init() method. |
| 55 | abort(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | render_ = webrtc::ViERender::GetInterface(video_engine); |
| 60 | assert(render_ != NULL); |
| 61 | |
| 62 | if (render_->AddRenderer(channel_, kVideoI420, this) != 0) { |
| 63 | abort(); |
| 64 | } |
| 65 | |
| 66 | clock_ = Clock::GetRealTimeClock(); |
| 67 | } |
| 68 | |
| 69 | VideoReceiveStream::~VideoReceiveStream() { |
| 70 | network_->DeregisterSendTransport(channel_); |
| 71 | |
| 72 | video_engine_base_->Release(); |
| 73 | codec_->Release(); |
| 74 | network_->Release(); |
| 75 | render_->Release(); |
| 76 | rtp_rtcp_->Release(); |
| 77 | } |
| 78 | |
| 79 | void VideoReceiveStream::StartReceive() { |
| 80 | if (render_->StartRender(channel_)) { |
| 81 | abort(); |
| 82 | } |
| 83 | if (video_engine_base_->StartReceive(channel_) != 0) { |
| 84 | abort(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void VideoReceiveStream::StopReceive() { |
| 89 | if (render_->StopRender(channel_)) { |
| 90 | abort(); |
| 91 | } |
| 92 | if (video_engine_base_->StopReceive(channel_) != 0) { |
| 93 | abort(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void VideoReceiveStream::GetCurrentReceiveCodec(VideoCodec* receive_codec) { |
| 98 | // TODO(pbos): Implement |
| 99 | } |
| 100 | |
| 101 | void VideoReceiveStream::GetReceiveStatistics( |
| 102 | newapi::ReceiveStatistics* statistics) { |
| 103 | // TODO(pbos): Implement |
| 104 | } |
| 105 | |
| 106 | bool VideoReceiveStream::DeliverRtcp(const void* packet, size_t length) { |
| 107 | return network_->ReceivedRTCPPacket(channel_, packet, length) == 0; |
| 108 | } |
| 109 | |
| 110 | bool VideoReceiveStream::DeliverRtp(const void* packet, size_t length) { |
| 111 | return network_->ReceivedRTPPacket(channel_, packet, length) == 0; |
| 112 | } |
| 113 | |
| 114 | int 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 | |
| 121 | int VideoReceiveStream::DeliverFrame(uint8_t* frame, int buffer_size, |
| 122 | uint32_t time_stamp, int64_t render_time) { |
| 123 | if (config_.renderer == NULL) { |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | I420VideoFrame video_frame; |
| 128 | video_frame.CreateEmptyFrame(width_, height_, width_, height_, height_); |
| 129 | ConvertToI420(kI420, frame, 0, 0, width_, height_, buffer_size, |
| 130 | webrtc::kRotateNone, &video_frame); |
| 131 | |
| 132 | if (config_.post_decode_callback != NULL) { |
| 133 | config_.post_decode_callback->FrameCallback(&video_frame); |
| 134 | } |
| 135 | |
| 136 | if (config_.renderer != NULL) { |
| 137 | // TODO(pbos): Add timing to RenderFrame call |
| 138 | config_.renderer |
| 139 | ->RenderFrame(video_frame, render_time - clock_->TimeInMilliseconds()); |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int VideoReceiveStream::SendPacket(int /*channel*/, const void* packet, |
| 146 | int length) { |
| 147 | assert(length >= 0); |
| 148 | return transport_->SendRTP(packet, static_cast<size_t>(length)) ? 0 : -1; |
| 149 | } |
| 150 | |
| 151 | int VideoReceiveStream::SendRTCPPacket(int /*channel*/, const void* packet, |
| 152 | int length) { |
| 153 | assert(length >= 0); |
| 154 | return transport_->SendRTCP(packet, static_cast<size_t>(length)) ? 0 : -1; |
| 155 | } |
| 156 | } // internal |
| 157 | } // webrtc |