summaryrefslogtreecommitdiff
path: root/chromium/third_party/libjingle/source/talk/media/webrtc/webrtcpassthroughrender.cc
blob: b4e78b44e8d7baa05f4a27d3c91090648e292ef2 (plain)
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
/*
 * libjingle
 * Copyright 2004 Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "talk/media/webrtc/webrtcpassthroughrender.h"

#include "talk/base/common.h"
#include "talk/base/logging.h"

namespace cricket {

#define LOG_FIND_STREAM_ERROR(func, id) LOG(LS_ERROR) \
    << "" << func << " - Failed to find stream: " << id

class PassthroughStream: public webrtc::VideoRenderCallback {
 public:
  explicit PassthroughStream(const uint32_t stream_id)
      : stream_id_(stream_id),
        running_(false) {
  }
  virtual ~PassthroughStream() {
  }
  virtual int32_t RenderFrame(const uint32_t stream_id,
                              webrtc::I420VideoFrame& videoFrame) {
    talk_base::CritScope cs(&stream_critical_);
    // Send frame for rendering directly
    if (running_ && renderer_) {
      renderer_->RenderFrame(stream_id, videoFrame);
    }
    return 0;
  }
  int32_t SetRenderer(VideoRenderCallback* renderer) {
    talk_base::CritScope cs(&stream_critical_);
    renderer_ = renderer;
    return 0;
  }

  int32_t StartRender() {
    talk_base::CritScope cs(&stream_critical_);
    running_ = true;
    return 0;
  }

  int32_t StopRender() {
    talk_base::CritScope cs(&stream_critical_);
    running_ = false;
    return 0;
  }

 private:
  uint32_t stream_id_;
  VideoRenderCallback* renderer_;
  talk_base::CriticalSection stream_critical_;
  bool running_;
};

WebRtcPassthroughRender::WebRtcPassthroughRender()
    : window_(NULL) {
}

WebRtcPassthroughRender::~WebRtcPassthroughRender() {
  while (!stream_render_map_.empty()) {
    PassthroughStream* stream = stream_render_map_.begin()->second;
    stream_render_map_.erase(stream_render_map_.begin());
    delete stream;
  }
}

webrtc::VideoRenderCallback* WebRtcPassthroughRender::AddIncomingRenderStream(
    const uint32_t stream_id,
    const uint32_t zOrder,
    const float left, const float top,
    const float right, const float bottom) {
  talk_base::CritScope cs(&render_critical_);
  // Stream already exist.
  if (FindStream(stream_id) != NULL) {
    LOG(LS_ERROR) << "AddIncomingRenderStream - Stream already exists: "
                  << stream_id;
    return NULL;
  }

  PassthroughStream* stream = new PassthroughStream(stream_id);
  // Store the stream
  stream_render_map_[stream_id] = stream;
  return stream;
}

int32_t WebRtcPassthroughRender::DeleteIncomingRenderStream(
    const uint32_t stream_id) {
  talk_base::CritScope cs(&render_critical_);
  PassthroughStream* stream = FindStream(stream_id);
  if (stream == NULL) {
    LOG_FIND_STREAM_ERROR("DeleteIncomingRenderStream", stream_id);
    return -1;
  }
  delete stream;
  stream_render_map_.erase(stream_id);
  return 0;
}

int32_t WebRtcPassthroughRender::AddExternalRenderCallback(
    const uint32_t stream_id,
    webrtc::VideoRenderCallback* render_object) {
  talk_base::CritScope cs(&render_critical_);
  PassthroughStream* stream = FindStream(stream_id);
  if (stream == NULL) {
    LOG_FIND_STREAM_ERROR("AddExternalRenderCallback", stream_id);
    return -1;
  }
  return stream->SetRenderer(render_object);
}

bool WebRtcPassthroughRender::HasIncomingRenderStream(
    const uint32_t stream_id) const {
  return (FindStream(stream_id) != NULL);
}

webrtc::RawVideoType WebRtcPassthroughRender::PreferredVideoType() const {
  return webrtc::kVideoI420;
}

int32_t WebRtcPassthroughRender::StartRender(const uint32_t stream_id) {
  talk_base::CritScope cs(&render_critical_);
  PassthroughStream* stream = FindStream(stream_id);
  if (stream == NULL) {
    LOG_FIND_STREAM_ERROR("StartRender", stream_id);
    return -1;
  }
  return stream->StartRender();
}

int32_t WebRtcPassthroughRender::StopRender(const uint32_t stream_id) {
  talk_base::CritScope cs(&render_critical_);
  PassthroughStream* stream = FindStream(stream_id);
  if (stream == NULL) {
    LOG_FIND_STREAM_ERROR("StopRender", stream_id);
    return -1;
  }
  return stream->StopRender();
}

// TODO(ronghuawu): Is it ok to return non-const pointer to PassthroughStream
// from this const function FindStream.
PassthroughStream* WebRtcPassthroughRender::FindStream(
    const uint32_t stream_id) const {
  StreamMap::const_iterator it = stream_render_map_.find(stream_id);
  if (it == stream_render_map_.end()) {
    return NULL;
  }
  return it->second;
}

}  // namespace cricket