aboutsummaryrefslogtreecommitdiff
path: root/cast/standalone_receiver/streaming_playback_controller.cc
blob: 8c93127376fb2bf8da3b850f6f287e4d3e16ad14 (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
// Copyright 2019 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 "cast/standalone_receiver/streaming_playback_controller.h"

#include <string>

#if defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
#include "cast/standalone_receiver/sdl_audio_player.h"  // nogncheck
#include "cast/standalone_receiver/sdl_glue.h"          // nogncheck
#include "cast/standalone_receiver/sdl_video_player.h"  // nogncheck
#else
#include "cast/standalone_receiver/dummy_player.h"  // nogncheck
#endif  // defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)

#include "util/trace_logging.h"

namespace openscreen {
namespace cast {

StreamingPlaybackController::Client::~Client() = default;

#if defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
StreamingPlaybackController::StreamingPlaybackController(
    TaskRunner* task_runner,
    StreamingPlaybackController::Client* client)
    : task_runner_(task_runner),
      client_(client),
      sdl_event_loop_(task_runner_, [this] {
        client_->OnPlaybackError(this,
                                 Error{Error::Code::kOperationCancelled,
                                       std::string("SDL event loop closed.")});
      }) {
  OSP_DCHECK(task_runner_ != nullptr);
  OSP_DCHECK(client_ != nullptr);
  constexpr int kDefaultWindowWidth = 1280;
  constexpr int kDefaultWindowHeight = 720;
  window_ = MakeUniqueSDLWindow(
      "Cast Streaming Receiver Demo",
      SDL_WINDOWPOS_UNDEFINED /* initial X position */,
      SDL_WINDOWPOS_UNDEFINED /* initial Y position */, kDefaultWindowWidth,
      kDefaultWindowHeight, SDL_WINDOW_RESIZABLE);
  OSP_CHECK(window_) << "Failed to create SDL window: " << SDL_GetError();
  renderer_ = MakeUniqueSDLRenderer(window_.get(), -1, 0);
  OSP_CHECK(renderer_) << "Failed to create SDL renderer: " << SDL_GetError();
}
#else
StreamingPlaybackController::StreamingPlaybackController(
    TaskRunner* task_runner,
    StreamingPlaybackController::Client* client)
    : task_runner_(task_runner), client_(client) {
  OSP_DCHECK(task_runner_ != nullptr);
  OSP_DCHECK(client_ != nullptr);
}
#endif  // defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)

void StreamingPlaybackController::OnNegotiated(
    const ReceiverSession* session,
    ReceiverSession::ConfiguredReceivers receivers) {
  TRACE_DEFAULT_SCOPED(TraceCategory::kStandaloneReceiver);
  Initialize(receivers);
}

void StreamingPlaybackController::OnRemotingNegotiated(
    const ReceiverSession* session,
    ReceiverSession::RemotingNegotiation negotiation) {
  remoting_receiver_ =
      std::make_unique<SimpleRemotingReceiver>(negotiation.messenger);
  remoting_receiver_->SendInitializeMessage(
      [this, receivers = negotiation.receivers](AudioCodec audio_codec,
                                                VideoCodec video_codec) {
        // The configurations in |negotiation| do not have the actual codecs,
        // only REMOTE_AUDIO and REMOTE_VIDEO. Once we receive the
        // initialization callback method, we can override with the actual
        // codecs here.
        auto mutable_receivers = receivers;
        mutable_receivers.audio_config.codec = audio_codec;
        mutable_receivers.video_config.codec = video_codec;
        Initialize(mutable_receivers);
      });
}

void StreamingPlaybackController::OnReceiversDestroying(
    const ReceiverSession* session,
    ReceiversDestroyingReason reason) {
  audio_player_.reset();
  video_player_.reset();
}

void StreamingPlaybackController::OnError(const ReceiverSession* session,
                                          Error error) {
  client_->OnPlaybackError(this, error);
}

void StreamingPlaybackController::Initialize(
    ReceiverSession::ConfiguredReceivers receivers) {
#if defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
  if (receivers.audio_receiver) {
    audio_player_ = std::make_unique<SDLAudioPlayer>(
        &Clock::now, task_runner_, receivers.audio_receiver,
        receivers.audio_config.codec, [this] {
          client_->OnPlaybackError(this, audio_player_->error_status());
        });
  }
  if (receivers.video_receiver) {
    video_player_ = std::make_unique<SDLVideoPlayer>(
        &Clock::now, task_runner_, receivers.video_receiver,
        receivers.video_config.codec, renderer_.get(), [this] {
          client_->OnPlaybackError(this, video_player_->error_status());
        });
  }
#else
  if (receivers.audio_receiver) {
    audio_player_ = std::make_unique<DummyPlayer>(receivers.audio_receiver);
  }

  if (receivers.video_receiver) {
    video_player_ = std::make_unique<DummyPlayer>(receivers.video_receiver);
  }
#endif  // defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
}

}  // namespace cast
}  // namespace openscreen