Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/PeerConnection/objc_video_encoder_factory.mm

Issue 2966023002: Injectable Obj-C video codecs (Closed)
Patch Set: Support nil encoder / decoder. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2017 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 #include "webrtc/sdk/objc/Framework/Classes/PeerConnection/objc_video_encoder_fa ctory.h"
12
13 #import "RTCVideoCodec+Private.h"
14 #import "WebRTC/RTCVideoCodec.h"
15 #import "WebRTC/RTCVideoCodecFactory.h"
16 #import "WebRTC/RTCVideoCodecH264.h"
17 #import "WebRTC/RTCVideoFrame.h"
18 #import "WebRTC/RTCVideoFrameBuffer.h"
19
20 #include "webrtc/api/video/video_frame.h"
21 #include "webrtc/api/video_codecs/video_encoder.h"
22 #include "webrtc/base/logging.h"
23 #include "webrtc/base/timeutils.h"
24 #include "webrtc/modules/include/module_common_types.h"
25 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
26 #include "webrtc/modules/video_coding/include/video_error_codes.h"
27 #include "webrtc/sdk/objc/Framework/Classes/Common/helpers.h"
28 #include "webrtc/sdk/objc/Framework/Classes/Video/objc_frame_buffer.h"
29
30 namespace webrtc {
31
32 namespace {
33 class ObjCVideoEncoder : public VideoEncoder {
34 public:
35 ObjCVideoEncoder(id<RTCVideoEncoder> encoder) : encoder_(encoder) {}
36 ~ObjCVideoEncoder() {}
37
38 int32_t InitEncode(const VideoCodec *codec_settings,
39 int32_t number_of_cores,
40 size_t max_payload_size) {
41 RTCVideoEncoderSettings *settings =
42 [[RTCVideoEncoderSettings alloc] initWithVideoCodec:codec_settings];
43 [encoder_ initEncodeWithSettings:settings numberOfCores:number_of_cores];
44
45 return WEBRTC_VIDEO_CODEC_OK;
46 }
47
48 int32_t RegisterEncodeCompleteCallback(EncodedImageCallback *callback) {
49 [encoder_ setCallback:^(RTCEncodedImage *frame,
50 id<RTCCodecSpecificInfo> info,
51 RTCRtpFragmentationHeader *header) {
52 EncodedImage encodedImage = [frame toCpp];
53
54 // Handle types than can be converted into one of webrtc::CodecSpecificInf o's hard coded
55 // cases.
56 CodecSpecificInfo codecSpecificInfo;
57 if ([info isKindOfClass:[RTCCodecSpecificInfoH264 class]]) {
58 codecSpecificInfo = [(RTCCodecSpecificInfoH264 *)info toCpp];
59 }
60
61 RTPFragmentationHeader *fragmentationHeader = [header toCpp];
62 callback->OnEncodedImage(encodedImage, &codecSpecificInfo, fragmentationHe ader);
63 }];
64
65 return WEBRTC_VIDEO_CODEC_OK;
66 }
67
68 int32_t Release() {
69 [encoder_ releaseEncode];
70 return WEBRTC_VIDEO_CODEC_OK;
71 }
72
73 int32_t Encode(const VideoFrame &frame,
74 const CodecSpecificInfo *codec_specific_info,
75 const std::vector<FrameType> *frame_types) {
76 RTC_CHECK(frame.video_frame_buffer()->type() == VideoFrameBuffer::Type::kNat ive);
77
78 id<RTCVideoFrameBuffer> frame_buffer =
79 static_cast<ObjCFrameBuffer *>(frame.video_frame_buffer().get())->wrappe d_frame_buffer();
80 RTCVideoFrame *rtcFrame =
81 [[RTCVideoFrame alloc] initWithBuffer:frame_buffer
82 rotation:RTCVideoRotation(frame.rotation())
83 timeStampNs:frame.timestamp_us() * rtc::kNumNa nosecsPerMicrosec];
84 rtcFrame.timeStamp = frame.timestamp();
85
86 // webrtc::CodecSpecificInfo only handles a hard coded list of codecs
87 id<RTCCodecSpecificInfo> rtcCodecSpecificInfo = nil;
88 if (codec_specific_info) {
89 if (strcmp(codec_specific_info->codec_name, "H264") == 0) {
90 RTCCodecSpecificInfoH264 *h264Info = [[RTCCodecSpecificInfoH264 alloc] i nit];
91 h264Info.packetizationMode =
92 (RTCH264PacketizationMode)codec_specific_info->codecSpecific.H264.pa cketization_mode;
93 rtcCodecSpecificInfo = h264Info;
94 }
95 }
96
97 NSMutableArray<NSNumber *> *rtcFrameTypes = [NSMutableArray array];
98 for (size_t i = 0; i < frame_types->size(); ++i) {
99 [rtcFrameTypes addObject:@(RTCFrameType(frame_types->at(i)))];
100 }
101
102 [encoder_ encode:rtcFrame codecSpecificInfo:rtcCodecSpecificInfo frameTypes: rtcFrameTypes];
103 return WEBRTC_VIDEO_CODEC_OK;
104 }
105
106 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) {
107 return WEBRTC_VIDEO_CODEC_ERROR;
108 }
109
110 int32_t SetRates(uint32_t bitrate, uint32_t framerate) {
111 if ([encoder_ setBitrate:bitrate framerate:framerate]) {
112 return WEBRTC_VIDEO_CODEC_OK;
113 } else {
114 return WEBRTC_VIDEO_CODEC_ERROR;
115 }
116 }
117
118 bool SupportsNativeHandle() const { return true; }
119
120 private:
121 id<RTCVideoEncoder> encoder_;
122 };
123 } // namespace
124
125 ObjCVideoEncoderFactory::ObjCVideoEncoderFactory(id<RTCVideoEncoderFactory> enco der_factory)
126 : encoder_factory_(encoder_factory) {}
127
128 ObjCVideoEncoderFactory::~ObjCVideoEncoderFactory() {}
129
130 id<RTCVideoEncoderFactory> ObjCVideoEncoderFactory::wrapped_encoder_factory() co nst {
131 return encoder_factory_;
132 }
133
134 webrtc::VideoEncoder *ObjCVideoEncoderFactory::CreateVideoEncoder(
135 const cricket::VideoCodec &codec) {
136 RTCVideoCodecInfo *info = [[RTCVideoCodecInfo alloc] initWithVideoCodec:codec] ;
137 id<RTCVideoEncoder> encoder = [encoder_factory_ createEncoder:info];
138 return new ObjCVideoEncoder(encoder);
139 }
140
141 const std::vector<cricket::VideoCodec> &ObjCVideoEncoderFactory::supported_codec s() const {
142 supported_codecs_.clear();
143 for (RTCVideoCodecInfo *supportedCodec in encoder_factory_.supportedCodecs) {
144 cricket::VideoCodec codec = [supportedCodec toCpp];
145 supported_codecs_.push_back(codec);
146 }
147
148 return supported_codecs_;
149 }
150
151 void ObjCVideoEncoderFactory::DestroyVideoEncoder(webrtc::VideoEncoder *encoder) {
152 delete encoder;
153 encoder = nullptr;
154 }
155
156 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698