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
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220

media / renderers / win / media_engine_notify_impl.cc [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/renderers/win/media_engine_notify_impl.h"

#include <mferror.h>

#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "media/base/win/mf_helpers.h"

namespace media {

namespace {

#define ENUM_TO_STRING(enum) \
  case enum:                 \
    return #enum

std::string MediaEngineErrorToString(MF_MEDIA_ENGINE_ERR error) {
  switch (error) {
    ENUM_TO_STRING(MF_MEDIA_ENGINE_ERR_NOERROR);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_ERR_ABORTED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_ERR_NETWORK);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_ERR_DECODE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_ERR_ENCRYPTED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED);
    default:
      return "Unknown MF_MEDIA_ENGINE_ERR";
  }
}

std::string MediaEngineEventToString(MF_MEDIA_ENGINE_EVENT event) {
  switch (event) {
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_LOADSTART);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_PROGRESS);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_SUSPEND);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_ABORT);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_ERROR);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_EMPTIED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_STALLED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_PLAY);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_PAUSE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_LOADEDMETADATA);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_LOADEDDATA);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_WAITING);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_PLAYING);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_CANPLAY);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_CANPLAYTHROUGH);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_SEEKING);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_SEEKED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_TIMEUPDATE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_ENDED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_RATECHANGE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_DURATIONCHANGE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_VOLUMECHANGE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_FORMATCHANGE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_PURGEQUEUEDEVENTS);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_TIMELINE_MARKER);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_BALANCECHANGE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_DOWNLOADCOMPLETE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_BUFFERINGSTARTED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_BUFFERINGENDED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_FRAMESTEPCOMPLETED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_NOTIFYSTABLESTATE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_FIRSTFRAMEREADY);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_TRACKSCHANGE);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_OPMINFO);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_RESOURCELOST);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_DELAYLOADEVENT_CHANGED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_STREAMRENDERINGERROR);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_SUPPORTEDRATES_CHANGED);
    ENUM_TO_STRING(MF_MEDIA_ENGINE_EVENT_AUDIOENDPOINTCHANGE);
    default:
      return "Unknown MF_MEDIA_ENGINE_EVENT";
  }
}

#undef ENUM_TO_STRING

PipelineStatus MediaEngineErrorToPipelineStatus(
    MF_MEDIA_ENGINE_ERR media_engine_error) {
  switch (media_engine_error) {
    case MF_MEDIA_ENGINE_ERR_NOERROR:
      return PIPELINE_OK;
    case MF_MEDIA_ENGINE_ERR_ABORTED:
      return PIPELINE_ERROR_ABORT;
    case MF_MEDIA_ENGINE_ERR_NETWORK:
      return PIPELINE_ERROR_NETWORK;
    case MF_MEDIA_ENGINE_ERR_DECODE:
      [[fallthrough]];
    case MF_MEDIA_ENGINE_ERR_ENCRYPTED:
      return PIPELINE_ERROR_DECODE;
    case MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED:
      return DEMUXER_ERROR_COULD_NOT_OPEN;
    default:
      NOTREACHED();
  }
}

}  // namespace

MediaEngineNotifyImpl::MediaEngineNotifyImpl() = default;
MediaEngineNotifyImpl::~MediaEngineNotifyImpl() = default;

HRESULT MediaEngineNotifyImpl::RuntimeClassInitialize(
    ErrorCB error_cb,
    EndedCB ended_cb,
    FormatChangeCB format_change_cb,
    LoadedDataCB loaded_data_cb,
    CanPlayThroughCB can_play_through_cb,
    PlayingCB playing_cb,
    WaitingCB waiting_cb,
    FrameStepCompletedCB frame_step_completed_cb,
    TimeUpdateCB time_update_cb,
    std::optional<VideoDecoderConfig> video_decoder_config,
    std::optional<AudioDecoderConfig> audio_decoder_config) {
  DVLOG_FUNC(1);

  error_cb_ = std::move(error_cb);
  ended_cb_ = std::move(ended_cb);
  format_change_cb_ = std::move(format_change_cb);
  loaded_data_cb_ = std::move(loaded_data_cb);
  can_play_through_cb_ = std::move(can_play_through_cb);
  playing_cb_ = std::move(playing_cb);
  waiting_cb_ = std::move(waiting_cb);
  frame_step_completed_cb_ = std::move(frame_step_completed_cb);
  time_update_cb_ = std::move(time_update_cb);

  audio_decoder_config_ = std::move(audio_decoder_config);
  video_decoder_config_ = std::move(video_decoder_config);
  return S_OK;
}

