niklase@google.com | 77ae29b | 2011-05-30 11:22:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 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 { |
| 16 | VideoCoder::VideoCoder(WebRtc_UWord32 instanceID) |
| 17 | : _instanceID( instanceID), |
| 18 | _vcm(VideoCodingModule::Create(instanceID)), |
| 19 | _decodedVideo(0) |
| 20 | { |
| 21 | _vcm->InitializeSender(); |
| 22 | _vcm->InitializeReceiver(); |
| 23 | |
| 24 | _vcm->RegisterTransportCallback(this); |
| 25 | _vcm->RegisterReceiveCallback(this); |
| 26 | } |
| 27 | |
| 28 | VideoCoder::~VideoCoder() |
| 29 | { |
| 30 | VideoCodingModule::Destroy(_vcm); |
| 31 | } |
| 32 | |
| 33 | WebRtc_Word32 VideoCoder::Reset() |
| 34 | { |
| 35 | _vcm->ResetDecoder(); |
| 36 | _vcm->ResetEncoder(); |
| 37 | |
| 38 | _vcm->InitializeSender(); |
| 39 | _vcm->InitializeReceiver(); |
| 40 | |
| 41 | _vcm->RegisterTransportCallback(this); |
| 42 | _vcm->RegisterReceiveCallback(this); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | WebRtc_Word32 VideoCoder::SetEncodeCodec(VideoCodec& videoCodecInst, |
| 47 | WebRtc_UWord32 numberOfCores, |
| 48 | WebRtc_UWord32 maxPayloadSize) |
| 49 | { |
| 50 | if(_vcm->RegisterSendCodec(&videoCodecInst, numberOfCores, |
| 51 | maxPayloadSize) != VCM_OK) |
| 52 | { |
| 53 | return -1; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | WebRtc_Word32 VideoCoder::SetDecodeCodec(VideoCodec& videoCodecInst, |
| 60 | WebRtc_Word32 numberOfCores) |
| 61 | { |
| 62 | if (videoCodecInst.plType == 0) |
| 63 | { |
| 64 | WebRtc_Word8 plType = DefaultPayloadType(videoCodecInst.plName); |
| 65 | if (plType == -1) |
| 66 | { |
| 67 | return -1; |
| 68 | } |
| 69 | videoCodecInst.plType = plType; |
| 70 | } |
| 71 | |
| 72 | if(_vcm->RegisterReceiveCodec(&videoCodecInst, numberOfCores) != VCM_OK) |
| 73 | { |
| 74 | return -1; |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | WebRtc_Word32 VideoCoder::CodecConfigParameters(WebRtc_UWord8* buffer, |
| 81 | WebRtc_Word32 size) |
| 82 | { |
| 83 | return _vcm->CodecConfigParameters(buffer, size); |
| 84 | } |
| 85 | |
| 86 | WebRtc_Word32 VideoCoder::SetCodecConfigParameters(WebRtc_UWord8 payloadType, |
| 87 | const WebRtc_UWord8* buffer, |
| 88 | WebRtc_Word32 length) |
| 89 | { |
| 90 | return _vcm->SetCodecConfigParameters(payloadType, buffer, length); |
| 91 | } |
| 92 | |
| 93 | WebRtc_Word32 VideoCoder::Decode(VideoFrame& decodedVideo, |
| 94 | const EncodedVideoData& encodedData) |
| 95 | { |
| 96 | decodedVideo.SetLength(0); |
| 97 | if(encodedData.payloadSize <= 0) |
| 98 | { |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | _decodedVideo = &decodedVideo; |
| 103 | if(_vcm->DecodeFromStorage(encodedData) != VCM_OK) |
| 104 | { |
| 105 | return -1; |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | WebRtc_Word32 VideoCoder::Encode(const VideoFrame& videoFrame, |
| 112 | EncodedVideoData& videoEncodedData) |
| 113 | { |
| 114 | // The AddVideoFrame(..) call will (indirectly) call SendData(). Store a |
| 115 | // pointer to videoFrame so that it can be updated. |
| 116 | _videoEncodedData = &videoEncodedData; |
| 117 | videoEncodedData.payloadSize = 0; |
| 118 | if(_vcm->AddVideoFrame(videoFrame) != VCM_OK) |
| 119 | { |
| 120 | return -1; |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | WebRtc_Word8 VideoCoder::DefaultPayloadType(const WebRtc_Word8* plName) |
| 126 | { |
| 127 | VideoCodec tmpCodec; |
| 128 | WebRtc_Word32 numberOfCodecs = _vcm->NumberOfCodecs(); |
| 129 | for (WebRtc_UWord8 i = 0; i < numberOfCodecs; i++) |
| 130 | { |
| 131 | _vcm->Codec(i, &tmpCodec); |
| 132 | if(strncmp(tmpCodec.plName, plName, kPayloadNameSize) == 0) |
| 133 | { |
| 134 | return tmpCodec.plType; |
| 135 | } |
| 136 | } |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | WebRtc_Word32 VideoCoder::FrameToRender(VideoFrame& videoFrame) |
| 141 | { |
| 142 | return _decodedVideo->CopyFrame(videoFrame); |
| 143 | } |
| 144 | |
| 145 | WebRtc_Word32 VideoCoder::SendData( |
| 146 | FrameType frameType, |
| 147 | WebRtc_UWord8 payloadType, |
| 148 | WebRtc_UWord32 timeStamp, |
| 149 | const WebRtc_UWord8* payloadData, |
| 150 | WebRtc_UWord32 payloadSize, |
| 151 | const RTPFragmentationHeader& fragmentationHeader) |
| 152 | { |
| 153 | // Store the data in _videoEncodedData which is a pointer to videoFrame in |
| 154 | // Encode(..) |
| 155 | _videoEncodedData->VerifyAndAllocate(payloadSize); |
| 156 | _videoEncodedData->frameType = frameType; |
| 157 | _videoEncodedData->payloadType = payloadType; |
| 158 | _videoEncodedData->timeStamp = timeStamp; |
| 159 | _videoEncodedData->fragmentationHeader = fragmentationHeader; |
| 160 | memcpy(_videoEncodedData->payloadData, payloadData, |
| 161 | sizeof(WebRtc_UWord8) * payloadSize); |
| 162 | _videoEncodedData->payloadSize = payloadSize; |
| 163 | return 0; |
| 164 | } |
| 165 | } // namespace webrtc |
| 166 | #endif // WEBRTC_MODULE_UTILITY_VIDEO |