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