• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "cast/standalone_receiver/streaming_playback_controller.h"
6 
7 #include <string>
8 
9 #if defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
10 #include "cast/standalone_receiver/sdl_audio_player.h"
11 #include "cast/standalone_receiver/sdl_glue.h"
12 #include "cast/standalone_receiver/sdl_video_player.h"
13 #else
14 #include "cast/standalone_receiver/dummy_player.h"
15 #endif  // defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
16 
17 #include "util/trace_logging.h"
18 
19 namespace openscreen {
20 namespace cast {
21 
22 #if defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
StreamingPlaybackController(TaskRunner * task_runner,StreamingPlaybackController::Client * client)23 StreamingPlaybackController::StreamingPlaybackController(
24     TaskRunner* task_runner,
25     StreamingPlaybackController::Client* client)
26     : task_runner_(task_runner),
27       client_(client),
28       sdl_event_loop_(task_runner_, [this] {
29         client_->OnPlaybackError(this,
30                                  Error{Error::Code::kOperationCancelled,
31                                        std::string("SDL event loop closed.")});
32       }) {
33   OSP_DCHECK(task_runner_ != nullptr);
34   OSP_DCHECK(client_ != nullptr);
35   constexpr int kDefaultWindowWidth = 1280;
36   constexpr int kDefaultWindowHeight = 720;
37   window_ = MakeUniqueSDLWindow(
38       "Cast Streaming Receiver Demo",
39       SDL_WINDOWPOS_UNDEFINED /* initial X position */,
40       SDL_WINDOWPOS_UNDEFINED /* initial Y position */, kDefaultWindowWidth,
41       kDefaultWindowHeight, SDL_WINDOW_RESIZABLE);
42   OSP_CHECK(window_) << "Failed to create SDL window: " << SDL_GetError();
43   renderer_ = MakeUniqueSDLRenderer(window_.get(), -1, 0);
44   OSP_CHECK(renderer_) << "Failed to create SDL renderer: " << SDL_GetError();
45 }
46 #else
47 StreamingPlaybackController::StreamingPlaybackController(
48     TaskRunner* task_runner,
49     StreamingPlaybackController::Client* client)
50     : task_runner_(task_runner), client_(client) {
51   OSP_DCHECK(task_runner_ != nullptr);
52   OSP_DCHECK(client_ != nullptr);
53 }
54 #endif  // defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
55 
OnMirroringNegotiated(const ReceiverSession * session,ReceiverSession::ConfiguredReceivers receivers)56 void StreamingPlaybackController::OnMirroringNegotiated(
57     const ReceiverSession* session,
58     ReceiverSession::ConfiguredReceivers receivers) {
59   TRACE_DEFAULT_SCOPED(TraceCategory::kStandaloneReceiver);
60 #if defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
61   if (receivers.audio_receiver) {
62     audio_player_ = std::make_unique<SDLAudioPlayer>(
63         &Clock::now, task_runner_, receivers.audio_receiver,
64         receivers.audio_config.codec, [this] {
65           client_->OnPlaybackError(this, audio_player_->error_status());
66         });
67   }
68   if (receivers.video_receiver) {
69     video_player_ = std::make_unique<SDLVideoPlayer>(
70         &Clock::now, task_runner_, receivers.video_receiver,
71         receivers.video_config.codec, renderer_.get(), [this] {
72           client_->OnPlaybackError(this, video_player_->error_status());
73         });
74   }
75 #else
76   if (receivers.audio_receiver) {
77     audio_player_ = std::make_unique<DummyPlayer>(receivers.audio_receiver);
78   }
79 
80   if (receivers.video_receiver) {
81     video_player_ = std::make_unique<DummyPlayer>(receivers.video_receiver);
82   }
83 #endif  // defined(CAST_STANDALONE_RECEIVER_HAVE_EXTERNAL_LIBS)
84 }
85 
OnReceiversDestroying(const ReceiverSession * session,ReceiversDestroyingReason reason)86 void StreamingPlaybackController::OnReceiversDestroying(
87     const ReceiverSession* session,
88     ReceiversDestroyingReason reason) {
89   audio_player_.reset();
90   video_player_.reset();
91 }
92 
OnError(const ReceiverSession * session,Error error)93 void StreamingPlaybackController::OnError(const ReceiverSession* session,
94                                           Error error) {
95   client_->OnPlaybackError(this, error);
96 }
97 
98 }  // namespace cast
99 }  // namespace openscreen
100