1/*
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/app/webrtc/java/jni/native_handle_impl.h"
30#include "talk/media/webrtc/webrtcvideoframe.h"
31#include "webrtc/base/common.h"
32#include "webrtc/base/json.h"
33#include "webrtc/base/timeutils.h"
34
35namespace webrtc {
36
37// 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.
44class AndroidVideoCapturer::FrameFactory : public cricket::VideoFrameFactory {
45 public:
46  FrameFactory(const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
47      : delegate_(delegate) {
48    // Create a CapturedFrame that only contains header information, not the
49    // actual pixel data.
50    captured_frame_.pixel_height = 1;
51    captured_frame_.pixel_width = 1;
52    captured_frame_.data = nullptr;
53    captured_frame_.data_size = cricket::CapturedFrame::kUnknownDataSize;
54    captured_frame_.fourcc = static_cast<uint32_t>(cricket::FOURCC_ANY);
55  }
56
57  void UpdateCapturedFrame(
58      const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
59      int rotation,
60      int64_t time_stamp_in_ns) {
61    RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
62               rotation == 270);
63    buffer_ = buffer;
64    captured_frame_.width = buffer->width();
65    captured_frame_.height = buffer->height();
66    captured_frame_.time_stamp = time_stamp_in_ns;
67    captured_frame_.rotation = static_cast<webrtc::VideoRotation>(rotation);
68  }
69
70  void ClearCapturedFrame() {
71    buffer_ = nullptr;
72    captured_frame_.width = 0;
73    captured_frame_.height = 0;
74    captured_frame_.time_stamp = 0;
75  }
76
77  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 {
85    // Check that captured_frame is actually our frame.
86    RTC_CHECK(captured_frame == &captured_frame_);
87    RTC_CHECK(buffer_->native_handle() == nullptr);
88
89    rtc::scoped_ptr<cricket::VideoFrame> frame(new cricket::WebRtcVideoFrame(
90        ShallowCenterCrop(buffer_, dst_width, dst_height),
91        captured_frame->time_stamp, captured_frame->rotation));
92    // Caller takes ownership.
93    // TODO(magjed): Change CreateAliasedFrame() to return a rtc::scoped_ptr.
94    return apply_rotation_ ? frame->GetCopyWithRotationApplied()->Copy()
95                           : frame.release();
96  }
97
98  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) {
105      // TODO(perkj) Implement cropping.
106      RTC_CHECK_EQ(cropped_input_width, buffer_->width());
107      RTC_CHECK_EQ(cropped_input_height, buffer_->height());
108      rtc::scoped_refptr<webrtc::VideoFrameBuffer> scaled_buffer(
109          static_cast<webrtc_jni::AndroidTextureBuffer*>(buffer_.get())
110              ->ScaleAndRotate(output_width, output_height,
111                               apply_rotation_ ? input_frame->rotation :
112                                   webrtc::kVideoRotation_0));
113      return new cricket::WebRtcVideoFrame(
114          scaled_buffer, input_frame->time_stamp,
115          apply_rotation_ ? webrtc::kVideoRotation_0 : input_frame->rotation);
116    }
117    return VideoFrameFactory::CreateAliasedFrame(input_frame,
118                                                 cropped_input_width,
119                                                 cropped_input_height,
120                                                 output_width,
121                                                 output_height);
122  }
123
124 private:
125  rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_;
126  cricket::CapturedFrame captured_frame_;
127  rtc::scoped_refptr<AndroidVideoCapturerDelegate> delegate_;
128};
129
130AndroidVideoCapturer::AndroidVideoCapturer(
131    const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
132    : running_(false),
133      delegate_(delegate),
134      frame_factory_(NULL),
135      current_state_(cricket::CS_STOPPED) {
136  thread_checker_.DetachFromThread();
137  std::string json_string = delegate_->GetSupportedFormats();
138  LOG(LS_INFO) << json_string;
139
140  Json::Value json_values;
141  Json::Reader reader(Json::Features::strictMode());
142  if (!reader.parse(json_string, json_values)) {
143    LOG(LS_ERROR) << "Failed to parse formats.";
144  }
145
146  std::vector<cricket::VideoFormat> formats;
147  for (Json::ArrayIndex i = 0; i < json_values.size(); ++i) {
148      const Json::Value& json_value = json_values[i];
149      RTC_CHECK(!json_value["width"].isNull() &&
150                !json_value["height"].isNull() &&
151                !json_value["framerate"].isNull());
152      cricket::VideoFormat format(
153          json_value["width"].asInt(),
154          json_value["height"].asInt(),
155          cricket::VideoFormat::FpsToInterval(json_value["framerate"].asInt()),
156          cricket::FOURCC_YV12);
157      formats.push_back(format);
158  }
159  SetSupportedFormats(formats);
160  // Do not apply frame rotation by default.
161  SetApplyRotation(false);
162}
163
164AndroidVideoCapturer::~AndroidVideoCapturer() {
165  RTC_CHECK(!running_);
166}
167
168cricket::CaptureState AndroidVideoCapturer::Start(
169    const cricket::VideoFormat& capture_format) {
170  RTC_CHECK(thread_checker_.CalledOnValidThread());
171  RTC_CHECK(!running_);
172  const int fps = cricket::VideoFormat::IntervalToFps(capture_format.interval);
173  LOG(LS_INFO) << " AndroidVideoCapturer::Start " << capture_format.width << "x"
174               << capture_format.height << "@" << fps;
175
176  frame_factory_ = new AndroidVideoCapturer::FrameFactory(delegate_.get());
177  set_frame_factory(frame_factory_);
178
179  running_ = true;
180  delegate_->Start(capture_format.width, capture_format.height, fps, this);
181  SetCaptureFormat(&capture_format);
182  current_state_ = cricket::CS_STARTING;
183  return current_state_;
184}
185
186void AndroidVideoCapturer::Stop() {
187  LOG(LS_INFO) << " AndroidVideoCapturer::Stop ";
188  RTC_CHECK(thread_checker_.CalledOnValidThread());
189  RTC_CHECK(running_);
190  running_ = false;
191  SetCaptureFormat(NULL);
192
193  delegate_->Stop();
194  current_state_ = cricket::CS_STOPPED;
195  SignalStateChange(this, current_state_);
196}
197
198bool AndroidVideoCapturer::IsRunning() {
199  RTC_CHECK(thread_checker_.CalledOnValidThread());
200  return running_;
201}
202
203bool AndroidVideoCapturer::GetPreferredFourccs(std::vector<uint32_t>* fourccs) {
204  RTC_CHECK(thread_checker_.CalledOnValidThread());
205  fourccs->push_back(cricket::FOURCC_YV12);
206  return true;
207}
208
209void AndroidVideoCapturer::OnCapturerStarted(bool success) {
210  RTC_CHECK(thread_checker_.CalledOnValidThread());
211  cricket::CaptureState new_state =
212      success ? cricket::CS_RUNNING : cricket::CS_FAILED;
213  if (new_state == current_state_)
214    return;
215  current_state_ = new_state;
216
217  // TODO(perkj): SetCaptureState can not be used since it posts to |thread_|.
218  // But |thread_ | is currently just the thread that happened to create the
219  // cricket::VideoCapturer.
220  SignalStateChange(this, new_state);
221}
222
223void AndroidVideoCapturer::OnIncomingFrame(
224    const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
225    int rotation,
226    int64_t time_stamp) {
227  RTC_CHECK(thread_checker_.CalledOnValidThread());
228  frame_factory_->UpdateCapturedFrame(buffer, rotation, time_stamp);
229  SignalFrameCaptured(this, frame_factory_->GetCapturedFrame());
230  frame_factory_->ClearCapturedFrame();
231}
232
233void AndroidVideoCapturer::OnOutputFormatRequest(
234    int width, int height, int fps) {
235  RTC_CHECK(thread_checker_.CalledOnValidThread());
236  const cricket::VideoFormat& current = video_adapter()->output_format();
237  cricket::VideoFormat format(
238      width, height, cricket::VideoFormat::FpsToInterval(fps), current.fourcc);
239  video_adapter()->OnOutputFormatRequest(format);
240}
241
242bool AndroidVideoCapturer::GetBestCaptureFormat(
243    const cricket::VideoFormat& desired,
244    cricket::VideoFormat* best_format) {
245  // Delegate this choice to VideoCapturerAndroid.startCapture().
246  *best_format = desired;
247  return true;
248}
249
250}  // namespace webrtc
251