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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/cast/test/utility/in_process_receiver.h"
#include "base/bind_helpers.h"
#include "base/synchronization/waitable_event.h"
#include "base/time/time.h"
#include "base/values.h"
#include "media/base/video_frame.h"
#include "media/cast/cast_config.h"
#include "media/cast/cast_environment.h"
#include "media/cast/cast_receiver.h"
#include "media/cast/net/cast_transport_config.h"
#include "media/cast/net/udp_transport.h"
using media::cast::CastTransportStatus;
using media::cast::UdpTransport;
namespace media {
namespace cast {
InProcessReceiver::InProcessReceiver(
const scoped_refptr<CastEnvironment>& cast_environment,
const net::IPEndPoint& local_end_point,
const net::IPEndPoint& remote_end_point,
const FrameReceiverConfig& audio_config,
const FrameReceiverConfig& video_config)
: cast_environment_(cast_environment),
local_end_point_(local_end_point),
remote_end_point_(remote_end_point),
audio_config_(audio_config),
video_config_(video_config),
weak_factory_(this) {}
InProcessReceiver::~InProcessReceiver() {
Stop();
}
void InProcessReceiver::Start() {
cast_environment_->PostTask(CastEnvironment::MAIN,
FROM_HERE,
base::Bind(&InProcessReceiver::StartOnMainThread,
base::Unretained(this)));
}
void InProcessReceiver::Stop() {
base::WaitableEvent event(false, false);
if (cast_environment_->CurrentlyOn(CastEnvironment::MAIN)) {
StopOnMainThread(&event);
} else {
cast_environment_->PostTask(CastEnvironment::MAIN,
FROM_HERE,
base::Bind(&InProcessReceiver::StopOnMainThread,
base::Unretained(this),
&event));
event.Wait();
}
}
void InProcessReceiver::StopOnMainThread(base::WaitableEvent* event) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
cast_receiver_.reset(NULL);
transport_.reset(NULL);
weak_factory_.InvalidateWeakPtrs();
event->Signal();
}
void InProcessReceiver::UpdateCastTransportStatus(CastTransportStatus status) {
LOG_IF(ERROR, status == media::cast::TRANSPORT_SOCKET_ERROR)
<< "Transport socket error occurred. InProcessReceiver is likely dead.";
VLOG(1) << "CastTransportStatus is now " << status;
}
void InProcessReceiver::StartOnMainThread() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
DCHECK(!transport_ && !cast_receiver_);
transport_ = CastTransportSender::Create(
NULL,
cast_environment_->Clock(),
local_end_point_,
remote_end_point_,
scoped_ptr<base::DictionaryValue>(new base::DictionaryValue),
base::Bind(&InProcessReceiver::UpdateCastTransportStatus,
base::Unretained(this)),
BulkRawEventsCallback(),
base::TimeDelta(),
base::Bind(&InProcessReceiver::ReceivePacket,
base::Unretained(this)),
cast_environment_->GetTaskRunner(CastEnvironment::MAIN));
cast_receiver_ = CastReceiver::Create(
cast_environment_, audio_config_, video_config_, transport_.get());
PullNextAudioFrame();
PullNextVideoFrame();
}
void InProcessReceiver::GotAudioFrame(scoped_ptr<AudioBus> audio_frame,
const base::TimeTicks& playout_time,
bool is_continuous) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (audio_frame.get())
OnAudioFrame(audio_frame.Pass(), playout_time, is_continuous);
PullNextAudioFrame();
}
void InProcessReceiver::GotVideoFrame(
const scoped_refptr<VideoFrame>& video_frame,
const base::TimeTicks& playout_time,
bool is_continuous) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (video_frame.get())
OnVideoFrame(video_frame, playout_time, is_continuous);
PullNextVideoFrame();
}
void InProcessReceiver::PullNextAudioFrame() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
cast_receiver_->RequestDecodedAudioFrame(
base::Bind(&InProcessReceiver::GotAudioFrame,
weak_factory_.GetWeakPtr()));
}
void InProcessReceiver::PullNextVideoFrame() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
cast_receiver_->RequestDecodedVideoFrame(base::Bind(
&InProcessReceiver::GotVideoFrame, weak_factory_.GetWeakPtr()));
}
void InProcessReceiver::ReceivePacket(scoped_ptr<Packet> packet) {
// TODO(Hubbe): Make an InsertPacket method instead.
cast_receiver_->ReceivePacket(packet.Pass());
}
} // namespace cast
} // namespace media
|