blob: 2aff128ad98abddb9f1a6a829b5380e804b81517 [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 {
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000017VideoCoder::VideoCoder() : _vcm(VideoCodingModule::Create()), _decodedVideo(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000018 _vcm->InitializeSender();
19 _vcm->InitializeReceiver();
20
21 _vcm->RegisterTransportCallback(this);
22 _vcm->RegisterReceiveCallback(this);
23}
24
25VideoCoder::~VideoCoder()
26{
27 VideoCodingModule::Destroy(_vcm);
28}
29
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000030int32_t VideoCoder::SetEncodeCodec(VideoCodec& videoCodecInst,
31 uint32_t numberOfCores,
32 uint32_t maxPayloadSize)
niklase@google.com470e71d2011-07-07 08:21:25 +000033{
34 if(_vcm->RegisterSendCodec(&videoCodecInst, numberOfCores,
35 maxPayloadSize) != VCM_OK)
36 {
37 return -1;
38 }
39 return 0;
40}
41
42
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000043int32_t VideoCoder::SetDecodeCodec(VideoCodec& videoCodecInst,
44 int32_t numberOfCores)
niklase@google.com470e71d2011-07-07 08:21:25 +000045{
46 if (videoCodecInst.plType == 0)
47 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000048 int8_t plType = DefaultPayloadType(videoCodecInst.plName);
niklase@google.com470e71d2011-07-07 08:21:25 +000049 if (plType == -1)
50 {
51 return -1;
52 }
53 videoCodecInst.plType = plType;
54 }
55
56 if(_vcm->RegisterReceiveCodec(&videoCodecInst, numberOfCores) != VCM_OK)
57 {
58 return -1;
59 }
60 return 0;
61}
62
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000063int32_t VideoCoder::Decode(I420VideoFrame& decodedVideo,
64 const EncodedVideoData& encodedData)
niklase@google.com470e71d2011-07-07 08:21:25 +000065{
niklase@google.com470e71d2011-07-07 08:21:25 +000066 if(encodedData.payloadSize <= 0)
67 {
68 return -1;
69 }
70
71 _decodedVideo = &decodedVideo;
niklase@google.com470e71d2011-07-07 08:21:25 +000072 return 0;
73}
74
75
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000076int32_t VideoCoder::Encode(const I420VideoFrame& videoFrame,
77 EncodedVideoData& videoEncodedData)
niklase@google.com470e71d2011-07-07 08:21:25 +000078{
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.orgc75102e2013-04-09 13:32:55 +000090int8_t VideoCoder::DefaultPayloadType(const char* plName)
niklase@google.com470e71d2011-07-07 08:21:25 +000091{
92 VideoCodec tmpCodec;
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000093 int32_t numberOfCodecs = _vcm->NumberOfCodecs();
94 for (uint8_t i = 0; i < numberOfCodecs; i++)
niklase@google.com470e71d2011-07-07 08:21:25 +000095 {
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.orgc75102e2013-04-09 13:32:55 +0000105int32_t VideoCoder::FrameToRender(I420VideoFrame& videoFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000106{
107 return _decodedVideo->CopyFrame(videoFrame);
108}
109
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000110int32_t VideoCoder::SendData(
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000111 const uint8_t payloadType,
112 const EncodedImage& encoded_image,
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 const RTPFragmentationHeader& fragmentationHeader,
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000114 const RTPVideoHeader* /*rtpVideoHdr*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000115{
116 // Store the data in _videoEncodedData which is a pointer to videoFrame in
117 // Encode(..)
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000118 _videoEncodedData->VerifyAndAllocate(encoded_image._length);
119 _videoEncodedData->frameType =
120 VCMEncodedFrame::ConvertFrameType(encoded_image._frameType);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121 _videoEncodedData->payloadType = payloadType;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000122 _videoEncodedData->timeStamp = encoded_image._timeStamp;
andrew@webrtc.org418443c2012-11-23 19:17:23 +0000123 _videoEncodedData->fragmentationHeader.CopyFrom(fragmentationHeader);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000124 memcpy(_videoEncodedData->payloadData, encoded_image._buffer,
125 sizeof(uint8_t) * encoded_image._length);
126 _videoEncodedData->payloadSize = encoded_image._length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 return 0;
128}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000129} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000130#endif // WEBRTC_MODULE_UTILITY_VIDEO