blob: 78a99f0d1c7e0deee4d09da30e696c2fbfdd0156 [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"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000014#include "webrtc/modules/video_coding/main/source/encoded_frame.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16namespace webrtc {
pbos@webrtc.org891d4832015-02-26 13:15:22 +000017VideoCoder::VideoCoder()
18 : _vcm(VideoCodingModule::Create(nullptr)), _decodedVideo(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000019 _vcm->InitializeSender();
20 _vcm->InitializeReceiver();
21
22 _vcm->RegisterTransportCallback(this);
23 _vcm->RegisterReceiveCallback(this);
24}
25
26VideoCoder::~VideoCoder()
27{
28 VideoCodingModule::Destroy(_vcm);
29}
30
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000031int32_t VideoCoder::SetEncodeCodec(VideoCodec& videoCodecInst,
32 uint32_t numberOfCores,
33 uint32_t maxPayloadSize)
niklase@google.com470e71d2011-07-07 08:21:25 +000034{
35 if(_vcm->RegisterSendCodec(&videoCodecInst, numberOfCores,
36 maxPayloadSize) != VCM_OK)
37 {
38 return -1;
39 }
40 return 0;
41}
42
43
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000044int32_t VideoCoder::SetDecodeCodec(VideoCodec& videoCodecInst,
45 int32_t numberOfCores)
niklase@google.com470e71d2011-07-07 08:21:25 +000046{
47 if (videoCodecInst.plType == 0)
48 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000049 int8_t plType = DefaultPayloadType(videoCodecInst.plName);
niklase@google.com470e71d2011-07-07 08:21:25 +000050 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.orgc75102e2013-04-09 13:32:55 +000064int32_t VideoCoder::Decode(I420VideoFrame& decodedVideo,
65 const EncodedVideoData& encodedData)
niklase@google.com470e71d2011-07-07 08:21:25 +000066{
niklase@google.com470e71d2011-07-07 08:21:25 +000067 if(encodedData.payloadSize <= 0)
68 {
69 return -1;
70 }
71
72 _decodedVideo = &decodedVideo;
niklase@google.com470e71d2011-07-07 08:21:25 +000073 return 0;
74}
75
76
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000077int32_t VideoCoder::Encode(const I420VideoFrame& videoFrame,
78 EncodedVideoData& videoEncodedData)
niklase@google.com470e71d2011-07-07 08:21:25 +000079{
80 // The AddVideoFrame(..) call will (indirectly) call SendData(). Store a
81 // pointer to videoFrame so that it can be updated.
82 _videoEncodedData = &videoEncodedData;
83 videoEncodedData.payloadSize = 0;
84 if(_vcm->AddVideoFrame(videoFrame) != VCM_OK)
85 {
86 return -1;
87 }
88 return 0;
89}
90
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000091int8_t VideoCoder::DefaultPayloadType(const char* plName)
niklase@google.com470e71d2011-07-07 08:21:25 +000092{
93 VideoCodec tmpCodec;
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000094 int32_t numberOfCodecs = _vcm->NumberOfCodecs();
95 for (uint8_t i = 0; i < numberOfCodecs; i++)
niklase@google.com470e71d2011-07-07 08:21:25 +000096 {
97 _vcm->Codec(i, &tmpCodec);
98 if(strncmp(tmpCodec.plName, plName, kPayloadNameSize) == 0)
99 {
100 return tmpCodec.plType;
101 }
102 }
103 return -1;
104}
105
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000106int32_t VideoCoder::FrameToRender(I420VideoFrame& videoFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000107{
108 return _decodedVideo->CopyFrame(videoFrame);
109}
110
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000111int32_t VideoCoder::SendData(
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000112 const uint8_t payloadType,
113 const EncodedImage& encoded_image,
niklase@google.com470e71d2011-07-07 08:21:25 +0000114 const RTPFragmentationHeader& fragmentationHeader,
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000115 const RTPVideoHeader* /*rtpVideoHdr*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000116{
117 // Store the data in _videoEncodedData which is a pointer to videoFrame in
118 // Encode(..)
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000119 _videoEncodedData->VerifyAndAllocate(encoded_image._length);
120 _videoEncodedData->frameType =
121 VCMEncodedFrame::ConvertFrameType(encoded_image._frameType);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122 _videoEncodedData->payloadType = payloadType;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000123 _videoEncodedData->timeStamp = encoded_image._timeStamp;
andrew@webrtc.org418443c2012-11-23 19:17:23 +0000124 _videoEncodedData->fragmentationHeader.CopyFrom(fragmentationHeader);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000125 memcpy(_videoEncodedData->payloadData, encoded_image._buffer,
126 sizeof(uint8_t) * encoded_image._length);
127 _videoEncodedData->payloadSize = encoded_image._length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 return 0;
129}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000130} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000131#endif // WEBRTC_MODULE_UTILITY_VIDEO