OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "content/renderer/media/rtc_encoding_video_capturer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "media/base/encoded_bitstream_buffer.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 // Client of EncodedVideoSource. |
| 13 class RtcEncodingVideoCapturer::EncodedVideoSourceClient : |
| 14 public media::EncodedVideoSource::Client { |
| 15 public: |
| 16 EncodedVideoSourceClient( |
| 17 media::EncodedVideoSource* encoded_video_source, |
| 18 media::VideoEncodingParameters params, |
| 19 webrtc::VideoCodecType rtc_codec_type); |
| 20 virtual ~EncodedVideoSourceClient(); |
| 21 |
| 22 // media::EncodedVideoSource::Client implementation. |
| 23 virtual void OnOpened( |
| 24 const media::VideoEncodingParameters& params) OVERRIDE; |
| 25 virtual void OnClosed() OVERRIDE; |
| 26 virtual void OnBufferReady( |
| 27 scoped_refptr<const media::EncodedBitstreamBuffer> buffer) OVERRIDE; |
| 28 virtual void OnConfigChanged( |
| 29 const media::RuntimeVideoEncodingParameters& params) OVERRIDE; |
| 30 |
| 31 // Getters and setters for bitstream properties. |
| 32 bool finished() const; |
| 33 media::RuntimeVideoEncodingParameters runtime_params() const; |
| 34 void set_round_trip_time(base::TimeDelta round_trip_time); |
| 35 void set_callback(webrtc::EncodedImageCallback* callback); |
| 36 |
| 37 private: |
| 38 // Convert buffer to webrtc types and invoke encode complete callback. |
| 39 void ReportEncodedFrame( |
| 40 scoped_refptr<const media::EncodedBitstreamBuffer> buffer); |
| 41 |
| 42 media::VideoEncodingParameters params_; |
| 43 webrtc::VideoCodecType rtc_codec_type_; |
| 44 bool finished_; |
| 45 |
| 46 base::Time time_base_; |
| 47 base::TimeDelta round_trip_time_; |
| 48 media::EncodedVideoSource* encoded_video_source_; |
| 49 webrtc::EncodedImageCallback* callback_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(EncodedVideoSourceClient); |
| 52 }; |
| 53 |
| 54 RtcEncodingVideoCapturer::EncodedVideoSourceClient::EncodedVideoSourceClient( |
| 55 media::EncodedVideoSource* encoded_video_source, |
| 56 media::VideoEncodingParameters params, |
| 57 webrtc::VideoCodecType rtc_codec_type) |
| 58 : params_(params), |
| 59 rtc_codec_type_(rtc_codec_type), |
| 60 finished_(false), |
| 61 encoded_video_source_(encoded_video_source), |
| 62 callback_(NULL) { |
| 63 DCHECK(encoded_video_source_); |
| 64 encoded_video_source_->OpenBitstream(this, params); |
| 65 } |
| 66 |
| 67 RtcEncodingVideoCapturer::EncodedVideoSourceClient::~EncodedVideoSourceClient( |
| 68 ) { |
| 69 encoded_video_source_->CloseBitstream(); |
| 70 } |
| 71 |
| 72 bool RtcEncodingVideoCapturer::EncodedVideoSourceClient::finished() const { |
| 73 return finished_; |
| 74 } |
| 75 |
| 76 media::RuntimeVideoEncodingParameters |
| 77 RtcEncodingVideoCapturer::EncodedVideoSourceClient::runtime_params() const { |
| 78 return params_.runtime_params; |
| 79 } |
| 80 |
| 81 void RtcEncodingVideoCapturer::EncodedVideoSourceClient::set_round_trip_time( |
| 82 base::TimeDelta round_trip_time) { |
| 83 round_trip_time_ = round_trip_time; |
| 84 } |
| 85 |
| 86 void RtcEncodingVideoCapturer::EncodedVideoSourceClient::set_callback( |
| 87 webrtc::EncodedImageCallback* callback) { |
| 88 DCHECK(!callback_); |
| 89 callback_ = callback; |
| 90 } |
| 91 |
| 92 void RtcEncodingVideoCapturer::EncodedVideoSourceClient::OnOpened( |
| 93 const media::VideoEncodingParameters& params) { |
| 94 params_ = params; |
| 95 } |
| 96 |
| 97 void RtcEncodingVideoCapturer::EncodedVideoSourceClient::OnClosed() { |
| 98 finished_ = true; |
| 99 } |
| 100 |
| 101 void RtcEncodingVideoCapturer::EncodedVideoSourceClient::OnBufferReady( |
| 102 scoped_refptr<const media::EncodedBitstreamBuffer> buffer) { |
| 103 DCHECK(buffer); |
| 104 |
| 105 // First buffer constitutes the origin of the time for this bitstream context. |
| 106 if (time_base_.is_null()) |
| 107 time_base_ = buffer->metadata().timestamp; |
| 108 |
| 109 ReportEncodedFrame(buffer); |
| 110 encoded_video_source_->ReturnBitstreamBuffer(buffer); |
| 111 } |
| 112 |
| 113 void RtcEncodingVideoCapturer::EncodedVideoSourceClient::OnConfigChanged( |
| 114 const media::RuntimeVideoEncodingParameters& params) { |
| 115 params_.runtime_params = params; |
| 116 } |
| 117 |
| 118 void RtcEncodingVideoCapturer::EncodedVideoSourceClient::ReportEncodedFrame( |
| 119 scoped_refptr<const media::EncodedBitstreamBuffer> buffer) { |
| 120 if (!callback_) |
| 121 return; |
| 122 |
| 123 webrtc::EncodedImage image; |
| 124 webrtc::CodecSpecificInfo codecInfo; |
| 125 webrtc::RTPFragmentationHeader fragHeader; |
| 126 |
| 127 // TODO(hshi): remove this const_cast. Unfortunately webrtc::EncodedImage |
| 128 // defines member |_buffer| of type uint8_t* even though webrtc never modifies |
| 129 // the buffer contents. |
| 130 image._buffer = const_cast<uint8_t*>(buffer->buffer()); |
| 131 image._length = buffer->size(); |
| 132 image._size = image._length; |
| 133 |
| 134 const media::BufferEncodingMetadata& metadata = buffer->metadata(); |
| 135 base::TimeDelta capture_time = metadata.timestamp - time_base_; |
| 136 image.capture_time_ms_ = capture_time.InMilliseconds(); |
| 137 // Convert capture time to 90 kHz RTP timestamp. |
| 138 image._timeStamp = (capture_time * 90000).InSeconds(); |
| 139 if (metadata.key_frame) { |
| 140 image._frameType = webrtc::kKeyFrame; |
| 141 } else { |
| 142 image._frameType = webrtc::kDeltaFrame; |
| 143 } |
| 144 image._completeFrame = true; |
| 145 image._encodedWidth = params_.resolution.width(); |
| 146 image._encodedHeight = params_.resolution.height(); |
| 147 |
| 148 // TODO(hshi): generate codec specific info for VP8. |
| 149 codecInfo.codecType = rtc_codec_type_; |
| 150 |
| 151 // Generate header containing a single fragmentation. |
| 152 fragHeader.VerifyAndAllocateFragmentationHeader(1); |
| 153 fragHeader.fragmentationOffset[0] = 0; |
| 154 fragHeader.fragmentationLength[0] = buffer->size(); |
| 155 fragHeader.fragmentationPlType[0] = 0; |
| 156 fragHeader.fragmentationTimeDiff[0] = 0; |
| 157 |
| 158 callback_->Encoded(image, &codecInfo, &fragHeader); |
| 159 } |
| 160 |
| 161 // RtcEncodingVideoCapturer |
| 162 RtcEncodingVideoCapturer::RtcEncodingVideoCapturer( |
| 163 media::EncodedVideoSource* encoded_video_source, |
| 164 webrtc::VideoCodecType codec_type) |
| 165 : encoded_video_source_(encoded_video_source), |
| 166 rtc_codec_type_(codec_type) { |
| 167 } |
| 168 |
| 169 RtcEncodingVideoCapturer::~RtcEncodingVideoCapturer() { |
| 170 } |
| 171 |
| 172 int32_t RtcEncodingVideoCapturer::InitEncode( |
| 173 const webrtc::VideoCodec* codecSettings, |
| 174 int32_t numberOfCores, |
| 175 uint32_t maxPayloadSize) { |
| 176 DCHECK(!encoded_video_source_client_); |
| 177 if (codecSettings->codecType != rtc_codec_type_) |
| 178 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 179 |
| 180 // Convert |codecSettings| to |params|. |
| 181 media::VideoEncodingParameters params; |
| 182 params.codec_name = codecSettings->plName; |
| 183 params.resolution = gfx::Size(codecSettings->width, codecSettings->height); |
| 184 params.runtime_params.target_bitrate = codecSettings->startBitrate; |
| 185 params.runtime_params.max_bitrate = codecSettings->maxBitrate; |
| 186 params.runtime_params.frames_per_second = codecSettings->maxFramerate; |
| 187 encoded_video_source_client_.reset(new EncodedVideoSourceClient( |
| 188 encoded_video_source_, params, rtc_codec_type_)); |
| 189 return WEBRTC_VIDEO_CODEC_OK; |
| 190 } |
| 191 |
| 192 int32_t RtcEncodingVideoCapturer::Encode( |
| 193 const webrtc::I420VideoFrame& /* inputImage */, |
| 194 const webrtc::CodecSpecificInfo* codecSpecificInfo, |
| 195 const std::vector<webrtc::VideoFrameType>* frame_types) { |
| 196 // TODO(hshi): request specific frame type. |
| 197 return WEBRTC_VIDEO_CODEC_OK; |
| 198 } |
| 199 |
| 200 int32_t RtcEncodingVideoCapturer::RegisterEncodeCompleteCallback( |
| 201 webrtc::EncodedImageCallback* callback) { |
| 202 DCHECK(encoded_video_source_client_); |
| 203 encoded_video_source_client_->set_callback(callback); |
| 204 return WEBRTC_VIDEO_CODEC_OK; |
| 205 } |
| 206 |
| 207 int32_t RtcEncodingVideoCapturer::Release() { |
| 208 DCHECK(encoded_video_source_client_); |
| 209 encoded_video_source_client_.reset(NULL); |
| 210 return WEBRTC_VIDEO_CODEC_OK; |
| 211 } |
| 212 |
| 213 int32_t RtcEncodingVideoCapturer::SetChannelParameters( |
| 214 uint32_t /* packetLoss */, |
| 215 int rtt_in_ms) { |
| 216 if (!encoded_video_source_client_) |
| 217 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 218 encoded_video_source_client_->set_round_trip_time( |
| 219 base::TimeDelta::FromMilliseconds(rtt_in_ms)); |
| 220 return WEBRTC_VIDEO_CODEC_OK; |
| 221 } |
| 222 |
| 223 int32_t RtcEncodingVideoCapturer::SetRates(uint32_t newBitRate, |
| 224 uint32_t frameRate) { |
| 225 if (!encoded_video_source_client_) |
| 226 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 227 // TODO(hshi): wire up runtime rate control. |
| 228 return WEBRTC_VIDEO_CODEC_OK; |
| 229 } |
| 230 |
| 231 } // namespace content |
OLD | NEW |