blob: d850ab8addd54ee77dac2e987e16adebef32d06a [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
29#include "talk/media/webrtc/webrtcvideoframe.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000030#include "webrtc/base/common.h"
31#include "webrtc/base/json.h"
32#include "webrtc/base/timeutils.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000033
34namespace webrtc {
35
Magnus Jedvertc464f502015-08-25 23:22:08 +020036// A hack for avoiding deep frame copies in
37// cricket::VideoCapturer.SignalFrameCaptured() using a custom FrameFactory.
38// A frame is injected using UpdateCapturedFrame(), and converted into a
39// cricket::VideoFrame with CreateAliasedFrame(). UpdateCapturedFrame() should
40// be called before CreateAliasedFrame() for every frame.
41// TODO(magjed): Add an interface cricket::VideoCapturer::OnFrameCaptured()
42// for ref counted I420 frames instead of this hack.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000043class AndroidVideoCapturer::FrameFactory : public cricket::VideoFrameFactory {
44 public:
Magnus Jedvertc464f502015-08-25 23:22:08 +020045 FrameFactory(const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
magjedb09b6602015-10-01 03:02:44 -070046 : delegate_(delegate) {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000047 // Create a CapturedFrame that only contains header information, not the
48 // actual pixel data.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000049 captured_frame_.pixel_height = 1;
50 captured_frame_.pixel_width = 1;
Magnus Jedvertc464f502015-08-25 23:22:08 +020051 captured_frame_.data = nullptr;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000052 captured_frame_.data_size = cricket::CapturedFrame::kUnknownDataSize;
Peter Boström0c4e06b2015-10-07 12:23:21 +020053 captured_frame_.fourcc = static_cast<uint32_t>(cricket::FOURCC_ANY);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000054 }
55
Magnus Jedvertc464f502015-08-25 23:22:08 +020056 void UpdateCapturedFrame(
57 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
58 int rotation,
Peter Boström0c4e06b2015-10-07 12:23:21 +020059 int64_t time_stamp_in_ns) {
perkj7755e202015-11-19 12:02:21 -080060 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
61 rotation == 270);
Magnus Jedvertc464f502015-08-25 23:22:08 +020062 buffer_ = buffer;
63 captured_frame_.width = buffer->width();
64 captured_frame_.height = buffer->height();
Per33544192015-04-02 12:30:51 +020065 captured_frame_.time_stamp = time_stamp_in_ns;
perkj7755e202015-11-19 12:02:21 -080066 captured_frame_.rotation = static_cast<webrtc::VideoRotation>(rotation);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000067 }
68
Magnus Jedvertc464f502015-08-25 23:22:08 +020069 void ClearCapturedFrame() {
70 buffer_ = nullptr;
magjedfcf8ece2015-08-06 04:00:16 -070071 captured_frame_.width = 0;
72 captured_frame_.height = 0;
magjedfcf8ece2015-08-06 04:00:16 -070073 captured_frame_.time_stamp = 0;
magjedfcf8ece2015-08-06 04:00:16 -070074 }
75
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000076 const cricket::CapturedFrame* GetCapturedFrame() const {
77 return &captured_frame_;
78 }
79
80 cricket::VideoFrame* CreateAliasedFrame(
81 const cricket::CapturedFrame* captured_frame,
82 int dst_width,
83 int dst_height) const override {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000084 // Check that captured_frame is actually our frame.
henrikg91d6ede2015-09-17 00:24:34 -070085 RTC_CHECK(captured_frame == &captured_frame_);
perkjac306422015-10-08 15:32:38 +020086 RTC_CHECK(buffer_->native_handle() == nullptr);
87
Magnus Jedvertc464f502015-08-25 23:22:08 +020088 rtc::scoped_ptr<cricket::VideoFrame> frame(new cricket::WebRtcVideoFrame(
89 ShallowCenterCrop(buffer_, dst_width, dst_height),
perkj7755e202015-11-19 12:02:21 -080090 captured_frame->time_stamp, captured_frame->rotation));
Magnus Jedvertc464f502015-08-25 23:22:08 +020091 // Caller takes ownership.
92 // TODO(magjed): Change CreateAliasedFrame() to return a rtc::scoped_ptr.
93 return apply_rotation_ ? frame->GetCopyWithRotationApplied()->Copy()
94 : frame.release();
Per33544192015-04-02 12:30:51 +020095 }
96
perkjac306422015-10-08 15:32:38 +020097 cricket::VideoFrame* CreateAliasedFrame(
98 const cricket::CapturedFrame* input_frame,
99 int cropped_input_width,
100 int cropped_input_height,
101 int output_width,
102 int output_height) const override {
103 if (buffer_->native_handle() != nullptr) {
104 // TODO(perkj): Implement CreateAliasedFrame properly for textures.
105 rtc::scoped_ptr<cricket::VideoFrame> frame(new cricket::WebRtcVideoFrame(
perkj7755e202015-11-19 12:02:21 -0800106 buffer_, input_frame->time_stamp, input_frame->rotation));
perkjac306422015-10-08 15:32:38 +0200107 return frame.release();
108 }
109 return VideoFrameFactory::CreateAliasedFrame(input_frame,
110 cropped_input_width,
111 cropped_input_height,
112 output_width,
113 output_height);
114 }
115
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000116 private:
Magnus Jedvertc464f502015-08-25 23:22:08 +0200117 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_;
118 cricket::CapturedFrame captured_frame_;
119 rtc::scoped_refptr<AndroidVideoCapturerDelegate> delegate_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000120};
121
122AndroidVideoCapturer::AndroidVideoCapturer(
Per33544192015-04-02 12:30:51 +0200123 const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000124 : running_(false),
Per33544192015-04-02 12:30:51 +0200125 delegate_(delegate),
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000126 frame_factory_(NULL),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000127 current_state_(cricket::CS_STOPPED) {
Per33544192015-04-02 12:30:51 +0200128 thread_checker_.DetachFromThread();
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000129 std::string json_string = delegate_->GetSupportedFormats();
130 LOG(LS_INFO) << json_string;
131
132 Json::Value json_values;
133 Json::Reader reader(Json::Features::strictMode());
134 if (!reader.parse(json_string, json_values)) {
135 LOG(LS_ERROR) << "Failed to parse formats.";
136 }
137
138 std::vector<cricket::VideoFormat> formats;
139 for (Json::ArrayIndex i = 0; i < json_values.size(); ++i) {
140 const Json::Value& json_value = json_values[i];
henrikg91d6ede2015-09-17 00:24:34 -0700141 RTC_CHECK(!json_value["width"].isNull() &&
142 !json_value["height"].isNull() &&
143 !json_value["framerate"].isNull());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000144 cricket::VideoFormat format(
145 json_value["width"].asInt(),
146 json_value["height"].asInt(),
147 cricket::VideoFormat::FpsToInterval(json_value["framerate"].asInt()),
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000148 cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000149 formats.push_back(format);
150 }
151 SetSupportedFormats(formats);
Magnus Jedverta6cba3a2015-08-29 15:57:43 +0200152 // Do not apply frame rotation by default.
153 SetApplyRotation(false);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000154}
155
156AndroidVideoCapturer::~AndroidVideoCapturer() {
henrikg91d6ede2015-09-17 00:24:34 -0700157 RTC_CHECK(!running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000158}
159
160cricket::CaptureState AndroidVideoCapturer::Start(
161 const cricket::VideoFormat& capture_format) {
henrikg91d6ede2015-09-17 00:24:34 -0700162 RTC_CHECK(thread_checker_.CalledOnValidThread());
163 RTC_CHECK(!running_);
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200164 const int fps = cricket::VideoFormat::IntervalToFps(capture_format.interval);
165 LOG(LS_INFO) << " AndroidVideoCapturer::Start " << capture_format.width << "x"
166 << capture_format.height << "@" << fps;
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700167
Magnus Jedvertc464f502015-08-25 23:22:08 +0200168 frame_factory_ = new AndroidVideoCapturer::FrameFactory(delegate_.get());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000169 set_frame_factory(frame_factory_);
170
171 running_ = true;
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200172 delegate_->Start(capture_format.width, capture_format.height, fps, this);
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000173 SetCaptureFormat(&capture_format);
174 current_state_ = cricket::CS_STARTING;
175 return current_state_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000176}
177
178void AndroidVideoCapturer::Stop() {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000179 LOG(LS_INFO) << " AndroidVideoCapturer::Stop ";
henrikg91d6ede2015-09-17 00:24:34 -0700180 RTC_CHECK(thread_checker_.CalledOnValidThread());
181 RTC_CHECK(running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000182 running_ = false;
183 SetCaptureFormat(NULL);
184
185 delegate_->Stop();
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000186 current_state_ = cricket::CS_STOPPED;
187 SignalStateChange(this, current_state_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000188}
189
190bool AndroidVideoCapturer::IsRunning() {
henrikg91d6ede2015-09-17 00:24:34 -0700191 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000192 return running_;
193}
194
Peter Boström0c4e06b2015-10-07 12:23:21 +0200195bool AndroidVideoCapturer::GetPreferredFourccs(std::vector<uint32_t>* fourccs) {
henrikg91d6ede2015-09-17 00:24:34 -0700196 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000197 fourccs->push_back(cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000198 return true;
199}
200
201void AndroidVideoCapturer::OnCapturerStarted(bool success) {
henrikg91d6ede2015-09-17 00:24:34 -0700202 RTC_CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000203 cricket::CaptureState new_state =
204 success ? cricket::CS_RUNNING : cricket::CS_FAILED;
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000205 if (new_state == current_state_)
206 return;
207 current_state_ = new_state;
208
209 // TODO(perkj): SetCaptureState can not be used since it posts to |thread_|.
210 // But |thread_ | is currently just the thread that happened to create the
211 // cricket::VideoCapturer.
212 SignalStateChange(this, new_state);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000213}
214
Magnus Jedvertc464f502015-08-25 23:22:08 +0200215void AndroidVideoCapturer::OnIncomingFrame(
olka30a5b5e2015-10-20 11:04:56 -0700216 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200217 int rotation,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200218 int64_t time_stamp) {
henrikg91d6ede2015-09-17 00:24:34 -0700219 RTC_CHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200220 frame_factory_->UpdateCapturedFrame(buffer, rotation, time_stamp);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000221 SignalFrameCaptured(this, frame_factory_->GetCapturedFrame());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200222 frame_factory_->ClearCapturedFrame();
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000223}
224
Åsa Persson2b679252015-06-15 09:53:05 +0200225void AndroidVideoCapturer::OnOutputFormatRequest(
226 int width, int height, int fps) {
henrikg91d6ede2015-09-17 00:24:34 -0700227 RTC_CHECK(thread_checker_.CalledOnValidThread());
Åsa Persson2b679252015-06-15 09:53:05 +0200228 const cricket::VideoFormat& current = video_adapter()->output_format();
229 cricket::VideoFormat format(
230 width, height, cricket::VideoFormat::FpsToInterval(fps), current.fourcc);
231 video_adapter()->OnOutputFormatRequest(format);
232}
233
Magnus Jedvert6ec1f922015-08-28 11:40:59 +0200234bool AndroidVideoCapturer::GetBestCaptureFormat(
235 const cricket::VideoFormat& desired,
236 cricket::VideoFormat* best_format) {
237 // Delegate this choice to VideoCapturerAndroid.startCapture().
238 *best_format = desired;
239 return true;
240}
241
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000242} // namespace webrtc