blob: 6711b6ed0ae24431f84e119fd1e8f655a18b0def [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"
36
37namespace webrtc {
38
39using cricket::WebRtcVideoFrame;
40using rtc::scoped_ptr;
Per33544192015-04-02 12:30:51 +020041using rtc::scoped_refptr;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000042
43// An implementation of cricket::VideoFrameFactory for frames that are not
44// guaranteed to outlive the created cricket::VideoFrame.
45// A frame is injected using UpdateCapturedFrame, and converted into a
46// cricket::VideoFrame with
47// CreateAliasedFrame. UpdateCapturedFrame should be called before
48// CreateAliasedFrame for every frame.
49class AndroidVideoCapturer::FrameFactory : public cricket::VideoFrameFactory {
50 public:
Per33544192015-04-02 12:30:51 +020051 FrameFactory(int width,
52 int height,
53 const scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
54 : start_time_(rtc::TimeNanos()), delegate_(delegate) {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000055 // Create a CapturedFrame that only contains header information, not the
56 // actual pixel data.
57 captured_frame_.width = width;
58 captured_frame_.height = height;
59 captured_frame_.pixel_height = 1;
60 captured_frame_.pixel_width = 1;
61 captured_frame_.rotation = 0;
62 captured_frame_.data = NULL;
63 captured_frame_.data_size = cricket::CapturedFrame::kUnknownDataSize;
64 captured_frame_.fourcc = static_cast<uint32>(cricket::FOURCC_ANY);
65 }
66
perkj@webrtc.org112f1272015-02-25 09:20:07 +000067 void UpdateCapturedFrame(void* frame_data,
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000068 int length,
69 int rotation,
Per33544192015-04-02 12:30:51 +020070 int64 time_stamp_in_ns) {
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +000071 captured_frame_.fourcc = static_cast<uint32>(cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000072 captured_frame_.data = frame_data;
73 captured_frame_.elapsed_time = rtc::TimeNanos() - start_time_;
Per33544192015-04-02 12:30:51 +020074 captured_frame_.time_stamp = time_stamp_in_ns;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000075 captured_frame_.rotation = rotation;
76 captured_frame_.data_size = length;
77 }
78
79 const cricket::CapturedFrame* GetCapturedFrame() const {
80 return &captured_frame_;
81 }
82
83 cricket::VideoFrame* CreateAliasedFrame(
84 const cricket::CapturedFrame* captured_frame,
85 int dst_width,
86 int dst_height) const override {
87 // This override of CreateAliasedFrame creates a copy of the frame since
88 // |captured_frame_.data| is only guaranteed to be valid during the scope
89 // of |AndroidVideoCapturer::OnIncomingFrame_w|.
90 // Check that captured_frame is actually our frame.
91 DCHECK(captured_frame == &captured_frame_);
Per33544192015-04-02 12:30:51 +020092
93 if (!apply_rotation_ || captured_frame->rotation == kVideoRotation_0) {
94 DCHECK(captured_frame->fourcc == cricket::FOURCC_YV12);
95 const uint8_t* y_plane = static_cast<uint8_t*>(captured_frame_.data);
96 const int y_stride = captured_frame->width;
97 const uint8_t* v_plane = y_plane +
98 captured_frame->width * captured_frame->height;
99 const int uv_stride = (captured_frame->width + 1) / 2;
100 const int uv_height = (captured_frame->height + 1) / 2;
101 const uint8_t* u_plane = v_plane + uv_stride * uv_height;
102
103 // Create a WrappedI420Buffer and bind the |no_longer_used| callback
104 // to the static method ReturnFrame. The |delegate_| is bound as an
105 // argument which means that the callback will hold a reference to
106 // |delegate_|.
107 rtc::scoped_refptr<WrappedI420Buffer> buffer(
108 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
109 dst_width, dst_height, captured_frame->width,
110 captured_frame->height, y_plane, y_stride, u_plane, uv_stride,
111 v_plane, uv_stride,
112 rtc::Bind(&AndroidVideoCapturer::FrameFactory::ReturnFrame,
113 delegate_,
114 captured_frame->time_stamp)));
115 return new WebRtcVideoFrame(
116 buffer, captured_frame->elapsed_time,
117 captured_frame->time_stamp, captured_frame->GetRotation());
118 }
119
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000120 scoped_ptr<WebRtcVideoFrame> frame(new WebRtcVideoFrame());
perkj@webrtc.org1d828132015-03-03 06:44:06 +0000121 frame->Init(captured_frame, dst_width, dst_height, apply_rotation_);
Per33544192015-04-02 12:30:51 +0200122 // frame->Init copies the data in |captured_frame| so it is safe to return
123 // the buffer immediately.
124 delegate_->ReturnBuffer(captured_frame->time_stamp);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000125 return frame.release();
126 }
127
Per33544192015-04-02 12:30:51 +0200128 static void ReturnFrame(scoped_refptr<AndroidVideoCapturerDelegate> delegate,
129 int64 time_stamp) {
130 delegate->ReturnBuffer(time_stamp);
131 }
132
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000133 private:
134 uint64 start_time_;
135 cricket::CapturedFrame captured_frame_;
Per33544192015-04-02 12:30:51 +0200136 scoped_refptr<AndroidVideoCapturerDelegate> delegate_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000137};
138
139AndroidVideoCapturer::AndroidVideoCapturer(
Per33544192015-04-02 12:30:51 +0200140 const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate)
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000141 : running_(false),
Per33544192015-04-02 12:30:51 +0200142 delegate_(delegate),
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000143 frame_factory_(NULL),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000144 current_state_(cricket::CS_STOPPED) {
Per33544192015-04-02 12:30:51 +0200145 thread_checker_.DetachFromThread();
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000146 std::string json_string = delegate_->GetSupportedFormats();
147 LOG(LS_INFO) << json_string;
148
149 Json::Value json_values;
150 Json::Reader reader(Json::Features::strictMode());
151 if (!reader.parse(json_string, json_values)) {
152 LOG(LS_ERROR) << "Failed to parse formats.";
153 }
154
155 std::vector<cricket::VideoFormat> formats;
156 for (Json::ArrayIndex i = 0; i < json_values.size(); ++i) {
157 const Json::Value& json_value = json_values[i];
158 DCHECK(!json_value["width"].isNull() && !json_value["height"].isNull() &&
159 !json_value["framerate"].isNull());
160 cricket::VideoFormat format(
161 json_value["width"].asInt(),
162 json_value["height"].asInt(),
163 cricket::VideoFormat::FpsToInterval(json_value["framerate"].asInt()),
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000164 cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000165 formats.push_back(format);
166 }
167 SetSupportedFormats(formats);
168}
169
170AndroidVideoCapturer::~AndroidVideoCapturer() {
171 DCHECK(!running_);
172}
173
174cricket::CaptureState AndroidVideoCapturer::Start(
175 const cricket::VideoFormat& capture_format) {
Per33544192015-04-02 12:30:51 +0200176 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000177 DCHECK(!running_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000178
179 LOG(LS_INFO) << " AndroidVideoCapturer::Start w = " << capture_format.width
180 << " h = " << capture_format.height;
181 frame_factory_ = new AndroidVideoCapturer::FrameFactory(
Per33544192015-04-02 12:30:51 +0200182 capture_format.width, capture_format.height, delegate_.get());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000183 set_frame_factory(frame_factory_);
184
185 running_ = true;
186 delegate_->Start(
187 capture_format.width, capture_format.height,
188 cricket::VideoFormat::IntervalToFps(capture_format.interval), this);
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000189 SetCaptureFormat(&capture_format);
190 current_state_ = cricket::CS_STARTING;
191 return current_state_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000192}
193
194void AndroidVideoCapturer::Stop() {
Per33544192015-04-02 12:30:51 +0200195 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000196 LOG(LS_INFO) << " AndroidVideoCapturer::Stop ";
197 DCHECK(running_);
198 running_ = false;
199 SetCaptureFormat(NULL);
200
201 delegate_->Stop();
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000202 current_state_ = cricket::CS_STOPPED;
203 SignalStateChange(this, current_state_);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000204}
205
206bool AndroidVideoCapturer::IsRunning() {
Per33544192015-04-02 12:30:51 +0200207 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000208 return running_;
209}
210
211bool AndroidVideoCapturer::GetPreferredFourccs(std::vector<uint32>* fourccs) {
Per33544192015-04-02 12:30:51 +0200212 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org2ad3bb12015-02-23 11:14:57 +0000213 fourccs->push_back(cricket::FOURCC_YV12);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000214 return true;
215}
216
217void AndroidVideoCapturer::OnCapturerStarted(bool success) {
Per33544192015-04-02 12:30:51 +0200218 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000219 cricket::CaptureState new_state =
220 success ? cricket::CS_RUNNING : cricket::CS_FAILED;
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000221 if (new_state == current_state_)
222 return;
223 current_state_ = new_state;
224
225 // TODO(perkj): SetCaptureState can not be used since it posts to |thread_|.
226 // But |thread_ | is currently just the thread that happened to create the
227 // cricket::VideoCapturer.
228 SignalStateChange(this, new_state);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000229}
230
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000231void AndroidVideoCapturer::OnIncomingFrame(void* frame_data,
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000232 int length,
233 int rotation,
234 int64 time_stamp) {
Per33544192015-04-02 12:30:51 +0200235 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000236 frame_factory_->UpdateCapturedFrame(frame_data, length, rotation, time_stamp);
237 SignalFrameCaptured(this, frame_factory_->GetCapturedFrame());
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000238}
239
240} // namespace webrtc