1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* 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.
*/
#include "webrtc/video_engine/vie_frame_provider_base.h"
#include <algorithm>
#include "webrtc/common_video/interface/i420_video_frame.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
#include "webrtc/system_wrappers/interface/logging.h"
#include "webrtc/system_wrappers/interface/tick_util.h"
#include "webrtc/video_engine/vie_defines.h"
namespace webrtc {
ViEFrameProviderBase::ViEFrameProviderBase(int Id, int engine_id)
: id_(Id),
engine_id_(engine_id),
provider_cs_(CriticalSectionWrapper::CreateCriticalSection()),
frame_delay_(0) {
}
ViEFrameProviderBase::~ViEFrameProviderBase() {
if (frame_callbacks_.size() > 0) {
LOG_F(LS_WARNING) << "FrameCallbacks still exist when Provider deleted: "
<< frame_callbacks_.size();
}
for (FrameCallbacks::iterator it = frame_callbacks_.begin();
it != frame_callbacks_.end(); ++it) {
(*it)->ProviderDestroyed(id_);
}
frame_callbacks_.clear();
}
int ViEFrameProviderBase::Id() {
return id_;
}
void ViEFrameProviderBase::DeliverFrame(I420VideoFrame* video_frame,
const std::vector<uint32_t>& csrcs) {
#ifdef DEBUG_
const TickTime start_process_time = TickTime::Now();
#endif
CriticalSectionScoped cs(provider_cs_.get());
// Deliver the frame to all registered callbacks.
if (frame_callbacks_.size() > 0) {
if (frame_callbacks_.size() == 1) {
// We don't have to copy the frame.
frame_callbacks_.front()->DeliverFrame(id_, video_frame, csrcs);
} else {
for (FrameCallbacks::iterator it = frame_callbacks_.begin();
it != frame_callbacks_.end(); ++it) {
if (video_frame->native_handle() != NULL) {
(*it)->DeliverFrame(id_, video_frame, csrcs);
} else {
// Make a copy of the frame for all callbacks.
if (!extra_frame_.get()) {
extra_frame_.reset(new I420VideoFrame());
}
extra_frame_->CopyFrame(*video_frame);
(*it)->DeliverFrame(id_, extra_frame_.get(), csrcs);
}
}
}
}
#ifdef DEBUG_
const int process_time =
static_cast<int>((TickTime::Now() - start_process_time).Milliseconds());
if (process_time > 25) {
// Warn if the delivery time is too long.
LOG(LS_WARNING) << "Too long time delivering frame " << process_time;
}
#endif
}
void ViEFrameProviderBase::SetFrameDelay(int frame_delay) {
CriticalSectionScoped cs(provider_cs_.get());
frame_delay_ = frame_delay;
for (FrameCallbacks::iterator it = frame_callbacks_.begin();
it != frame_callbacks_.end(); ++it) {
(*it)->DelayChanged(id_, frame_delay);
}
}
int ViEFrameProviderBase::FrameDelay() {
return frame_delay_;
}
int ViEFrameProviderBase::GetBestFormat(int* best_width,
int* best_height,
int* best_frame_rate) {
int largest_width = 0;
int largest_height = 0;
int highest_frame_rate = 0;
CriticalSectionScoped cs(provider_cs_.get());
for (FrameCallbacks::iterator it = frame_callbacks_.begin();
it != frame_callbacks_.end(); ++it) {
int prefered_width = 0;
int prefered_height = 0;
int prefered_frame_rate = 0;
if ((*it)->GetPreferedFrameSettings(&prefered_width, &prefered_height,
&prefered_frame_rate) == 0) {
if (prefered_width > largest_width) {
largest_width = prefered_width;
}
if (prefered_height > largest_height) {
largest_height = prefered_height;
}
if (prefered_frame_rate > highest_frame_rate) {
highest_frame_rate = prefered_frame_rate;
}
}
}
*best_width = largest_width;
*best_height = largest_height;
*best_frame_rate = highest_frame_rate;
return 0;
}
int ViEFrameProviderBase::RegisterFrameCallback(
int observer_id, ViEFrameCallback* callback_object) {
assert(callback_object);
{
CriticalSectionScoped cs(provider_cs_.get());
if (std::find(frame_callbacks_.begin(), frame_callbacks_.end(),
callback_object) != frame_callbacks_.end()) {
assert(false && "frameObserver already registered");
return -1;
}
frame_callbacks_.push_back(callback_object);
}
// Report current capture delay.
callback_object->DelayChanged(id_, frame_delay_);
// Notify implementer of this class that the callback list have changed.
FrameCallbackChanged();
return 0;
}
int ViEFrameProviderBase::DeregisterFrameCallback(
const ViEFrameCallback* callback_object) {
assert(callback_object);
CriticalSectionScoped cs(provider_cs_.get());
FrameCallbacks::iterator it = std::find(frame_callbacks_.begin(),
frame_callbacks_.end(),
callback_object);
if (it == frame_callbacks_.end()) {
return -1;
}
frame_callbacks_.erase(it);
// Notify implementer of this class that the callback list have changed.
FrameCallbackChanged();
return 0;
}
bool ViEFrameProviderBase::IsFrameCallbackRegistered(
const ViEFrameCallback* callback_object) {
assert(callback_object);
CriticalSectionScoped cs(provider_cs_.get());
return std::find(frame_callbacks_.begin(), frame_callbacks_.end(),
callback_object) != frame_callbacks_.end();
}
int ViEFrameProviderBase::NumberOfRegisteredFrameCallbacks() {
CriticalSectionScoped cs(provider_cs_.get());
return frame_callbacks_.size();
}
} // namespac webrtc
|