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