niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | 52fd98d | 2012-02-13 09:03:53 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [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 | |
| 11 | #ifdef WEBRTC_MODULE_UTILITY_VIDEO |
| 12 | |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 13 | #include "webrtc/modules/utility/source/video_coder.h" |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 14 | #include "webrtc/modules/video_coding/main/source/encoded_frame.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 17 | VideoCoder::VideoCoder() |
| 18 | : _vcm(VideoCodingModule::Create(nullptr)), _decodedVideo(0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | _vcm->InitializeSender(); |
| 20 | _vcm->InitializeReceiver(); |
| 21 | |
| 22 | _vcm->RegisterTransportCallback(this); |
| 23 | _vcm->RegisterReceiveCallback(this); |
| 24 | } |
| 25 | |
| 26 | VideoCoder::~VideoCoder() |
| 27 | { |
| 28 | VideoCodingModule::Destroy(_vcm); |
| 29 | } |
| 30 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 31 | int32_t VideoCoder::SetEncodeCodec(VideoCodec& videoCodecInst, |
| 32 | uint32_t numberOfCores, |
| 33 | uint32_t maxPayloadSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | { |
| 35 | if(_vcm->RegisterSendCodec(&videoCodecInst, numberOfCores, |
| 36 | maxPayloadSize) != VCM_OK) |
| 37 | { |
| 38 | return -1; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 44 | int32_t VideoCoder::SetDecodeCodec(VideoCodec& videoCodecInst, |
| 45 | int32_t numberOfCores) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | { |
| 47 | if (videoCodecInst.plType == 0) |
| 48 | { |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 49 | int8_t plType = DefaultPayloadType(videoCodecInst.plName); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | if (plType == -1) |
| 51 | { |
| 52 | return -1; |
| 53 | } |
| 54 | videoCodecInst.plType = plType; |
| 55 | } |
| 56 | |
| 57 | if(_vcm->RegisterReceiveCodec(&videoCodecInst, numberOfCores) != VCM_OK) |
| 58 | { |
| 59 | return -1; |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 64 | int32_t VideoCoder::Decode(I420VideoFrame& decodedVideo, |
| 65 | const EncodedVideoData& encodedData) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | { |
magjed@webrtc.org | 7400e0b | 2015-02-27 15:18:26 +0000 | [diff] [blame] | 67 | decodedVideo.ResetSize(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | if(encodedData.payloadSize <= 0) |
| 69 | { |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | _decodedVideo = &decodedVideo; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 78 | int32_t VideoCoder::Encode(const I420VideoFrame& videoFrame, |
| 79 | EncodedVideoData& videoEncodedData) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | { |
| 81 | // The AddVideoFrame(..) call will (indirectly) call SendData(). Store a |
| 82 | // pointer to videoFrame so that it can be updated. |
| 83 | _videoEncodedData = &videoEncodedData; |
| 84 | videoEncodedData.payloadSize = 0; |
| 85 | if(_vcm->AddVideoFrame(videoFrame) != VCM_OK) |
| 86 | { |
| 87 | return -1; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 92 | int8_t VideoCoder::DefaultPayloadType(const char* plName) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | { |
| 94 | VideoCodec tmpCodec; |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 95 | int32_t numberOfCodecs = _vcm->NumberOfCodecs(); |
| 96 | for (uint8_t i = 0; i < numberOfCodecs; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | { |
| 98 | _vcm->Codec(i, &tmpCodec); |
| 99 | if(strncmp(tmpCodec.plName, plName, kPayloadNameSize) == 0) |
| 100 | { |
| 101 | return tmpCodec.plType; |
| 102 | } |
| 103 | } |
| 104 | return -1; |
| 105 | } |
| 106 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 107 | int32_t VideoCoder::FrameToRender(I420VideoFrame& videoFrame) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | { |
| 109 | return _decodedVideo->CopyFrame(videoFrame); |
| 110 | } |
| 111 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 112 | int32_t VideoCoder::SendData( |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 113 | const uint8_t payloadType, |
| 114 | const EncodedImage& encoded_image, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | const RTPFragmentationHeader& fragmentationHeader, |
pwestin@webrtc.org | 1da1ce0 | 2011-10-13 15:19:55 +0000 | [diff] [blame] | 116 | const RTPVideoHeader* /*rtpVideoHdr*/) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | { |
| 118 | // Store the data in _videoEncodedData which is a pointer to videoFrame in |
| 119 | // Encode(..) |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 120 | _videoEncodedData->VerifyAndAllocate(encoded_image._length); |
| 121 | _videoEncodedData->frameType = |
| 122 | VCMEncodedFrame::ConvertFrameType(encoded_image._frameType); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | _videoEncodedData->payloadType = payloadType; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 124 | _videoEncodedData->timeStamp = encoded_image._timeStamp; |
andrew@webrtc.org | 418443c | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 125 | _videoEncodedData->fragmentationHeader.CopyFrom(fragmentationHeader); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 126 | memcpy(_videoEncodedData->payloadData, encoded_image._buffer, |
| 127 | sizeof(uint8_t) * encoded_image._length); |
| 128 | _videoEncodedData->payloadSize = encoded_image._length; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | return 0; |
| 130 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 131 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | #endif // WEBRTC_MODULE_UTILITY_VIDEO |