blob: 267ed810489008c7f82aca8c5cd07e756e64eab0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.org52fd98d2012-02-13 09:03:53 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.org8b062002013-07-12 08:28:10 +000013#include "webrtc/modules/utility/source/video_coder.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000014
15namespace webrtc {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000016VideoCoder::VideoCoder(uint32_t instanceID)
wu@webrtc.org2259f852012-06-19 14:56:50 +000017 : _vcm(VideoCodingModule::Create(instanceID)),
niklase@google.com470e71d2011-07-07 08:21:25 +000018 _decodedVideo(0)
19{
20 _vcm->InitializeSender();
21 _vcm->InitializeReceiver();
22
23 _vcm->RegisterTransportCallback(this);
24 _vcm->RegisterReceiveCallback(this);
25}
26
27VideoCoder::~VideoCoder()
28{
29 VideoCodingModule::Destroy(_vcm);
30}
31
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000032int32_t VideoCoder::SetEncodeCodec(VideoCodec& videoCodecInst,
33 uint32_t numberOfCores,
34 uint32_t maxPayloadSize)
niklase@google.com470e71d2011-07-07 08:21:25 +000035{
36 if(_vcm->RegisterSendCodec(&videoCodecInst, numberOfCores,
37 maxPayloadSize) != VCM_OK)
38 {
39 return -1;
40 }
41 return 0;
42}
43
44
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000045int32_t VideoCoder::SetDecodeCodec(VideoCodec& videoCodecInst,
46 int32_t numberOfCores)
niklase@google.com470e71d2011-07-07 08:21:25 +000047{
48 if (videoCodecInst.plType == 0)
49 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000050 int8_t plType = DefaultPayloadType(videoCodecInst.plName);
niklase@google.com470e71d2011-07-07 08:21:25 +000051 if (plType == -1)
52 {
53 return -1;
54 }
55 videoCodecInst.plType = plType;
56 }
57
58 if(_vcm->RegisterReceiveCodec(&videoCodecInst, numberOfCores) != VCM_OK)
59 {
60 return -1;
61 }
62 return 0;
63}
64
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000065int32_t VideoCoder::Decode(I420VideoFrame& decodedVideo,
66 const EncodedVideoData& encodedData)
niklase@google.com470e71d2011-07-07 08:21:25 +000067{
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000068 decodedVideo.ResetSize();
niklase@google.com470e71d2011-07-07 08:21:25 +000069 if(encodedData.payloadSize <= 0)
70 {
71 return -1;
72 }
73
74 _decodedVideo = &decodedVideo;
niklase@google.com470e71d2011-07-07 08:21:25 +000075 return 0;
76}
77
78
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000079int32_t VideoCoder::Encode(const I420VideoFrame& videoFrame,
80 EncodedVideoData& videoEncodedData)
niklase@google.com470e71d2011-07-07 08:21:25 +000081{
82 // The AddVideoFrame(..) call will (indirectly) call SendData(). Store a
83 // pointer to videoFrame so that it can be updated.
84 _videoEncodedData = &videoEncodedData;
85 videoEncodedData.payloadSize = 0;
86 if(_vcm->AddVideoFrame(videoFrame) != VCM_OK)
87 {
88 return -1;
89 }
90 return 0;
91}
92
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000093int8_t VideoCoder::DefaultPayloadType(const char* plName)
niklase@google.com470e71d2011-07-07 08:21:25 +000094{
95 VideoCodec tmpCodec;
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000096 int32_t numberOfCodecs = _vcm->NumberOfCodecs();
97 for (uint8_t i = 0; i < numberOfCodecs; i++)
niklase@google.com470e71d2011-07-07 08:21:25 +000098 {
99 _vcm->Codec(i, &tmpCodec);
100 if(strncmp(tmpCodec.plName, plName, kPayloadNameSize) == 0)
101 {
102 return tmpCodec.plType;
103 }
104 }
105 return -1;
106}
107
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000108int32_t VideoCoder::FrameToRender(I420VideoFrame& videoFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000109{
110 return _decodedVideo->CopyFrame(videoFrame);
111}
112
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000113int32_t VideoCoder::SendData(
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000114 const FrameType frameType,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000115 const uint8_t payloadType,
116 const uint32_t timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000117 int64_t capture_time_ms,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000118 const uint8_t* payloadData,
119 uint32_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 const RTPFragmentationHeader& fragmentationHeader,
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000121 const RTPVideoHeader* /*rtpVideoHdr*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000122{
123 // Store the data in _videoEncodedData which is a pointer to videoFrame in
124 // Encode(..)
125 _videoEncodedData->VerifyAndAllocate(payloadSize);
126 _videoEncodedData->frameType = frameType;
127 _videoEncodedData->payloadType = payloadType;
128 _videoEncodedData->timeStamp = timeStamp;
andrew@webrtc.org418443c2012-11-23 19:17:23 +0000129 _videoEncodedData->fragmentationHeader.CopyFrom(fragmentationHeader);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 memcpy(_videoEncodedData->payloadData, payloadData,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000131 sizeof(uint8_t) * payloadSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000132 _videoEncodedData->payloadSize = payloadSize;
133 return 0;
134}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000135} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000136#endif // WEBRTC_MODULE_UTILITY_VIDEO