// |param1| and |param2|'s meaning depends on the |event_code| from
// https://docs.microsoft.com/en-us/windows/win32/api/mfmediaengine/ne-mfmediaengine-mf_media_engine_event
// This method always return S_OK. Even for error |event_code| because we
// successfully handled the event.
HRESULT MediaEngineNotifyImpl::EventNotify(DWORD event_code,
                                           DWORD_PTR param1,
                                           DWORD param2) {
  auto event = static_cast<MF_MEDIA_ENGINE_EVENT>(event_code);
  DVLOG_FUNC(3) << "event=" << MediaEngineEventToString(event);

  base::AutoLock lock(lock_);
  if (has_shutdown_)
    return S_OK;

  switch (event) {
    case MF_MEDIA_ENGINE_EVENT_ERROR: {
      // |param1| - A member of the MF_MEDIA_ENGINE_ERR enumeration.
      // |param2| - An HRESULT error code, or zero.
      MF_MEDIA_ENGINE_ERR error = static_cast<MF_MEDIA_ENGINE_ERR>(param1);
      HRESULT hr = param2;
      LOG(ERROR) << __func__ << ": error=" << error << ", hr=" << PrintHr(hr);

      // Report the HRESULT corresponding to certain MF_MEDIA_ENGINE_ERR
      // TODO(crbug.com/315860185): Remove this after the investigation is done.
      base::UmaHistogramSparse(
          base::StrCat({"Media.MediaFoundation.MediaEngineError.",
                        MediaEngineErrorToString(error), ".Hresult"}),
          hr);

      // Report the Video and Audio codec used when encountering the HRESULT
      // MF_E_TOPO_CODEC_NOT_FOUND.
      // TODO(crbug.com/315860185): Remove this after the investigation is done.
      if (hr == MF_E_TOPO_CODEC_NOT_FOUND &&
          audio_decoder_config_.has_value() &&
          video_decoder_config_.has_value()) {
        base::UmaHistogramEnumeration(
            base::StrCat(
                {"Media.MediaFoundation.MF_E_TOPO_CODEC_NOT_FOUND.",
                 media::GetCodecNameForUMA(video_decoder_config_->codec())}),
            audio_decoder_config_->codec());
      }

      error_cb_.Run(MediaEngineErrorToPipelineStatus(error), hr);
      break;
    }
    case MF_MEDIA_ENGINE_EVENT_ENDED:
      ended_cb_.Run();
      break;
    case MF_MEDIA_ENGINE_EVENT_FORMATCHANGE:
      format_change_cb_.Run();
      break;
    case MF_MEDIA_ENGINE_EVENT_LOADEDDATA:
      loaded_data_cb_.Run();
      break;
    case MF_MEDIA_ENGINE_EVENT_CANPLAYTHROUGH:
      can_play_through_cb_.Run();
      break;
    case MF_MEDIA_ENGINE_EVENT_PLAYING:
      playing_cb_.Run();
      break;
    case MF_MEDIA_ENGINE_EVENT_WAITING:
      waiting_cb_.Run();
      break;
    case MF_MEDIA_ENGINE_EVENT_FRAMESTEPCOMPLETED:
      frame_step_completed_cb_.Run();
      break;
    case MF_MEDIA_ENGINE_EVENT_TIMEUPDATE:
      time_update_cb_.Run();
      break;

    default:
      DVLOG_FUNC(2) << "Unhandled event=" << MediaEngineEventToString(event);
      break;
  }
  return S_OK;
}

void MediaEngineNotifyImpl::Shutdown() {
  DVLOG_FUNC(1);

  base::AutoLock lock(lock_);
  has_shutdown_ = true;
}

}  // namespace media