summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/media/cast_receiver_session_delegate.cc
blob: 06ea93d8100becbb5a3b7b0f2ed654ff70beb451 (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
// Copyright 2015 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 "chrome/renderer/media/cast_receiver_session_delegate.h"

#include "base/bind.h"
#include "base/synchronization/waitable_event.h"
#include "base/values.h"

CastReceiverSessionDelegate::CastReceiverSessionDelegate()
    : weak_factory_(this) {
}
CastReceiverSessionDelegate::~CastReceiverSessionDelegate() {}

void CastReceiverSessionDelegate::Start(
    const media::cast::FrameReceiverConfig& audio_config,
    const media::cast::FrameReceiverConfig& video_config,
    const net::IPEndPoint& local_endpoint,
    const net::IPEndPoint& remote_endpoint,
    scoped_ptr<base::DictionaryValue> options,
    const media::VideoCaptureFormat& format,
    const ErrorCallback& error_callback) {
  format_ = format;
  DCHECK(io_task_runner_->BelongsToCurrentThread());
  CastSessionDelegateBase::StartUDP(local_endpoint,
                                    remote_endpoint,
                                    options.Pass(),
                                    error_callback);
  cast_receiver_ = media::cast::CastReceiver::Create(cast_environment_,
                                                     audio_config,
                                                     video_config,
                                                     cast_transport_.get());
  on_audio_decoded_cb_ = base::Bind(
      &CastReceiverSessionDelegate::OnDecodedAudioFrame,
      weak_factory_.GetWeakPtr());
  on_video_decoded_cb_ = base::Bind(
      &CastReceiverSessionDelegate::OnDecodedVideoFrame,
      weak_factory_.GetWeakPtr());
}

void CastReceiverSessionDelegate::ReceivePacket(
    scoped_ptr<media::cast::Packet> packet) {
  cast_receiver_->ReceivePacket(packet.Pass());
}

void CastReceiverSessionDelegate::StartAudio(
    scoped_refptr<CastReceiverAudioValve> audio_valve) {
  DCHECK(io_task_runner_->BelongsToCurrentThread());
  audio_valve_ = audio_valve;
  cast_receiver_->RequestDecodedAudioFrame(on_audio_decoded_cb_);
}

void CastReceiverSessionDelegate::OnDecodedAudioFrame(
    scoped_ptr<media::AudioBus> audio_bus,
    const base::TimeTicks& playout_time,
    bool is_continous) {
  DCHECK(io_task_runner_->BelongsToCurrentThread());
  if (!audio_valve_)
    return;

  // We're on the IO thread, which doesn't allow blocking
  // operations. Since we don't know what the Capture callback
  // will do exactly, we need to jump to a different thread.
  // Let's re-use the audio decoder thread.
  base::TimeTicks now = cast_environment_->Clock()->NowTicks();
  cast_environment_->PostTask(
      media::cast::CastEnvironment::AUDIO,
      FROM_HERE,
      base::Bind(&CastReceiverAudioValve::Capture,
                 audio_valve_,
                 base::Owned(audio_bus.release()),
                 (playout_time - now).InMilliseconds(),
                 1.0,
                 false));
  cast_receiver_->RequestDecodedAudioFrame(on_audio_decoded_cb_);
}

void CastReceiverSessionDelegate::StartVideo(
    content::VideoCaptureDeliverFrameCB video_callback) {
  DCHECK(io_task_runner_->BelongsToCurrentThread());
  frame_callback_ = video_callback;
  cast_receiver_->RequestDecodedVideoFrame(on_video_decoded_cb_);
}

void  CastReceiverSessionDelegate::StopVideo() {
  frame_callback_ = content::VideoCaptureDeliverFrameCB();
}

void CastReceiverSessionDelegate::OnDecodedVideoFrame(
    const scoped_refptr<media::VideoFrame>& video_frame,
    const base::TimeTicks& playout_time,
    bool is_continous) {
  if (frame_callback_.is_null())
    return;
  frame_callback_.Run(video_frame, playout_time);
  cast_receiver_->RequestDecodedVideoFrame(on_video_decoded_cb_);
}