blob: c4c5a480b4f759253d7062f0f2c8e671540259ca [file] [log] [blame]
perkj@webrtc.org83bc7212015-02-11 11:26:56 +00001/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27#include "talk/app/webrtc/androidvideocapturer.h"
28
Pera3c20bb2015-11-26 13:41:44 +010029#include "talk/app/webrtc/java/jni/native_handle_impl.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000030#include "talk/media/webrtc/webrtcvideoframe.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000031#include "webrtc/base/common.h"
32#include "webrtc/base/json.h"
33#include "webrtc/base/timeutils.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000034
35namespace webrtc {
36
Magnus Jedvertc464f502015-08-25 23:22:08 +020037// A hack for avoiding deep frame copies in
38// cricket::VideoCapturer.SignalFrameCaptured() using a custom FrameFactory.
39// A frame is injected using UpdateCapturedFrame(), and converted into a
40// cricket::VideoFrame with CreateAliasedFrame(). UpdateCapturedFrame() should
41// be called before CreateAliasedFrame() for every frame.
42// TODO(magjed): Add an interface cricket::VideoCapturer::OnFrameCaptured()
43// for ref counted I420 frames instead of this hack.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000044class AndroidVideoCapturer::FrameFactory : public cricket::VideoFrameFactory {
45 public:
Magnus Jedvertc464f502015-08-25 23:22:08 +020046 FrameFactory(const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
magjedb09b6602015-10-01 03:02:44 -070047 : delegate_(delegate) {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000048 // Create a CapturedFrame that only contains header information, not the
49 // actual pixel data.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000050 captured_frame_.pixel_height = 1;
51 captured_frame_.pixel_width = 1;
Magnus Jedvertc464f502015-08-25 23:22:08 +020052 captured_frame_.data = nullptr;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000053 captured_frame_.data_size = cricket::CapturedFrame::kUnknownDataSize;
Peter Boström0c4e06b2015-10-07 12:23:21 +020054 captured_frame_.fourcc = static_cast<uint32_t>(cricket::FOURCC_ANY);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000055 }
56
Magnus Jedvertc464f502015-08-25 23:22:08 +020057 void UpdateCapturedFrame(
58 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
59 int rotation,
Peter Boström0c4e06b2015-10-07 12:23:21 +020060 int64_t time_stamp_in_ns) {
perkj7755e202015-11-19 12:02:21 -080061 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
62 rotation == 270);
Magnus Jedvertc464f502015-08-25 23:22:08 +020063 buffer_ = buffer;
64 captured_frame_.width = buffer->width();
65 captured_frame_.height = buffer->height();
Per33544192015-04-02 12:30:51 +020066 captured_frame_.time_stamp = time_stamp_in_ns;
perkj7755e202015-11-19 12:02:21 -080067 captured_frame_.rotation = static_cast<webrtc::VideoRotation>(rotation);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000068 }
69
Magnus Jedvertc464f502015-08-25 23:22:08 +020070 void ClearCapturedFrame() {
71 buffer_ = nullptr;
magjedfcf8ece2015-08-06 04:00:16 -070072 captured_frame_.width = 0;
73 captured_frame_.height = 0;
magjedfcf8ece2015-08-06 04:00:16 -070074 captured_frame_.time_stamp = 0;
magjedfcf8ece2015-08-06 04:00:16 -070075 }
76
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000077 const cricket::CapturedFrame* GetCapturedFrame() const {
78 return &captured_frame_;
79 }
80
81 cricket::VideoFrame* CreateAliasedFrame(
82 const cricket::CapturedFrame* captured_frame,
83 int dst_width,
84 int dst_height) const override {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000085 // Check that captured_frame is actually our frame.
henrikg91d6ede2015-09-17 00:24:34 -070086 RTC_CHECK(captured_frame == &captured_frame_);
perkjac306422015-10-08 15:32:38 +020087 RTC_CHECK(buffer_->native_handle() == nullptr);
88
Magnus Jedvertc464f502015-08-25 23:22:08 +020089 rtc::scoped_ptr<cricket::VideoFrame> frame(new cricket::WebRtcVideoFrame(
90 ShallowCenterCrop(buffer_, dst_width, dst_height),
perkj7755e202015-11-19 12:02:21 -080091 captured_frame->time_stamp, captured_frame->rotation));
Magnus Jedvertc464f502015-08-25 23:22:08 +020092 // Caller takes ownership.
93 // TODO(magjed): Change CreateAliasedFrame() to return a rtc::scoped_ptr.
94 return apply_rotation_ ? frame->GetCopyWithRotationApplied()->Copy()
95 : frame.release();
Per33544192015-04-02 12:30:51 +020096 }
97
perkjac306422015-10-08 15:32:38 +020098 cricket::VideoFrame* CreateAliasedFrame(
99 const cricket::CapturedFrame* input_frame,
100 int cropped_input_width,
101 int cropped_input_height,
102 int output_width,
103 int output_height) const override {
104 if (buffer_->native_handle() != nullptr) {
Pera3c20bb2015-11-26 13:41:44 +0100105 rtc::scoped_refptr<webrtc::VideoFrameBuffer> scaled_buffer(
106 static_cast<webrtc_jni::AndroidTextureBuffer*>(buffer_.get())
107 ->CropAndScale(cropped_input_width, cropped_input_height,
108 output_width, output_height));
109 return new cricket::WebRtcVideoFrame(
110 scaled_buffer, input_frame->time_stamp, input_frame->rotation);
perkjac306422015-10-08 15:32:38 +0200111 }
112 return VideoFrameFactory::CreateAliasedFrame(input_frame,
113 cropped_input_width,
114 cropped_input_height,
115 output_width,
116 output_height);
117 }
118
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000119 private:
Magnus Jedvertc464f502015-08-25 23:22:08 +0200120 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_;
121 cricket::CapturedFrame captured_frame_;
122 rtc::scoped_refptr<AndroidVideoCapturerDelegate> delegate_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000123};
124
125AndroidVideoCapturer::AndroidVideoCapturer(
Per33544192015-04-02 12:30:51 +0200126 const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000127 : running_(false),
Per33544192015-04-02 12:30:51 +0200128 delegate_(delegate),
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000129 frame_factory_(NULL),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000130 current_state_(cricket::CS_STOPPED) {
Per33544192015-04-02 12:30:51 +0200131 thread_checker_.DetachFromThread();
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000132 std::string json_string = delegate_->GetSupportedFormats();
133 LOG(LS_INFO) << json_string;
134
135 Json::Value json_values;
136 Json::Reader reader(Json::Features::strictMode());
137 if (!reader.parse(json_string, json_values)) {
138 LOG(LS_ERROR) << "Failed to parse formats.";
139 }
140
141 std::vector<cricket::VideoFormat> formats;
142 for (Json::ArrayIndex i = 0; i < json_values.size(); ++i) {
143 const Json::Value& json_value = json_values[i];
henrikg91d6ede2015-09-17 00:24:34 -0700144 RTC_CHECK(!json_value["width"].isNull() &&
145 !json_value["height"].isNull() &&
146 !json_value["framerate"].isNull());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000147 cricket::VideoFormat format(
148 json_value["width"].asInt(),
149 json_value["height"].asInt(),
150 cricket::VideoFormat::FpsToInterval(json_value["framerate"].asInt()),
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000151 cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000152 formats.push_back(format);
153 }
154 SetSupportedFormats(formats);
Magnus Jedverta6cba3a2015-08-29 15:57:43 +0200155 // Do not apply frame rotation by default.
156 SetApplyRotation(false);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000157}
158
159AndroidVideoCapturer::~AndroidVideoCapturer() {
henrikg91d6ede2015-09-17 00:24:34 -0700160 RTC_CHECK(!running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000161}
162
163cricket::CaptureState AndroidVideoCapturer::Start(
164 const cricket::VideoFormat& capture_format) {
henrikg91d6ede2015-09-17 00:24:34 -0700165 RTC_CHECK(thread_checker_.CalledOnValidThread());
166 RTC_CHECK(!running_);
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200167 const int fps = cricket::VideoFormat::IntervalToFps(capture_format.interval);
168 LOG(LS_INFO) << " AndroidVideoCapturer::Start " << capture_format.width << "x"
169 << capture_format.height << "@" << fps;
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700170
Magnus Jedvertc464f502015-08-25 23:22:08 +0200171 frame_factory_ = new AndroidVideoCapturer::FrameFactory(delegate_.get());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000172 set_frame_factory(frame_factory_);
173
174 running_ = true;
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200175 delegate_->Start(capture_format.width, capture_format.height, fps, this);
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000176 SetCaptureFormat(&capture_format);
177 current_state_ = cricket::CS_STARTING;
178 return current_state_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000179}
180
181void AndroidVideoCapturer::Stop() {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000182 LOG(LS_INFO) << " AndroidVideoCapturer::Stop ";
henrikg91d6ede2015-09-17 00:24:34 -0700183 RTC_CHECK(thread_checker_.CalledOnValidThread());
184 RTC_CHECK(running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000185 running_ = false;
186 SetCaptureFormat(NULL);
187
188 delegate_->Stop();
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000189 current_state_ = cricket::CS_STOPPED;
190 SignalStateChange(this, current_state_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000191}
192
193bool AndroidVideoCapturer::IsRunning() {
henrikg91d6ede2015-09-17 00:24:34 -0700194 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000195 return running_;
196}
197
Peter Boström0c4e06b2015-10-07 12:23:21 +0200198bool AndroidVideoCapturer::GetPreferredFourccs(std::vector<uint32_t>* fourccs) {
henrikg91d6ede2015-09-17 00:24:34 -0700199 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000200 fourccs->push_back(cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000201 return true;
202}
203
204void AndroidVideoCapturer::OnCapturerStarted(bool success) {
henrikg91d6ede2015-09-17 00:24:34 -0700205 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000206 cricket::CaptureState new_state =
207 success ? cricket::CS_RUNNING : cricket::CS_FAILED;
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000208 if (new_state == current_state_)
209 return;
210 current_state_ = new_state;
211
212 // TODO(perkj): SetCaptureState can not be used since it posts to |thread_|.
213 // But |thread_ | is currently just the thread that happened to create the
214 // cricket::VideoCapturer.
215 SignalStateChange(this, new_state);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000216}
217
Magnus Jedvertc464f502015-08-25 23:22:08 +0200218void AndroidVideoCapturer::OnIncomingFrame(
olka30a5b5e2015-10-20 11:04:56 -0700219 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200220 int rotation,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200221 int64_t time_stamp) {
henrikg91d6ede2015-09-17 00:24:34 -0700222 RTC_CHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200223 frame_factory_->UpdateCapturedFrame(buffer, rotation, time_stamp);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000224 SignalFrameCaptured(this, frame_factory_->GetCapturedFrame());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200225 frame_factory_->ClearCapturedFrame();
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000226}
227
Åsa Persson2b679252015-06-15 09:53:05 +0200228void AndroidVideoCapturer::OnOutputFormatRequest(
229 int width, int height, int fps) {
henrikg91d6ede2015-09-17 00:24:34 -0700230 RTC_CHECK(thread_checker_.CalledOnValidThread());
Åsa Persson2b679252015-06-15 09:53:05 +0200231 const cricket::VideoFormat& current = video_adapter()->output_format();
232 cricket::VideoFormat format(
233 width, height, cricket::VideoFormat::FpsToInterval(fps), current.fourcc);
234 video_adapter()->OnOutputFormatRequest(format);
235}
236
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200237bool AndroidVideoCapturer::GetBestCaptureFormat(
238 const cricket::VideoFormat& desired,
239 cricket::VideoFormat* best_format) {
240 // Delegate this choice to VideoCapturerAndroid.startCapture().
241 *best_format = desired;
242 return true;
243}
244
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000245} // namespace webrtc