blob: 8901188492166fd66a6e7f5b5dab721802a4bba1 [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"
Per33544192015-04-02 12:30:51 +020030#include "webrtc/base/bind.h"
31#include "webrtc/base/callback.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000032#include "webrtc/base/common.h"
33#include "webrtc/base/json.h"
34#include "webrtc/base/timeutils.h"
35#include "webrtc/base/thread.h"
Henrik Boström09a9ea82015-04-17 17:31:53 +020036#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000037
38namespace webrtc {
39
40using cricket::WebRtcVideoFrame;
41using rtc::scoped_ptr;
Per33544192015-04-02 12:30:51 +020042using rtc::scoped_refptr;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000043
44// An implementation of cricket::VideoFrameFactory for frames that are not
45// guaranteed to outlive the created cricket::VideoFrame.
46// A frame is injected using UpdateCapturedFrame, and converted into a
47// cricket::VideoFrame with
48// CreateAliasedFrame. UpdateCapturedFrame should be called before
49// CreateAliasedFrame for every frame.
50class AndroidVideoCapturer::FrameFactory : public cricket::VideoFrameFactory {
51 public:
Per33544192015-04-02 12:30:51 +020052 FrameFactory(int width,
53 int height,
54 const scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
55 : start_time_(rtc::TimeNanos()), delegate_(delegate) {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000056 // Create a CapturedFrame that only contains header information, not the
57 // actual pixel data.
58 captured_frame_.width = width;
59 captured_frame_.height = height;
60 captured_frame_.pixel_height = 1;
61 captured_frame_.pixel_width = 1;
62 captured_frame_.rotation = 0;
63 captured_frame_.data = NULL;
64 captured_frame_.data_size = cricket::CapturedFrame::kUnknownDataSize;
65 captured_frame_.fourcc = static_cast<uint32>(cricket::FOURCC_ANY);
66 }
67
perkj@webrtc.org112f1272015-02-25 09:20:07 +000068 void UpdateCapturedFrame(void* frame_data,
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000069 int length,
magjedb69ab792015-07-22 02:32:00 -070070 int width,
71 int height,
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000072 int rotation,
Per33544192015-04-02 12:30:51 +020073 int64 time_stamp_in_ns) {
magjedfcf8ece2015-08-06 04:00:16 -070074 // Make sure we don't overwrite the previous frame.
75 CHECK(captured_frame_.data == nullptr);
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +000076 captured_frame_.fourcc = static_cast<uint32>(cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000077 captured_frame_.data = frame_data;
magjedb69ab792015-07-22 02:32:00 -070078 captured_frame_.width = width;
79 captured_frame_.height = height;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000080 captured_frame_.elapsed_time = rtc::TimeNanos() - start_time_;
Per33544192015-04-02 12:30:51 +020081 captured_frame_.time_stamp = time_stamp_in_ns;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000082 captured_frame_.rotation = rotation;
83 captured_frame_.data_size = length;
84 }
85
magjedfcf8ece2015-08-06 04:00:16 -070086 void ClearCapturedFrame() const {
87 captured_frame_.data = nullptr;
88 captured_frame_.width = 0;
89 captured_frame_.height = 0;
90 captured_frame_.elapsed_time = 0;
91 captured_frame_.time_stamp = 0;
92 captured_frame_.data_size = 0;
93 }
94
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000095 const cricket::CapturedFrame* GetCapturedFrame() const {
96 return &captured_frame_;
97 }
98
99 cricket::VideoFrame* CreateAliasedFrame(
100 const cricket::CapturedFrame* captured_frame,
101 int dst_width,
102 int dst_height) const override {
103 // This override of CreateAliasedFrame creates a copy of the frame since
104 // |captured_frame_.data| is only guaranteed to be valid during the scope
105 // of |AndroidVideoCapturer::OnIncomingFrame_w|.
106 // Check that captured_frame is actually our frame.
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700107 CHECK(captured_frame == &captured_frame_);
magjedfcf8ece2015-08-06 04:00:16 -0700108 CHECK(captured_frame->data != nullptr);
Per33544192015-04-02 12:30:51 +0200109
110 if (!apply_rotation_ || captured_frame->rotation == kVideoRotation_0) {
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700111 CHECK(captured_frame->fourcc == cricket::FOURCC_YV12);
Per33544192015-04-02 12:30:51 +0200112 const uint8_t* y_plane = static_cast<uint8_t*>(captured_frame_.data);
Henrik Boström09a9ea82015-04-17 17:31:53 +0200113
114 // Android guarantees that the stride is a multiple of 16.
115 // http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29
116 int y_stride;
117 int uv_stride;
118 webrtc::Calc16ByteAlignedStride(captured_frame->width, &y_stride,
119 &uv_stride);
120 const uint8_t* v_plane = y_plane + y_stride * captured_frame->height;
121 const uint8_t* u_plane =
122 v_plane + uv_stride * webrtc::AlignInt(captured_frame->height, 2) / 2;
Per33544192015-04-02 12:30:51 +0200123
124 // Create a WrappedI420Buffer and bind the |no_longer_used| callback
125 // to the static method ReturnFrame. The |delegate_| is bound as an
126 // argument which means that the callback will hold a reference to
127 // |delegate_|.
128 rtc::scoped_refptr<WrappedI420Buffer> buffer(
129 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
130 dst_width, dst_height, captured_frame->width,
131 captured_frame->height, y_plane, y_stride, u_plane, uv_stride,
132 v_plane, uv_stride,
133 rtc::Bind(&AndroidVideoCapturer::FrameFactory::ReturnFrame,
134 delegate_,
135 captured_frame->time_stamp)));
magjedfcf8ece2015-08-06 04:00:16 -0700136 cricket::VideoFrame* cricket_frame = new WebRtcVideoFrame(
Per33544192015-04-02 12:30:51 +0200137 buffer, captured_frame->elapsed_time,
138 captured_frame->time_stamp, captured_frame->GetRotation());
magjedfcf8ece2015-08-06 04:00:16 -0700139 // |cricket_frame| is now responsible for returning the frame. Clear
140 // |captured_frame_| so the frame isn't returned twice.
141 ClearCapturedFrame();
142 return cricket_frame;
Per33544192015-04-02 12:30:51 +0200143 }
144
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000145 scoped_ptr<WebRtcVideoFrame> frame(new WebRtcVideoFrame());
perkj@webrtc.org1d828132015-03-03 06:44:06 +0000146 frame->Init(captured_frame, dst_width, dst_height, apply_rotation_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000147 return frame.release();
148 }
149
Per33544192015-04-02 12:30:51 +0200150 static void ReturnFrame(scoped_refptr<AndroidVideoCapturerDelegate> delegate,
151 int64 time_stamp) {
152 delegate->ReturnBuffer(time_stamp);
153 }
154
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000155 private:
156 uint64 start_time_;
magjedfcf8ece2015-08-06 04:00:16 -0700157 // |captured_frame_| is mutable as a hacky way to modify it inside
158 // CreateAliasedframe().
159 mutable cricket::CapturedFrame captured_frame_;
Per33544192015-04-02 12:30:51 +0200160 scoped_refptr<AndroidVideoCapturerDelegate> delegate_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000161};
162
163AndroidVideoCapturer::AndroidVideoCapturer(
Per33544192015-04-02 12:30:51 +0200164 const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000165 : running_(false),
Per33544192015-04-02 12:30:51 +0200166 delegate_(delegate),
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000167 frame_factory_(NULL),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000168 current_state_(cricket::CS_STOPPED) {
Per33544192015-04-02 12:30:51 +0200169 thread_checker_.DetachFromThread();
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000170 std::string json_string = delegate_->GetSupportedFormats();
171 LOG(LS_INFO) << json_string;
172
173 Json::Value json_values;
174 Json::Reader reader(Json::Features::strictMode());
175 if (!reader.parse(json_string, json_values)) {
176 LOG(LS_ERROR) << "Failed to parse formats.";
177 }
178
179 std::vector<cricket::VideoFormat> formats;
180 for (Json::ArrayIndex i = 0; i < json_values.size(); ++i) {
181 const Json::Value& json_value = json_values[i];
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700182 CHECK(!json_value["width"].isNull() && !json_value["height"].isNull() &&
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000183 !json_value["framerate"].isNull());
184 cricket::VideoFormat format(
185 json_value["width"].asInt(),
186 json_value["height"].asInt(),
187 cricket::VideoFormat::FpsToInterval(json_value["framerate"].asInt()),
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000188 cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000189 formats.push_back(format);
190 }
191 SetSupportedFormats(formats);
192}
193
194AndroidVideoCapturer::~AndroidVideoCapturer() {
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700195 CHECK(!running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000196}
197
198cricket::CaptureState AndroidVideoCapturer::Start(
199 const cricket::VideoFormat& capture_format) {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000200 LOG(LS_INFO) << " AndroidVideoCapturer::Start w = " << capture_format.width
201 << " h = " << capture_format.height;
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700202 CHECK(thread_checker_.CalledOnValidThread());
203 CHECK(!running_);
204
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000205 frame_factory_ = new AndroidVideoCapturer::FrameFactory(
Per33544192015-04-02 12:30:51 +0200206 capture_format.width, capture_format.height, delegate_.get());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000207 set_frame_factory(frame_factory_);
208
209 running_ = true;
210 delegate_->Start(
211 capture_format.width, capture_format.height,
212 cricket::VideoFormat::IntervalToFps(capture_format.interval), this);
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000213 SetCaptureFormat(&capture_format);
214 current_state_ = cricket::CS_STARTING;
215 return current_state_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000216}
217
218void AndroidVideoCapturer::Stop() {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000219 LOG(LS_INFO) << " AndroidVideoCapturer::Stop ";
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700220 CHECK(thread_checker_.CalledOnValidThread());
221 CHECK(running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000222 running_ = false;
223 SetCaptureFormat(NULL);
224
225 delegate_->Stop();
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000226 current_state_ = cricket::CS_STOPPED;
227 SignalStateChange(this, current_state_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000228}
229
230bool AndroidVideoCapturer::IsRunning() {
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700231 CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000232 return running_;
233}
234
235bool AndroidVideoCapturer::GetPreferredFourccs(std::vector<uint32>* fourccs) {
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700236 CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000237 fourccs->push_back(cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000238 return true;
239}
240
241void AndroidVideoCapturer::OnCapturerStarted(bool success) {
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700242 CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000243 cricket::CaptureState new_state =
244 success ? cricket::CS_RUNNING : cricket::CS_FAILED;
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000245 if (new_state == current_state_)
246 return;
247 current_state_ = new_state;
248
249 // TODO(perkj): SetCaptureState can not be used since it posts to |thread_|.
250 // But |thread_ | is currently just the thread that happened to create the
251 // cricket::VideoCapturer.
252 SignalStateChange(this, new_state);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000253}
254
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000255void AndroidVideoCapturer::OnIncomingFrame(void* frame_data,
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000256 int length,
magjedb69ab792015-07-22 02:32:00 -0700257 int width,
258 int height,
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000259 int rotation,
260 int64 time_stamp) {
Alex Glaznev2f5be9a2015-05-19 10:56:32 -0700261 CHECK(thread_checker_.CalledOnValidThread());
magjedb69ab792015-07-22 02:32:00 -0700262 frame_factory_->UpdateCapturedFrame(frame_data, length, width, height,
263 rotation, time_stamp);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000264 SignalFrameCaptured(this, frame_factory_->GetCapturedFrame());
magjedfcf8ece2015-08-06 04:00:16 -0700265 if (frame_factory_->GetCapturedFrame()->data == nullptr) {
266 // Ownership has been passed to a WrappedI420Buffer. Do nothing.
267 } else {
268 // |captured_frame_| has either been copied or dropped, return it
269 // immediately.
270 delegate_->ReturnBuffer(time_stamp);
271 frame_factory_->ClearCapturedFrame();
272 }
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000273}
274
Åsa Persson2b679252015-06-15 09:53:05 +0200275void AndroidVideoCapturer::OnOutputFormatRequest(
276 int width, int height, int fps) {
277 CHECK(thread_checker_.CalledOnValidThread());
278 const cricket::VideoFormat& current = video_adapter()->output_format();
279 cricket::VideoFormat format(
280 width, height, cricket::VideoFormat::FpsToInterval(fps), current.fourcc);
281 video_adapter()->OnOutputFormatRequest(format);
282}
283
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000284} // namespace webrtc