| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 "remoting/protocol/webrtc_video_encoder_factory.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/synchronization/lock.h" | |
| 14 | |
| 15 namespace remoting { | |
| 16 | |
| 17 WebrtcVideoEncoder::WebrtcVideoEncoder(webrtc::VideoCodecType codec) | |
| 18 : state_(kUninitialized), video_codec_type_(codec) { | |
| 19 VLOG(1) << "video codecType " << video_codec_type_; | |
| 20 } | |
| 21 | |
| 22 WebrtcVideoEncoder::~WebrtcVideoEncoder() {} | |
| 23 | |
| 24 int32_t WebrtcVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, | |
| 25 int32_t number_of_cores, | |
| 26 size_t max_payload_size) { | |
| 27 base::AutoLock lock(lock_); | |
| 28 DCHECK(codec_settings); | |
| 29 VLOG(1) << "video codecType " << codec_settings->codecType << " width " | |
| 30 << codec_settings->width << " height " << codec_settings->height | |
| 31 << " startBitrate " << codec_settings->startBitrate << " maxBitrate " | |
| 32 << codec_settings->maxBitrate << " minBitrate " | |
| 33 << codec_settings->minBitrate << " targetBitrate " | |
| 34 << codec_settings->targetBitrate << " maxFramerate " | |
| 35 << codec_settings->maxFramerate; | |
| 36 | |
| 37 int streamCount = codec_settings->numberOfSimulcastStreams; | |
| 38 // Validate request is to support a single stream. | |
| 39 if (streamCount > 1) { | |
| 40 for (int i = 0; i < streamCount; ++i) { | |
| 41 if (codec_settings->simulcastStream[i].maxBitrate != 0) { | |
| 42 LOG(ERROR) << "Simulcast unsupported"; | |
| 43 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | |
| 44 } | |
| 45 } | |
| 46 } | |
| 47 state_ = kInitialized; | |
| 48 return WEBRTC_VIDEO_CODEC_OK; | |
| 49 } | |
| 50 | |
| 51 int32_t WebrtcVideoEncoder::RegisterEncodeCompleteCallback( | |
| 52 webrtc::EncodedImageCallback* callback) { | |
| 53 base::AutoLock lock(lock_); | |
| 54 DCHECK(callback); | |
| 55 encoded_callback_ = callback; | |
| 56 return WEBRTC_VIDEO_CODEC_OK; | |
| 57 } | |
| 58 | |
| 59 int32_t WebrtcVideoEncoder::Release() { | |
| 60 base::AutoLock lock(lock_); | |
| 61 encoded_callback_ = nullptr; | |
| 62 return WEBRTC_VIDEO_CODEC_OK; | |
| 63 } | |
| 64 | |
| 65 int32_t WebrtcVideoEncoder::Encode( | |
| 66 const webrtc::VideoFrame& frame, | |
| 67 const webrtc::CodecSpecificInfo* codec_specific_info, | |
| 68 const std::vector<webrtc::FrameType>* frame_types) { | |
| 69 base::AutoLock lock(lock_); | |
| 70 if (!key_frame_request_.is_null()) | |
| 71 key_frame_request_.Run(); | |
| 72 return WEBRTC_VIDEO_CODEC_OK; | |
| 73 } | |
| 74 | |
| 75 int32_t WebrtcVideoEncoder::SetChannelParameters(uint32_t packet_loss, | |
| 76 int64_t rtt) { | |
| 77 VLOG(1) << "WebrtcVideoEncoder::SetChannelParameters " | |
| 78 << "loss:RTT " << packet_loss << ":" << rtt; | |
| 79 // Unused right now. | |
| 80 return WEBRTC_VIDEO_CODEC_OK; | |
| 81 } | |
| 82 | |
| 83 int32_t WebrtcVideoEncoder::SetRates(uint32_t bitrate, uint32_t framerate) { | |
| 84 VLOG(1) << "WebrtcVideoEncoder::SetRates bitrate:framerate " << bitrate << ":" | |
| 85 << framerate; | |
| 86 if (!target_bitrate_cb_.is_null()) | |
| 87 target_bitrate_cb_.Run(bitrate); | |
| 88 // framerate is not expected to be valid given we never report captured | |
| 89 // frames | |
| 90 return WEBRTC_VIDEO_CODEC_OK; | |
| 91 } | |
| 92 | |
| 93 webrtc::EncodedImageCallback::Result WebrtcVideoEncoder::SendEncodedFrame( | |
| 94 std::unique_ptr<VideoPacket> frame, | |
| 95 base::TimeTicks capture_time) { | |
| 96 uint8_t* buffer = | |
| 97 reinterpret_cast<uint8_t*>(const_cast<char*>(frame->data().data())); | |
| 98 size_t buffer_size = frame->data().size(); | |
| 99 base::AutoLock lock(lock_); | |
| 100 if (state_ == kUninitialized) { | |
| 101 LOG(ERROR) << "encoder interface uninitialized"; | |
| 102 return webrtc::EncodedImageCallback::Result( | |
| 103 webrtc::EncodedImageCallback::Result::ERROR_SEND_FAILED); | |
| 104 } | |
| 105 | |
| 106 webrtc::EncodedImage encoded_image(buffer, buffer_size, buffer_size); | |
| 107 encoded_image._encodedWidth = frame->format().screen_width(); | |
| 108 encoded_image._encodedHeight = frame->format().screen_height(); | |
| 109 encoded_image._completeFrame = true; | |
| 110 encoded_image._frameType = | |
| 111 frame->key_frame() ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta; | |
| 112 int64_t capture_time_ms = (capture_time - base::TimeTicks()).InMilliseconds(); | |
| 113 encoded_image.capture_time_ms_ = capture_time_ms; | |
| 114 encoded_image._timeStamp = static_cast<uint32_t>(capture_time_ms * 90); | |
| 115 encoded_image.playout_delay_.min_ms = 0; | |
| 116 encoded_image.playout_delay_.max_ms = 0; | |
| 117 | |
| 118 webrtc::CodecSpecificInfo codec_specific_info; | |
| 119 memset(&codec_specific_info, 0, sizeof(codec_specific_info)); | |
| 120 codec_specific_info.codecType = webrtc::kVideoCodecVP8; | |
| 121 | |
| 122 webrtc::RTPFragmentationHeader header; | |
| 123 memset(&header, 0, sizeof(header)); | |
| 124 | |
| 125 codec_specific_info.codecSpecific.VP8.simulcastIdx = 0; | |
| 126 codec_specific_info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; | |
| 127 codec_specific_info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; | |
| 128 codec_specific_info.codecSpecific.VP8.pictureId = webrtc::kNoPictureId; | |
| 129 | |
| 130 header.VerifyAndAllocateFragmentationHeader(1); | |
| 131 header.fragmentationOffset[0] = 0; | |
| 132 header.fragmentationLength[0] = buffer_size; | |
| 133 header.fragmentationPlType[0] = 0; | |
| 134 header.fragmentationTimeDiff[0] = 0; | |
| 135 | |
| 136 return encoded_callback_->OnEncodedImage(encoded_image, &codec_specific_info, | |
| 137 &header); | |
| 138 } | |
| 139 | |
| 140 void WebrtcVideoEncoder::SetKeyFrameRequestCallback( | |
| 141 const base::Closure& key_frame_request) { | |
| 142 base::AutoLock lock(lock_); | |
| 143 key_frame_request_ = key_frame_request; | |
| 144 } | |
| 145 | |
| 146 void WebrtcVideoEncoder::SetTargetBitrateCallback( | |
| 147 const TargetBitrateCallback& target_bitrate_cb) { | |
| 148 base::AutoLock lock(lock_); | |
| 149 target_bitrate_cb_ = target_bitrate_cb; | |
| 150 } | |
| 151 | |
| 152 WebrtcVideoEncoderFactory::WebrtcVideoEncoderFactory() { | |
| 153 // TODO(isheriff): These do not really affect anything internally | |
| 154 // in webrtc. | |
| 155 codecs_.push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec( | |
| 156 webrtc::kVideoCodecVP8, "VP8", 1280, 720, 30)); | |
| 157 } | |
| 158 | |
| 159 WebrtcVideoEncoderFactory::~WebrtcVideoEncoderFactory() { | |
| 160 DCHECK(encoders_.empty()); | |
| 161 } | |
| 162 | |
| 163 webrtc::VideoEncoder* WebrtcVideoEncoderFactory::CreateVideoEncoder( | |
| 164 webrtc::VideoCodecType type) { | |
| 165 VLOG(2) << "WebrtcVideoEncoderFactory::CreateVideoEncoder " << type; | |
| 166 DCHECK(type == webrtc::kVideoCodecVP8); | |
| 167 WebrtcVideoEncoder* encoder = new WebrtcVideoEncoder(type); | |
| 168 base::AutoLock lock(lock_); | |
| 169 encoder->SetKeyFrameRequestCallback(key_frame_request_); | |
| 170 encoder->SetTargetBitrateCallback(target_bitrate_cb_); | |
| 171 VLOG(1) << "Created " << encoder; | |
| 172 encoders_.push_back(base::WrapUnique(encoder)); | |
| 173 return encoder; | |
| 174 } | |
| 175 | |
| 176 const std::vector<cricket::WebRtcVideoEncoderFactory::VideoCodec>& | |
| 177 WebrtcVideoEncoderFactory::codecs() const { | |
| 178 VLOG(2) << "WebrtcVideoEncoderFactory::codecs"; | |
| 179 return codecs_; | |
| 180 } | |
| 181 | |
| 182 bool WebrtcVideoEncoderFactory::EncoderTypeHasInternalSource( | |
| 183 webrtc::VideoCodecType type) const { | |
| 184 VLOG(2) << "WebrtcVideoEncoderFactory::EncoderTypeHasInternalSource"; | |
| 185 return true; | |
| 186 } | |
| 187 | |
| 188 void WebrtcVideoEncoderFactory::DestroyVideoEncoder( | |
| 189 webrtc::VideoEncoder* encoder) { | |
| 190 VLOG(2) << "WebrtcVideoEncoderFactory::DestroyVideoEncoder"; | |
| 191 if (encoder == nullptr) { | |
| 192 LOG(ERROR) << "Attempting to destroy null encoder"; | |
| 193 return; | |
| 194 } | |
| 195 for (auto pos = encoders_.begin(); pos != encoders_.end(); ++pos) { | |
| 196 if ((*pos).get() == encoder) { | |
| 197 encoders_.erase(pos); | |
| 198 return; | |
| 199 } | |
| 200 } | |
| 201 DCHECK(false) << "Asked to remove encoder not owned by factory"; | |
| 202 } | |
| 203 | |
| 204 webrtc::EncodedImageCallback::Result | |
| 205 WebrtcVideoEncoderFactory::SendEncodedFrame(std::unique_ptr<VideoPacket> frame, | |
| 206 base::TimeTicks capture_time) { | |
| 207 if (encoders_.size() != 1) { | |
| 208 LOG(ERROR) << "Unexpected number of encoders " << encoders_.size(); | |
| 209 return webrtc::EncodedImageCallback::Result( | |
| 210 webrtc::EncodedImageCallback::Result::ERROR_SEND_FAILED); | |
| 211 } | |
| 212 return encoders_.front()->SendEncodedFrame(std::move(frame), capture_time); | |
| 213 } | |
| 214 | |
| 215 void WebrtcVideoEncoderFactory::SetKeyFrameRequestCallback( | |
| 216 const base::Closure& key_frame_request) { | |
| 217 base::AutoLock lock(lock_); | |
| 218 key_frame_request_ = key_frame_request; | |
| 219 if (encoders_.size() == 1) { | |
| 220 encoders_.front()->SetKeyFrameRequestCallback(key_frame_request); | |
| 221 } else { | |
| 222 LOG(ERROR) << "Dropping key frame request callback with unexpected" | |
| 223 " number of encoders: " | |
| 224 << encoders_.size(); | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 void WebrtcVideoEncoderFactory::SetTargetBitrateCallback( | |
| 229 const TargetBitrateCallback& target_bitrate_cb) { | |
| 230 base::AutoLock lock(lock_); | |
| 231 target_bitrate_cb_ = target_bitrate_cb; | |
| 232 if (encoders_.size() == 1) { | |
| 233 encoders_.front()->SetTargetBitrateCallback(target_bitrate_cb); | |
| 234 } else { | |
| 235 LOG(ERROR) << "Dropping target bitrate request callback with unexpected" | |
| 236 " number of encoders: " | |
| 237 << encoders_.size(); | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 } // namespace remoting | |
| OLD | NEW |