blob: 0814dae376324f9de58125fe952cefbed0e0267c [file] [log] [blame]
perkj@webrtc.org83bc7212015-02-11 11:26:56 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +00009 */
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000010
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/androidvideocapturer.h"
12
13#include "webrtc/api/java/jni/native_handle_impl.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000014#include "webrtc/base/common.h"
15#include "webrtc/base/json.h"
16#include "webrtc/base/timeutils.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010017#include "webrtc/media/engine/webrtcvideoframe.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000018
19namespace webrtc {
20
Magnus Jedvertc464f502015-08-25 23:22:08 +020021// A hack for avoiding deep frame copies in
22// cricket::VideoCapturer.SignalFrameCaptured() using a custom FrameFactory.
23// A frame is injected using UpdateCapturedFrame(), and converted into a
24// cricket::VideoFrame with CreateAliasedFrame(). UpdateCapturedFrame() should
25// be called before CreateAliasedFrame() for every frame.
26// TODO(magjed): Add an interface cricket::VideoCapturer::OnFrameCaptured()
27// for ref counted I420 frames instead of this hack.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000028class AndroidVideoCapturer::FrameFactory : public cricket::VideoFrameFactory {
29 public:
Pera5092412016-02-12 13:30:57 +010030 explicit FrameFactory(
31 const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
magjedb09b6602015-10-01 03:02:44 -070032 : delegate_(delegate) {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000033 // Create a CapturedFrame that only contains header information, not the
34 // actual pixel data.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000035 captured_frame_.pixel_height = 1;
36 captured_frame_.pixel_width = 1;
Magnus Jedvertc464f502015-08-25 23:22:08 +020037 captured_frame_.data = nullptr;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000038 captured_frame_.data_size = cricket::CapturedFrame::kUnknownDataSize;
Peter Boström0c4e06b2015-10-07 12:23:21 +020039 captured_frame_.fourcc = static_cast<uint32_t>(cricket::FOURCC_ANY);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000040 }
41
Magnus Jedvertc464f502015-08-25 23:22:08 +020042 void UpdateCapturedFrame(
43 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
44 int rotation,
Peter Boström0c4e06b2015-10-07 12:23:21 +020045 int64_t time_stamp_in_ns) {
perkj7755e202015-11-19 12:02:21 -080046 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
47 rotation == 270);
Magnus Jedvertc464f502015-08-25 23:22:08 +020048 buffer_ = buffer;
49 captured_frame_.width = buffer->width();
50 captured_frame_.height = buffer->height();
Per33544192015-04-02 12:30:51 +020051 captured_frame_.time_stamp = time_stamp_in_ns;
perkj7755e202015-11-19 12:02:21 -080052 captured_frame_.rotation = static_cast<webrtc::VideoRotation>(rotation);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000053 }
54
Magnus Jedvertc464f502015-08-25 23:22:08 +020055 void ClearCapturedFrame() {
56 buffer_ = nullptr;
magjedfcf8ece2015-08-06 04:00:16 -070057 captured_frame_.width = 0;
58 captured_frame_.height = 0;
magjedfcf8ece2015-08-06 04:00:16 -070059 captured_frame_.time_stamp = 0;
magjedfcf8ece2015-08-06 04:00:16 -070060 }
61
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000062 const cricket::CapturedFrame* GetCapturedFrame() const {
63 return &captured_frame_;
64 }
65
66 cricket::VideoFrame* CreateAliasedFrame(
67 const cricket::CapturedFrame* captured_frame,
68 int dst_width,
69 int dst_height) const override {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000070 // Check that captured_frame is actually our frame.
henrikg91d6ede2015-09-17 00:24:34 -070071 RTC_CHECK(captured_frame == &captured_frame_);
perkjac306422015-10-08 15:32:38 +020072 RTC_CHECK(buffer_->native_handle() == nullptr);
73
Magnus Jedvertc464f502015-08-25 23:22:08 +020074 rtc::scoped_ptr<cricket::VideoFrame> frame(new cricket::WebRtcVideoFrame(
75 ShallowCenterCrop(buffer_, dst_width, dst_height),
perkj7755e202015-11-19 12:02:21 -080076 captured_frame->time_stamp, captured_frame->rotation));
Magnus Jedvertc464f502015-08-25 23:22:08 +020077 // Caller takes ownership.
78 // TODO(magjed): Change CreateAliasedFrame() to return a rtc::scoped_ptr.
79 return apply_rotation_ ? frame->GetCopyWithRotationApplied()->Copy()
80 : frame.release();
Per33544192015-04-02 12:30:51 +020081 }
82
perkjac306422015-10-08 15:32:38 +020083 cricket::VideoFrame* CreateAliasedFrame(
84 const cricket::CapturedFrame* input_frame,
85 int cropped_input_width,
86 int cropped_input_height,
87 int output_width,
88 int output_height) const override {
89 if (buffer_->native_handle() != nullptr) {
Per71f5a9a2015-12-11 09:32:37 +010090 // TODO(perkj) Implement cropping.
91 RTC_CHECK_EQ(cropped_input_width, buffer_->width());
92 RTC_CHECK_EQ(cropped_input_height, buffer_->height());
Pera3c20bb2015-11-26 13:41:44 +010093 rtc::scoped_refptr<webrtc::VideoFrameBuffer> scaled_buffer(
94 static_cast<webrtc_jni::AndroidTextureBuffer*>(buffer_.get())
Per71f5a9a2015-12-11 09:32:37 +010095 ->ScaleAndRotate(output_width, output_height,
96 apply_rotation_ ? input_frame->rotation :
97 webrtc::kVideoRotation_0));
Pera3c20bb2015-11-26 13:41:44 +010098 return new cricket::WebRtcVideoFrame(
Per71f5a9a2015-12-11 09:32:37 +010099 scaled_buffer, input_frame->time_stamp,
100 apply_rotation_ ? webrtc::kVideoRotation_0 : input_frame->rotation);
perkjac306422015-10-08 15:32:38 +0200101 }
102 return VideoFrameFactory::CreateAliasedFrame(input_frame,
103 cropped_input_width,
104 cropped_input_height,
105 output_width,
106 output_height);
107 }
108
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000109 private:
Magnus Jedvertc464f502015-08-25 23:22:08 +0200110 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_;
111 cricket::CapturedFrame captured_frame_;
112 rtc::scoped_refptr<AndroidVideoCapturerDelegate> delegate_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000113};
114
115AndroidVideoCapturer::AndroidVideoCapturer(
Per33544192015-04-02 12:30:51 +0200116 const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000117 : running_(false),
Per33544192015-04-02 12:30:51 +0200118 delegate_(delegate),
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000119 frame_factory_(NULL),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000120 current_state_(cricket::CS_STOPPED) {
Per33544192015-04-02 12:30:51 +0200121 thread_checker_.DetachFromThread();
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000122 std::string json_string = delegate_->GetSupportedFormats();
123 LOG(LS_INFO) << json_string;
124
125 Json::Value json_values;
126 Json::Reader reader(Json::Features::strictMode());
127 if (!reader.parse(json_string, json_values)) {
128 LOG(LS_ERROR) << "Failed to parse formats.";
129 }
130
131 std::vector<cricket::VideoFormat> formats;
132 for (Json::ArrayIndex i = 0; i < json_values.size(); ++i) {
133 const Json::Value& json_value = json_values[i];
henrikg91d6ede2015-09-17 00:24:34 -0700134 RTC_CHECK(!json_value["width"].isNull() &&
135 !json_value["height"].isNull() &&
136 !json_value["framerate"].isNull());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000137 cricket::VideoFormat format(
138 json_value["width"].asInt(),
139 json_value["height"].asInt(),
140 cricket::VideoFormat::FpsToInterval(json_value["framerate"].asInt()),
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000141 cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000142 formats.push_back(format);
143 }
144 SetSupportedFormats(formats);
145}
146
147AndroidVideoCapturer::~AndroidVideoCapturer() {
henrikg91d6ede2015-09-17 00:24:34 -0700148 RTC_CHECK(!running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000149}
150
151cricket::CaptureState AndroidVideoCapturer::Start(
152 const cricket::VideoFormat& capture_format) {
henrikg91d6ede2015-09-17 00:24:34 -0700153 RTC_CHECK(thread_checker_.CalledOnValidThread());
154 RTC_CHECK(!running_);
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200155 const int fps = cricket::VideoFormat::IntervalToFps(capture_format.interval);
156 LOG(LS_INFO) << " AndroidVideoCapturer::Start " << capture_format.width << "x"
157 << capture_format.height << "@" << fps;
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700158
Magnus Jedvertc464f502015-08-25 23:22:08 +0200159 frame_factory_ = new AndroidVideoCapturer::FrameFactory(delegate_.get());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000160 set_frame_factory(frame_factory_);
161
162 running_ = true;
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200163 delegate_->Start(capture_format.width, capture_format.height, fps, this);
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000164 SetCaptureFormat(&capture_format);
165 current_state_ = cricket::CS_STARTING;
166 return current_state_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000167}
168
169void AndroidVideoCapturer::Stop() {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000170 LOG(LS_INFO) << " AndroidVideoCapturer::Stop ";
henrikg91d6ede2015-09-17 00:24:34 -0700171 RTC_CHECK(thread_checker_.CalledOnValidThread());
172 RTC_CHECK(running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000173 running_ = false;
174 SetCaptureFormat(NULL);
175
176 delegate_->Stop();
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000177 current_state_ = cricket::CS_STOPPED;
178 SignalStateChange(this, current_state_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000179}
180
181bool AndroidVideoCapturer::IsRunning() {
henrikg91d6ede2015-09-17 00:24:34 -0700182 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000183 return running_;
184}
185
Peter Boström0c4e06b2015-10-07 12:23:21 +0200186bool AndroidVideoCapturer::GetPreferredFourccs(std::vector<uint32_t>* fourccs) {
henrikg91d6ede2015-09-17 00:24:34 -0700187 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000188 fourccs->push_back(cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000189 return true;
190}
191
192void AndroidVideoCapturer::OnCapturerStarted(bool success) {
henrikg91d6ede2015-09-17 00:24:34 -0700193 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000194 cricket::CaptureState new_state =
195 success ? cricket::CS_RUNNING : cricket::CS_FAILED;
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000196 if (new_state == current_state_)
197 return;
198 current_state_ = new_state;
199
200 // TODO(perkj): SetCaptureState can not be used since it posts to |thread_|.
201 // But |thread_ | is currently just the thread that happened to create the
202 // cricket::VideoCapturer.
203 SignalStateChange(this, new_state);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000204}
205
Magnus Jedvertc464f502015-08-25 23:22:08 +0200206void AndroidVideoCapturer::OnIncomingFrame(
olka30a5b5e2015-10-20 11:04:56 -0700207 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200208 int rotation,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200209 int64_t time_stamp) {
henrikg91d6ede2015-09-17 00:24:34 -0700210 RTC_CHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200211 frame_factory_->UpdateCapturedFrame(buffer, rotation, time_stamp);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000212 SignalFrameCaptured(this, frame_factory_->GetCapturedFrame());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200213 frame_factory_->ClearCapturedFrame();
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000214}
215
Åsa Persson2b679252015-06-15 09:53:05 +0200216void AndroidVideoCapturer::OnOutputFormatRequest(
217 int width, int height, int fps) {
henrikg91d6ede2015-09-17 00:24:34 -0700218 RTC_CHECK(thread_checker_.CalledOnValidThread());
Åsa Persson2b679252015-06-15 09:53:05 +0200219 const cricket::VideoFormat& current = video_adapter()->output_format();
220 cricket::VideoFormat format(
221 width, height, cricket::VideoFormat::FpsToInterval(fps), current.fourcc);
222 video_adapter()->OnOutputFormatRequest(format);
223}
224
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200225bool AndroidVideoCapturer::GetBestCaptureFormat(
226 const cricket::VideoFormat& desired,
227 cricket::VideoFormat* best_format) {
228 // Delegate this choice to VideoCapturerAndroid.startCapture().
229 *best_format = desired;
230 return true;
231}
232
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000233} // namespace webrtc