2015-02-11 11:26:56 +00:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
|
2015-02-11 11:26:56 +00:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2015-02-11 11:26:56 +00:00
|
|
|
*/
|
|
|
|
|
|
2016-02-10 10:53:12 +01:00
|
|
|
#include "webrtc/api/androidvideocapturer.h"
|
|
|
|
|
|
2016-04-27 06:47:29 -07:00
|
|
|
#include <memory>
|
|
|
|
|
|
2016-02-10 10:53:12 +01:00
|
|
|
#include "webrtc/api/java/jni/native_handle_impl.h"
|
2015-02-11 11:26:56 +00:00
|
|
|
#include "webrtc/base/common.h"
|
|
|
|
|
#include "webrtc/base/timeutils.h"
|
2016-02-12 06:39:40 +01:00
|
|
|
#include "webrtc/media/engine/webrtcvideoframe.h"
|
2015-02-11 11:26:56 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2015-08-25 23:22:08 +02:00
|
|
|
// A hack for avoiding deep frame copies in
|
|
|
|
|
// cricket::VideoCapturer.SignalFrameCaptured() using a custom FrameFactory.
|
|
|
|
|
// A frame is injected using UpdateCapturedFrame(), and converted into a
|
|
|
|
|
// cricket::VideoFrame with CreateAliasedFrame(). UpdateCapturedFrame() should
|
|
|
|
|
// be called before CreateAliasedFrame() for every frame.
|
|
|
|
|
// TODO(magjed): Add an interface cricket::VideoCapturer::OnFrameCaptured()
|
|
|
|
|
// for ref counted I420 frames instead of this hack.
|
2015-02-11 11:26:56 +00:00
|
|
|
class AndroidVideoCapturer::FrameFactory : public cricket::VideoFrameFactory {
|
|
|
|
|
public:
|
2016-02-12 13:30:57 +01:00
|
|
|
explicit FrameFactory(
|
|
|
|
|
const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
|
2015-10-01 03:02:44 -07:00
|
|
|
: delegate_(delegate) {
|
2015-02-11 11:26:56 +00:00
|
|
|
// Create a CapturedFrame that only contains header information, not the
|
|
|
|
|
// actual pixel data.
|
|
|
|
|
captured_frame_.pixel_height = 1;
|
|
|
|
|
captured_frame_.pixel_width = 1;
|
2015-08-25 23:22:08 +02:00
|
|
|
captured_frame_.data = nullptr;
|
2015-02-11 11:26:56 +00:00
|
|
|
captured_frame_.data_size = cricket::CapturedFrame::kUnknownDataSize;
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
captured_frame_.fourcc = static_cast<uint32_t>(cricket::FOURCC_ANY);
|
2015-02-11 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-25 23:22:08 +02:00
|
|
|
void UpdateCapturedFrame(
|
|
|
|
|
const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
|
|
|
|
|
int rotation,
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
int64_t time_stamp_in_ns) {
|
2015-11-19 12:02:21 -08:00
|
|
|
RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
|
|
|
|
|
rotation == 270);
|
2015-08-25 23:22:08 +02:00
|
|
|
buffer_ = buffer;
|
|
|
|
|
captured_frame_.width = buffer->width();
|
|
|
|
|
captured_frame_.height = buffer->height();
|
2015-04-02 12:30:51 +02:00
|
|
|
captured_frame_.time_stamp = time_stamp_in_ns;
|
2015-11-19 12:02:21 -08:00
|
|
|
captured_frame_.rotation = static_cast<webrtc::VideoRotation>(rotation);
|
2015-02-11 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-25 23:22:08 +02:00
|
|
|
void ClearCapturedFrame() {
|
|
|
|
|
buffer_ = nullptr;
|
2015-08-06 04:00:16 -07:00
|
|
|
captured_frame_.width = 0;
|
|
|
|
|
captured_frame_.height = 0;
|
|
|
|
|
captured_frame_.time_stamp = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 11:26:56 +00:00
|
|
|
const cricket::CapturedFrame* GetCapturedFrame() const {
|
|
|
|
|
return &captured_frame_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cricket::VideoFrame* CreateAliasedFrame(
|
|
|
|
|
const cricket::CapturedFrame* captured_frame,
|
|
|
|
|
int dst_width,
|
|
|
|
|
int dst_height) const override {
|
|
|
|
|
// Check that captured_frame is actually our frame.
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(captured_frame == &captured_frame_);
|
2015-10-08 15:32:38 +02:00
|
|
|
RTC_CHECK(buffer_->native_handle() == nullptr);
|
|
|
|
|
|
2016-04-27 06:47:29 -07:00
|
|
|
std::unique_ptr<cricket::VideoFrame> frame(new cricket::WebRtcVideoFrame(
|
2015-08-25 23:22:08 +02:00
|
|
|
ShallowCenterCrop(buffer_, dst_width, dst_height),
|
2015-11-19 12:02:21 -08:00
|
|
|
captured_frame->time_stamp, captured_frame->rotation));
|
2015-08-25 23:22:08 +02:00
|
|
|
// Caller takes ownership.
|
2016-04-27 06:47:29 -07:00
|
|
|
// TODO(magjed): Change CreateAliasedFrame() to return a std::unique_ptr.
|
2015-08-25 23:22:08 +02:00
|
|
|
return apply_rotation_ ? frame->GetCopyWithRotationApplied()->Copy()
|
|
|
|
|
: frame.release();
|
2015-04-02 12:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
2015-10-08 15:32:38 +02:00
|
|
|
cricket::VideoFrame* CreateAliasedFrame(
|
|
|
|
|
const cricket::CapturedFrame* input_frame,
|
|
|
|
|
int cropped_input_width,
|
|
|
|
|
int cropped_input_height,
|
|
|
|
|
int output_width,
|
|
|
|
|
int output_height) const override {
|
|
|
|
|
if (buffer_->native_handle() != nullptr) {
|
2015-12-11 09:32:37 +01:00
|
|
|
// TODO(perkj) Implement cropping.
|
|
|
|
|
RTC_CHECK_EQ(cropped_input_width, buffer_->width());
|
|
|
|
|
RTC_CHECK_EQ(cropped_input_height, buffer_->height());
|
2015-11-26 13:41:44 +01:00
|
|
|
rtc::scoped_refptr<webrtc::VideoFrameBuffer> scaled_buffer(
|
|
|
|
|
static_cast<webrtc_jni::AndroidTextureBuffer*>(buffer_.get())
|
2015-12-11 09:32:37 +01:00
|
|
|
->ScaleAndRotate(output_width, output_height,
|
|
|
|
|
apply_rotation_ ? input_frame->rotation :
|
|
|
|
|
webrtc::kVideoRotation_0));
|
2015-11-26 13:41:44 +01:00
|
|
|
return new cricket::WebRtcVideoFrame(
|
2015-12-11 09:32:37 +01:00
|
|
|
scaled_buffer, input_frame->time_stamp,
|
|
|
|
|
apply_rotation_ ? webrtc::kVideoRotation_0 : input_frame->rotation);
|
2015-10-08 15:32:38 +02:00
|
|
|
}
|
|
|
|
|
return VideoFrameFactory::CreateAliasedFrame(input_frame,
|
|
|
|
|
cropped_input_width,
|
|
|
|
|
cropped_input_height,
|
|
|
|
|
output_width,
|
|
|
|
|
output_height);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 11:26:56 +00:00
|
|
|
private:
|
2015-08-25 23:22:08 +02:00
|
|
|
rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_;
|
|
|
|
|
cricket::CapturedFrame captured_frame_;
|
|
|
|
|
rtc::scoped_refptr<AndroidVideoCapturerDelegate> delegate_;
|
2015-02-11 11:26:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AndroidVideoCapturer::AndroidVideoCapturer(
|
2015-04-02 12:30:51 +02:00
|
|
|
const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
|
2015-02-11 11:26:56 +00:00
|
|
|
: running_(false),
|
2015-04-02 12:30:51 +02:00
|
|
|
delegate_(delegate),
|
2015-02-17 13:53:56 +00:00
|
|
|
frame_factory_(NULL),
|
2015-02-19 08:43:38 +00:00
|
|
|
current_state_(cricket::CS_STOPPED) {
|
2015-04-02 12:30:51 +02:00
|
|
|
thread_checker_.DetachFromThread();
|
2016-02-18 13:09:54 +01:00
|
|
|
SetSupportedFormats(delegate_->GetSupportedFormats());
|
2015-02-11 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AndroidVideoCapturer::~AndroidVideoCapturer() {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(!running_);
|
2015-02-11 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cricket::CaptureState AndroidVideoCapturer::Start(
|
|
|
|
|
const cricket::VideoFormat& capture_format) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(thread_checker_.CalledOnValidThread());
|
|
|
|
|
RTC_CHECK(!running_);
|
2015-08-28 11:40:59 +02:00
|
|
|
const int fps = cricket::VideoFormat::IntervalToFps(capture_format.interval);
|
|
|
|
|
LOG(LS_INFO) << " AndroidVideoCapturer::Start " << capture_format.width << "x"
|
|
|
|
|
<< capture_format.height << "@" << fps;
|
2015-05-19 10:56:32 -07:00
|
|
|
|
2015-08-25 23:22:08 +02:00
|
|
|
frame_factory_ = new AndroidVideoCapturer::FrameFactory(delegate_.get());
|
2015-02-11 11:26:56 +00:00
|
|
|
set_frame_factory(frame_factory_);
|
|
|
|
|
|
|
|
|
|
running_ = true;
|
2015-08-28 11:40:59 +02:00
|
|
|
delegate_->Start(capture_format.width, capture_format.height, fps, this);
|
2015-02-17 13:53:56 +00:00
|
|
|
SetCaptureFormat(&capture_format);
|
|
|
|
|
current_state_ = cricket::CS_STARTING;
|
|
|
|
|
return current_state_;
|
2015-02-11 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AndroidVideoCapturer::Stop() {
|
|
|
|
|
LOG(LS_INFO) << " AndroidVideoCapturer::Stop ";
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(thread_checker_.CalledOnValidThread());
|
|
|
|
|
RTC_CHECK(running_);
|
2015-02-11 11:26:56 +00:00
|
|
|
running_ = false;
|
|
|
|
|
SetCaptureFormat(NULL);
|
|
|
|
|
|
|
|
|
|
delegate_->Stop();
|
2015-02-17 13:53:56 +00:00
|
|
|
current_state_ = cricket::CS_STOPPED;
|
2016-02-29 12:07:35 +01:00
|
|
|
SetCaptureState(current_state_);
|
2015-02-11 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AndroidVideoCapturer::IsRunning() {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(thread_checker_.CalledOnValidThread());
|
2015-02-11 11:26:56 +00:00
|
|
|
return running_;
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
bool AndroidVideoCapturer::GetPreferredFourccs(std::vector<uint32_t>* fourccs) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(thread_checker_.CalledOnValidThread());
|
2015-02-23 11:14:57 +00:00
|
|
|
fourccs->push_back(cricket::FOURCC_YV12);
|
2015-02-11 11:26:56 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AndroidVideoCapturer::OnCapturerStarted(bool success) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(thread_checker_.CalledOnValidThread());
|
2015-02-11 11:26:56 +00:00
|
|
|
cricket::CaptureState new_state =
|
|
|
|
|
success ? cricket::CS_RUNNING : cricket::CS_FAILED;
|
2015-02-17 13:53:56 +00:00
|
|
|
if (new_state == current_state_)
|
|
|
|
|
return;
|
|
|
|
|
current_state_ = new_state;
|
2016-02-29 12:07:35 +01:00
|
|
|
SetCaptureState(new_state);
|
2015-02-11 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-25 23:22:08 +02:00
|
|
|
void AndroidVideoCapturer::OnIncomingFrame(
|
2015-10-20 11:04:56 -07:00
|
|
|
const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
|
2015-08-25 23:22:08 +02:00
|
|
|
int rotation,
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
int64_t time_stamp) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(thread_checker_.CalledOnValidThread());
|
2015-08-25 23:22:08 +02:00
|
|
|
frame_factory_->UpdateCapturedFrame(buffer, rotation, time_stamp);
|
2015-02-11 11:26:56 +00:00
|
|
|
SignalFrameCaptured(this, frame_factory_->GetCapturedFrame());
|
2015-08-25 23:22:08 +02:00
|
|
|
frame_factory_->ClearCapturedFrame();
|
2015-02-11 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-15 09:53:05 +02:00
|
|
|
void AndroidVideoCapturer::OnOutputFormatRequest(
|
|
|
|
|
int width, int height, int fps) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(thread_checker_.CalledOnValidThread());
|
2016-04-05 15:23:49 +02:00
|
|
|
cricket::VideoFormat format(width, height,
|
|
|
|
|
cricket::VideoFormat::FpsToInterval(fps), 0);
|
2015-06-15 09:53:05 +02:00
|
|
|
video_adapter()->OnOutputFormatRequest(format);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-28 11:40:59 +02:00
|
|
|
bool AndroidVideoCapturer::GetBestCaptureFormat(
|
|
|
|
|
const cricket::VideoFormat& desired,
|
|
|
|
|
cricket::VideoFormat* best_format) {
|
2016-02-12 17:05:29 +01:00
|
|
|
// Delegate this choice to VideoCapturer.startCapture().
|
2015-08-28 11:40:59 +02:00
|
|
|
*best_format = desired;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 11:26:56 +00:00
|
|
|
} // namespace webrtc
|