niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 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 | |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 | [diff] [blame] | 11 | #include "webrtc/common_types.h" |
| 12 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 13 | #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" |
| 14 | #include "webrtc/modules/video_coding/main/source/encoded_frame.h" |
| 15 | #include "webrtc/modules/video_coding/main/source/jitter_buffer.h" |
| 16 | #include "webrtc/modules/video_coding/main/source/packet.h" |
| 17 | #include "webrtc/modules/video_coding/main/source/video_coding_impl.h" |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/interface/clock.h" |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/interface/trace.h" |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/trace_event.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 21 | |
agalusza@google.com | a7e360e | 2013-08-01 03:15:08 | [diff] [blame] | 22 | namespace webrtc { |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame^] | 23 | namespace vcm { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 24 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 | [diff] [blame] | 25 | uint32_t |
agalusza@google.com | a7e360e | 2013-08-01 03:15:08 | [diff] [blame] | 26 | VCMProcessTimer::Period() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 27 | return _periodMs; |
| 28 | } |
| 29 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 | [diff] [blame] | 30 | uint32_t |
agalusza@google.com | a7e360e | 2013-08-01 03:15:08 | [diff] [blame] | 31 | VCMProcessTimer::TimeUntilProcess() const { |
stefan@webrtc.org | d98e784 | 2013-05-08 06:38:53 | [diff] [blame] | 32 | const int64_t time_since_process = _clock->TimeInMilliseconds() - |
| 33 | static_cast<int64_t>(_latestMs); |
| 34 | const int64_t time_until_process = static_cast<int64_t>(_periodMs) - |
| 35 | time_since_process; |
| 36 | if (time_until_process < 0) |
| 37 | return 0; |
| 38 | return time_until_process; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void |
agalusza@google.com | a7e360e | 2013-08-01 03:15:08 | [diff] [blame] | 42 | VCMProcessTimer::Processed() { |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 | [diff] [blame] | 43 | _latestMs = _clock->TimeInMilliseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 44 | } |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame^] | 45 | } // namespace vcm |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 46 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame^] | 47 | namespace { |
| 48 | class VideoCodingModuleImpl : public VideoCodingModule { |
| 49 | public: |
| 50 | VideoCodingModuleImpl(const int32_t id, |
| 51 | Clock* clock, |
| 52 | EventFactory* event_factory, |
| 53 | bool owns_event_factory) |
| 54 | : VideoCodingModule(), |
| 55 | sender_(new vcm::VideoSender(id, clock)), |
| 56 | receiver_(new vcm::VideoReceiver(id, clock, event_factory)), |
| 57 | own_event_factory_(owns_event_factory ? event_factory : NULL) {} |
| 58 | |
| 59 | virtual ~VideoCodingModuleImpl() { |
| 60 | sender_.reset(); |
| 61 | receiver_.reset(); |
| 62 | own_event_factory_.reset(); |
| 63 | } |
| 64 | |
| 65 | virtual int32_t TimeUntilNextProcess() OVERRIDE { |
| 66 | int32_t sender_time = sender_->TimeUntilNextProcess(); |
| 67 | int32_t receiver_time = receiver_->TimeUntilNextProcess(); |
| 68 | assert(sender_time >= 0); |
| 69 | assert(receiver_time >= 0); |
| 70 | return VCM_MIN(sender_time, receiver_time); |
| 71 | } |
| 72 | |
| 73 | virtual int32_t Process() OVERRIDE { |
| 74 | int32_t sender_return = sender_->Process(); |
| 75 | int32_t receiver_return = receiver_->Process(); |
| 76 | if (sender_return != VCM_OK) |
| 77 | return sender_return; |
| 78 | return receiver_return; |
| 79 | } |
| 80 | |
| 81 | virtual int32_t InitializeSender() OVERRIDE { |
| 82 | return sender_->InitializeSender(); |
| 83 | } |
| 84 | |
| 85 | virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec, |
| 86 | uint32_t numberOfCores, |
| 87 | uint32_t maxPayloadSize) OVERRIDE { |
| 88 | return sender_->RegisterSendCodec(sendCodec, numberOfCores, maxPayloadSize); |
| 89 | } |
| 90 | |
| 91 | virtual int32_t SendCodec(VideoCodec* currentSendCodec) const OVERRIDE { |
| 92 | return sender_->SendCodec(currentSendCodec); |
| 93 | } |
| 94 | |
| 95 | virtual VideoCodecType SendCodec() const OVERRIDE { |
| 96 | return sender_->SendCodec(); |
| 97 | } |
| 98 | |
| 99 | virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder, |
| 100 | uint8_t payloadType, |
| 101 | bool internalSource) OVERRIDE { |
| 102 | return sender_->RegisterExternalEncoder( |
| 103 | externalEncoder, payloadType, internalSource); |
| 104 | } |
| 105 | |
| 106 | virtual int32_t CodecConfigParameters(uint8_t* buffer, int32_t size) |
| 107 | OVERRIDE { |
| 108 | return sender_->CodecConfigParameters(buffer, size); |
| 109 | } |
| 110 | |
| 111 | virtual int Bitrate(unsigned int* bitrate) const OVERRIDE { |
| 112 | return sender_->Bitrate(bitrate); |
| 113 | } |
| 114 | |
| 115 | virtual int FrameRate(unsigned int* framerate) const OVERRIDE { |
| 116 | return sender_->FrameRate(framerate); |
| 117 | } |
| 118 | |
| 119 | virtual int32_t SetChannelParameters(uint32_t target_bitrate, // bits/s. |
| 120 | uint8_t lossRate, |
| 121 | uint32_t rtt) OVERRIDE { |
| 122 | return sender_->SetChannelParameters(target_bitrate, lossRate, rtt); |
| 123 | } |
| 124 | |
| 125 | virtual int32_t RegisterTransportCallback(VCMPacketizationCallback* transport) |
| 126 | OVERRIDE { |
| 127 | return sender_->RegisterTransportCallback(transport); |
| 128 | } |
| 129 | |
| 130 | virtual int32_t RegisterSendStatisticsCallback( |
| 131 | VCMSendStatisticsCallback* sendStats) OVERRIDE { |
| 132 | return sender_->RegisterSendStatisticsCallback(sendStats); |
| 133 | } |
| 134 | |
| 135 | virtual int32_t RegisterVideoQMCallback( |
| 136 | VCMQMSettingsCallback* videoQMSettings) OVERRIDE { |
| 137 | return sender_->RegisterVideoQMCallback(videoQMSettings); |
| 138 | } |
| 139 | |
| 140 | virtual int32_t RegisterProtectionCallback(VCMProtectionCallback* protection) |
| 141 | OVERRIDE { |
| 142 | return sender_->RegisterProtectionCallback(protection); |
| 143 | } |
| 144 | |
| 145 | virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection, |
| 146 | bool enable) OVERRIDE { |
| 147 | int32_t sender_return = |
| 148 | sender_->SetVideoProtection(videoProtection, enable); |
| 149 | int32_t receiver_return = |
| 150 | receiver_->SetVideoProtection(videoProtection, enable); |
| 151 | if (sender_return == VCM_OK) |
| 152 | return receiver_return; |
| 153 | return sender_return; |
| 154 | } |
| 155 | |
| 156 | virtual int32_t AddVideoFrame(const I420VideoFrame& videoFrame, |
| 157 | const VideoContentMetrics* contentMetrics, |
| 158 | const CodecSpecificInfo* codecSpecificInfo) |
| 159 | OVERRIDE { |
| 160 | return sender_->AddVideoFrame( |
| 161 | videoFrame, contentMetrics, codecSpecificInfo); |
| 162 | } |
| 163 | |
| 164 | virtual int32_t IntraFrameRequest(int stream_index) OVERRIDE { |
| 165 | return sender_->IntraFrameRequest(stream_index); |
| 166 | } |
| 167 | |
| 168 | virtual int32_t EnableFrameDropper(bool enable) OVERRIDE { |
| 169 | return sender_->EnableFrameDropper(enable); |
| 170 | } |
| 171 | |
| 172 | virtual int32_t SentFrameCount(VCMFrameCount& frameCount) const OVERRIDE { |
| 173 | return sender_->SentFrameCount(&frameCount); |
| 174 | } |
| 175 | |
| 176 | virtual int SetSenderNackMode(SenderNackMode mode) OVERRIDE { |
| 177 | return sender_->SetSenderNackMode(mode); |
| 178 | } |
| 179 | |
| 180 | virtual int SetSenderReferenceSelection(bool enable) OVERRIDE { |
| 181 | return sender_->SetSenderReferenceSelection(enable); |
| 182 | } |
| 183 | |
| 184 | virtual int SetSenderFEC(bool enable) OVERRIDE { |
| 185 | return sender_->SetSenderFEC(enable); |
| 186 | } |
| 187 | |
| 188 | virtual int SetSenderKeyFramePeriod(int periodMs) OVERRIDE { |
| 189 | return sender_->SetSenderKeyFramePeriod(periodMs); |
| 190 | } |
| 191 | |
| 192 | virtual int StartDebugRecording(const char* file_name_utf8) OVERRIDE { |
| 193 | return sender_->StartDebugRecording(file_name_utf8); |
| 194 | } |
| 195 | |
| 196 | virtual int StopDebugRecording() OVERRIDE { |
| 197 | return sender_->StopDebugRecording(); |
| 198 | } |
| 199 | |
| 200 | virtual int32_t InitializeReceiver() OVERRIDE { |
| 201 | return receiver_->InitializeReceiver(); |
| 202 | } |
| 203 | |
| 204 | virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec, |
| 205 | int32_t numberOfCores, |
| 206 | bool requireKeyFrame) OVERRIDE { |
| 207 | return receiver_->RegisterReceiveCodec( |
| 208 | receiveCodec, numberOfCores, requireKeyFrame); |
| 209 | } |
| 210 | |
| 211 | virtual int32_t RegisterExternalDecoder(VideoDecoder* externalDecoder, |
| 212 | uint8_t payloadType, |
| 213 | bool internalRenderTiming) OVERRIDE { |
| 214 | return receiver_->RegisterExternalDecoder( |
| 215 | externalDecoder, payloadType, internalRenderTiming); |
| 216 | } |
| 217 | |
| 218 | virtual int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback) |
| 219 | OVERRIDE { |
| 220 | return receiver_->RegisterReceiveCallback(receiveCallback); |
| 221 | } |
| 222 | |
| 223 | virtual int32_t RegisterReceiveStatisticsCallback( |
| 224 | VCMReceiveStatisticsCallback* receiveStats) OVERRIDE { |
| 225 | return receiver_->RegisterReceiveStatisticsCallback(receiveStats); |
| 226 | } |
| 227 | |
| 228 | virtual int32_t RegisterFrameTypeCallback( |
| 229 | VCMFrameTypeCallback* frameTypeCallback) OVERRIDE { |
| 230 | return receiver_->RegisterFrameTypeCallback(frameTypeCallback); |
| 231 | } |
| 232 | |
| 233 | virtual int32_t RegisterPacketRequestCallback( |
| 234 | VCMPacketRequestCallback* callback) OVERRIDE { |
| 235 | return receiver_->RegisterPacketRequestCallback(callback); |
| 236 | } |
| 237 | |
| 238 | virtual int RegisterRenderBufferSizeCallback( |
| 239 | VCMRenderBufferSizeCallback* callback) OVERRIDE { |
| 240 | return receiver_->RegisterRenderBufferSizeCallback(callback); |
| 241 | } |
| 242 | |
| 243 | virtual int32_t Decode(uint16_t maxWaitTimeMs) OVERRIDE { |
| 244 | return receiver_->Decode(maxWaitTimeMs); |
| 245 | } |
| 246 | |
| 247 | virtual int32_t DecodeDualFrame(uint16_t maxWaitTimeMs) OVERRIDE { |
| 248 | return receiver_->DecodeDualFrame(maxWaitTimeMs); |
| 249 | } |
| 250 | |
| 251 | virtual int32_t ResetDecoder() OVERRIDE { return receiver_->ResetDecoder(); } |
| 252 | |
| 253 | virtual int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const { |
| 254 | return receiver_->ReceiveCodec(currentReceiveCodec); |
| 255 | } |
| 256 | |
| 257 | virtual VideoCodecType ReceiveCodec() const OVERRIDE { |
| 258 | return receiver_->ReceiveCodec(); |
| 259 | } |
| 260 | |
| 261 | virtual int32_t IncomingPacket(const uint8_t* incomingPayload, |
| 262 | uint32_t payloadLength, |
| 263 | const WebRtcRTPHeader& rtpInfo) OVERRIDE { |
| 264 | return receiver_->IncomingPacket(incomingPayload, payloadLength, rtpInfo); |
| 265 | } |
| 266 | |
| 267 | virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) OVERRIDE { |
| 268 | return receiver_->SetMinimumPlayoutDelay(minPlayoutDelayMs); |
| 269 | } |
| 270 | |
| 271 | virtual int32_t SetRenderDelay(uint32_t timeMS) OVERRIDE { |
| 272 | return receiver_->SetRenderDelay(timeMS); |
| 273 | } |
| 274 | |
| 275 | virtual int32_t Delay() const OVERRIDE { return receiver_->Delay(); } |
| 276 | |
| 277 | virtual int32_t ReceivedFrameCount(VCMFrameCount& frameCount) const OVERRIDE { |
| 278 | return receiver_->ReceivedFrameCount(&frameCount); |
| 279 | } |
| 280 | |
| 281 | virtual uint32_t DiscardedPackets() const OVERRIDE { |
| 282 | return receiver_->DiscardedPackets(); |
| 283 | } |
| 284 | |
| 285 | virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode, |
| 286 | VCMDecodeErrorMode errorMode) OVERRIDE { |
| 287 | return receiver_->SetReceiverRobustnessMode(robustnessMode, errorMode); |
| 288 | } |
| 289 | |
| 290 | virtual void SetNackSettings(size_t max_nack_list_size, |
| 291 | int max_packet_age_to_nack, |
| 292 | int max_incomplete_time_ms) OVERRIDE { |
| 293 | return receiver_->SetNackSettings( |
| 294 | max_nack_list_size, max_packet_age_to_nack, max_incomplete_time_ms); |
| 295 | } |
| 296 | |
| 297 | void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) OVERRIDE { |
| 298 | return receiver_->SetDecodeErrorMode(decode_error_mode); |
| 299 | } |
| 300 | |
| 301 | virtual int SetMinReceiverDelay(int desired_delay_ms) OVERRIDE { |
| 302 | return receiver_->SetMinReceiverDelay(desired_delay_ms); |
| 303 | } |
| 304 | |
| 305 | virtual int32_t SetReceiveChannelParameters(uint32_t rtt) OVERRIDE { |
| 306 | return receiver_->SetReceiveChannelParameters(rtt); |
| 307 | } |
| 308 | |
| 309 | private: |
| 310 | scoped_ptr<vcm::VideoSender> sender_; |
| 311 | scoped_ptr<vcm::VideoReceiver> receiver_; |
| 312 | scoped_ptr<EventFactory> own_event_factory_; |
| 313 | }; |
| 314 | } // namespace |
| 315 | |
| 316 | uint8_t VideoCodingModule::NumberOfCodecs() { |
| 317 | return VCMCodecDataBase::NumberOfCodecs(); |
| 318 | } |
| 319 | |
| 320 | int32_t VideoCodingModule::Codec(uint8_t listId, VideoCodec* codec) { |
| 321 | if (codec == NULL) { |
| 322 | return VCM_PARAMETER_ERROR; |
| 323 | } |
| 324 | return VCMCodecDataBase::Codec(listId, codec) ? 0 : -1; |
| 325 | } |
| 326 | |
| 327 | int32_t VideoCodingModule::Codec(VideoCodecType codecType, VideoCodec* codec) { |
| 328 | if (codec == NULL) { |
| 329 | return VCM_PARAMETER_ERROR; |
| 330 | } |
| 331 | return VCMCodecDataBase::Codec(codecType, codec) ? 0 : -1; |
| 332 | } |
| 333 | |
| 334 | VideoCodingModule* VideoCodingModule::Create(const int32_t id) { |
| 335 | return new VideoCodingModuleImpl( |
| 336 | id, Clock::GetRealTimeClock(), new EventFactoryImpl, true); |
| 337 | } |
| 338 | |
| 339 | VideoCodingModule* VideoCodingModule::Create(const int32_t id, |
stefan@webrtc.org | 2baf5f5 | 2013-03-13 08:46:25 | [diff] [blame] | 340 | Clock* clock, |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame^] | 341 | EventFactory* event_factory) { |
| 342 | assert(clock); |
| 343 | assert(event_factory); |
| 344 | return new VideoCodingModuleImpl(id, clock, event_factory, false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 345 | } |
| 346 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame^] | 347 | void VideoCodingModule::Destroy(VideoCodingModule* module) { |
| 348 | if (module != NULL) { |
| 349 | delete static_cast<VideoCodingModuleImpl*>(module); |
stefan@webrtc.org | 439be29 | 2012-02-16 14:45:37 | [diff] [blame] | 350 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 351 | } |
stefan@webrtc.org | 791eec7 | 2011-10-11 07:53:43 | [diff] [blame] | 352 | } // namespace webrtc |